A neopixel light painting strip

Dependencies:   mbed

Fork of LocalFileSystem_HelloWorld by mbed official

Committer:
lz307
Date:
Tue Nov 10 21:17:10 2015 +0000
Revision:
2:5021f3ba2043
Parent:
1:cb842cdf5858
Commit program for including in notebook.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:cc465aef98cf 1 #include "mbed.h"
lz307 1:cb842cdf5858 2 #include "BitmapFile.h"
lz307 1:cb842cdf5858 3 #include <string>
lz307 1:cb842cdf5858 4 #include "PinDetect.h"
lz307 1:cb842cdf5858 5 #include "NeoStrip.h"
lz307 1:cb842cdf5858 6
lz307 1:cb842cdf5858 7 # define MAX_FILE 30
lz307 1:cb842cdf5858 8
lz307 1:cb842cdf5858 9 #define N 32
lz307 1:cb842cdf5858 10 NeoStrip strip(p5, N);
lz307 1:cb842cdf5858 11 float bright = 0.2; // 20% is plenty for indoor use
lz307 1:cb842cdf5858 12
lz307 1:cb842cdf5858 13 PinDetect b1(p25, PullUp);
lz307 1:cb842cdf5858 14 PinDetect b2(p29, PullUp);
lz307 1:cb842cdf5858 15 PinDetect b3(p22, PullUp);
lz307 1:cb842cdf5858 16 PinDetect b4(p27, PullUp);
lz307 1:cb842cdf5858 17
mbed_official 0:cc465aef98cf 18 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
lz307 1:cb842cdf5858 19 Serial pc(USBTX, USBRX); // tx, rx
lz307 1:cb842cdf5858 20 BitmapFile *MyBitmap;
lz307 1:cb842cdf5858 21
lz307 1:cb842cdf5858 22 char *f_list[MAX_FILE];
lz307 1:cb842cdf5858 23 int file_index = 0; // get the index of file being displayed
lz307 1:cb842cdf5858 24 unsigned max_file = 0; // register how many files there are
lz307 1:cb842cdf5858 25
lz307 1:cb842cdf5858 26 char * get_file_name() {
lz307 1:cb842cdf5858 27 char *fn = NULL;
lz307 1:cb842cdf5858 28 unsigned i = 0; // index in the file list;
lz307 1:cb842cdf5858 29 DIR *d = opendir("/local"); // Opens the root directory of the local file system
lz307 1:cb842cdf5858 30 struct dirent *p;
lz307 1:cb842cdf5858 31 while((p = readdir(d)) != NULL) { // Print the names of the files in the local file system
lz307 1:cb842cdf5858 32 int l = strlen(p->d_name);
lz307 1:cb842cdf5858 33 if (strcmp(&(p->d_name[l-3]), "BMP") == 0) {
lz307 1:cb842cdf5858 34 i++;
lz307 1:cb842cdf5858 35 if (file_index == i) {
lz307 1:cb842cdf5858 36 fn = (char *) malloc(50);
lz307 1:cb842cdf5858 37 strcpy(fn, p->d_name);
lz307 1:cb842cdf5858 38 break;
lz307 1:cb842cdf5858 39 }
lz307 1:cb842cdf5858 40 }
lz307 1:cb842cdf5858 41 }
lz307 1:cb842cdf5858 42 closedir(d);
lz307 1:cb842cdf5858 43
lz307 1:cb842cdf5858 44 return fn;
lz307 1:cb842cdf5858 45 }
lz307 1:cb842cdf5858 46
lz307 2:5021f3ba2043 47 void strip_off() {
lz307 2:5021f3ba2043 48 for (int i=0; i<N; i++) {
lz307 2:5021f3ba2043 49 strip.setPixel(i, 0);
lz307 2:5021f3ba2043 50 }
lz307 2:5021f3ba2043 51 strip.write();
lz307 2:5021f3ba2043 52 }
lz307 2:5021f3ba2043 53
lz307 2:5021f3ba2043 54 void display_number(float f) {
lz307 2:5021f3ba2043 55 for (int i=0; i<N; i++) {
lz307 2:5021f3ba2043 56 if (i<(f*N))
lz307 2:5021f3ba2043 57 strip.setPixel(i, 0xffffff);
lz307 2:5021f3ba2043 58 }
lz307 2:5021f3ba2043 59 strip.write();
lz307 2:5021f3ba2043 60 wait(0.2);
lz307 2:5021f3ba2043 61 strip_off();
lz307 2:5021f3ba2043 62 }
lz307 1:cb842cdf5858 63
lz307 1:cb842cdf5858 64 void brightnessUp(void) {
lz307 1:cb842cdf5858 65 if (bright < 1)
lz307 1:cb842cdf5858 66 {
lz307 1:cb842cdf5858 67 bright += 0.01;
lz307 1:cb842cdf5858 68 if (bright > 1)
lz307 1:cb842cdf5858 69 bright = 1;
lz307 1:cb842cdf5858 70 printf("increase brightness\r\n");
lz307 1:cb842cdf5858 71 strip.setBrightness(bright);
lz307 2:5021f3ba2043 72 display_number(bright);
lz307 1:cb842cdf5858 73 }
lz307 1:cb842cdf5858 74 }
lz307 1:cb842cdf5858 75
lz307 1:cb842cdf5858 76 void brightnessDown(void) {
lz307 1:cb842cdf5858 77 if (bright > 0)
lz307 1:cb842cdf5858 78 {
lz307 1:cb842cdf5858 79 bright -= 0.01;
lz307 1:cb842cdf5858 80 if (bright < 0)
lz307 1:cb842cdf5858 81 bright = 0;
lz307 1:cb842cdf5858 82 printf("decrease brightness\r\n");
lz307 1:cb842cdf5858 83 strip.setBrightness(bright);
lz307 2:5021f3ba2043 84 display_number(bright);
lz307 1:cb842cdf5858 85 }
lz307 1:cb842cdf5858 86 }
lz307 1:cb842cdf5858 87
lz307 1:cb842cdf5858 88 void fileDown(void) {
lz307 1:cb842cdf5858 89 file_index--;
lz307 1:cb842cdf5858 90 if (file_index < 0)
lz307 1:cb842cdf5858 91 file_index = max_file;
lz307 1:cb842cdf5858 92 printf("fileDown to %i:%s\r\n", file_index, f_list[file_index]);
lz307 2:5021f3ba2043 93 display_number(file_index/float(N));
lz307 1:cb842cdf5858 94 }
lz307 1:cb842cdf5858 95
lz307 1:cb842cdf5858 96 void fileUp(void) {
lz307 1:cb842cdf5858 97 file_index++;
lz307 1:cb842cdf5858 98 if (file_index > max_file)
lz307 1:cb842cdf5858 99 file_index = 0;
lz307 1:cb842cdf5858 100 printf("fileUp to %i:%s\r\n", file_index, f_list[file_index]);
lz307 2:5021f3ba2043 101 display_number(file_index/float(N));
lz307 1:cb842cdf5858 102 }
lz307 1:cb842cdf5858 103
lz307 2:5021f3ba2043 104
lz307 2:5021f3ba2043 105 // Converts HSV to RGB with the given hue, assuming
lz307 2:5021f3ba2043 106 // maximum saturation and value
lz307 2:5021f3ba2043 107 int hueToRGB(float h)
lz307 2:5021f3ba2043 108 {
lz307 2:5021f3ba2043 109 // lots of floating point magic from the internet and scratching my head
lz307 2:5021f3ba2043 110 float r, g, b;
lz307 2:5021f3ba2043 111 if (h > 360)
lz307 2:5021f3ba2043 112 h -= 360;
lz307 2:5021f3ba2043 113 if (h < 0)
lz307 2:5021f3ba2043 114 h += 360;
lz307 2:5021f3ba2043 115 int i = (int)(h / 60.0);
lz307 2:5021f3ba2043 116 float f = (h / 60.0) - i;
lz307 2:5021f3ba2043 117 float q = 1 - f;
lz307 2:5021f3ba2043 118
lz307 2:5021f3ba2043 119 switch (i % 6)
lz307 2:5021f3ba2043 120 {
lz307 2:5021f3ba2043 121 case 0: r = 1; g = f; b = 0; break;
lz307 2:5021f3ba2043 122 case 1: r = q; g = 1; b = 0; break;
lz307 2:5021f3ba2043 123 case 2: r = 0; g = 1; b = f; break;
lz307 2:5021f3ba2043 124 case 3: r = 0; g = q; b = 1; break;
lz307 2:5021f3ba2043 125 case 4: r = f; g = 0; b = 1; break;
lz307 2:5021f3ba2043 126 case 5: r = 1; g = 0; b = q; break;
lz307 2:5021f3ba2043 127 default: r = 0; g = 0; b = 0; break;
lz307 2:5021f3ba2043 128 }
lz307 2:5021f3ba2043 129
lz307 2:5021f3ba2043 130 // scale to integers and return the packed value
lz307 2:5021f3ba2043 131 uint8_t R = (uint8_t)(r * 255);
lz307 2:5021f3ba2043 132 uint8_t G = (uint8_t)(g * 255);
lz307 2:5021f3ba2043 133 uint8_t B = (uint8_t)(b * 255);
lz307 2:5021f3ba2043 134
lz307 2:5021f3ba2043 135 return (R << 16) | (G << 8) | B;
lz307 2:5021f3ba2043 136 }
lz307 2:5021f3ba2043 137
lz307 2:5021f3ba2043 138
lz307 2:5021f3ba2043 139 void pattern1()
lz307 2:5021f3ba2043 140 {
lz307 2:5021f3ba2043 141 static float dh = 360.0 / N;
lz307 2:5021f3ba2043 142 static float x = 0;
lz307 2:5021f3ba2043 143 printf("%f\r\n",x);
lz307 2:5021f3ba2043 144
lz307 2:5021f3ba2043 145 for (int i = 0; i < N; i++) {
lz307 2:5021f3ba2043 146 int c = hueToRGB((dh * i) - x);
lz307 2:5021f3ba2043 147 //printf("R %i, G %i, B %i\r\n", (c>>16)&0xff, (c>>8)&0xff, c&0xff);
lz307 2:5021f3ba2043 148 strip.setPixel(i, c);
lz307 2:5021f3ba2043 149 }
lz307 2:5021f3ba2043 150
lz307 2:5021f3ba2043 151 x += 1;
lz307 2:5021f3ba2043 152 if (x > 360)
lz307 2:5021f3ba2043 153 x = 0;
lz307 2:5021f3ba2043 154 }
lz307 2:5021f3ba2043 155
lz307 2:5021f3ba2043 156
lz307 1:cb842cdf5858 157 void patternStart(void) {
lz307 1:cb842cdf5858 158 printf("file_index: %i\r\n", file_index);
lz307 2:5021f3ba2043 159 if (file_index == 0) {
lz307 2:5021f3ba2043 160 for ( int j=0; j<100; j++ ){
lz307 2:5021f3ba2043 161 pattern1();
lz307 2:5021f3ba2043 162 strip.write();
lz307 2:5021f3ba2043 163 wait_ms(20);
lz307 2:5021f3ba2043 164 }
lz307 1:cb842cdf5858 165 return;
lz307 2:5021f3ba2043 166 }
lz307 1:cb842cdf5858 167
lz307 1:cb842cdf5858 168 printf("fn: %s\r\n", f_list[file_index]);
lz307 1:cb842cdf5858 169
lz307 1:cb842cdf5858 170 char *fn = get_file_name();
lz307 1:cb842cdf5858 171
lz307 1:cb842cdf5858 172 unsigned l = strlen(fn);
lz307 1:cb842cdf5858 173 char *path = (char *) malloc(l+7);
lz307 1:cb842cdf5858 174 path[0] = 0;
lz307 1:cb842cdf5858 175 strcat(path, "/local/");
lz307 1:cb842cdf5858 176 strcat(path, fn);
lz307 1:cb842cdf5858 177 printf("path: %s\r\n", path);
lz307 1:cb842cdf5858 178
lz307 1:cb842cdf5858 179 MyBitmap = new BitmapFile(path);
lz307 1:cb842cdf5858 180 for(int row = 0; row < MyBitmap->getHeight(); row++)
lz307 1:cb842cdf5858 181 {
lz307 1:cb842cdf5858 182 int *row_color = MyBitmap->getRow(row, false);
lz307 1:cb842cdf5858 183 for(int col = 0; col < MyBitmap->getWidth(); col++)
lz307 1:cb842cdf5858 184 {
lz307 1:cb842cdf5858 185 unsigned c = row_color[col] & 0xffffff;
lz307 2:5021f3ba2043 186 strip.setPixel(col, c);
lz307 1:cb842cdf5858 187 if ( c > 0xfff )
lz307 1:cb842cdf5858 188 printf(" ");
lz307 1:cb842cdf5858 189 else
lz307 1:cb842cdf5858 190 printf("*");
lz307 1:cb842cdf5858 191 }
lz307 1:cb842cdf5858 192 strip.write();
lz307 2:5021f3ba2043 193 wait_ms(20);
lz307 1:cb842cdf5858 194 printf("\r\n");
lz307 1:cb842cdf5858 195 delete [] row_color;
lz307 1:cb842cdf5858 196 }
lz307 1:cb842cdf5858 197 printf("closing\r\n");
lz307 1:cb842cdf5858 198 MyBitmap->close();
lz307 1:cb842cdf5858 199 printf("closed\r\n");
lz307 1:cb842cdf5858 200 free(fn);
lz307 1:cb842cdf5858 201 free(path);
lz307 2:5021f3ba2043 202 strip_off();
lz307 1:cb842cdf5858 203 }
lz307 1:cb842cdf5858 204
lz307 1:cb842cdf5858 205
lz307 2:5021f3ba2043 206
lz307 2:5021f3ba2043 207
mbed_official 0:cc465aef98cf 208 int main() {
lz307 1:cb842cdf5858 209 b1.setAssertValue( 0 );
lz307 1:cb842cdf5858 210 b2.setAssertValue( 0 );
lz307 1:cb842cdf5858 211 b3.setAssertValue( 0 );
lz307 1:cb842cdf5858 212 b4.setAssertValue( 0 );
lz307 1:cb842cdf5858 213
lz307 1:cb842cdf5858 214 b1.attach_asserted( &brightnessDown );
lz307 1:cb842cdf5858 215 b2.attach_asserted( &patternStart );
lz307 1:cb842cdf5858 216 b3.attach_asserted( &brightnessUp );
lz307 1:cb842cdf5858 217
lz307 1:cb842cdf5858 218 b1.attach_asserted_held( &fileDown );
lz307 1:cb842cdf5858 219 b3.attach_asserted_held( &fileUp );
lz307 1:cb842cdf5858 220
lz307 1:cb842cdf5858 221 strip.setBrightness(bright); // set default brightness
lz307 1:cb842cdf5858 222
lz307 1:cb842cdf5858 223
lz307 1:cb842cdf5858 224 // build a list of files
lz307 1:cb842cdf5858 225 for (unsigned i=0; i<MAX_FILE; i++)
lz307 1:cb842cdf5858 226 f_list[i] = NULL;
lz307 1:cb842cdf5858 227
lz307 1:cb842cdf5858 228 unsigned i = 0; // index in the file list;
lz307 1:cb842cdf5858 229 DIR *d = opendir("/local"); // Opens the root directory of the local file system
lz307 1:cb842cdf5858 230 struct dirent *p;
lz307 1:cb842cdf5858 231 while((p = readdir(d)) != NULL) { // Print the names of the files in the local file system
lz307 1:cb842cdf5858 232 int l = strlen(p->d_name);
lz307 1:cb842cdf5858 233 if (strcmp(&(p->d_name[l-3]), "BMP") == 0) {
lz307 1:cb842cdf5858 234 char *tmp_str = (char *) malloc(l+1);
lz307 1:cb842cdf5858 235 strcpy(tmp_str, p->d_name);
lz307 1:cb842cdf5858 236 i++;
lz307 1:cb842cdf5858 237 f_list[i] = tmp_str;
lz307 1:cb842cdf5858 238 printf("f_list: %i:%s\r\n", i, tmp_str);
lz307 1:cb842cdf5858 239 }
lz307 1:cb842cdf5858 240 }
lz307 1:cb842cdf5858 241 closedir(d);
lz307 1:cb842cdf5858 242
lz307 1:cb842cdf5858 243 max_file = i;
lz307 1:cb842cdf5858 244 b1.setSampleFrequency();
lz307 1:cb842cdf5858 245 b2.setSampleFrequency();
lz307 1:cb842cdf5858 246 b3.setSampleFrequency();
lz307 1:cb842cdf5858 247 b4.setSampleFrequency();
lz307 1:cb842cdf5858 248
lz307 1:cb842cdf5858 249 while(1)
lz307 1:cb842cdf5858 250 __WFI();
mbed_official 0:cc465aef98cf 251 }