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 APA102b Ping SLCD
main.cpp
00001 #include "mbed.h" 00002 #include "Ping.h" 00003 #include "SLCD.h" 00004 00005 #include "APA102a.h" 00006 00007 SLCD slcd; 00008 DigitalIn Button(PTC12); 00009 Ping ping(PTA13); 00010 00011 APA102a LEDs(PTA16, PTA17, PTA15,1000000); // mosi, miso, sclk, rate 00012 00013 // This function was downloaded from: 00014 // http://blog.saikoled.com/post/43693602826/why-every-led-light-should-be-using-hsi 00015 // Blog Post attributed to Brian Neltner 00016 00017 // Function example takes H, S, I, and a pointer to the 00018 // returned RGB colorspace converted vector. It should 00019 // be initialized with: 00020 // 00021 // int rgb[3]; 00022 // 00023 // in the calling function. After calling hsi2rgb 00024 // the vector rgb will contain red, green, and blue 00025 // calculated values. 00026 // 00027 00028 void hsi2rgb(float H, float S, float I, int* rgb) { 00029 00030 int r, g, b; 00031 H = fmod(H,360); // cycle H around to 0-360 degrees 00032 H = 3.14159*H/(float)180; // Convert to radians. 00033 S = S>0?(S<1?S:1):0; // clamp S and I to interval [0,1] 00034 I = I>0?(I<1?I:1):0; 00035 00036 // Math! Thanks in part to Kyle Miller. 00037 if(H < 2.09439) { 00038 r = 255*I/3*(1+S*cos(H)/cos(1.047196667-H)); 00039 g = 255*I/3*(1+S*(1-cos(H)/cos(1.047196667-H))); 00040 b = 255*I/3*(1-S); 00041 } else if(H < 4.188787) { 00042 H = H - 2.09439; 00043 g = 255*I/3*(1+S*cos(H)/cos(1.047196667-H)); 00044 b = 255*I/3*(1+S*(1-cos(H)/cos(1.047196667-H))); 00045 r = 255*I/3*(1-S); 00046 } else { 00047 H = H - 4.188787; 00048 b = 255*I/3*(1+S*cos(H)/cos(1.047196667-H)); 00049 r = 255*I/3*(1+S*(1-cos(H)/cos(1.047196667-H))); 00050 g = 255*I/3*(1-S); 00051 } 00052 rgb[0]=r; 00053 rgb[1]=g; 00054 rgb[2]=b; 00055 } 00056 /// pseudo random number 00057 unsigned int m_z=12434,m_w=33254; 00058 00059 unsigned int rnd() { 00060 m_z = 36969 * (m_z & 65535) + (m_z >>16); 00061 m_w = 18000 * (m_w & 65535) + (m_w >>16); 00062 return ((m_z <<16) + m_w); 00063 } 00064 00065 int main() 00066 { 00067 // Quick example to drive an APA-102 LED Strip from a FRDM-KL46z 00068 00069 // http://www.insomnialighting.com/catalog/index.php?main_page=product_info&products_id=61 00070 // Wire the unit up to SPI, common ground and give it 5 volt power. 00071 00072 // Shift through the spectrum, slowly rotate. 00073 00074 // Setup the spi for 8 bit data, high steady state clock, 00075 // second edge capture, with a 1MHz clock rate 00076 00077 int rgb[3]; 00078 unsigned char r,g,b; 00079 const int N=66; // Number of APA-102 Elements 00080 // const int N=144; 00081 int range; 00082 unsigned int Pixel; 00083 unsigned int Pixels[N]; 00084 00085 float hue,sat,ints; 00086 float hue_adjust =0; 00087 00088 const int NumberOfRays = 19; 00089 00090 00091 float RayHues[19] = // hues are 0 to 360, http://en.wikipedia.org/wiki/HSL_and_HSV 00092 {0.0,19.0,38.0,57.0,76.0,95.0,114.0, 00093 133.0,152.0,171.0,190.0,209.0,228.0, 00094 247.0,266.0,285.0,304.0,323.0,342.0}; 00095 00096 float RaySats[19] = // hues are 0 to 360, http://en.wikipedia.org/wiki/HSL_and_HSV 00097 {0.8,0.8,0.8,0.8,0.8,0.8,0.8, 00098 0.8,0.8,0.8,0.8,0.8,0.8,0.8, 00099 0.8,0.8,0.8,0.8,0.8}; 00100 00101 float RayInts[19] = // hues are 0 to 360, http://en.wikipedia.org/wiki/HSL_and_HSV 00102 {0.8,0.8,0.8,0.8,0.8,0.8,0.8, 00103 0.8,0.8,0.8,0.8,0.8,0.8,0.8, 00104 0.8,0.8,0.8,0.8,0.8}; 00105 00106 int RayLengths[19] = {3, 4, 5, 4, 2, 5, 4, 2, 4,5,3,4,3,2,2,3,2,4,5}; 00107 00108 int Rays[19][5] = { 00109 {1 ,20,39, 0, 0}, //3 00110 {2 ,21,40,53, 0}, //4 00111 {3 ,22,41,54,64}, //5 00112 {4 ,23,42,55, 0}, //4 00113 {5 ,24, 0, 0, 0}, //2 00114 {6 ,25,43,56,65}, //5 00115 {7 ,26,44,57, 0}, //4 00116 {8 ,27, 0, 0, 0}, //2 00117 {9 ,28,45,58, 0}, //4 00118 {10,29,46,59,66}, //5 00119 {11,30,47, 0, 0}, //3 00120 {12,31,48,60, 0}, //4 00121 {13,32,49, 0, 0}, //3 00122 {14,33, 0, 0, 0}, //2 00123 {15,34, 0, 0, 0}, //2 00124 {16,35,50, 0, 0}, //3 00125 {17,36, 0, 0, 0}, //2 00126 {18,37,51,61,0}, //4 00127 {19,38,52,62,63}}; //5 00128 00129 00130 int colors=0x000000; 00131 00132 int Mode=0; 00133 const int NModes = 8; 00134 00135 LEDs.SetBuffer(Pixels,1,N, N,0, false,false); 00136 slcd.Home(); 00137 slcd.printf("%d",Mode); 00138 00139 00140 00141 while (true) { 00142 ping.Send(); 00143 wait_ms(50); // update rate. 00144 range = ping.Read_cm(); 00145 colors+=range/10; 00146 00147 if (!Button ){ 00148 Mode++; 00149 Mode = (Mode %NModes); 00150 slcd.Home(); 00151 slcd.printf("%d",Mode); 00152 00153 } 00154 00155 for(int i = 0;i<19;i++) RaySats[i] = 0.8; 00156 for(int i = 0;i<19;i++) RayInts[i] = 0.8; 00157 00158 00159 switch(Mode){ 00160 case 0: hue_adjust =0; 00161 break; // just display the last "static" image 00162 case 1: hue_adjust = range; // rotate the hue by the range 00163 break; 00164 case 2: hue_adjust = colors; // change the spin rate by the range 00165 break; 00166 case 3: hue_adjust++; // spin at a fixed rate 00167 break; 00168 case 4: for(int i = 0;i<19;i++) RaySats[i] = 1.0/(1.0*(rnd()%16+1)); 00169 break; 00170 case 5: for(int i = 0;i<19;i++) RayInts[i] = 1.0/(1.0*(rnd()%16+1)); 00171 break; 00172 case 6: for(int i = 0;i<19;i++) RayInts[i] = 1.0/(1.0*(rnd()%16+1)); 00173 for(int i = 0;i<19;i++) RaySats[i] = 1.0/(1.0*(rnd()%16+1)); 00174 break; 00175 case 7: hue_adjust = rnd()%16; 00176 break; 00177 00178 } 00179 00180 00181 for(int i=0;i<NumberOfRays;i++) 00182 { 00183 for(int j=0;j<RayLengths[i];j++) 00184 { 00185 00186 hue = RayHues[i]+ hue_adjust; 00187 sat = RaySats[i]; 00188 ints = RayInts[i]; 00189 hsi2rgb(hue, sat, ints,rgb); 00190 r = rgb[0]; 00191 g = rgb[1]; 00192 b = rgb[2]; 00193 00194 Pixel=LEDs.IRGB(7,r,g,b); 00195 Pixels[Rays[i][j]-1]=Pixel; // arrays in C++ start at zero. Need to subtract 1. 00196 00197 } 00198 } 00199 /* 00200 for(int i=0;i<N;i++) 00201 { 00202 hue = (1.0*i)/(1.0*N) * 360 + hue_adjust; 00203 sat = 0.8; 00204 ints = 0.7; 00205 hsi2rgb(hue, sat, ints,rgb); 00206 r = rgb[0]; 00207 g = rgb[1]; 00208 b = rgb[2]; 00209 00210 Pixel=LEDs.IRGB(7,r,g,b); 00211 Pixels[i]=Pixel; 00212 } 00213 */ 00214 LEDs.Repaint(); 00215 00216 } 00217 }
Generated on Sun Jul 24 2022 21:48:23 by
