Peripheral_HRS example: Only notify when connected?

I've adapted the peripheral_hr example for my purposes and its working but one thing I see is that the example never checks if the connection is established before calling notify. 

I guess this will just fill up the buffer and shouldn't massively impact battery life but is there a simple way to check if the connection is active before calling notify? 


  • Here's what I was doing before. 

    setting up a timer interrupt makes sense. 

    what I tried to implement in connected was actually moving the loop inside the connected callback which obviously caused problems. Setting a flag makes a lot more sense. 


    void measure(int readings)
    {
    	static struct sensor_value weight;
    	int ret;
    	
    	int32_t avg;
    
    	avia_hx711_power(hx711_dev,HX711_POWER_ON);
    
    	ret = sensor_sample_fetch(hx711_dev);
    	
    	if (ret != 0) {
    		LOG_ERR("Cannot take measurement: %d", ret);
    	} else {
    		readings = 100; 
    		for (int i = 0; i < readings; i++) {
    			sensor_channel_get(hx711_dev, HX711_SENSOR_CHAN_WEIGHT, &weight);
    			avg += weight.val1;
    		}
    		
    		int32_t adjusted = avg/readings;
    		LOG_INF("Weight: %i", adjusted);
    		bt_ws_notify(adjusted);
    	}
    
    	avia_hx711_power(hx711_dev, HX711_POWER_OFF);
    }
    
    
    void main(void)
    {
    	int err;
    	
    	int calibration_weight = 100; // grams
    	hx711_dev = DEVICE_DT_GET_ANY(avia_hx711);
    	__ASSERT(hx711_dev == NULL, "Failed to get device binding");
    
    	LOG_INF("Device is %p, name is %s", hx711_dev, hx711_dev->name);
    	LOG_INF("Calculating offset...");
    	//avia_hx711_tare(hx711_dev, 5);
    
    	struct hx711_data *data = hx711_dev->data;
    
    	data->offset = -1*(2^23);
    	data->slope.val1 = 1; 
    	data->slope.val2 = 0; 
    
    
    	err = bt_enable(NULL);
    	if (err) {
    		printk("Bluetooth init failed (err %d)\n", err);
    		return;
    	}
    
    	bt_ready();
    
    	bt_conn_auth_cb_register(&auth_cb_display);
    
    	/* Implement notification. At the moment there is no suitable way
    	 * of starting delayed work so we do it here
    	 */
    
    	
    	while (1) {
    		k_sleep(K_SECONDS(5));
    
    		bt_conn_foreach(connect_active,NULL);
    
    			//LOG_INF("BT Status : %i", default_conn);
    		measure(100);
    			/* Battery level simulation */
    		//	bas_notify();
    
    			//log_inf("disconnected");
    		
    
    
    	}
    }

  • I am glad to read that you found the comment helpful :) 
    In general it is better to move processing outside of the interrupt context, since they should be kept as short as possible.

    Are you sampling at a specific interval or frequency?
    If so, you might benefit from starting a timer instead, and then having that RTC / TIMER instance trigger the sampling operation when it expires.
    If you are sampling at high frequencies you should also consider setting this up using PPI to connect the RTC/TIMER EVENT and the sampling TASK.
    This could reduce your power consumption since the CPU does not have to wake up to trigger each sampling.

    Best regards,
    Karl

Related