![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
my new gear...
neopixel.cpp@22:394337a4205a, 22 months ago (annotated)
- Committer:
- yootee
- Date:
- Sat Oct 15 07:52:25 2022 +0000
- Revision:
- 22:394337a4205a
- Parent:
- 3:a9b4b2565a23
upgrade
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yootee | 3:a9b4b2565a23 | 1 | #include "mbed.h" |
yootee | 3:a9b4b2565a23 | 2 | #include "neopixel.h" |
yootee | 3:a9b4b2565a23 | 3 | |
yootee | 3:a9b4b2565a23 | 4 | #if defined(TARGET_NUCLEO_L432KC) |
yootee | 3:a9b4b2565a23 | 5 | |
yootee | 3:a9b4b2565a23 | 6 | #define PIXEL_WAIT_1_ (6) |
yootee | 3:a9b4b2565a23 | 7 | #define PIXEL_WAIT_2_ (2) |
yootee | 3:a9b4b2565a23 | 8 | #define PIXEL_WAIT_3_ (3) |
yootee | 3:a9b4b2565a23 | 9 | #define PIXEL_WAIT_4_ (5) |
yootee | 3:a9b4b2565a23 | 10 | |
yootee | 3:a9b4b2565a23 | 11 | #elif defined(TARGET_NUCLEO_F446RE) |
yootee | 3:a9b4b2565a23 | 12 | |
yootee | 3:a9b4b2565a23 | 13 | #define PIXEL_WAIT_1_ (9) |
yootee | 3:a9b4b2565a23 | 14 | #define PIXEL_WAIT_2_ (5) |
yootee | 3:a9b4b2565a23 | 15 | #define PIXEL_WAIT_3_ (4) |
yootee | 3:a9b4b2565a23 | 16 | #define PIXEL_WAIT_4_ (9) |
yootee | 3:a9b4b2565a23 | 17 | |
yootee | 3:a9b4b2565a23 | 18 | #else |
yootee | 3:a9b4b2565a23 | 19 | #error This board is not supported |
yootee | 3:a9b4b2565a23 | 20 | #endif |
yootee | 3:a9b4b2565a23 | 21 | |
yootee | 3:a9b4b2565a23 | 22 | NeoPixelOut::NeoPixelOut(PinName pin, int num) : DigitalOut(pin) |
yootee | 3:a9b4b2565a23 | 23 | { |
yootee | 3:a9b4b2565a23 | 24 | normalize = false; |
yootee | 3:a9b4b2565a23 | 25 | global_scale = 1.0f; |
yootee | 3:a9b4b2565a23 | 26 | num_pixels_ = num; |
yootee | 3:a9b4b2565a23 | 27 | strip_.resize(num); |
yootee | 3:a9b4b2565a23 | 28 | } |
yootee | 3:a9b4b2565a23 | 29 | |
yootee | 3:a9b4b2565a23 | 30 | // The timing should be approximately 800ns/300ns, 300ns/800ns |
yootee | 3:a9b4b2565a23 | 31 | inline void NeoPixelOut::byte(register uint32_t byte) |
yootee | 3:a9b4b2565a23 | 32 | { |
yootee | 3:a9b4b2565a23 | 33 | for (int i = 0; i < 8; i++) { |
yootee | 3:a9b4b2565a23 | 34 | gpio_write(&gpio, 1); |
yootee | 3:a9b4b2565a23 | 35 | |
yootee | 3:a9b4b2565a23 | 36 | // duty cycle determines bit value |
yootee | 3:a9b4b2565a23 | 37 | if (byte & 0x80) { |
yootee | 3:a9b4b2565a23 | 38 | // one |
yootee | 3:a9b4b2565a23 | 39 | for(int j = 0; j < PIXEL_WAIT_1_; j++) asm("NOP");//6 9 |
yootee | 3:a9b4b2565a23 | 40 | |
yootee | 3:a9b4b2565a23 | 41 | gpio_write(&gpio, 0); |
yootee | 3:a9b4b2565a23 | 42 | for(int j = 0; j < PIXEL_WAIT_2_; j++) asm("NOP");//2 5 |
yootee | 3:a9b4b2565a23 | 43 | } |
yootee | 3:a9b4b2565a23 | 44 | else { |
yootee | 3:a9b4b2565a23 | 45 | // zero |
yootee | 3:a9b4b2565a23 | 46 | for(int j = 0; j < PIXEL_WAIT_3_; j++) asm("NOP");//3 4 |
yootee | 3:a9b4b2565a23 | 47 | |
yootee | 3:a9b4b2565a23 | 48 | gpio_write(&gpio, 0); |
yootee | 3:a9b4b2565a23 | 49 | for(int j = 0; j < PIXEL_WAIT_4_; j++) asm("NOP");//5 9 |
yootee | 3:a9b4b2565a23 | 50 | } |
yootee | 3:a9b4b2565a23 | 51 | |
yootee | 3:a9b4b2565a23 | 52 | byte = byte << 1; // shift to next bit |
yootee | 3:a9b4b2565a23 | 53 | } |
yootee | 3:a9b4b2565a23 | 54 | |
yootee | 3:a9b4b2565a23 | 55 | } |
yootee | 3:a9b4b2565a23 | 56 | |
yootee | 3:a9b4b2565a23 | 57 | void NeoPixelOut::setPixelColor(uint32_t i,uint8_t r,uint8_t g,uint8_t b){ |
yootee | 3:a9b4b2565a23 | 58 | setPixelColor(i,color(r, g, b)); |
yootee | 3:a9b4b2565a23 | 59 | } |
yootee | 3:a9b4b2565a23 | 60 | |
yootee | 3:a9b4b2565a23 | 61 | void NeoPixelOut::setPixelColor(uint32_t i,uint32_t color){ |
yootee | 3:a9b4b2565a23 | 62 | if(i < num_pixels_){ |
yootee | 3:a9b4b2565a23 | 63 | buf_.hex = color; |
yootee | 3:a9b4b2565a23 | 64 | if (normalize) { |
yootee | 3:a9b4b2565a23 | 65 | float sum = buf_.r+buf_.g+buf_.b; |
yootee | 3:a9b4b2565a23 | 66 | if(sum != 0){ |
yootee | 3:a9b4b2565a23 | 67 | float scale = 255.0f/sum; |
yootee | 3:a9b4b2565a23 | 68 | buf_.r *= scale; |
yootee | 3:a9b4b2565a23 | 69 | buf_.g *= scale; |
yootee | 3:a9b4b2565a23 | 70 | buf_.b *= scale; |
yootee | 3:a9b4b2565a23 | 71 | } |
yootee | 3:a9b4b2565a23 | 72 | } |
yootee | 3:a9b4b2565a23 | 73 | buf_.r *= global_scale; |
yootee | 3:a9b4b2565a23 | 74 | buf_.g *= global_scale; |
yootee | 3:a9b4b2565a23 | 75 | buf_.b *= global_scale; |
yootee | 3:a9b4b2565a23 | 76 | |
yootee | 3:a9b4b2565a23 | 77 | strip_[i] = buf_; |
yootee | 3:a9b4b2565a23 | 78 | } |
yootee | 3:a9b4b2565a23 | 79 | } |
yootee | 3:a9b4b2565a23 | 80 | |
yootee | 3:a9b4b2565a23 | 81 | void NeoPixelOut::show(bool flipwait){ |
yootee | 3:a9b4b2565a23 | 82 | // Disable interrupts in the critical section |
yootee | 3:a9b4b2565a23 | 83 | __disable_irq(); |
yootee | 3:a9b4b2565a23 | 84 | |
yootee | 3:a9b4b2565a23 | 85 | for (int i = 0; i < num_pixels_; i++) { |
yootee | 3:a9b4b2565a23 | 86 | // Black magic to fix distorted timing |
yootee | 3:a9b4b2565a23 | 87 | #ifdef __HAL_FLASH_INSTRUCTION_CACHE_DISABLE |
yootee | 3:a9b4b2565a23 | 88 | __HAL_FLASH_INSTRUCTION_CACHE_DISABLE(); |
yootee | 3:a9b4b2565a23 | 89 | #endif |
yootee | 3:a9b4b2565a23 | 90 | |
yootee | 3:a9b4b2565a23 | 91 | byte((int)strip_[i].g); |
yootee | 3:a9b4b2565a23 | 92 | byte((int)strip_[i].r); |
yootee | 3:a9b4b2565a23 | 93 | byte((int)strip_[i].b); |
yootee | 3:a9b4b2565a23 | 94 | |
yootee | 3:a9b4b2565a23 | 95 | #ifdef __HAL_FLASH_INSTRUCTION_CACHE_ENABLE |
yootee | 3:a9b4b2565a23 | 96 | __HAL_FLASH_INSTRUCTION_CACHE_ENABLE(); |
yootee | 3:a9b4b2565a23 | 97 | #endif |
yootee | 3:a9b4b2565a23 | 98 | } |
yootee | 3:a9b4b2565a23 | 99 | |
yootee | 3:a9b4b2565a23 | 100 | __enable_irq(); |
yootee | 3:a9b4b2565a23 | 101 | |
yootee | 3:a9b4b2565a23 | 102 | if (flipwait) flip(); |
yootee | 3:a9b4b2565a23 | 103 | } |
yootee | 3:a9b4b2565a23 | 104 | |
yootee | 3:a9b4b2565a23 | 105 | void NeoPixelOut::send(Pixel *colors, uint32_t count, bool flipwait) |
yootee | 3:a9b4b2565a23 | 106 | { |
yootee | 3:a9b4b2565a23 | 107 | changeNum(count); |
yootee | 3:a9b4b2565a23 | 108 | Pixel* rgb; |
yootee | 3:a9b4b2565a23 | 109 | for (int i = 0; i < count; i++) { |
yootee | 3:a9b4b2565a23 | 110 | rgb = colors++; |
yootee | 3:a9b4b2565a23 | 111 | setPixelColor(i, rgb->hex); |
yootee | 3:a9b4b2565a23 | 112 | } |
yootee | 3:a9b4b2565a23 | 113 | show(flipwait); |
yootee | 3:a9b4b2565a23 | 114 | } |
yootee | 3:a9b4b2565a23 | 115 | |
yootee | 3:a9b4b2565a23 | 116 | uint32_t NeoPixelOut::color(uint8_t r,uint8_t g,uint8_t b){ |
yootee | 3:a9b4b2565a23 | 117 | return (uint32_t)b + ((uint32_t)g << 8) + ((uint32_t)r << 16); |
yootee | 3:a9b4b2565a23 | 118 | } |
yootee | 3:a9b4b2565a23 | 119 | |
yootee | 3:a9b4b2565a23 | 120 | int NeoPixelOut::numPixels(){ |
yootee | 3:a9b4b2565a23 | 121 | return num_pixels_; |
yootee | 3:a9b4b2565a23 | 122 | } |
yootee | 3:a9b4b2565a23 | 123 | |
yootee | 3:a9b4b2565a23 | 124 | void NeoPixelOut::off(bool flag){ |
yootee | 3:a9b4b2565a23 | 125 | for(int i = 0;i < num_pixels_;i++){ |
yootee | 3:a9b4b2565a23 | 126 | strip_[i].hex = 0; |
yootee | 3:a9b4b2565a23 | 127 | } |
yootee | 3:a9b4b2565a23 | 128 | if(flag){ |
yootee | 3:a9b4b2565a23 | 129 | show(); |
yootee | 3:a9b4b2565a23 | 130 | } |
yootee | 3:a9b4b2565a23 | 131 | } |
yootee | 3:a9b4b2565a23 | 132 | |
yootee | 3:a9b4b2565a23 | 133 | void NeoPixelOut::changeNum(uint32_t num){ |
yootee | 3:a9b4b2565a23 | 134 | if(num_pixels_ != num){ |
yootee | 3:a9b4b2565a23 | 135 | strip_.resize(num); |
yootee | 3:a9b4b2565a23 | 136 | num_pixels_ = num; |
yootee | 3:a9b4b2565a23 | 137 | } |
yootee | 3:a9b4b2565a23 | 138 | } |
yootee | 3:a9b4b2565a23 | 139 | |
yootee | 3:a9b4b2565a23 | 140 | void NeoPixelOut::setBrightness(float brightness){ |
yootee | 3:a9b4b2565a23 | 141 | global_scale = brightness; |
yootee | 3:a9b4b2565a23 | 142 | } |
yootee | 3:a9b4b2565a23 | 143 | |
yootee | 3:a9b4b2565a23 | 144 | void NeoPixelOut::flip(void) |
yootee | 3:a9b4b2565a23 | 145 | { |
yootee | 3:a9b4b2565a23 | 146 | wait_us(50); |
yootee | 3:a9b4b2565a23 | 147 | } |