14
14
15
15
/* This sketch demonstrate the BLE Adafruit Service that is used with
16
16
* "Adafruit Bluefruit Playground" app. Supported boards are
17
- * - Circuit Playground Bluefruit (https://www.adafruit.com/product/4333)
18
- * - CLUE nRF52840 (https://www.adafruit.com/product/4500)
17
+ * - Circuit Playground Bluefruit : https://www.adafruit.com/product/4333
18
+ * - CLUE nRF52840 : https://www.adafruit.com/product/4500
19
+ * - Feather Sense : https://www.adafruit.com/product/4516
19
20
*/
20
21
21
22
#include < Adafruit_LittleFS.h>
@@ -41,59 +42,92 @@ BLEAdafruitTone bleTone;
41
42
42
43
BLEAdafruitAddressablePixel blePixel;
43
44
44
- // Circuit Playground Bluefruit
45
- #if defined( ARDUINO_NRF52840_CIRCUITPLAY )
46
- #include < Adafruit_CircuitPlayground.h>
45
+ #if defined(ARDUINO_NRF52840_CIRCUITPLAY)
46
+ // ------------- Circuit Playground Bluefruit -------------//
47
47
48
- #define DEVICE_NAME " CPlay"
49
- #define NEOPIXEL_COUNT 10
48
+ #include < Adafruit_CircuitPlayground.h>
50
49
50
+ #define DEVICE_NAME " CPlay"
51
+ #define NEOPIXEL_COUNT 10
51
52
52
- #elif defined( ARDUINO_NRF52840_CLUE ) || defined( ARDUINO_NRF52840_FEATHER_SENSE )
53
- #include < Adafruit_APDS9960.h>
54
- #include < Adafruit_LSM6DS33.h>
55
- #include < Adafruit_BMP280.h>
53
+ uint16_t measure_temperature (uint8_t * buf, uint16_t bufsize)
54
+ {
55
+ float temp = CircuitPlayground.temperature ();
56
+ memcpy (buf, &temp, 4 );
57
+ return 4 ;
58
+ }
56
59
57
- #define DEVICE_NAME " CLUE"
58
- #define NEOPIXEL_COUNT 1
60
+ uint16_t measure_accel (uint8_t * buf, uint16_t bufsize)
61
+ {
62
+ float * accel_buf = (float *) buf;
59
63
60
- Adafruit_APDS9960 apds9960;
61
- Adafruit_LSM6DS33 lsm6ds33;
62
- Adafruit_BMP280 bmp280;
63
- #else
64
+ accel_buf[0 ] = CircuitPlayground.motionX ();
65
+ accel_buf[1 ] = CircuitPlayground.motionY ();
66
+ accel_buf[2 ] = CircuitPlayground.motionZ ();
64
67
65
- # error "Board is not supported"
66
- # endif
68
+ return 3 * sizeof ( float ); // 12
69
+ }
67
70
68
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(NEOPIXEL_COUNT, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
71
+ uint16_t measure_light_sensor (uint8_t * buf, uint16_t bufsize)
72
+ {
73
+ float lux;
74
+ lux = CircuitPlayground.lightSensor ();
75
+ memcpy (buf, &lux, 4 );
76
+ return 4 ;
77
+ }
69
78
70
- uint16_t measure_temperature (uint8_t * buf, uint16_t bufsize)
79
+ uint16_t measure_gyro (uint8_t * buf, uint16_t bufsize)
71
80
{
72
- float temp;
73
81
74
- #if defined ARDUINO_NRF52840_CIRCUITPLAY
75
- temp = CircuitPlayground.temperature ();
82
+ }
83
+
84
+ uint16_t measure_magnetic (uint8_t * buf, uint16_t bufsize)
85
+ {
86
+
87
+ }
88
+
89
+ uint16_t measure_button (uint8_t * buf, uint16_t bufsize)
90
+ {
91
+ uint32_t button = 0 ;
76
92
77
- #elif defined ARDUINO_NRF52840_CLUE
78
- temp = bmp280.readTemperature ();
93
+ button |= ( CircuitPlayground.slideSwitch () ? 0x01 : 0x00 );
94
+ button |= ( CircuitPlayground.leftButton () ? 0x02 : 0x00 );
95
+ button |= ( CircuitPlayground.rightButton () ? 0x04 : 0x00 );
96
+
97
+ memcpy (buf, &button, 4 );
98
+ return 4 ;
99
+ }
100
+
101
+ #elif defined(ARDUINO_NRF52840_CLUE) || defined(ARDUINO_NRF52840_FEATHER_SENSE)
102
+ // ------------- CLUE & Feather Sense -------------//
103
+
104
+ #include < Adafruit_APDS9960.h>
105
+ #include < Adafruit_LSM6DS33.h>
106
+ #include < Adafruit_BMP280.h>
79
107
108
+ #if defined(ARDUINO_NRF52840_CLUE)
109
+ #define DEVICE_NAME " CLUE"
110
+ #else
111
+ #define DEVICE_NAME " Sense"
80
112
#endif
81
113
114
+ #define NEOPIXEL_COUNT 1
115
+
116
+ Adafruit_APDS9960 apds9960;
117
+ Adafruit_LSM6DS33 lsm6ds33;
118
+ Adafruit_BMP280 bmp280;
119
+
120
+ uint16_t measure_temperature (uint8_t * buf, uint16_t bufsize)
121
+ {
122
+ float temp = bmp280.readTemperature ();
82
123
memcpy (buf, &temp, 4 );
83
124
return 4 ;
84
125
}
85
126
86
127
uint16_t measure_accel (uint8_t * buf, uint16_t bufsize)
87
- {
128
+ {
88
129
float * accel_buf = (float *) buf;
89
130
90
- #if defined ARDUINO_NRF52840_CIRCUITPLAY
91
- accel_buf[0 ] = CircuitPlayground.motionX ();
92
- accel_buf[1 ] = CircuitPlayground.motionY ();
93
- accel_buf[2 ] = CircuitPlayground.motionZ ();
94
-
95
- #elif defined ARDUINO_NRF52840_CLUE
96
-
97
131
sensors_event_t accel, gyro, temp;
98
132
(void ) gyro; (void ) temp;
99
133
@@ -103,23 +137,17 @@ uint16_t measure_accel(uint8_t* buf, uint16_t bufsize)
103
137
accel_buf[1 ] = accel.acceleration .y ;
104
138
accel_buf[2 ] = accel.acceleration .z ;
105
139
106
- #endif
107
-
108
140
return 3 *sizeof (float ); // 12
109
141
}
110
142
111
143
uint16_t measure_light_sensor (uint8_t * buf, uint16_t bufsize)
112
144
{
113
145
float lux;
114
146
115
- #if defined ARDUINO_NRF52840_CIRCUITPLAY
116
- lux = CircuitPlayground.lightSensor ();
117
- #else
118
147
uint16_t r, g, b, c;
119
148
apds9960.getColorData (&r, &g, &b, &c);
120
149
121
150
lux = c;
122
- #endif
123
151
124
152
memcpy (buf, &lux, 4 );
125
153
return 4 ;
@@ -137,38 +165,35 @@ uint16_t measure_magnetic(uint8_t* buf, uint16_t bufsize)
137
165
138
166
uint16_t measure_button (uint8_t * buf, uint16_t bufsize)
139
167
{
140
- uint32_t button = 0 ;
141
-
142
- #if defined ARDUINO_NRF52840_CIRCUITPLAY
143
- button |= ( CircuitPlayground.slideSwitch () ? 0x01 : 0x00 );
144
- button |= ( CircuitPlayground.leftButton () ? 0x02 : 0x00 );
145
- button |= ( CircuitPlayground.rightButton () ? 0x04 : 0x00 );
146
-
147
- #elif defined ARDUINO_NRF52840_CLUE
148
168
// Button is active LOW on most board except CPlay
149
-
150
169
// No slide switch
170
+
171
+ uint32_t button = 0 ;
151
172
button |= ( digitalRead (PIN_BUTTON1) ? 0x00 : 0x02 );
152
173
button |= ( digitalRead (PIN_BUTTON2) ? 0x00 : 0x04 );
153
174
154
- #endif
155
-
156
175
memcpy (buf, &button, 4 );
157
176
return 4 ;
158
177
}
159
178
179
+ #else
180
+ #error "Board is not supported"
181
+ #endif
182
+
183
+ Adafruit_NeoPixel strip = Adafruit_NeoPixel(NEOPIXEL_COUNT, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
184
+
185
+ // ------------- Setup -------------//
160
186
void setup ()
161
187
{
162
188
Serial.begin (115200 );
163
- // while ( !Serial ) delay(10); // for nrf52840 with native usb
164
189
165
190
Serial.println (" Bluefruit52 BLEUART Example" );
166
191
Serial.println (" ---------------------------\n " );
167
192
168
193
#if defined ARDUINO_NRF52840_CIRCUITPLAY
169
194
CircuitPlayground.begin ();
170
195
171
- #elif defined ARDUINO_NRF52840_CLUE
196
+ #else
172
197
173
198
// Button
174
199
pinMode (PIN_BUTTON1, INPUT_PULLUP);
@@ -229,12 +254,6 @@ void setup()
229
254
bleLight.begin ();
230
255
bleLight.setMeasureCallback (measure_light_sensor);
231
256
232
- // bleGyro.begin();
233
- // bleGyro.setMeasureCallback(measure_gyro);
234
- //
235
- // bleMagnetic.begin();
236
- // bleMagnetic.setMeasureCallback(measure_magnetic);
237
-
238
257
bleButton.begin ();
239
258
bleButton.setMeasureCallback (measure_button);
240
259
bleButton.setPeriod (0 ); // only notify if there is changes with buttons
@@ -244,6 +263,15 @@ void setup()
244
263
strip.begin ();
245
264
blePixel.begin (&strip);
246
265
266
+ // CPB doesn't support these on-board sensor
267
+ #ifndef ARDUINO_NRF52840_CIRCUITPLAY
268
+ // bleGyro.begin();
269
+ // bleGyro.setMeasureCallback(measure_gyro);
270
+ //
271
+ // bleMagnetic.begin();
272
+ // bleMagnetic.setMeasureCallback(measure_magnetic);
273
+ #endif
274
+
247
275
// Set up and start advertising
248
276
startAdv ();
249
277
0 commit comments