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: mbed DebounceIn WS2812
LED_WS2812.cpp
00001 #include "LED_WS2812.h" 00002 00003 00004 00005 LED_WS2812::LED_WS2812(PinName _PinOut, int _nbLeds) { 00006 nbLeds = _nbLeds; 00007 double period_ns; 00008 Timer tuneTimings; 00009 int sum = 0; 00010 int nopRun; 00011 00012 for(int kavg = 0; kavg<20;kavg++) { 00013 tuneTimings.reset(); 00014 tuneTimings.start(); 00015 for(int nopCount=0; nopCount < 10000; nopCount ++) { 00016 __nop(); 00017 } 00018 tuneTimings.stop(); 00019 nopRun = tuneTimings.read_us(); 00020 sum=nopRun+sum; 00021 } 00022 period_ns = sum/200; /* *1000 for nanoseconds /20 average /10000 count */ 00023 00024 int zero_high = ZERO_HIGH/period_ns; 00025 int zero_low = ZERO_LOW/period_ns; 00026 int one_high = ONE_HIGH/period_ns; 00027 int one_low = ONE_LOW/period_ns; 00028 00029 ws = new WS2812(_PinOut, nbLeds, zero_high, zero_low, one_high, one_low); 00030 ws->useII(WS2812::PER_PIXEL); 00031 00032 pxArray = new PixelArray(nbLeds); 00033 pxInsert = new PixelArray(nbLeds); 00034 ResetColor(); 00035 rotationState = false; 00036 rotationPosition = nbLeds-1; 00037 blinkState = false; 00038 blinkONOFF = false; 00039 intensity = 0xff; 00040 }; 00041 00042 LED_WS2812::~LED_WS2812() { 00043 delete(ws); 00044 delete(pxArray); 00045 } 00046 00047 00048 00049 void LED_WS2812::SetColor(LED_COLORS _color, int position) { 00050 SetColor((unsigned int) _color, position); 00051 }; 00052 00053 void LED_WS2812::SetColor(unsigned int _color, int position) { 00054 if(position < nbLeds && position >=0 ) { 00055 pxArray->Set(position, _color); 00056 pxArray->SetI(position,intensity); 00057 00058 } 00059 __writeBuf(0); 00060 nbInsert = 0; 00061 if(rotationState) StopRotation(); 00062 rotationPosition = nbLeds; 00063 }; 00064 00065 void LED_WS2812::SetColor(LED_COLORS _color) { 00066 SetColor((unsigned int) _color); 00067 }; 00068 00069 void LED_WS2812::SetColor(unsigned int _color) { 00070 for(int i=0;i<nbLeds;i++) { 00071 pxArray->Set(i, _color); 00072 pxArray->SetI(i,intensity); 00073 } 00074 __writeBuf(0); 00075 nbInsert = 0; 00076 if(rotationState) StopRotation(); 00077 rotationPosition = nbLeds; 00078 }; 00079 00080 00081 void LED_WS2812::ResetColor() { 00082 SetColor(BLACK); 00083 00084 } 00085 00086 void LED_WS2812::__writeBuf(int z) { 00087 ws->write_offsets(pxArray->getBuf(),z,z,z); 00088 wait(0.01); 00089 } 00090 00091 void LED_WS2812::__insert2buf() { 00092 for(int i=0;i<nbLeds;i++) { 00093 pxArray->Set(i, pxInsert->Get(i%nbInsert)); 00094 } 00095 __writeBuf(0); 00096 rotationPosition = nbLeds; 00097 } 00098 00099 void LED_WS2812::__insertColor(unsigned int _color, int _intensity) { 00100 pxInsert->Set(nbInsert%nbLeds,_color); 00101 pxInsert->SetI(nbInsert%nbLeds,_intensity); 00102 nbInsert++; 00103 00104 }; 00105 00106 void LED_WS2812::InsertColor(unsigned int _color) { 00107 InsertColor(_color,intensity*100/0xFF); 00108 }; 00109 00110 00111 void LED_WS2812::InsertColor(unsigned int _color, float brightness) { 00112 int pixelIntensity = brightness*0xFF/100; 00113 __insertColor(_color, pixelIntensity); 00114 __insert2buf(); 00115 }; 00116 00117 00118 void LED_WS2812::InsertColorNtimes(int N, unsigned int _color, float brightness) { 00119 for(int i=0;i<N;i++) { 00120 InsertColor(_color, brightness); 00121 } 00122 }; 00123 00124 void LED_WS2812::InsertColorNtimes(int N, unsigned int _color) { 00125 InsertColorNtimes(N, _color, intensity*100/0xFF); 00126 }; 00127 00128 00129 void LED_WS2812::InsertColor(LED_COLORS _color) { 00130 InsertColor((unsigned int)_color); 00131 }; 00132 00133 00134 void LED_WS2812::InsertColor(LED_COLORS _color, float brightness) { 00135 InsertColor((unsigned int)_color, brightness); 00136 }; 00137 00138 00139 void LED_WS2812::InsertColorNtimes(int N, LED_COLORS _color, float brightness) { 00140 InsertColorNtimes(N, (unsigned int)_color, brightness); 00141 }; 00142 00143 void LED_WS2812::InsertColorNtimes(int N, LED_COLORS _color) { 00144 InsertColorNtimes(N, _color, intensity*100/0xFF); 00145 }; 00146 00147 00148 void LED_WS2812::SetIntensity(float perCent) { 00149 intensity = perCent*0xFF/100; 00150 ws->setII(intensity); 00151 for(int i=0;i<nbLeds;i++) { 00152 pxArray->SetI(i,intensity); 00153 } 00154 } 00155 00156 void LED_WS2812::StartRotation(float interval) { 00157 if(rotationState==false) { 00158 rotationState = true; 00159 LEDSystemTick.attach_us(callback(this, &LED_WS2812::Rotate), interval*1000000); 00160 } 00161 } 00162 00163 00164 void LED_WS2812::StopRotation() { 00165 if(rotationState==true) { 00166 rotationState = false; 00167 rotationPosition = 0; 00168 LEDSystemTick.detach(); 00169 } 00170 } 00171 00172 void LED_WS2812::Rotate() { 00173 rotationPosition--; 00174 if (rotationPosition == -1) 00175 rotationPosition = nbLeds-1; 00176 if(!blinkState) __writeBuf(rotationPosition); 00177 } 00178 00179 00180 void LED_WS2812::StartBlink(float interval) { 00181 StopBlink(); 00182 if(blinkState==false) { 00183 blinkState = true; 00184 LEDBlinkSystemTick.attach_us(callback(this, &LED_WS2812::Blink), interval*1000000); 00185 } 00186 } 00187 00188 00189 void LED_WS2812::StopBlink() { 00190 if(blinkState==true) { 00191 blinkState = false; 00192 LEDBlinkSystemTick.detach(); 00193 } 00194 } 00195 00196 void LED_WS2812::Blink() { 00197 blinkONOFF = !blinkONOFF; 00198 if (blinkONOFF) 00199 __writeBuf(rotationPosition); 00200 else { 00201 ws->useII(WS2812::GLOBAL); 00202 ws->setII(0); 00203 __writeBuf(rotationPosition); 00204 ws->useII(WS2812::PER_PIXEL); 00205 ws->setII(intensity); 00206 00207 } 00208 00209 }
Generated on Sat Jul 16 2022 08:24:20 by
1.7.2