30
30
// --------------------------------------------------------------------+
31
31
32
32
BLEAdafruitSensor::BLEAdafruitSensor (BLEUuid service_uuid, BLEUuid data_uuid)
33
- : BLEService(service_uuid), _measurement(data_uuid), Period (UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD)
33
+ : BLEService(service_uuid), _measurement(data_uuid), _period (UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD)
34
34
{
35
35
_measure_cb = NULL ;
36
36
}
@@ -49,20 +49,27 @@ err_t BLEAdafruitSensor::begin(int32_t ms)
49
49
50
50
VERIFY_STATUS ( _measurement.begin () );
51
51
52
- Period .setProperties (CHR_PROPS_READ | CHR_PROPS_WRITE);
53
- Period .setPermission (SECMODE_OPEN, SECMODE_OPEN);
54
- Period .setFixedLen (4 );
55
- VERIFY_STATUS ( Period .begin () );
56
- Period .write32 (ms);
52
+ _period .setProperties (CHR_PROPS_READ | CHR_PROPS_WRITE);
53
+ _period .setPermission (SECMODE_OPEN, SECMODE_OPEN);
54
+ _period .setFixedLen (4 );
55
+ VERIFY_STATUS ( _period .begin () );
56
+ _period .write32 (ms);
57
57
58
- Period .setWriteCallback (sensor_period_write_cb, true );
58
+ _period .setWriteCallback (sensor_period_write_cb, true );
59
59
60
60
// setup timer
61
61
_timer.begin (ms, sensor_timer_cb, this , true );
62
62
63
63
return ERROR_NONE;
64
64
}
65
65
66
+ void BLEAdafruitSensor::setPeriod (int32_t period_ms)
67
+ {
68
+ _period.write32 (period_ms);
69
+ _update_timer (period_ms);
70
+ }
71
+
72
+
66
73
void BLEAdafruitSensor::startMeasuring (void )
67
74
{
68
75
_timer.start ();
@@ -73,25 +80,54 @@ void BLEAdafruitSensor::stopMeasuring(void)
73
80
_timer.stop ();
74
81
}
75
82
76
- // --------------------------------------------------------------------+
77
- // Static Callbacks
78
- // --------------------------------------------------------------------+
79
83
80
- void BLEAdafruitSensor::sensor_timer_cb (TimerHandle_t xTimer )
84
+ void BLEAdafruitSensor::_update_timer ( int32_t ms )
81
85
{
82
- BLEAdafruitSensor* svc = (BLEAdafruitSensor*) pvTimerGetTimerID (xTimer);
86
+ // TODO handle period = 0 which notify on changes ASAP
87
+ if ( ms < 0 )
88
+ {
89
+ _timer.stop ();
90
+ }else if ( ms > 0 )
91
+ {
92
+ _timer.setPeriod (ms);
93
+ }else
94
+ {
95
+ // Period = 0: keeping the current interval, but report on changes only
96
+ }
97
+ }
83
98
84
- if (svc->_measure_cb )
99
+ void BLEAdafruitSensor::_timer_callback (void )
100
+ {
101
+ if (_measure_cb)
85
102
{
86
- uint8_t buf[svc-> _measurement .getMaxLen ()];
87
- uint16_t len = svc-> _measure_cb (buf, sizeof (buf));
103
+ uint8_t buf[_measurement.getMaxLen ()];
104
+ uint16_t len = _measure_cb (buf, sizeof (buf));
88
105
len = min (len, sizeof (buf));
89
106
90
- // notify
91
- svc->_measurement .notify (buf, len);
107
+ // Period = 0, compare with old data, only update on changes
108
+ if ( 0 == _period.read32 () )
109
+ {
110
+ uint8_t prev_buf[_measurement.getMaxLen ()];
111
+ _measurement.read (prev_buf, sizeof (prev_buf));
112
+
113
+ // skip notify if there is no changes
114
+ if ( 0 == memcmp (prev_buf, buf, len) ) return ;
115
+ }
116
+
117
+ _measurement.notify (buf, len);
92
118
}
93
119
}
94
120
121
+ // --------------------------------------------------------------------+
122
+ // Static Callbacks
123
+ // --------------------------------------------------------------------+
124
+
125
+ void BLEAdafruitSensor::sensor_timer_cb (TimerHandle_t xTimer)
126
+ {
127
+ BLEAdafruitSensor* svc = (BLEAdafruitSensor*) pvTimerGetTimerID (xTimer);
128
+ svc->_timer_callback ();
129
+ }
130
+
95
131
// Client update period, adjust timer accordingly
96
132
void BLEAdafruitSensor::sensor_period_write_cb (uint16_t conn_hdl, BLECharacteristic* chr, uint8_t * data, uint16_t len)
97
133
{
@@ -100,14 +136,7 @@ void BLEAdafruitSensor::sensor_period_write_cb(uint16_t conn_hdl, BLECharacteris
100
136
int32_t ms = 0 ;
101
137
memcpy (&ms, data, len);
102
138
103
- // TODO handle period = 0 which notify on changes ASAP
104
- if ( ms < 0 )
105
- {
106
- svc._timer .stop ();
107
- }else
108
- {
109
- svc._timer .setPeriod (ms);
110
- }
139
+ svc._update_timer (ms);
111
140
}
112
141
113
142
void BLEAdafruitSensor::sensor_data_cccd_cb (uint16_t conn_hdl, BLECharacteristic* chr, uint16_t value)
0 commit comments