share for RS

Fork of rbVectorIQ by Mark @RRVA

Committer:
MarkSPA
Date:
Mon Nov 28 15:38:58 2016 +0000
Revision:
4:c8515fbd2e44
Initial (buggy) version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MarkSPA 4:c8515fbd2e44 1 /* Lava
MarkSPA 4:c8515fbd2e44 2 *
MarkSPA 4:c8515fbd2e44 3 * WS2812B Tricolor LED (neopixel) controller
MarkSPA 4:c8515fbd2e44 4 *
MarkSPA 4:c8515fbd2e44 5 *
MarkSPA 4:c8515fbd2e44 6 * Example code:
MarkSPA 4:c8515fbd2e44 7
MarkSPA 4:c8515fbd2e44 8 neopixel_strip_t m_strip;
MarkSPA 4:c8515fbd2e44 9 uint8_t dig_pin_num = 6;
MarkSPA 4:c8515fbd2e44 10 uint8_t leds_per_strip = 24;
MarkSPA 4:c8515fbd2e44 11 uint8_t error;
MarkSPA 4:c8515fbd2e44 12 uint8_t led_to_enable = 10;
MarkSPA 4:c8515fbd2e44 13 uint8_t red = 255;
MarkSPA 4:c8515fbd2e44 14 uint8_t green = 0;
MarkSPA 4:c8515fbd2e44 15 uint8_t blue = 159;
MarkSPA 4:c8515fbd2e44 16
MarkSPA 4:c8515fbd2e44 17 neopixel_init(&m_strip, dig_pin_num, leds_per_strip);
MarkSPA 4:c8515fbd2e44 18 neopixel_clear(&m_strip);
MarkSPA 4:c8515fbd2e44 19 error = neopixel_set_color_and_show(&m_strip, led_to_enable, red, green, blue);
MarkSPA 4:c8515fbd2e44 20 if (error) {
MarkSPA 4:c8515fbd2e44 21 //led_to_enable was not within number leds_per_strip
MarkSPA 4:c8515fbd2e44 22 }
MarkSPA 4:c8515fbd2e44 23 //clear and remove strip
MarkSPA 4:c8515fbd2e44 24 neopixel_clear(&m_strip);
MarkSPA 4:c8515fbd2e44 25 neopixel_destroy(&m_strip);
MarkSPA 4:c8515fbd2e44 26
MarkSPA 4:c8515fbd2e44 27
MarkSPA 4:c8515fbd2e44 28 * For use with BLE stack, see information below:
MarkSPA 4:c8515fbd2e44 29 - Include in main.c
MarkSPA 4:c8515fbd2e44 30 #include "ble_radio_notification.h"
MarkSPA 4:c8515fbd2e44 31 - Call (see nrf_soc.h: NRF_RADIO_NOTIFICATION_DISTANCES and NRF_APP_PRIORITIES)
MarkSPA 4:c8515fbd2e44 32 ble_radio_notification_init(NRF_APP_PRIORITY_xxx,
MarkSPA 4:c8515fbd2e44 33 NRF_RADIO_NOTIFICATION_DISTANCE_xxx,
MarkSPA 4:c8515fbd2e44 34 your_radio_callback_handler);
MarkSPA 4:c8515fbd2e44 35 - Create
MarkSPA 4:c8515fbd2e44 36 void your_radio_callback_handler(bool radio_active)
MarkSPA 4:c8515fbd2e44 37 {
MarkSPA 4:c8515fbd2e44 38 if (radio_active == false)
MarkSPA 4:c8515fbd2e44 39 {
MarkSPA 4:c8515fbd2e44 40 neopixel_show(&strip1);
MarkSPA 4:c8515fbd2e44 41 neopixel_show(&strip2);
MarkSPA 4:c8515fbd2e44 42 //...etc
MarkSPA 4:c8515fbd2e44 43 }
MarkSPA 4:c8515fbd2e44 44 }
MarkSPA 4:c8515fbd2e44 45 - Do not use neopixel_set_color_and_show(...) with BLE, instead use uint8_t neopixel_set_color(...);
MarkSPA 4:c8515fbd2e44 46 */
MarkSPA 4:c8515fbd2e44 47
MarkSPA 4:c8515fbd2e44 48 #include "mbed.h" // remove line if not using mbed
MarkSPA 4:c8515fbd2e44 49
MarkSPA 4:c8515fbd2e44 50 #include <stdbool.h>
MarkSPA 4:c8515fbd2e44 51 #include <stdint.h>
MarkSPA 4:c8515fbd2e44 52 #include <stdlib.h>
MarkSPA 4:c8515fbd2e44 53 #include "nrf_delay.h"
MarkSPA 4:c8515fbd2e44 54 #include "nrf_gpio.h"
MarkSPA 4:c8515fbd2e44 55 #include "neopixel.h"
MarkSPA 4:c8515fbd2e44 56
MarkSPA 4:c8515fbd2e44 57
MarkSPA 4:c8515fbd2e44 58 void neopixel_init(neopixel_strip_t *strip, uint8_t pin_num, uint16_t num_leds)
MarkSPA 4:c8515fbd2e44 59 {
MarkSPA 4:c8515fbd2e44 60 strip->leds = (color_t*) malloc(sizeof(color_t) * num_leds);
MarkSPA 4:c8515fbd2e44 61 strip->pin_num = pin_num;
MarkSPA 4:c8515fbd2e44 62 strip->num_leds = num_leds;
MarkSPA 4:c8515fbd2e44 63 nrf_gpio_cfg_output(pin_num);
MarkSPA 4:c8515fbd2e44 64 NRF_GPIO->OUTCLR = (1UL << pin_num);
MarkSPA 4:c8515fbd2e44 65 for (int i = 0; i < num_leds; i++)
MarkSPA 4:c8515fbd2e44 66 {
MarkSPA 4:c8515fbd2e44 67 strip->leds[i].simple.g = 0;
MarkSPA 4:c8515fbd2e44 68 strip->leds[i].simple.r = 0;
MarkSPA 4:c8515fbd2e44 69 strip->leds[i].simple.b = 0;
MarkSPA 4:c8515fbd2e44 70 }
MarkSPA 4:c8515fbd2e44 71 }
MarkSPA 4:c8515fbd2e44 72
MarkSPA 4:c8515fbd2e44 73 void neopixel_clear(neopixel_strip_t *strip)
MarkSPA 4:c8515fbd2e44 74 {
MarkSPA 4:c8515fbd2e44 75 for (int i = 0; i < strip->num_leds; i++)
MarkSPA 4:c8515fbd2e44 76 {
MarkSPA 4:c8515fbd2e44 77 strip->leds[i].simple.g = 0;
MarkSPA 4:c8515fbd2e44 78 strip->leds[i].simple.r = 0;
MarkSPA 4:c8515fbd2e44 79 strip->leds[i].simple.b = 0;
MarkSPA 4:c8515fbd2e44 80 }
MarkSPA 4:c8515fbd2e44 81 neopixel_show(strip);
MarkSPA 4:c8515fbd2e44 82 }
MarkSPA 4:c8515fbd2e44 83
MarkSPA 4:c8515fbd2e44 84 void neopixel_show(neopixel_strip_t *strip)
MarkSPA 4:c8515fbd2e44 85 {
MarkSPA 4:c8515fbd2e44 86 const uint8_t PIN = strip->pin_num;
MarkSPA 4:c8515fbd2e44 87 NRF_GPIO->OUTCLR = (1UL << PIN);
MarkSPA 4:c8515fbd2e44 88 nrf_delay_us(50);
MarkSPA 4:c8515fbd2e44 89 for (int i = 0; i < strip->num_leds; i++)
MarkSPA 4:c8515fbd2e44 90 {
MarkSPA 4:c8515fbd2e44 91 for (int j = 0; j < 3; j++)
MarkSPA 4:c8515fbd2e44 92 {
MarkSPA 4:c8515fbd2e44 93 if ((strip->leds[i].grb[j] & 128) > 0) {NEOPIXEL_SEND_ONE}
MarkSPA 4:c8515fbd2e44 94 else {NEOPIXEL_SEND_ZERO}
MarkSPA 4:c8515fbd2e44 95
MarkSPA 4:c8515fbd2e44 96 if ((strip->leds[i].grb[j] & 64) > 0) {NEOPIXEL_SEND_ONE}
MarkSPA 4:c8515fbd2e44 97 else {NEOPIXEL_SEND_ZERO}
MarkSPA 4:c8515fbd2e44 98
MarkSPA 4:c8515fbd2e44 99 if ((strip->leds[i].grb[j] & 32) > 0) {NEOPIXEL_SEND_ONE}
MarkSPA 4:c8515fbd2e44 100 else {NEOPIXEL_SEND_ZERO}
MarkSPA 4:c8515fbd2e44 101
MarkSPA 4:c8515fbd2e44 102 if ((strip->leds[i].grb[j] & 16) > 0) {NEOPIXEL_SEND_ONE}
MarkSPA 4:c8515fbd2e44 103 else {NEOPIXEL_SEND_ZERO}
MarkSPA 4:c8515fbd2e44 104
MarkSPA 4:c8515fbd2e44 105 if ((strip->leds[i].grb[j] & 8) > 0) {NEOPIXEL_SEND_ONE}
MarkSPA 4:c8515fbd2e44 106 else {NEOPIXEL_SEND_ZERO}
MarkSPA 4:c8515fbd2e44 107
MarkSPA 4:c8515fbd2e44 108 if ((strip->leds[i].grb[j] & 4) > 0) {NEOPIXEL_SEND_ONE}
MarkSPA 4:c8515fbd2e44 109 else {NEOPIXEL_SEND_ZERO}
MarkSPA 4:c8515fbd2e44 110
MarkSPA 4:c8515fbd2e44 111 if ((strip->leds[i].grb[j] & 2) > 0) {NEOPIXEL_SEND_ONE}
MarkSPA 4:c8515fbd2e44 112 else {NEOPIXEL_SEND_ZERO}
MarkSPA 4:c8515fbd2e44 113
MarkSPA 4:c8515fbd2e44 114 if ((strip->leds[i].grb[j] & 1) > 0) {NEOPIXEL_SEND_ONE}
MarkSPA 4:c8515fbd2e44 115 else {NEOPIXEL_SEND_ZERO}
MarkSPA 4:c8515fbd2e44 116 }
MarkSPA 4:c8515fbd2e44 117 }
MarkSPA 4:c8515fbd2e44 118 }
MarkSPA 4:c8515fbd2e44 119
MarkSPA 4:c8515fbd2e44 120 uint8_t neopixel_set_color(neopixel_strip_t *strip, uint16_t index, uint8_t red, uint8_t green, uint8_t blue )
MarkSPA 4:c8515fbd2e44 121 {
MarkSPA 4:c8515fbd2e44 122 if (index < strip->num_leds)
MarkSPA 4:c8515fbd2e44 123 {
MarkSPA 4:c8515fbd2e44 124 strip->leds[index].simple.r = red;
MarkSPA 4:c8515fbd2e44 125 strip->leds[index].simple.g = green;
MarkSPA 4:c8515fbd2e44 126 strip->leds[index].simple.b = blue;
MarkSPA 4:c8515fbd2e44 127 }
MarkSPA 4:c8515fbd2e44 128 else
MarkSPA 4:c8515fbd2e44 129 return 1;
MarkSPA 4:c8515fbd2e44 130 return 0;
MarkSPA 4:c8515fbd2e44 131 }
MarkSPA 4:c8515fbd2e44 132
MarkSPA 4:c8515fbd2e44 133 uint8_t neopixel_set_color_and_show(neopixel_strip_t *strip, uint16_t index, uint8_t red, uint8_t green, uint8_t blue)
MarkSPA 4:c8515fbd2e44 134 {
MarkSPA 4:c8515fbd2e44 135 if (index < strip->num_leds)
MarkSPA 4:c8515fbd2e44 136 {
MarkSPA 4:c8515fbd2e44 137 strip->leds[index].simple.r = red;
MarkSPA 4:c8515fbd2e44 138 strip->leds[index].simple.g = green;
MarkSPA 4:c8515fbd2e44 139 strip->leds[index].simple.b = blue;
MarkSPA 4:c8515fbd2e44 140 neopixel_show(strip);
MarkSPA 4:c8515fbd2e44 141 }
MarkSPA 4:c8515fbd2e44 142 else
MarkSPA 4:c8515fbd2e44 143 return 1;
MarkSPA 4:c8515fbd2e44 144 return 0;
MarkSPA 4:c8515fbd2e44 145 }
MarkSPA 4:c8515fbd2e44 146
MarkSPA 4:c8515fbd2e44 147 void neopixel_destroy(neopixel_strip_t *strip)
MarkSPA 4:c8515fbd2e44 148 {
MarkSPA 4:c8515fbd2e44 149 free(strip->leds);
MarkSPA 4:c8515fbd2e44 150 strip->num_leds = 0;
MarkSPA 4:c8515fbd2e44 151 strip->pin_num = 0;
MarkSPA 4:c8515fbd2e44 152 }
MarkSPA 4:c8515fbd2e44 153 /*
MarkSPA 4:c8515fbd2e44 154 int main() {
MarkSPA 4:c8515fbd2e44 155 DigitalOut mypin(D4);
MarkSPA 4:c8515fbd2e44 156 neopixel_strip_t m_strip;
MarkSPA 4:c8515fbd2e44 157 uint8_t dig_pin_num = P0_21;
MarkSPA 4:c8515fbd2e44 158 uint8_t leds_per_strip = 30;
MarkSPA 4:c8515fbd2e44 159 uint8_t error;
MarkSPA 4:c8515fbd2e44 160 uint8_t led_to_enable = 30;
MarkSPA 4:c8515fbd2e44 161 uint8_t red = 0;
MarkSPA 4:c8515fbd2e44 162 uint8_t green = 128;
MarkSPA 4:c8515fbd2e44 163 uint8_t blue = 128;
MarkSPA 4:c8515fbd2e44 164
MarkSPA 4:c8515fbd2e44 165 neopixel_init(&m_strip, dig_pin_num, leds_per_strip);
MarkSPA 4:c8515fbd2e44 166 neopixel_clear(&m_strip);
MarkSPA 4:c8515fbd2e44 167 //error = neopixel_set_color_and_show(&m_strip, led_to_enable, red, green, blue);
MarkSPA 4:c8515fbd2e44 168 for (int cv=0; cv < 60; cv++) {
MarkSPA 4:c8515fbd2e44 169
MarkSPA 4:c8515fbd2e44 170 neopixel_set_color_and_show(&m_strip, cv, red, green, blue);
MarkSPA 4:c8515fbd2e44 171 wait_ms(500);
MarkSPA 4:c8515fbd2e44 172 }
MarkSPA 4:c8515fbd2e44 173
MarkSPA 4:c8515fbd2e44 174 if (error) {
MarkSPA 4:c8515fbd2e44 175 //led_to_enable was not within number leds_per_strip
MarkSPA 4:c8515fbd2e44 176 }
MarkSPA 4:c8515fbd2e44 177
MarkSPA 4:c8515fbd2e44 178 //error = neopixel_set_color_and_show(&m_strip, 1, red, green, blue);
MarkSPA 4:c8515fbd2e44 179 while (1) {
MarkSPA 4:c8515fbd2e44 180 wait_ms(500);
MarkSPA 4:c8515fbd2e44 181 }
MarkSPA 4:c8515fbd2e44 182 //clear and remove strip
MarkSPA 4:c8515fbd2e44 183 //neopixel_clear(&m_strip);
MarkSPA 4:c8515fbd2e44 184 //neopixel_destroy(&m_strip);
MarkSPA 4:c8515fbd2e44 185 }
MarkSPA 4:c8515fbd2e44 186 */