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.
main.cpp
00001 #include "mbed.h" 00002 #include "ws2811BitBang.h" 00003 #include "ColorConversion.h" 00004 00005 #define NLED (50*6) 00006 00007 DigitalIn button(USER_BUTTON); 00008 00009 DigitalOut boardLED(LED1); 00010 00011 DigitalOut ws2811Data(PA_8); 00012 00013 uint8_t bufferRGB[NLED*3]; 00014 00015 int bufferHS0[NLED*2]; //hue & saturation; value is constant. all variables are fixed precision with scale factor 1000. 00016 int bufferHS1[NLED*2]; //double buffer, for manipulation & swap 00017 #define CVALUE 1000 00018 00019 00020 #define max(a,b) ((a)>=(b)?(a):(b)) 00021 00022 00023 void fillHS0(int hue, int saturation,int hinc){ 00024 int hadd=0; 00025 for(size_t i=0;i<NLED;i++){ 00026 bufferHS0[2*i]=(hue+hadd)%360000; 00027 bufferHS0[2*i+1]=saturation; 00028 hadd+=hinc; 00029 } 00030 } 00031 00032 /* 00033 I made up this shimmer function as I went along; the idea is to make the lights whiteish (low saturation) most of the time 00034 but occasionally have local patches of lights shimmer with color in a way that travels along the string of lights in a wave 00035 and slowly settles down. 1/splashArrival is the probability that a splash of color will be added to the chain during this 00036 call. 00037 */ 00038 void shimmer(int *buf0, int *buf1, size_t len, 00039 int splashArrival = 10, 00040 int restingSaturation=0, 00041 int restingWeight=1, 00042 int neighborWeight=99){ 00043 int totalWeight = restingWeight+neighborWeight; 00044 int event = rand() % splashArrival; 00045 if(event==0){ 00046 int led = rand() % (len-5); 00047 int hue = rand() % (10*360000); 00048 int sat = 600;//rand() % 1000; 00049 for(size_t i=0;i<5;i++){ 00050 buf0[2*(led+i)] = hue; 00051 buf0[2*(led+i)+1]=sat; 00052 } 00053 } 00054 for(size_t i=0;i<len;i++){ 00055 int hl,hc,hr,sl,sc,sr; 00056 if(i==0){ 00057 hl=buf0[2*(len-1)]; 00058 sl=restingSaturation; 00059 hr=buf0[2*(i+1)]; 00060 sr=buf0[2*(i+1)+1]; 00061 }else if(i==len-1){ 00062 hl=buf0[2*(i-1)]; 00063 sl=buf0[2*(i-1)+1]; 00064 hr=buf0[0]; 00065 sr=restingSaturation; 00066 }else{ 00067 hl=buf0[2*(i-1)]; 00068 sl=buf0[2*(i-1)+1]; 00069 hr=buf0[2*(i+1)]; 00070 sr=buf0[2*(i+1)+1]; 00071 } 00072 hc = buf0[2*i]; 00073 sc = buf0[2*i+1]; 00074 buf1[2*i] = (((2*hl+hc+2*hr)/5)*99+300000*1)/100; 00075 //printf("baz: %d\n\r",buf1[2*i]); 00076 buf1[2*i+1]=(max(sl,max(sc,sr))*neighborWeight+restingSaturation*restingWeight)/totalWeight; 00077 } 00078 } 00079 00080 int main() { 00081 00082 int hue = 0; 00083 int arrival=1; 00084 fillHS0(hue,1000,60000); 00085 while(true){ 00086 if(!button){ 00087 arrival=1; 00088 } 00089 shimmer(bufferHS0,bufferHS1,NLED,arrival); 00090 convertHSVtoRGB(bufferHS1,bufferRGB,NLED,CVALUE); 00091 sendBuffer(bufferRGB,NLED,ws2811Data); 00092 boardLED = !boardLED; 00093 00094 shimmer(bufferHS1,bufferHS0,NLED,arrival); 00095 convertHSVtoRGB(bufferHS0,bufferRGB,NLED,CVALUE); 00096 sendBuffer(bufferRGB,NLED,ws2811Data); 00097 boardLED = !boardLED; 00098 00099 if(arrival < 10){ 00100 arrival++; 00101 } 00102 } 00103 }
Generated on Mon Jul 25 2022 05:08:35 by
1.7.2