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: NeoStrip PinDetect mbed
main.cpp
00001 #include "mbed.h" 00002 #include "BitmapFile.h" 00003 #include <string> 00004 #include "PinDetect.h" 00005 #include "NeoStrip.h" 00006 00007 # define MAX_FILE 30 00008 00009 #define N 32 00010 NeoStrip strip(p5, N); 00011 float bright = 0.2; // 20% is plenty for indoor use 00012 00013 PinDetect b1(p25, PullUp); 00014 PinDetect b2(p29, PullUp); 00015 PinDetect b3(p22, PullUp); 00016 PinDetect b4(p27, PullUp); 00017 00018 LocalFileSystem local("local"); // Create the local filesystem under the name "local" 00019 Serial pc(USBTX, USBRX); // tx, rx 00020 BitmapFile *MyBitmap; 00021 00022 char *f_list[MAX_FILE]; 00023 int file_index = 0; // get the index of file being displayed 00024 unsigned max_file = 0; // register how many files there are 00025 00026 char * get_file_name() { 00027 char *fn = NULL; 00028 unsigned i = 0; // index in the file list; 00029 DIR *d = opendir("/local"); // Opens the root directory of the local file system 00030 struct dirent *p; 00031 while((p = readdir(d)) != NULL) { // Print the names of the files in the local file system 00032 int l = strlen(p->d_name); 00033 if (strcmp(&(p->d_name[l-3]), "BMP") == 0) { 00034 i++; 00035 if (file_index == i) { 00036 fn = (char *) malloc(50); 00037 strcpy(fn, p->d_name); 00038 break; 00039 } 00040 } 00041 } 00042 closedir(d); 00043 00044 return fn; 00045 } 00046 00047 void strip_off() { 00048 for (int i=0; i<N; i++) { 00049 strip.setPixel(i, 0); 00050 } 00051 strip.write(); 00052 } 00053 00054 void display_number(float f) { 00055 for (int i=0; i<N; i++) { 00056 if (i<(f*N)) 00057 strip.setPixel(i, 0xffffff); 00058 } 00059 strip.write(); 00060 wait(0.2); 00061 strip_off(); 00062 } 00063 00064 void brightnessUp(void) { 00065 if (bright < 1) 00066 { 00067 bright += 0.01; 00068 if (bright > 1) 00069 bright = 1; 00070 printf("increase brightness\r\n"); 00071 strip.setBrightness(bright); 00072 display_number(bright); 00073 } 00074 } 00075 00076 void brightnessDown(void) { 00077 if (bright > 0) 00078 { 00079 bright -= 0.01; 00080 if (bright < 0) 00081 bright = 0; 00082 printf("decrease brightness\r\n"); 00083 strip.setBrightness(bright); 00084 display_number(bright); 00085 } 00086 } 00087 00088 void fileDown(void) { 00089 file_index--; 00090 if (file_index < 0) 00091 file_index = max_file; 00092 printf("fileDown to %i:%s\r\n", file_index, f_list[file_index]); 00093 display_number(file_index/float(N)); 00094 } 00095 00096 void fileUp(void) { 00097 file_index++; 00098 if (file_index > max_file) 00099 file_index = 0; 00100 printf("fileUp to %i:%s\r\n", file_index, f_list[file_index]); 00101 display_number(file_index/float(N)); 00102 } 00103 00104 00105 // Converts HSV to RGB with the given hue, assuming 00106 // maximum saturation and value 00107 int hueToRGB(float h) 00108 { 00109 // lots of floating point magic from the internet and scratching my head 00110 float r, g, b; 00111 if (h > 360) 00112 h -= 360; 00113 if (h < 0) 00114 h += 360; 00115 int i = (int)(h / 60.0); 00116 float f = (h / 60.0) - i; 00117 float q = 1 - f; 00118 00119 switch (i % 6) 00120 { 00121 case 0: r = 1; g = f; b = 0; break; 00122 case 1: r = q; g = 1; b = 0; break; 00123 case 2: r = 0; g = 1; b = f; break; 00124 case 3: r = 0; g = q; b = 1; break; 00125 case 4: r = f; g = 0; b = 1; break; 00126 case 5: r = 1; g = 0; b = q; break; 00127 default: r = 0; g = 0; b = 0; break; 00128 } 00129 00130 // scale to integers and return the packed value 00131 uint8_t R = (uint8_t)(r * 255); 00132 uint8_t G = (uint8_t)(g * 255); 00133 uint8_t B = (uint8_t)(b * 255); 00134 00135 return (R << 16) | (G << 8) | B; 00136 } 00137 00138 00139 void pattern1() 00140 { 00141 static float dh = 360.0 / N; 00142 static float x = 0; 00143 printf("%f\r\n",x); 00144 00145 for (int i = 0; i < N; i++) { 00146 int c = hueToRGB((dh * i) - x); 00147 //printf("R %i, G %i, B %i\r\n", (c>>16)&0xff, (c>>8)&0xff, c&0xff); 00148 strip.setPixel(i, c); 00149 } 00150 00151 x += 1; 00152 if (x > 360) 00153 x = 0; 00154 } 00155 00156 00157 void patternStart(void) { 00158 printf("file_index: %i\r\n", file_index); 00159 if (file_index == 0) { 00160 for ( int j=0; j<100; j++ ){ 00161 pattern1(); 00162 strip.write(); 00163 wait_ms(20); 00164 } 00165 return; 00166 } 00167 00168 printf("fn: %s\r\n", f_list[file_index]); 00169 00170 char *fn = get_file_name(); 00171 00172 unsigned l = strlen(fn); 00173 char *path = (char *) malloc(l+7); 00174 path[0] = 0; 00175 strcat(path, "/local/"); 00176 strcat(path, fn); 00177 printf("path: %s\r\n", path); 00178 00179 MyBitmap = new BitmapFile(path); 00180 for(int row = 0; row < MyBitmap->getHeight(); row++) 00181 { 00182 int *row_color = MyBitmap->getRow(row, false); 00183 for(int col = 0; col < MyBitmap->getWidth(); col++) 00184 { 00185 unsigned c = row_color[col] & 0xffffff; 00186 strip.setPixel(col, c); 00187 if ( c > 0xfff ) 00188 printf(" "); 00189 else 00190 printf("*"); 00191 } 00192 strip.write(); 00193 wait_ms(20); 00194 printf("\r\n"); 00195 delete [] row_color; 00196 } 00197 printf("closing\r\n"); 00198 MyBitmap->close(); 00199 printf("closed\r\n"); 00200 free(fn); 00201 free(path); 00202 strip_off(); 00203 } 00204 00205 00206 00207 00208 int main() { 00209 b1.setAssertValue( 0 ); 00210 b2.setAssertValue( 0 ); 00211 b3.setAssertValue( 0 ); 00212 b4.setAssertValue( 0 ); 00213 00214 b1.attach_asserted( &brightnessDown ); 00215 b2.attach_asserted( &patternStart ); 00216 b3.attach_asserted( &brightnessUp ); 00217 00218 b1.attach_asserted_held( &fileDown ); 00219 b3.attach_asserted_held( &fileUp ); 00220 00221 strip.setBrightness(bright); // set default brightness 00222 00223 00224 // build a list of files 00225 for (unsigned i=0; i<MAX_FILE; i++) 00226 f_list[i] = NULL; 00227 00228 unsigned i = 0; // index in the file list; 00229 DIR *d = opendir("/local"); // Opens the root directory of the local file system 00230 struct dirent *p; 00231 while((p = readdir(d)) != NULL) { // Print the names of the files in the local file system 00232 int l = strlen(p->d_name); 00233 if (strcmp(&(p->d_name[l-3]), "BMP") == 0) { 00234 char *tmp_str = (char *) malloc(l+1); 00235 strcpy(tmp_str, p->d_name); 00236 i++; 00237 f_list[i] = tmp_str; 00238 printf("f_list: %i:%s\r\n", i, tmp_str); 00239 } 00240 } 00241 closedir(d); 00242 00243 max_file = i; 00244 b1.setSampleFrequency(); 00245 b2.setSampleFrequency(); 00246 b3.setSampleFrequency(); 00247 b4.setSampleFrequency(); 00248 00249 while(1) 00250 __WFI(); 00251 }
Generated on Fri Jul 22 2022 22:17:39 by
1.7.2