shiftlightws2818bcoilxrpm

Dependencies:   mbed NeoStrip PinDetect

Committer:
lz307
Date:
Tue Nov 10 21:28:51 2015 +0000
Revision:
0:a26aca3e4760
Child:
1:b1ec0b8dbde5
Initial commit

Who changed what in which revision?

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