Tarek Lule / LED_WS2812

Dependencies:   WS2812 PixelArray

Fork of LED_WS2812 by CreaLab

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LED_WS2812.cpp Source File

LED_WS2812.cpp

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