Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
Fork of nRF51822_SimpleControls by
main.cpp
00001 /* 00002 00003 Copyright (c) 2012-2014 RedBearLab 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00006 and associated documentation files (the "Software"), to deal in the Software without restriction, 00007 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 00009 subject to the following conditions: 00010 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 00011 00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 00013 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 00014 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 00015 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00016 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 00018 */ 00019 00020 #include "mbed.h" 00021 #include "neopixel.h" 00022 #include "ble/BLE.h" 00023 #include "GattCallbackParamTypes.h" 00024 extern "C" { 00025 #include "ble_radio_notification.h" 00026 } 00027 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ 00028 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ 00029 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ 00030 00031 #define TXRX_BUF_LEN 20 00032 00033 #define DIGITAL_OUT_PIN P0_17 //D7 00034 #define BOARD_LED P0_19 // 00035 #define DIGITAL_IN_PIN P0_9 //P0_5 //A4 00036 #define PWM_PIN P0_16 //D6 00037 #define SERVO_PIN P0_14 //D10 00038 #define ANALOG_IN_PIN P0_6 //A5 00039 #define LED_DIG_OUT_PIN P0_8 //RTS 00040 00041 #define LED_HEADER 0x01 00042 #define ONOFF_HEADER 0x11 00043 #define COLOR_HEADER 0x22 00044 #define PATTERN_HEADER 0x33 00045 00046 #define NUM_LEDS 12 00047 00048 #define PATTERN_ON 0x01 00049 #define PATTERN_FAST 0x02 00050 #define PATTERN_SLOW 0x03 00051 #define PATTERN_CHASE 0x04 00052 #define PATTERN_MRB 0x05 00053 #define PATTERN_RAINBOW 0x06 00054 #define PATTERN_CANDY 0x07 00055 #define PATTERN_SNOW 0x08 00056 #define PATTERN_XMAS 0x09 00057 #define PATTERN_ICE 0x0A 00058 #define PATTERN_COL 0x0B 00059 #define PATTERN_IRON 0x0C 00060 #define LEDS_ON 0x01 00061 #define LEDS_OFF 0x02 00062 00063 BLE ble; 00064 00065 //DigitalOut LED_SET(DIGITAL_OUT_PIN); 00066 DigitalOut LED_SET(BOARD_LED); 00067 DigitalIn BUTTON(DIGITAL_IN_PIN); 00068 //PwmOut PWM(PWM_PIN); 00069 //AnalogIn ANALOG(ANALOG_IN_PIN); 00070 00071 //Serial pc(USBTX, USBRX); 00072 static uint8_t old_state = 0; 00073 neopixel_strip_t m_strip; 00074 uint8_t dig_pin_num = LED_DIG_OUT_PIN; 00075 uint8_t slow_count = 0; 00076 //uint8_t error; 00077 uint8_t led_to_enable = 2; 00078 uint8_t red = 255; 00079 uint8_t green = 0; 00080 uint8_t blue = 0; 00081 uint8_t pattern = 0; 00082 00083 char mDeviceName[128]; 00084 00085 uint8_t LEDS_ON_FLAG = 0; 00086 00087 // The Nordic UART Service 00088 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00089 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00090 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00091 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; 00092 00093 uint8_t txPayload[TXRX_BUF_LEN] = {0,}; 00094 uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; 00095 00096 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); 00097 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); 00098 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; 00099 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); 00100 void setup_advertising(void); 00101 00102 void rainbowCycle(int wait, uint8_t numLEDs, neopixel_strip_t m_strip); 00103 void candyChase(int wait, uint8_t numLEDs, neopixel_strip_t m_strip); 00104 void snowflakes(int wait, uint8_t numLEDs, neopixel_strip_t m_strip); 00105 void iceflakes(int delay, uint8_t numLEDs, neopixel_strip_t m_strip); 00106 void xmas(int delay, uint8_t numLEDs, neopixel_strip_t m_strip); 00107 void collegiate(int wait, uint8_t numLEDs, neopixel_strip_t m_strip); 00108 void irondude(int wait, uint8_t numLEDs, neopixel_strip_t m_strip); 00109 00110 extern uint32_t ble_radio_notification_init(nrf_app_irq_priority_t irq_priority, 00111 nrf_radio_notification_distance_t distance, 00112 ble_radio_notification_evt_handler_t evt_handler); 00113 00114 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) 00115 { 00116 neopixel_init(&m_strip, dig_pin_num, NUM_LEDS); 00117 neopixel_clear(&m_strip); 00118 LEDS_ON_FLAG = 0; 00119 } 00120 00121 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00122 { 00123 //pc.printf("Disconnected \r\n"); 00124 //pc.printf("Restart advertising \r\n"); 00125 00126 ble.startAdvertising(); 00127 } 00128 00129 void WrittenHandler(const GattWriteCallbackParams *Handler) 00130 { 00131 uint8_t buf[TXRX_BUF_LEN]; 00132 uint16_t bytesRead; 00133 00134 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) { 00135 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); 00136 memset(txPayload, 0, TXRX_BUF_LEN); 00137 memcpy(txPayload, buf, TXRX_BUF_LEN); 00138 00139 //for(index=0; index<bytesRead; index++) 00140 //pc.putc(buf[index]); 00141 00142 if(buf[0] == ONOFF_HEADER) { 00143 //pc.printf("LED value received \r\n"); 00144 if(buf[1] == LEDS_ON) { 00145 LEDS_ON_FLAG = 1; 00146 } else if(buf[1] == LEDS_OFF) { 00147 LEDS_ON_FLAG = 0; 00148 neopixel_init(&m_strip, dig_pin_num, NUM_LEDS); 00149 neopixel_clear(&m_strip); 00150 } 00151 } else if(buf[0] == COLOR_HEADER) { 00152 red = buf[1]; 00153 green = buf[2]; 00154 blue = buf[3]; 00155 pattern = buf[5]; 00156 } 00157 else 00158 { 00159 LEDS_ON_FLAG = 1; 00160 red = buf[1]; 00161 green = buf[2]; 00162 blue = buf[3]; 00163 pattern = buf[5]; 00164 } 00165 } 00166 } 00167 00168 void m_status_check_handle(void) 00169 { 00170 uint8_t buf[3]; 00171 00172 // If digital in changes, report the state 00173 if (BUTTON != old_state) { 00174 old_state = BUTTON; 00175 if (LEDS_ON_FLAG) 00176 LEDS_ON_FLAG = 0; 00177 else 00178 LEDS_ON_FLAG = 1; 00179 00180 if (BUTTON == 1) { 00181 buf[0] = (0x0A); 00182 buf[1] = (0x01); 00183 buf[2] = (0x00); 00184 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 00185 } else { 00186 buf[0] = (0x0A); 00187 buf[1] = (0x00); 00188 buf[2] = (0x00); 00189 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 00190 } 00191 } 00192 } 00193 00194 void ble_on_radio_active_evt(bool radio_active) 00195 { 00196 if ((radio_active == false) && (LEDS_ON_FLAG == 1)) { 00197 if (pattern == PATTERN_ON) { 00198 for (uint8_t i = 0; i <= NUM_LEDS; i++) 00199 neopixel_set_color(&m_strip, i, red, green, blue); 00200 neopixel_show(&m_strip); 00201 00202 } else if (pattern == PATTERN_FAST) { 00203 //neopixel_clear(&m_strip); 00204 for (uint8_t i = 0; i <= NUM_LEDS; i++) 00205 neopixel_set_color(&m_strip, i, 0, 0, 0); 00206 if (led_to_enable > NUM_LEDS) 00207 led_to_enable = 0; 00208 neopixel_set_color(&m_strip, led_to_enable, red, green, blue); 00209 neopixel_show(&m_strip); 00210 led_to_enable++; 00211 } else if (pattern == PATTERN_SLOW) { 00212 for (uint8_t i = 0; i <= NUM_LEDS; i++) 00213 neopixel_set_color(&m_strip, i, 0, 0, 0); 00214 if (led_to_enable > NUM_LEDS) 00215 led_to_enable = 0; 00216 neopixel_set_color(&m_strip, led_to_enable, red, green, blue); 00217 neopixel_show(&m_strip); 00218 led_to_enable++; 00219 wait_ms(1000); 00220 00221 } else if (pattern == PATTERN_CHASE) { 00222 for (uint8_t i = 0; i < NUM_LEDS; i++) 00223 neopixel_set_color(&m_strip, i, red, green, blue); 00224 neopixel_show(&m_strip); 00225 00226 } else if (pattern == PATTERN_MRB) { 00227 //neopixel_clear(&m_strip); 00228 for (uint8_t i = 0; i <= NUM_LEDS; i++) 00229 neopixel_set_color(&m_strip, i, 0, 0, 0); 00230 led_to_enable = rand()%NUM_LEDS; 00231 neopixel_set_color(&m_strip, led_to_enable, red, green, blue); 00232 neopixel_show(&m_strip); 00233 } 00234 00235 else if (pattern == PATTERN_RAINBOW) 00236 rainbowCycle(20, NUM_LEDS, m_strip); 00237 00238 else if (pattern == PATTERN_CANDY) 00239 candyChase(100, NUM_LEDS, m_strip); 00240 00241 else if (pattern == PATTERN_SNOW) 00242 snowflakes(250, NUM_LEDS, m_strip); 00243 00244 else if (pattern == PATTERN_ICE) 00245 iceflakes(150, NUM_LEDS, m_strip); 00246 00247 else if (pattern == PATTERN_XMAS) 00248 xmas(200, NUM_LEDS, m_strip); 00249 00250 else if (pattern == PATTERN_COL) 00251 collegiate(100, NUM_LEDS, m_strip); 00252 00253 else if (pattern == PATTERN_IRON) 00254 irondude(250, NUM_LEDS, m_strip); 00255 00256 else 00257 neopixel_clear(&m_strip); 00258 00259 } 00260 } 00261 00262 static void radio_notification_init(void) 00263 { 00264 //uint32_t err_code; 00265 ble_radio_notification_init(NRF_APP_PRIORITY_LOW, 00266 NRF_RADIO_NOTIFICATION_DISTANCE_800US, 00267 ble_on_radio_active_evt); 00268 //APP_ERROR_CHECK(err_code); 00269 } 00270 00271 void setup_advertising(void) 00272 { 00273 int length = strlen(mDeviceName); 00274 00275 // setup advertising 00276 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00277 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00278 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00279 (const uint8_t *)&mDeviceName, length); 00280 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00281 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); 00282 // In multiples of 0.625ms. 00283 ble.setAdvertisingInterval(80); 00284 ble.addService(uartService); 00285 ble.startAdvertising(); 00286 //pc.printf("Advertising Start \r\n"); 00287 } 00288 00289 int main(void) 00290 { 00291 //uint8_t led_error; 00292 00293 Ticker ticker; 00294 ticker.attach_us(m_status_check_handle, 200000); 00295 00296 //pc.printf("neoPixel Init \r\n"); 00297 neopixel_init(&m_strip, dig_pin_num, NUM_LEDS); 00298 neopixel_clear(&m_strip); 00299 00300 ble.init(); 00301 ble.onDisconnection(disconnectionCallback); 00302 ble.onConnection(connectionCallback); 00303 ble.onDataWritten(WrittenHandler); 00304 radio_notification_init(); 00305 //pc.baud(9600); 00306 //pc.format(8, SerialBase::None, 1); 00307 //pc.printf("rbChromaBand Init \r\n"); 00308 //pc.attach( uartCB , pc.RxIrq); 00309 00310 strcpy(mDeviceName, "RRVA BLE"); 00311 00312 setup_advertising(); 00313 00314 while(1) { 00315 //pc.putc(pc.getc()); 00316 ble.waitForEvent(); 00317 } 00318 } 00319 00320 00321 00322 00323 00324 00325 00326 00327 00328 00329 00330 00331 00332 00333 00334 00335 00336 00337 00338 00339 00340 00341 00342 00343 00344 00345 00346 00347 00348 00349 00350
Generated on Wed Jul 13 2022 12:45:47 by
