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.
Fork of NeoPixels by
main.cpp
00001 /* 00002 * Adafruit NeoPixel 8x8 matrix example 00003 * 00004 * This program displays a couple simple patterns on an 8x8 NeoPixel matrix. 00005 * 00006 * 3 buttons are used for DigitalIns, 2 control the brightness up and down, 00007 * and the third switches patterns 00008 */ 00009 00010 #include "mbed.h" 00011 #include "NeoStrip.h" 00012 #include "gt.h" 00013 00014 #define N 64 00015 #define PATTERNS 3 00016 00017 Serial pc(USBTX, USBRX); 00018 int hueToRGB(float h); 00019 void pattern0(); 00020 void pattern1(); 00021 void pattern2(); 00022 00023 // array of function pointers to the various patterns 00024 void (*patterns[])(void) = {&pattern0, &pattern1, &pattern2}; 00025 00026 NeoStrip strip(p21, N); 00027 int selekt=0; 00028 /*DigitalIn b1(p20); // brightness up 00029 DigitalIn b2(p19); // brightness down 00030 DigitalIn b3(p21); // next pattern*/ 00031 00032 // timer used for debugging 00033 Timer timer; 00034 00035 int main() 00036 { 00037 pc.baud(115200); 00038 printf("Test af LED\r\n"); 00039 00040 int pattern = 0; 00041 float bright = 0.2; // 20% is plenty for indoor use 00042 int b3o = selekt; // old copy of button 3 to poll for changes 00043 00044 strip.setBrightness(bright); // set default brightness 00045 00046 while (true) 00047 { 00048 timer.reset(); // use a timer to measure loop execution time for debugging purposes 00049 timer.start(); // for this application, the main loop takes approximately 3ms to run 00050 00051 // button 1 increases brightness 00052 if ((selekt==1) && bright < 1) 00053 { 00054 bright += 0.01; 00055 if (bright > 1) 00056 bright = 1; 00057 strip.setBrightness(bright); 00058 } 00059 00060 // button 2 decreases brightness 00061 if ((selekt==2) && bright > 0) 00062 { 00063 bright -= 0.01; 00064 if (bright < 0) 00065 bright = 0; 00066 strip.setBrightness(bright); 00067 } 00068 00069 // button 3 changes the pattern, only do stuff when its state has changed 00070 if ((selekt==3) != b3o) 00071 { 00072 if (++pattern == PATTERNS) pattern = 0; 00073 00074 } 00075 selekt=0; 00076 // run the pattern update function which sets the strip's pixels 00077 patterns[pattern](); 00078 strip.write(); 00079 00080 timer.stop(); 00081 // print loop time if b3 is pressed 00082 if (pc.readable()) { 00083 switch(pc.getc()) { 00084 case '1': selekt=1; break; 00085 case '2': selekt=2; break; 00086 case '3': selekt=3; break; 00087 } 00088 } 00089 wait_ms(10); 00090 } 00091 } 00092 00093 // pattern0 displays a static image 00094 void pattern0() 00095 { 00096 strip.setPixels(0, N, gt_img); 00097 } 00098 00099 // display a shifting rainbow, all colors have maximum 00100 // saturation and value, with evenly spaced hue 00101 void pattern1() 00102 { 00103 static float dh = 360.0 / N; 00104 static float x = 0; 00105 00106 for (int i = 0; i < N; i++) 00107 strip.setPixel(i, hueToRGB((dh * i) - x)); 00108 00109 x += 1; 00110 if (x > 360) 00111 x = 0; 00112 } 00113 00114 // display a shifting gradient between red and blue 00115 void pattern2() 00116 { 00117 // offset for each pixel to allow the pattern to move 00118 static float x = 0; 00119 00120 float r, b, y; 00121 00122 for (int i = 0; i < N; i++) 00123 { 00124 // y is a scaled position between 0 (red) and 1.0 (blue) 00125 y = 1.0 * i / (N - 1) + x; 00126 if (y > 1) 00127 y -= 1; 00128 00129 // if on the left half, red is decreasing and blue is increasng 00130 if (y < 0.5) 00131 { 00132 b = 2 * y; 00133 r = 1 - b; 00134 } 00135 00136 // else red is increasing and blue is decreasing 00137 else 00138 { 00139 r = 2 * (y - 0.5); 00140 b = 1 - r; 00141 } 00142 00143 // scale to integers and set the pixel 00144 strip.setPixel(i, (uint8_t)(r * 255), 0, (uint8_t)(b * 200)); 00145 } 00146 00147 x += 0.003; 00148 if (x > 1) 00149 x = 0; 00150 } 00151 00152 // Converts HSV to RGB with the given hue, assuming 00153 // maximum saturation and value 00154 int hueToRGB(float h) 00155 { 00156 // lots of floating point magic from the internet and scratching my head 00157 float r, g, b; 00158 if (h > 360) 00159 h -= 360; 00160 if (h < 0) 00161 h += 360; 00162 int i = (int)(h / 60.0); 00163 float f = (h / 60.0) - i; 00164 float q = 1 - f; 00165 00166 switch (i % 6) 00167 { 00168 case 0: r = 1; g = f; b = 0; break; 00169 case 1: r = q; g = 1; b = 0; break; 00170 case 2: r = 0; g = 1; b = f; break; 00171 case 3: r = 0; g = q; b = 1; break; 00172 case 4: r = f; g = 0; b = 1; break; 00173 case 5: r = 1; g = 0; b = q; break; 00174 default: r = 0; g = 0; b = 0; break; 00175 } 00176 00177 // scale to integers and return the packed value 00178 uint8_t R = (uint8_t)(r * 255); 00179 uint8_t G = (uint8_t)(g * 255); 00180 uint8_t B = (uint8_t)(b * 255); 00181 00182 return (R << 16) | (G << 8) | B; 00183 } 00184
Generated on Fri Jul 15 2022 10:29:52 by
1.7.2
