Skip to content

Commit 603447a

Browse files
committed
adding abstract class BLEAdafruitSensor
1 parent 33857e9 commit 603447a

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

libraries/Bluefruit52Lib/src/BLECharacteristic.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,11 @@ uint16_t BLECharacteristic::write32(int num)
545545
return write32( (uint32_t) num );
546546
}
547547

548+
uint16_t BLECharacteristic::write32(int32_t num)
549+
{
550+
return write32( (uint32_t) num );
551+
}
552+
548553
/*------------------------------------------------------------------*/
549554
/* READ
550555
*------------------------------------------------------------------*/

libraries/Bluefruit52Lib/src/BLECharacteristic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class BLECharacteristic
125125
uint16_t write16 (uint16_t num);
126126
uint16_t write32 (uint32_t num);
127127
uint16_t write32 (int num);
128+
uint16_t write32 (int32_t num);
128129

129130
/*------------- Read -------------*/
130131
uint16_t read (void* buffer, uint16_t bufsize, uint16_t offset = 0);
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Ha Thach (tinyusb.org) for Adafruit Industries
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#include "bluefruit.h"
26+
#include "BLEAdafruitSensor.h"
27+
28+
//--------------------------------------------------------------------+
29+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
30+
//--------------------------------------------------------------------+
31+
32+
BLEAdafruitSensor::BLEAdafruitSensor(void) :
33+
Period(UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD)
34+
{
35+
_measure_cb = NULL;
36+
}
37+
38+
void BLEAdafruitSensor::setMeasureCallback(measure_callback_t fp)
39+
{
40+
_measure_cb = fp;
41+
}
42+
43+
bool BLEAdafruitSensor::begin(int32_t ms)
44+
{
45+
// Invoke base class begin()
46+
VERIFY_STATUS( BLEService::begin() );
47+
48+
Period.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE);
49+
Period.setPermission(SECMODE_OPEN, SECMODE_OPEN);
50+
Period.setFixedLen(4);
51+
VERIFY_STATUS( Period.begin() );
52+
Period.write32(ms);
53+
54+
Period.setWriteCallback(sensor_period_write_cb, true);
55+
56+
// setup timer
57+
_timer.begin(ms, sensor_timer_cb, this, true);
58+
59+
return true;
60+
}
61+
62+
63+
void BLEAdafruitSensor::sensor_timer_cb(TimerHandle_t xTimer)
64+
{
65+
BLEAdafruitSensor* svc = (BLEAdafruitSensor*) pvTimerGetTimerID(xTimer);
66+
67+
// invoke measure callback
68+
if (svc->_measure_cb) svc->_measure_cb();
69+
}
70+
71+
// Client update period, adjust timer accordingly
72+
void BLEAdafruitSensor::sensor_period_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len)
73+
{
74+
BLEAdafruitSensor& svc = (BLEAdafruitSensor&) chr->parentService();
75+
76+
int32_t ms = 0;
77+
memcpy(&ms, data, len);
78+
79+
// TODO handle period = 0 which notify on changes ASAP
80+
if ( ms < 0 )
81+
{
82+
svc._timer.stop();
83+
}else
84+
{
85+
svc._timer.setPeriod(ms);
86+
}
87+
}
88+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Ha Thach (tinyusb.org) for Adafruit Industries
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#ifndef BLE_ADAFRUIT_SENSOR_H_
26+
#define BLE_ADAFRUIT_SENSOR_H_
27+
28+
#include "bluefruit_common.h"
29+
30+
#include "BLECharacteristic.h"
31+
#include "BLEService.h"
32+
33+
class BLEAdafruitSensor : public BLEService
34+
{
35+
public:
36+
typedef void (*measure_callback_t )(void);
37+
38+
BLEAdafruitSensor(void);
39+
virtual bool begin(int32_t ms);
40+
41+
void setMeasureCallback(measure_callback_t fp);
42+
43+
protected:
44+
measure_callback_t _measure_cb;
45+
SoftwareTimer _timer;
46+
47+
BLECharacteristic Period;
48+
49+
static void sensor_timer_cb(TimerHandle_t xTimer);
50+
static void sensor_period_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len);
51+
};
52+
53+
#endif /* BLE_ADAFRUIT_SENSOR_H_ */

0 commit comments

Comments
 (0)