
Pulsating LEDs
Dependencies: BLE_API mbed nRF51822
Fork of BLE_BADGE by
Revision 10:fdf87f019389, committed 2017-01-16
- Comitter:
- davidadkins1
- Date:
- Mon Jan 16 18:08:54 2017 +0000
- Parent:
- 9:5f0732aa3008
- Commit message:
- Pulsating function
Changed in this revision
diff -r 5f0732aa3008 -r fdf87f019389 BLE_API.lib --- a/BLE_API.lib Tue Sep 29 12:12:10 2015 +0000 +++ b/BLE_API.lib Mon Jan 16 18:08:54 2017 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#d494ad3e87bd +http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#65474dc93927
diff -r 5f0732aa3008 -r fdf87f019389 main.cpp --- a/main.cpp Tue Sep 29 12:12:10 2015 +0000 +++ b/main.cpp Mon Jan 16 18:08:54 2017 +0000 @@ -20,8 +20,39 @@ #include "UARTService.h" -#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; +#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; * it will have an impact on code-size and power consumption. */ + +#define TXRX_BUF_LEN 20 + +// sinetable is loaded with a progressive tick time +// designed to continuesly vary the PWM period +// for a 5 secound time frame. +extern float sinetable[256]; + +// LED defines and variables +#define GREEN_LED_PIN P0_7 +#define RED_LED_PIN P0_3 + +#define LED_ON 0x00 +#define LED_OFF 0x01 +#define MAX_LED_LEVEL 0x0A + +// Command set +#define CMD_GREEN 0x01 +#define CMD_RED 0x02 +#define CMD_OFF 0x03 + +#define TICK_FRAME 0.02 + +DigitalOut LED_SET_GREEN(GREEN_LED_PIN); +DigitalOut LED_SET_RED(RED_LED_PIN); +Ticker led_pwm_tick; // for LED PWM + +uint8_t next_angle = 0; // Counter for 5 secound pulsating +float next_tick; // Current PWM pulse width +uint8_t led_brightness = 0; // Used to scale the PWM period + // 0 is off, 10 is always on #if NEED_CONSOLE_OUTPUT #define DEBUG(STR) { if (uart) uart->write(STR, strlen(STR)); } @@ -30,31 +61,156 @@ #endif /* #if NEED_CONSOLE_OUTPUT */ BLEDevice ble; -DigitalOut led1(LED1); UARTService *uart; +uint8_t txPayload[TXRX_BUF_LEN] = {0,}; +//static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; + +//GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); + +// Local prototypes +void green_pwm_tick_off(void); +void green_pwm_tick_on(void); +void red_pwm_tick_off(void); +void red_pwm_tick_on(void); +void pwm_tick_min(void); + void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { - DEBUG("Disconnected!\n\r"); - DEBUG("Restarting the advertising process\n\r"); + //DEBUG("Disconnected!\n\r"); + //DEBUG("Restarting the advertising process\n\r"); ble.startAdvertising(); } -void periodicCallback(void) +void WrittenHandler(const GattWriteCallbackParams *Handler) +{ + uint8_t buf[TXRX_BUF_LEN]; + //uint16_t bytesRead; + //uint16_t index; + //if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) + { + //ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); + //memset(txPayload, 0, TXRX_BUF_LEN); + //memcpy(txPayload, buf, TXRX_BUF_LEN); + buf[0] = uart->_getc(); + + + if(CMD_GREEN == buf[0]) + { + led_brightness = uart->_getc(); + + if(led_brightness > MAX_LED_LEVEL) + { + led_brightness = MAX_LED_LEVEL; + } + + if(MAX_LED_LEVEL == led_brightness) + { + LED_SET_GREEN = LED_ON; + led_pwm_tick.attach(pwm_tick_min, 5.0); + } + else if(0 == led_brightness) + { + LED_SET_GREEN = LED_OFF; + led_pwm_tick.attach(pwm_tick_min, 5.0); + } + else + { + led_pwm_tick.attach(green_pwm_tick_on, .001); + next_angle = 0; + } + + LED_SET_RED = LED_OFF; + } + else if(CMD_RED == buf[0]) + { + led_brightness = uart->_getc(); + + if(led_brightness > MAX_LED_LEVEL) + { + led_brightness = MAX_LED_LEVEL; + } + + if(MAX_LED_LEVEL == led_brightness) + { + LED_SET_RED = LED_ON; + led_pwm_tick.attach(pwm_tick_min, 5.0); + } + else if(0 == led_brightness) + { + LED_SET_RED = LED_OFF; + led_pwm_tick.attach(pwm_tick_min, 5.0); + } + else + { + LED_SET_RED = LED_ON; + led_pwm_tick.attach(red_pwm_tick_on, .001); + } + + LED_SET_GREEN = LED_OFF; + } + else if(CMD_OFF == buf[0]) + { + LED_SET_GREEN = LED_OFF; + LED_SET_RED = LED_OFF; + led_pwm_tick.attach(pwm_tick_min, 5.0); + } + } +} + +void pwm_tick_min() { - led1 = !led1; - DEBUG("ping\r\n"); + // do nothing in this tick while LED's are off + // 5 second tick to allow processor max sleep time +} + +void green_pwm_tick_on(void) +{ + next_tick = sinetable[next_angle] * ((float)led_brightness / (MAX_LED_LEVEL - 1)); + + LED_SET_GREEN = LED_ON; + led_pwm_tick.attach(green_pwm_tick_off, next_tick); +} + +void green_pwm_tick_off() +{ + next_tick = TICK_FRAME - next_tick; + + LED_SET_GREEN = LED_OFF; + led_pwm_tick.attach(green_pwm_tick_on, next_tick); + + ++next_angle; +} + +void red_pwm_tick_on(void) +{ + next_tick = sinetable[next_angle] * ((float)led_brightness / (MAX_LED_LEVEL - 1)); + + LED_SET_RED = LED_ON; + led_pwm_tick.attach(red_pwm_tick_off, next_tick); +} + +void red_pwm_tick_off() +{ + next_tick = TICK_FRAME - next_tick; + + LED_SET_RED = LED_OFF; + led_pwm_tick.attach(red_pwm_tick_on, next_tick); + + ++next_angle; } int main(void) { - led1 = 1; - Ticker ticker; - ticker.attach(periodicCallback, 1); - - DEBUG("Initialising the nRF51822\n\r"); + LED_SET_GREEN = LED_OFF; + LED_SET_RED = LED_OFF; + ble.init(); ble.onDisconnection(disconnectionCallback); + ble.onDataWritten(WrittenHandler); + + //led_brightness = 5; + led_pwm_tick.attach(pwm_tick_min, 5.0); // uart = new UARTService(ble); @@ -62,14 +218,17 @@ ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, - (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); + (const uint8_t *)"Smart Badge", sizeof("Smart Badge") - 1); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, - (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); + (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); - ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ - ble.startAdvertising(); + ble.gap().setAdvertisingInterval(2000); /* in multiples of 1 ms. */ + ble.gap().setTxPower(-40); - while (true) { + ble.gap().startAdvertising(); + + while (true) + { ble.waitForEvent(); } }
diff -r 5f0732aa3008 -r fdf87f019389 mbed.bld --- a/mbed.bld Tue Sep 29 12:12:10 2015 +0000 +++ b/mbed.bld Mon Jan 16 18:08:54 2017 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/4f6c30876dfa \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/99b5ccf27215 \ No newline at end of file
diff -r 5f0732aa3008 -r fdf87f019389 nRF51822.lib --- a/nRF51822.lib Tue Sep 29 12:12:10 2015 +0000 +++ b/nRF51822.lib Mon Jan 16 18:08:54 2017 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#088f5738bf18 +http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#c90ae1400bf2
diff -r 5f0732aa3008 -r fdf87f019389 sinetable.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sinetable.c Mon Jan 16 18:08:54 2017 +0000 @@ -0,0 +1,260 @@ +const float sinetable[256] = +{ + 0.0100000, + 0.0102454, + 0.0104907, + 0.0107356, + 0.0109802, + 0.0112241, + 0.0114673, + 0.0117096, + 0.0119509, + 0.0121910, + 0.0124298, + 0.0126671, + 0.0129028, + 0.0131368, + 0.0133689, + 0.0135990, + 0.0138268, + 0.0140524, + 0.0142756, + 0.0144961, + 0.0147140, + 0.0149290, + 0.0151410, + 0.0153500, + 0.0155557, + 0.0157581, + 0.0159570, + 0.0161523, + 0.0163439, + 0.0165317, + 0.0167156, + 0.0168954, + 0.0170711, + 0.0172425, + 0.0174095, + 0.0175721, + 0.0177301, + 0.0178835, + 0.0180321, + 0.0181758, + 0.0183147, + 0.0184485, + 0.0185773, + 0.0187009, + 0.0188192, + 0.0189322, + 0.0190399, + 0.0191421, + 0.0192388, + 0.0193299, + 0.0194154, + 0.0194953, + 0.0195694, + 0.0196378, + 0.0197003, + 0.0197570, + 0.0198079, + 0.0198528, + 0.0198918, + 0.0199248, + 0.0199518, + 0.0199729, + 0.0199880, + 0.0199970, + 0.0200000, + 0.0199970, + 0.0199880, + 0.0199729, + 0.0199518, + 0.0199248, + 0.0198918, + 0.0198528, + 0.0198079, + 0.0197570, + 0.0197003, + 0.0196378, + 0.0195694, + 0.0194953, + 0.0194154, + 0.0193299, + 0.0192388, + 0.0191421, + 0.0190399, + 0.0189322, + 0.0188192, + 0.0187009, + 0.0185773, + 0.0184485, + 0.0183147, + 0.0181758, + 0.0180321, + 0.0178835, + 0.0177301, + 0.0175721, + 0.0174095, + 0.0172425, + 0.0170711, + 0.0168954, + 0.0167156, + 0.0165317, + 0.0163439, + 0.0161523, + 0.0159570, + 0.0157581, + 0.0155557, + 0.0153500, + 0.0151410, + 0.0149290, + 0.0147140, + 0.0144961, + 0.0142756, + 0.0140524, + 0.0138268, + 0.0135990, + 0.0133689, + 0.0131368, + 0.0129028, + 0.0126671, + 0.0124298, + 0.0121910, + 0.0119509, + 0.0117096, + 0.0114673, + 0.0112241, + 0.0109802, + 0.0107356, + 0.0104907, + 0.0102454, + 0.0100000, + 0.0097546, + 0.0095093, + 0.0092644, + 0.0090198, + 0.0087759, + 0.0085327, + 0.0082904, + 0.0080491, + 0.0078090, + 0.0075702, + 0.0073329, + 0.0070972, + 0.0068632, + 0.0066311, + 0.0064010, + 0.0061732, + 0.0059476, + 0.0057244, + 0.0055039, + 0.0052860, + 0.0050710, + 0.0048590, + 0.0046500, + 0.0044443, + 0.0042419, + 0.0040430, + 0.0038477, + 0.0036561, + 0.0034683, + 0.0032844, + 0.0031046, + 0.0029289, + 0.0027575, + 0.0025905, + 0.0024279, + 0.0022699, + 0.0021165, + 0.0019679, + 0.0018242, + 0.0016853, + 0.0015515, + 0.0014227, + 0.0012991, + 0.0011808, + 0.0010678, + 0.0009601, + 0.0008579, + 0.0007612, + 0.0006701, + 0.0005846, + 0.0005047, + 0.0004306, + 0.0003622, + 0.0002997, + 0.0002430, + 0.0001921, + 0.0001472, + 0.0001082, + 0.0000752, + 0.0000482, + 0.0000271, + 0.0000120, + 0.0000030, + 0.0000000, + 0.0000030, + 0.0000120, + 0.0000271, + 0.0000482, + 0.0000752, + 0.0001082, + 0.0001472, + 0.0001921, + 0.0002430, + 0.0002997, + 0.0003622, + 0.0004306, + 0.0005047, + 0.0005846, + 0.0006701, + 0.0007612, + 0.0008579, + 0.0009601, + 0.0010678, + 0.0011808, + 0.0012991, + 0.0014227, + 0.0015515, + 0.0016853, + 0.0018242, + 0.0019679, + 0.0021165, + 0.0022699, + 0.0024279, + 0.0025905, + 0.0027575, + 0.0029289, + 0.0031046, + 0.0032844, + 0.0034683, + 0.0036561, + 0.0038477, + 0.0040430, + 0.0042419, + 0.0044443, + 0.0046500, + 0.0048590, + 0.0050710, + 0.0052860, + 0.0055039, + 0.0057244, + 0.0059476, + 0.0061732, + 0.0064010, + 0.0066311, + 0.0068632, + 0.0070972, + 0.0073329, + 0.0075702, + 0.0078090, + 0.0080491, + 0.0082904, + 0.0085327, + 0.0087759, + 0.0090198, + 0.0092644, + 0.0095093, + 0.0097546, +}; +