API for driving Adafruit's NeoPixels using RedBear's BLE Nano or BLE nRF51822
Dependencies: BLE_API mbed nRF51822
Dependents: RedBearNano_NeoPixels_Example NeoTester
Revision 0:bb19df120222, committed 2015-07-09
- Comitter:
- bickster
- Date:
- Thu Jul 09 01:06:35 2015 +0000
- Commit message:
- Initial Commit
Changed in this revision
diff -r 000000000000 -r bb19df120222 BLE_API.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BLE_API.lib Thu Jul 09 01:06:35 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#9c2edf20ea56
diff -r 000000000000 -r bb19df120222 README.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Thu Jul 09 01:06:35 2015 +0000 @@ -0,0 +1,6 @@ +# NeoPixel API for RedBear Nano and BLE nRF51822 +This is the github repo from https://github.com/lavallc/nrf51-neopixel with modifications to support the RedBear boards. + +# Contributors +* RedBearLabs.com see https://redbearlab.zendesk.com/entries/73654999-BLE-Nano-with-Neopixel-WS2812-mbed +* https://github.com/lavallc/nrf51-neopixel/issues/1 \ No newline at end of file
diff -r 000000000000 -r bb19df120222 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Jul 09 01:06:35 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/7cff1c4259d7 \ No newline at end of file
diff -r 000000000000 -r bb19df120222 nRF51822.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nRF51822.lib Thu Jul 09 01:06:35 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#6c82f06746bb
diff -r 000000000000 -r bb19df120222 neopixel.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/neopixel.cpp Thu Jul 09 01:06:35 2015 +0000 @@ -0,0 +1,186 @@ +/* Lava + * + * WS2812B Tricolor LED (neopixel) controller + * + * + * Example code: + + neopixel_strip_t m_strip; + uint8_t dig_pin_num = 6; + uint8_t leds_per_strip = 24; + uint8_t error; + uint8_t led_to_enable = 10; + uint8_t red = 255; + uint8_t green = 0; + uint8_t blue = 159; + + neopixel_init(&m_strip, dig_pin_num, leds_per_strip); + neopixel_clear(&m_strip); + error = neopixel_set_color_and_show(&m_strip, led_to_enable, red, green, blue); + if (error) { + //led_to_enable was not within number leds_per_strip + } + //clear and remove strip + neopixel_clear(&m_strip); + neopixel_destroy(&m_strip); + + + * For use with BLE stack, see information below: + - Include in main.c + #include "ble_radio_notification.h" + - Call (see nrf_soc.h: NRF_RADIO_NOTIFICATION_DISTANCES and NRF_APP_PRIORITIES) + ble_radio_notification_init(NRF_APP_PRIORITY_xxx, + NRF_RADIO_NOTIFICATION_DISTANCE_xxx, + your_radio_callback_handler); + - Create + void your_radio_callback_handler(bool radio_active) + { + if (radio_active == false) + { + neopixel_show(&strip1); + neopixel_show(&strip2); + //...etc + } + } + - Do not use neopixel_set_color_and_show(...) with BLE, instead use uint8_t neopixel_set_color(...); + */ + +#include "mbed.h" // remove line if not using mbed + +#include <stdbool.h> +#include <stdint.h> +#include <stdlib.h> +#include "nrf_delay.h" +#include "nrf_gpio.h" +#include "neopixel.h" + + +void neopixel_init(neopixel_strip_t *strip, uint8_t pin_num, uint16_t num_leds) +{ + strip->leds = (color_t*) malloc(sizeof(color_t) * num_leds); + strip->pin_num = pin_num; + strip->num_leds = num_leds; + nrf_gpio_cfg_output(pin_num); + NRF_GPIO->OUTCLR = (1UL << pin_num); + for (int i = 0; i < num_leds; i++) + { + strip->leds[i].simple.g = 0; + strip->leds[i].simple.r = 0; + strip->leds[i].simple.b = 0; + } +} + +void neopixel_clear(neopixel_strip_t *strip) +{ + for (int i = 0; i < strip->num_leds; i++) + { + strip->leds[i].simple.g = 0; + strip->leds[i].simple.r = 0; + strip->leds[i].simple.b = 0; + } + neopixel_show(strip); +} + +void neopixel_show(neopixel_strip_t *strip) +{ + const uint8_t PIN = strip->pin_num; + NRF_GPIO->OUTCLR = (1UL << PIN); + nrf_delay_us(50); + for (int i = 0; i < strip->num_leds; i++) + { + for (int j = 0; j < 3; j++) + { + if ((strip->leds[i].grb[j] & 128) > 0) {NEOPIXEL_SEND_ONE} + else {NEOPIXEL_SEND_ZERO} + + if ((strip->leds[i].grb[j] & 64) > 0) {NEOPIXEL_SEND_ONE} + else {NEOPIXEL_SEND_ZERO} + + if ((strip->leds[i].grb[j] & 32) > 0) {NEOPIXEL_SEND_ONE} + else {NEOPIXEL_SEND_ZERO} + + if ((strip->leds[i].grb[j] & 16) > 0) {NEOPIXEL_SEND_ONE} + else {NEOPIXEL_SEND_ZERO} + + if ((strip->leds[i].grb[j] & 8) > 0) {NEOPIXEL_SEND_ONE} + else {NEOPIXEL_SEND_ZERO} + + if ((strip->leds[i].grb[j] & 4) > 0) {NEOPIXEL_SEND_ONE} + else {NEOPIXEL_SEND_ZERO} + + if ((strip->leds[i].grb[j] & 2) > 0) {NEOPIXEL_SEND_ONE} + else {NEOPIXEL_SEND_ZERO} + + if ((strip->leds[i].grb[j] & 1) > 0) {NEOPIXEL_SEND_ONE} + else {NEOPIXEL_SEND_ZERO} + } + } +} + +uint8_t neopixel_set_color(neopixel_strip_t *strip, uint16_t index, uint8_t red, uint8_t green, uint8_t blue ) +{ + if (index < strip->num_leds) + { + strip->leds[index].simple.r = red; + strip->leds[index].simple.g = green; + strip->leds[index].simple.b = blue; + } + else + return 1; + return 0; +} + +uint8_t neopixel_set_color_and_show(neopixel_strip_t *strip, uint16_t index, uint8_t red, uint8_t green, uint8_t blue) +{ + if (index < strip->num_leds) + { + strip->leds[index].simple.r = red; + strip->leds[index].simple.g = green; + strip->leds[index].simple.b = blue; + neopixel_show(strip); + } + else + return 1; + return 0; +} + +void neopixel_destroy(neopixel_strip_t *strip) +{ + free(strip->leds); + strip->num_leds = 0; + strip->pin_num = 0; +} +/* +int main() { + DigitalOut mypin(D4); + neopixel_strip_t m_strip; + uint8_t dig_pin_num = P0_21; + uint8_t leds_per_strip = 30; + uint8_t error; + uint8_t led_to_enable = 30; + uint8_t red = 0; + uint8_t green = 128; + uint8_t blue = 128; + + neopixel_init(&m_strip, dig_pin_num, leds_per_strip); + neopixel_clear(&m_strip); + //error = neopixel_set_color_and_show(&m_strip, led_to_enable, red, green, blue); + for (int cv=0; cv < 60; cv++) { + + neopixel_set_color_and_show(&m_strip, cv, red, green, blue); + wait_ms(500); + } + + if (error) { + //led_to_enable was not within number leds_per_strip + } + + //error = neopixel_set_color_and_show(&m_strip, 1, red, green, blue); + while (1) { + wait_ms(500); + } + //clear and remove strip + //neopixel_clear(&m_strip); + //neopixel_destroy(&m_strip); +} +*/ \ No newline at end of file
diff -r 000000000000 -r bb19df120222 neopixel.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/neopixel.h Thu Jul 09 01:06:35 2015 +0000 @@ -0,0 +1,101 @@ +/* Copyright (c) 2015 bickster, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + #ifndef NEOPIXEL_H + #define NEOPIXEL_H + +#include <stdbool.h> +#include <stdint.h> + + +//These defines are timed specific to a series of if statements and will need to be changed +//to compensate for different writing algorithms than the one in neopixel.c +#define NEOPIXEL_SEND_ONE NRF_GPIO->OUTSET = (1UL << PIN); \ + NRF_GPIO->OUTSET = (1UL << PIN); \ + NRF_GPIO->OUTSET = (1UL << PIN); \ + NRF_GPIO->OUTCLR = (1UL << PIN); + +#define NEOPIXEL_SEND_ZERO NRF_GPIO->OUTSET = (1UL << PIN); \ + NRF_GPIO->OUTCLR = (1UL << PIN); \ + NRF_GPIO->OUTCLR = (1UL << PIN); \ + NRF_GPIO->OUTCLR = (1UL << PIN); \ + NRF_GPIO->OUTCLR = (1UL << PIN); + +typedef union { + struct { + uint8_t g, r, b; + }simple; + uint8_t grb[3]; +} color_t; + +typedef struct { + uint8_t pin_num; + uint16_t num_leds; + color_t *leds; +} neopixel_strip_t; + +/** + @brief Initialize GPIO and data location + @param[in] pointer to Strip structure + @param[in] pin number for GPIO +*/ +void neopixel_init(neopixel_strip_t *strip, uint8_t pin_num, uint16_t num_leds); + +/** + @brief Turn all LEDs off + @param[in] pointer to Strip structure +*/ +void neopixel_clear(neopixel_strip_t *strip); + +/** + @brief Update strip with structure data + @param[in] pointer to Strip structure +*/ +void neopixel_show(neopixel_strip_t *strip); + +/** + @brief Write RGB value to LED structure + @param[in] pointer to Strip structure + @param[in] red value + @param[in] green value + @param[in] blue value + @param[in] LED number (starting at 1) + @retval 0 Successful write + @retval 1 LED number is out of bounds +*/ +uint8_t neopixel_set_color(neopixel_strip_t *strip, uint16_t index, uint8_t red, uint8_t green, uint8_t blue ); + + +/** + @brief Write RGB value to LED structure and update LED + @param[in] pointer to Strip structure + @param[in] red value + @param[in] green value + @param[in] blue value + @param[in] LED number (starting at 1) + @retval 0 Successful write + @retval 1 LED number is out of bounds +*/ +uint8_t neopixel_set_color_and_show(neopixel_strip_t *strip, uint16_t index, uint8_t red, uint8_t green, uint8_t blue); + +/** + @brief Clears structure data + @param[in] pointer to Strip structure +*/ +void neopixel_destroy(neopixel_strip_t *strip); + +#endif \ No newline at end of file