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.
neopixel.cpp
00001 #include "mbed.h" 00002 #include "neopixel.h" 00003 00004 #if defined(TARGET_NUCLEO_L432KC) 00005 00006 #define PIXEL_WAIT_1_ (6) 00007 #define PIXEL_WAIT_2_ (2) 00008 #define PIXEL_WAIT_3_ (3) 00009 #define PIXEL_WAIT_4_ (5) 00010 00011 #elif defined(TARGET_NUCLEO_F446RE) 00012 00013 #define PIXEL_WAIT_1_ (9) 00014 #define PIXEL_WAIT_2_ (5) 00015 #define PIXEL_WAIT_3_ (4) 00016 #define PIXEL_WAIT_4_ (9) 00017 00018 #else 00019 #error This board is not supported 00020 #endif 00021 00022 NeoPixelOut::NeoPixelOut(PinName pin, int num) : DigitalOut(pin) 00023 { 00024 normalize = false; 00025 global_scale = 1.0f; 00026 num_pixels_ = num; 00027 strip_.resize(num); 00028 } 00029 00030 // The timing should be approximately 800ns/300ns, 300ns/800ns 00031 inline void NeoPixelOut::byte(register uint32_t byte) 00032 { 00033 for (int i = 0; i < 8; i++) { 00034 gpio_write(&gpio, 1); 00035 00036 // duty cycle determines bit value 00037 if (byte & 0x80) { 00038 // one 00039 for(int j = 0; j < PIXEL_WAIT_1_; j++) asm("NOP");//6 9 00040 00041 gpio_write(&gpio, 0); 00042 for(int j = 0; j < PIXEL_WAIT_2_; j++) asm("NOP");//2 5 00043 } 00044 else { 00045 // zero 00046 for(int j = 0; j < PIXEL_WAIT_3_; j++) asm("NOP");//3 4 00047 00048 gpio_write(&gpio, 0); 00049 for(int j = 0; j < PIXEL_WAIT_4_; j++) asm("NOP");//5 9 00050 } 00051 00052 byte = byte << 1; // shift to next bit 00053 } 00054 00055 } 00056 00057 void NeoPixelOut::setPixelColor(uint32_t i,uint8_t r,uint8_t g,uint8_t b){ 00058 setPixelColor(i,color(r, g, b)); 00059 } 00060 00061 void NeoPixelOut::setPixelColor(uint32_t i,uint32_t color){ 00062 if(i < num_pixels_){ 00063 buf_.hex = color; 00064 if (normalize) { 00065 float sum = buf_.r+buf_.g+buf_.b; 00066 if(sum != 0){ 00067 float scale = 255.0f/sum; 00068 buf_.r *= scale; 00069 buf_.g *= scale; 00070 buf_.b *= scale; 00071 } 00072 } 00073 buf_.r *= global_scale; 00074 buf_.g *= global_scale; 00075 buf_.b *= global_scale; 00076 00077 strip_[i] = buf_; 00078 } 00079 } 00080 00081 void NeoPixelOut::show(bool flipwait){ 00082 // Disable interrupts in the critical section 00083 __disable_irq(); 00084 00085 for (int i = 0; i < num_pixels_; i++) { 00086 // Black magic to fix distorted timing 00087 #ifdef __HAL_FLASH_INSTRUCTION_CACHE_DISABLE 00088 __HAL_FLASH_INSTRUCTION_CACHE_DISABLE(); 00089 #endif 00090 00091 byte((int)strip_[i].g); 00092 byte((int)strip_[i].r); 00093 byte((int)strip_[i].b); 00094 00095 #ifdef __HAL_FLASH_INSTRUCTION_CACHE_ENABLE 00096 __HAL_FLASH_INSTRUCTION_CACHE_ENABLE(); 00097 #endif 00098 } 00099 00100 __enable_irq(); 00101 00102 if (flipwait) flip(); 00103 } 00104 00105 void NeoPixelOut::send(Pixel *colors, uint32_t count, bool flipwait) 00106 { 00107 changeNum(count); 00108 Pixel* rgb; 00109 for (int i = 0; i < count; i++) { 00110 rgb = colors++; 00111 setPixelColor(i, rgb->hex); 00112 } 00113 show(flipwait); 00114 } 00115 00116 uint32_t NeoPixelOut::color(uint8_t r,uint8_t g,uint8_t b){ 00117 return (uint32_t)b + ((uint32_t)g << 8) + ((uint32_t)r << 16); 00118 } 00119 00120 int NeoPixelOut::numPixels(){ 00121 return num_pixels_; 00122 } 00123 00124 void NeoPixelOut::off(bool flag){ 00125 for(int i = 0;i < num_pixels_;i++){ 00126 strip_[i].hex = 0; 00127 } 00128 if(flag){ 00129 show(); 00130 } 00131 } 00132 00133 void NeoPixelOut::changeNum(uint32_t num){ 00134 if(num_pixels_ != num){ 00135 strip_.resize(num); 00136 num_pixels_ = num; 00137 } 00138 } 00139 00140 void NeoPixelOut::setBrightness(float brightness){ 00141 global_scale = brightness; 00142 } 00143 00144 void NeoPixelOut::flip(void) 00145 { 00146 wait_us(50); 00147 }
Generated on Sat Oct 15 2022 07:52:33 by
1.7.2