1919 */
2020
2121#include < bluefruit.h>
22+ #include < Adafruit_NeoPixel.h>
2223
2324// max concurrent connections supported by this example
2425#define MAX_PRPH_CONNECTION 2
2526uint8_t connection_count = 0 ;
2627
27- /* HRM Service Definitions
28- * Heart Rate Monitor Service: 0x180D
29- * Heart Rate Measurement Char: 0x2A37
30- * Body Sensor Location Char: 0x2A38
31- */
32- BLEService hrms = BLEService(UUID16_SVC_HEART_RATE);
33- BLECharacteristic hrmc = BLECharacteristic(UUID16_CHR_HEART_RATE_MEASUREMENT);
34- BLECharacteristic bslc = BLECharacteristic(UUID16_CHR_BODY_SENSOR_LOCATION);
35-
36- /* LBS Service: 00001523-1212-EFDE-1523-785FEABCD123
28+ /* Nordic Led-Button Service (LBS)
29+ * LBS Service: 00001523-1212-EFDE-1523-785FEABCD123
3730 * LBS Button : 00001524-1212-EFDE-1523-785FEABCD123
3831 * LBS LED : 00001525-1212-EFDE-1523-785FEABCD123
3932 */
4033
4134const uint8_t LBS_UUID_SERVICE[] =
4235{
43- 0x23 , 0xD1 , 0xBC , 0xEA , 0x5F , 0x78 , 0x23 , 0x15 ,
44- 0xDE , 0xEF , 0x12 , 0x12 , 0x23 , 0x15 , 0x00 , 0x00
36+ 0x23 , 0xD1 , 0xBC , 0xEA , 0x5F , 0x78 , 0x23 , 0x15 ,
37+ 0xDE , 0xEF , 0x12 , 0x12 , 0x23 , 0x15 , 0x00 , 0x00
4538};
4639
4740const uint8_t LBS_UUID_CHR_BUTTON[] =
4841{
49- 0x23 , 0xD1 , 0xBC , 0xEA , 0x5F , 0x78 , 0x23 , 0x15 ,
50- 0xDE , 0xEF , 0x12 , 0x12 , 0x24 , 0x15 , 0x00 , 0x00
42+ 0x23 , 0xD1 , 0xBC , 0xEA , 0x5F , 0x78 , 0x23 , 0x15 ,
43+ 0xDE , 0xEF , 0x12 , 0x12 , 0x24 , 0x15 , 0x00 , 0x00
5144};
5245
5346const uint8_t LBS_UUID_CHR_LED[] =
5447{
55- 0x23 , 0xD1 , 0xBC , 0xEA , 0x5F , 0x78 , 0x23 , 0x15 ,
56- 0xDE , 0xEF , 0x12 , 0x12 , 0x25 , 0x15 , 0x00 , 0x00
48+ 0x23 , 0xD1 , 0xBC , 0xEA , 0x5F , 0x78 , 0x23 , 0x15 ,
49+ 0xDE , 0xEF , 0x12 , 0x12 , 0x25 , 0x15 , 0x00 , 0x00
5750};
5851
5952BLEService lbs (LBS_UUID_SERVICE);
@@ -67,15 +60,36 @@ BLECharacteristic lsbLED(LBS_UUID_CHR_LED);
6760 uint8_t button = A0;
6861#endif
6962
63+ // CPB button active state is HIGH, all other is low
64+ #ifdef ARDUINO_NRF52840_CIRCUITPLAY
65+ #define BUTTON_ACTIVE HIGH
66+ #else
67+ #define BUTTON_ACTIVE LOW
68+ #endif
69+
7070uint8_t buttonState;
7171
72+ #ifdef PIN_NEOPIXEL
73+
74+ #ifndef NEOPIXEL_NUM
75+ #define NEOPIXEL_NUM 1
76+ #endif
77+
78+ Adafruit_NeoPixel neopixel = Adafruit_NeoPixel(NEOPIXEL_NUM, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
79+
80+ #endif
81+
7282void setup ()
7383{
84+ pinMode (button, BUTTON_ACTIVE ? INPUT_PULLDOWN : INPUT_PULLUP);
85+ buttonState = (BUTTON_ACTIVE == digitalRead (button));
86+
7487 pinMode (LED_BUILTIN, OUTPUT);
7588 digitalWrite (LED_BUILTIN, 1 -LED_STATE_ON); // led off
7689
77- pinMode (button, INPUT_PULLUP);
78- buttonState = (uint8_t ) (1 -digitalRead (button)); // button is active LOW
90+ #ifdef PIN_NEOPIXEL
91+ neopixel.begin ();
92+ #endif
7993
8094 Serial.begin (115200 );
8195 // while ( !Serial ) delay(10); // for nrf52840 with native usb
@@ -162,15 +176,21 @@ void led_write_callback(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data
162176 // data = 1 -> LED = On
163177 // data = 0 -> LED = Off
164178 digitalWrite (LED_BUILTIN, data[0 ] ? LED_STATE_ON : (1 -LED_STATE_ON));
179+
180+ #ifdef PIN_NEOPIXEL
181+ uint32_t c = neopixel.Color (0x00 , 0x00 , data[0 ] ? 0x20 : 0x00 );
182+ neopixel.fill (c, 0 , NEOPIXEL_NUM);
183+ neopixel.show ();
184+ #endif
165185}
166186
167187void loop ()
168188{
169189 delay (10 ); // poll button every 10 ms
170190
171- uint8_t newState = (uint8_t ) ( 1 - digitalRead (button)); // button is active LOW
191+ uint8_t newState = (BUTTON_ACTIVE == digitalRead (button));
172192
173- // only notify if button state chagnes
193+ // only notify if button state changes
174194 if ( newState != buttonState)
175195 {
176196 buttonState = newState;
0 commit comments