1414
1515/* This sketch demonstrate the BLE Adafruit Service that is used with
1616 * "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
1920 */
2021
2122#include < Adafruit_LittleFS.h>
@@ -41,59 +42,92 @@ BLEAdafruitTone bleTone;
4142
4243BLEAdafruitAddressablePixel blePixel;
4344
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 -------------//
4747
48- #define DEVICE_NAME " CPlay"
49- #define NEOPIXEL_COUNT 10
48+ #include < Adafruit_CircuitPlayground.h>
5049
50+ #define DEVICE_NAME " CPlay"
51+ #define NEOPIXEL_COUNT 10
5152
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+ }
5659
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;
5963
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 ();
6467
65- # error "Board is not supported"
66- # endif
68+ return 3 * sizeof ( float ); // 12
69+ }
6770
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+ }
6978
70- uint16_t measure_temperature (uint8_t * buf, uint16_t bufsize)
79+ uint16_t measure_gyro (uint8_t * buf, uint16_t bufsize)
7180{
72- float temp;
7381
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 ;
7692
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>
79107
108+ #if defined(ARDUINO_NRF52840_CLUE)
109+ #define DEVICE_NAME " CLUE"
110+ #else
111+ #define DEVICE_NAME " Sense"
80112#endif
81113
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 ();
82123 memcpy (buf, &temp, 4 );
83124 return 4 ;
84125}
85126
86127uint16_t measure_accel (uint8_t * buf, uint16_t bufsize)
87- {
128+ {
88129 float * accel_buf = (float *) buf;
89130
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-
97131 sensors_event_t accel, gyro, temp;
98132 (void ) gyro; (void ) temp;
99133
@@ -103,23 +137,17 @@ uint16_t measure_accel(uint8_t* buf, uint16_t bufsize)
103137 accel_buf[1 ] = accel.acceleration .y ;
104138 accel_buf[2 ] = accel.acceleration .z ;
105139
106- #endif
107-
108140 return 3 *sizeof (float ); // 12
109141}
110142
111143uint16_t measure_light_sensor (uint8_t * buf, uint16_t bufsize)
112144{
113145 float lux;
114146
115- #if defined ARDUINO_NRF52840_CIRCUITPLAY
116- lux = CircuitPlayground.lightSensor ();
117- #else
118147 uint16_t r, g, b, c;
119148 apds9960.getColorData (&r, &g, &b, &c);
120149
121150 lux = c;
122- #endif
123151
124152 memcpy (buf, &lux, 4 );
125153 return 4 ;
@@ -137,38 +165,35 @@ uint16_t measure_magnetic(uint8_t* buf, uint16_t bufsize)
137165
138166uint16_t measure_button (uint8_t * buf, uint16_t bufsize)
139167{
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
148168 // Button is active LOW on most board except CPlay
149-
150169 // No slide switch
170+
171+ uint32_t button = 0 ;
151172 button |= ( digitalRead (PIN_BUTTON1) ? 0x00 : 0x02 );
152173 button |= ( digitalRead (PIN_BUTTON2) ? 0x00 : 0x04 );
153174
154- #endif
155-
156175 memcpy (buf, &button, 4 );
157176 return 4 ;
158177}
159178
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 -------------//
160186void setup ()
161187{
162188 Serial.begin (115200 );
163- // while ( !Serial ) delay(10); // for nrf52840 with native usb
164189
165190 Serial.println (" Bluefruit52 BLEUART Example" );
166191 Serial.println (" ---------------------------\n " );
167192
168193#if defined ARDUINO_NRF52840_CIRCUITPLAY
169194 CircuitPlayground.begin ();
170195
171- #elif defined ARDUINO_NRF52840_CLUE
196+ #else
172197
173198 // Button
174199 pinMode (PIN_BUTTON1, INPUT_PULLUP);
@@ -229,12 +254,6 @@ void setup()
229254 bleLight.begin ();
230255 bleLight.setMeasureCallback (measure_light_sensor);
231256
232- // bleGyro.begin();
233- // bleGyro.setMeasureCallback(measure_gyro);
234- //
235- // bleMagnetic.begin();
236- // bleMagnetic.setMeasureCallback(measure_magnetic);
237-
238257 bleButton.begin ();
239258 bleButton.setMeasureCallback (measure_button);
240259 bleButton.setPeriod (0 ); // only notify if there is changes with buttons
@@ -244,6 +263,15 @@ void setup()
244263 strip.begin ();
245264 blePixel.begin (&strip);
246265
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+
247275 // Set up and start advertising
248276 startAdv ();
249277
0 commit comments