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
Revision 4:c8515fbd2e44, committed 2016-11-28
- Comitter:
- MarkSPA
- Date:
- Mon Nov 28 15:38:58 2016 +0000
- Parent:
- 3:823f105078c7
- Child:
- 5:1f6311e0fc14
- Commit message:
- Initial (buggy) version
Changed in this revision
--- a/BLE_API.lib Thu Jan 07 02:49:37 2016 +0000 +++ b/BLE_API.lib Mon Nov 28 15:38:58 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#bfc5b9b6ecf5 +http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#66159681aa21
--- a/Servo.cpp Thu Jan 07 02:49:37 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,42 +0,0 @@ -/* - -Copyright (c) 2012-2014 RedBearLab - -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. - -*/ - -#include "Servo.h" - -Servo::Servo(PinName pin) : _servo(pin) -{ - _servo.period_ms(20); -} - -Servo::~Servo(void) -{ - -} - -void Servo::write(unsigned char degree) -{ - convert(degree); - _servo.pulsewidth_us(pulse); -} - -void Servo::convert(unsigned char degree) -{ - // 0~180 degree correspond to 500~2500 - pulse = degree * 11 + 500; -} \ No newline at end of file
--- a/Servo.h Thu Jan 07 02:49:37 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,40 +0,0 @@ -/* - -Copyright (c) 2012-2014 RedBearLab - -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 _SERVO_H -#define _SERVO_H - -#include "mbed.h" - -class Servo -{ -public: - Servo(PinName pin); - ~Servo(void); - - void write(unsigned char degree); - -private: - void convert(unsigned char degree); - - PwmOut _servo; - unsigned int pulse; -}; - -#endif \ No newline at end of file
--- a/main.cpp Thu Jan 07 02:49:37 2016 +0000 +++ b/main.cpp Mon Nov 28 15:38:58 2016 +0000 @@ -2,26 +2,28 @@ Copyright (c) 2012-2014 RedBearLab -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, +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, +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. */ #include "mbed.h" +#include "neopixel.h" #include "ble/BLE.h" -#include "Servo.h" #include "GattCallbackParamTypes.h" - +extern "C" { +#include "ble_radio_notification.h" +} #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ @@ -29,23 +31,55 @@ #define TXRX_BUF_LEN 20 #define DIGITAL_OUT_PIN P0_17 //D7 +#define BOARD_LED P0_19 // #define DIGITAL_IN_PIN P0_5 //A4 #define PWM_PIN P0_16 //D6 #define SERVO_PIN P0_14 //D10 #define ANALOG_IN_PIN P0_6 //A5 +#define BB_DIG_OUT_PIN P0_9 //D1 + +#define LED_HEADER 0x01 +#define ONOFF_HEADER 0x11 +#define COLOR_HEADER 0x22 +#define PATTERN_HEADER 0x33 + +#define NUM_LEDS 12 + +#define PATTERN_ON 0x01 +#define PATTERN_FAST 0x02 +#define PATTERN_SLOW 0x03 +#define PATTERN_CHASE 0x04 +#define PATTERN_MRB 0x05 +#define PATTERN_RAINBOW 0x06 +#define PATTERN_CANDY 0x07 +#define PATTERN_SNOW 0x08 +#define PATTERN_ICE 0x09 +#define LEDS_ON 0x01 +#define LEDS_OFF 0x02 BLE ble; -DigitalOut LED_SET(DIGITAL_OUT_PIN); +//DigitalOut LED_SET(DIGITAL_OUT_PIN); +DigitalOut LED_SET(BOARD_LED); DigitalIn BUTTON(DIGITAL_IN_PIN); -PwmOut PWM(PWM_PIN); -AnalogIn ANALOG(ANALOG_IN_PIN); -Servo MYSERVO(SERVO_PIN); +//PwmOut PWM(PWM_PIN); +//AnalogIn ANALOG(ANALOG_IN_PIN); -Serial pc(USBTX, USBRX); +//Serial pc(USBTX, USBRX); +static uint8_t old_state = 0; +neopixel_strip_t m_strip; +//uint8_t dig_pin_num = BB_DIG_OUT_PIN; //DIGITAL_OUT_PIN; //6; +uint8_t dig_pin_num = P0_8; +//uint8_t leds_per_strip = 4; //24; +uint8_t slow_count = 0; +//uint8_t error; +uint8_t led_to_enable = 2; //10; +uint8_t red = 255; +uint8_t green = 128; +uint8_t blue = 159; +uint8_t pattern = 0; -static uint8_t analog_enabled = 0; -static uint8_t old_state = 0; +uint8_t LEDS_ON_FLAG = 0; // The Nordic UART Service static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; @@ -53,168 +87,198 @@ static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; - uint8_t txPayload[TXRX_BUF_LEN] = {0,}; uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; -//static uint8_t rx_buf[TXRX_BUF_LEN]; -//static uint8_t rx_len=0; - - GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); - GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); - GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; - GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); +void rainbowCycle(int wait, uint8_t numLEDs, neopixel_strip_t m_strip); +void candyChase(int wait, uint8_t numLEDs, neopixel_strip_t m_strip); +void snowflakes(int wait, uint8_t numLEDs, neopixel_strip_t m_strip); +void iceflakes(int delay, uint8_t numLEDs, neopixel_strip_t m_strip); +extern uint32_t ble_radio_notification_init(nrf_app_irq_priority_t irq_priority, + nrf_radio_notification_distance_t distance, + ble_radio_notification_evt_handler_t evt_handler); + +void connectionCallback(const Gap::ConnectionCallbackParams_t *params) +{ + neopixel_init(&m_strip, dig_pin_num, NUM_LEDS); + neopixel_clear(&m_strip); +} void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { - pc.printf("Disconnected \r\n"); - pc.printf("Restart advertising \r\n"); + //pc.printf("Disconnected \r\n"); + //pc.printf("Restart advertising \r\n"); + //clear and remove strip + //neopixel_clear(&m_strip); + //neopixel_destroy(&m_strip); ble.startAdvertising(); } void WrittenHandler(const GattWriteCallbackParams *Handler) -{ +{ uint8_t buf[TXRX_BUF_LEN]; - uint16_t bytesRead, index; - - if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) - { + uint16_t bytesRead; + + 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); - - for(index=0; index<bytesRead; index++) - pc.putc(buf[index]); - - if(buf[0] == 0x01) - { - if(buf[1] == 0x01) - LED_SET = 1; - else - LED_SET = 0; - } - else if(buf[0] == 0xA0) - { - if(buf[1] == 0x01) - analog_enabled = 1; - else - analog_enabled = 0; - } - else if(buf[0] == 0x02) - { - float value = (float)buf[1]/255; - PWM = value; - } - else if(buf[0] == 0x03) - { - MYSERVO.write(buf[1]); - } - else if(buf[0] == 0x04) - { - analog_enabled = 0; - PWM = 0; - MYSERVO.write(0); - LED_SET = 0; - old_state = 0; - } + memcpy(txPayload, buf, TXRX_BUF_LEN); + + //for(index=0; index<bytesRead; index++) + //pc.putc(buf[index]); - } -} -/* -void uartCB(void) -{ - while(pc.readable()) - { - rx_buf[rx_len++] = pc.getc(); - if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') - { - ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); - pc.printf("RecHandler \r\n"); - pc.printf("Length: "); - pc.putc(rx_len); - pc.printf("\r\n"); - rx_len = 0; - break; + if(buf[0] == ONOFF_HEADER) { + //pc.printf("LED value received \r\n"); + if(buf[1] == LEDS_ON) { + LEDS_ON_FLAG = 1; + //neopixel_init(&m_strip, dig_pin_num, NUM_LEDS); + //neopixel_clear(&m_strip); + } else if(buf[1] == LEDS_OFF) { + LEDS_ON_FLAG = 0; + neopixel_init(&m_strip, dig_pin_num, NUM_LEDS); + neopixel_clear(&m_strip); + } + } else if(buf[0] == COLOR_HEADER) { + red = buf[1]; + green = buf[2]; + blue = buf[3]; + pattern = buf[5]; } } } -*/ + void m_status_check_handle(void) -{ +{ uint8_t buf[3]; - if (analog_enabled) // if analog reading enabled - { - // Read and send out - float s = ANALOG; - uint16_t value = s*1024; - buf[0] = (0x0B); - buf[1] = (value >> 8); - buf[2] = (value); - ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); - } - + // If digital in changes, report the state - if (BUTTON != old_state) - { + if (BUTTON != old_state) { old_state = BUTTON; - - if (BUTTON == 1) - { + + if (BUTTON == 1) { buf[0] = (0x0A); buf[1] = (0x01); - buf[2] = (0x00); - ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); - } - else - { + buf[2] = (0x00); + ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); + } else { buf[0] = (0x0A); buf[1] = (0x00); buf[2] = (0x00); - ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); + ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); } } } +void ble_on_radio_active_evt(bool radio_active) +{ + if ((radio_active == false) && (LEDS_ON_FLAG == 1)) { + if (pattern == PATTERN_ON) { + for (uint8_t i = 0; i < NUM_LEDS; i++) + neopixel_set_color(&m_strip, i, red, green, blue); + neopixel_show(&m_strip); + + } else if (pattern == PATTERN_FAST) { + neopixel_clear(&m_strip); + if (led_to_enable > NUM_LEDS) + led_to_enable = 0; + + neopixel_set_color(&m_strip, led_to_enable, red, green, blue); + neopixel_show(&m_strip); + led_to_enable++; + } else if (pattern == PATTERN_SLOW) { + if (slow_count > 20) { + slow_count = 0; + neopixel_clear(&m_strip); + if (led_to_enable > NUM_LEDS) + led_to_enable = 0; + + neopixel_set_color(&m_strip, led_to_enable, red, green, blue); + neopixel_show(&m_strip); + led_to_enable++; + } + slow_count++; + } else if (pattern == PATTERN_CHASE) { + for (uint8_t i = 0; i < NUM_LEDS; i++) + neopixel_set_color(&m_strip, i, red, green, blue); + neopixel_show(&m_strip); + } else if (pattern == PATTERN_MRB) { + neopixel_clear(&m_strip); + led_to_enable = rand()%NUM_LEDS; + + neopixel_set_color(&m_strip, led_to_enable, red, green, blue); + neopixel_show(&m_strip); + + led_to_enable++; + } + + else if (pattern == PATTERN_RAINBOW) + rainbowCycle(20, NUM_LEDS, m_strip); + + else if (pattern == PATTERN_CANDY) + candyChase(100, NUM_LEDS, m_strip); + + else if (pattern == PATTERN_SNOW) + snowflakes(100, NUM_LEDS, m_strip); + + else if (pattern == PATTERN_ICE) + iceflakes(100, NUM_LEDS, m_strip); + + } +} + +static void radio_notification_init(void) +{ + //uint32_t err_code; + ble_radio_notification_init(NRF_APP_PRIORITY_LOW, + NRF_RADIO_NOTIFICATION_DISTANCE_800US, + ble_on_radio_active_evt); + //APP_ERROR_CHECK(err_code); +} int main(void) -{ +{ + //uint8_t led_error; + Ticker ticker; ticker.attach_us(m_status_check_handle, 200000); - + + //pc.printf("neoPixel Init \r\n"); + neopixel_init(&m_strip, dig_pin_num, NUM_LEDS); + neopixel_clear(&m_strip); + ble.init(); ble.onDisconnection(disconnectionCallback); - ble.onDataWritten(WrittenHandler); - - pc.baud(9600); - pc.printf("SimpleChat Init \r\n"); + ble.onConnection(connectionCallback); + ble.onDataWritten(WrittenHandler); + radio_notification_init(); + //pc.baud(9600); + //pc.format(8, SerialBase::None, 1); + //pc.printf("rbChromaBand Init \r\n"); + //pc.attach( uartCB , pc.RxIrq); - //pc.attach( uartCB , pc.RxIrq); - - // setup advertising + // setup advertising ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, - (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1); + (const uint8_t *)"rbCBand", sizeof("rbCBand") - 1); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, - (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); - // 100ms; in multiples of 0.625ms. - ble.setAdvertisingInterval(160); - + (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); + // 100ms; in multiples of 0.625ms. + //ble.setAdvertisingInterval(160); + ble.setAdvertisingInterval(80); ble.addService(uartService); - - ble.startAdvertising(); - - pc.printf("Advertising Start \r\n"); - - while(1) - { - ble.waitForEvent(); + ble.startAdvertising(); + //pc.printf("Advertising Start \r\n"); + + while(1) { + //pc.putc(pc.getc()); + ble.waitForEvent(); } }
--- a/mbed.bld Thu Jan 07 02:49:37 2016 +0000 +++ b/mbed.bld Mon Nov 28 15:38:58 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/4336505e4b1c \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/f9eeca106725 \ No newline at end of file
--- a/nRF51822.lib Thu Jan 07 02:49:37 2016 +0000 +++ b/nRF51822.lib Mon Nov 28 15:38:58 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#3cc0718d98d0 +http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#f7faad332abc
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/neopixel.cpp Mon Nov 28 15:38:58 2016 +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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/neopixel.h Mon Nov 28 15:38:58 2016 +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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/patterns.cpp Mon Nov 28 15:38:58 2016 +0000 @@ -0,0 +1,105 @@ +#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" + +// Input a value 0 to 255 to get a color value. +// The colours are a transition r - g - b - back to r. +uint32_t Wheel(uint8_t WheelPos, neopixel_strip_t m_strip, uint8_t led_to_enable) { + WheelPos = 255 - WheelPos; + if(WheelPos < 85) { + return neopixel_set_color(&m_strip, led_to_enable, 255 - WheelPos * 3, 0, WheelPos * 3); + } + if(WheelPos < 170) { + WheelPos -= 85; + return neopixel_set_color(&m_strip, 0, led_to_enable, WheelPos * 3, 255 - WheelPos * 3); + } + WheelPos -= 170; + return neopixel_set_color(&m_strip, led_to_enable, WheelPos * 3, 255 - WheelPos * 3, 0); +} + +void rainbowCycle(int delay, uint8_t numLEDs, neopixel_strip_t m_strip) { + uint16_t i, j; + uint32_t ret; + + for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel + for(i=0; i< numLEDs; i++) { + //neopixel_set_color(&m_strip, i, red, green, blue); + //strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255)); + ret = Wheel(i, m_strip, ((i * 256 / numLEDs) + j & 255)); + } + neopixel_show(&m_strip); + wait_ms(delay); + } +} + +void candyChase(int delay, uint8_t numLEDs, neopixel_strip_t m_strip) { + for (int j=0; j<10; j++) { //do 10 cycles of chasing + for (int q=0; q < 3; q++) { + for (uint16_t i=0; i < numLEDs; i++) { + neopixel_set_color(&m_strip, i, 255,255,255); //turn every pixel white + } + for (uint16_t i=0; i < numLEDs; i=i+3) { + neopixel_set_color(&m_strip, i, 255,0,0); //turn every third pixel red + } + neopixel_show(&m_strip); + + wait_ms(delay); + + for (uint16_t i=0; i < numLEDs; i=i+3) { + neopixel_set_color(&m_strip, i, 0,0,0); //turn every third pixel off + } + } + } +} + +void snowflakes(int delay, uint8_t numLEDs, neopixel_strip_t m_strip) { + + // Setup the pixel array + int pixel[numLEDs]; + for(int p=0; p<numLEDs; p++) + pixel[p] = rand()%255; + + + // Run some snowflake cycles + for (int j=0; j<20; j++) { + // Every five cycles, light a new pixel + if((j%5)==0) + neopixel_set_color(&m_strip, rand()%numLEDs, 255,255,255); + + // Dim all pixels by 10 + for(int p=0; p<numLEDs; p++){ + neopixel_set_color(&m_strip, p, pixel[p],pixel[p],pixel[p] ); + pixel[p] = pixel[p] - 10; + } + neopixel_show(&m_strip); + wait_ms(delay); + } +} + +void iceflakes(int delay, uint8_t numLEDs, neopixel_strip_t m_strip) { + + // Setup the pixel array + int pixel[numLEDs]; + for(int p=0; p<numLEDs; p++) + pixel[p] = rand()%255; + + // Run some snowflake cycles + for (int j=0; j<20; j++) { + // Every five cycles, light a new pixel + if((j%5)==0) + neopixel_set_color(&m_strip, rand()%numLEDs, 0,0,255); + + // Dim all pixels by 10 + for(int p=0; p<numLEDs; p++){ + neopixel_set_color(&m_strip, p, 0,0,pixel[p] ); + pixel[p] = pixel[p] - 10; + } + neopixel_show(&m_strip); + wait_ms(delay); + } +} \ No newline at end of file