A neopixel light painting strip
Fork of LocalFileSystem_HelloWorld by
main.cpp@1:cb842cdf5858, 2015-07-06 (annotated)
- Committer:
- lz307
- Date:
- Mon Jul 06 20:50:42 2015 +0000
- Revision:
- 1:cb842cdf5858
- Parent:
- 0:cc465aef98cf
- Child:
- 2:5021f3ba2043
Building light painter with neopixel.
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:cb842cdf5858 | 47 | |
lz307 | 1:cb842cdf5858 | 48 | void brightnessUp(void) { |
lz307 | 1:cb842cdf5858 | 49 | if (bright < 1) |
lz307 | 1:cb842cdf5858 | 50 | { |
lz307 | 1:cb842cdf5858 | 51 | bright += 0.01; |
lz307 | 1:cb842cdf5858 | 52 | if (bright > 1) |
lz307 | 1:cb842cdf5858 | 53 | bright = 1; |
lz307 | 1:cb842cdf5858 | 54 | printf("increase brightness\r\n"); |
lz307 | 1:cb842cdf5858 | 55 | strip.setBrightness(bright); |
lz307 | 1:cb842cdf5858 | 56 | } |
lz307 | 1:cb842cdf5858 | 57 | } |
lz307 | 1:cb842cdf5858 | 58 | |
lz307 | 1:cb842cdf5858 | 59 | void brightnessDown(void) { |
lz307 | 1:cb842cdf5858 | 60 | if (bright > 0) |
lz307 | 1:cb842cdf5858 | 61 | { |
lz307 | 1:cb842cdf5858 | 62 | bright -= 0.01; |
lz307 | 1:cb842cdf5858 | 63 | if (bright < 0) |
lz307 | 1:cb842cdf5858 | 64 | bright = 0; |
lz307 | 1:cb842cdf5858 | 65 | printf("decrease brightness\r\n"); |
lz307 | 1:cb842cdf5858 | 66 | strip.setBrightness(bright); |
lz307 | 1:cb842cdf5858 | 67 | } |
lz307 | 1:cb842cdf5858 | 68 | } |
lz307 | 1:cb842cdf5858 | 69 | |
lz307 | 1:cb842cdf5858 | 70 | void fileDown(void) { |
lz307 | 1:cb842cdf5858 | 71 | file_index--; |
lz307 | 1:cb842cdf5858 | 72 | if (file_index < 0) |
lz307 | 1:cb842cdf5858 | 73 | file_index = max_file; |
lz307 | 1:cb842cdf5858 | 74 | printf("fileDown to %i:%s\r\n", file_index, f_list[file_index]); |
lz307 | 1:cb842cdf5858 | 75 | } |
lz307 | 1:cb842cdf5858 | 76 | |
lz307 | 1:cb842cdf5858 | 77 | void fileUp(void) { |
lz307 | 1:cb842cdf5858 | 78 | file_index++; |
lz307 | 1:cb842cdf5858 | 79 | if (file_index > max_file) |
lz307 | 1:cb842cdf5858 | 80 | file_index = 0; |
lz307 | 1:cb842cdf5858 | 81 | printf("fileUp to %i:%s\r\n", file_index, f_list[file_index]); |
lz307 | 1:cb842cdf5858 | 82 | } |
lz307 | 1:cb842cdf5858 | 83 | |
lz307 | 1:cb842cdf5858 | 84 | void patternStart(void) { |
lz307 | 1:cb842cdf5858 | 85 | printf("file_index: %i\r\n", file_index); |
lz307 | 1:cb842cdf5858 | 86 | if (file_index == 0) |
lz307 | 1:cb842cdf5858 | 87 | return; |
lz307 | 1:cb842cdf5858 | 88 | |
lz307 | 1:cb842cdf5858 | 89 | printf("fn: %s\r\n", f_list[file_index]); |
lz307 | 1:cb842cdf5858 | 90 | |
lz307 | 1:cb842cdf5858 | 91 | char *fn = get_file_name(); |
lz307 | 1:cb842cdf5858 | 92 | |
lz307 | 1:cb842cdf5858 | 93 | unsigned l = strlen(fn); |
lz307 | 1:cb842cdf5858 | 94 | char *path = (char *) malloc(l+7); |
lz307 | 1:cb842cdf5858 | 95 | path[0] = 0; |
lz307 | 1:cb842cdf5858 | 96 | strcat(path, "/local/"); |
lz307 | 1:cb842cdf5858 | 97 | strcat(path, fn); |
lz307 | 1:cb842cdf5858 | 98 | printf("path: %s\r\n", path); |
lz307 | 1:cb842cdf5858 | 99 | |
lz307 | 1:cb842cdf5858 | 100 | MyBitmap = new BitmapFile(path); |
lz307 | 1:cb842cdf5858 | 101 | for(int row = 0; row < MyBitmap->getHeight(); row++) |
lz307 | 1:cb842cdf5858 | 102 | { |
lz307 | 1:cb842cdf5858 | 103 | int *row_color = MyBitmap->getRow(row, false); |
lz307 | 1:cb842cdf5858 | 104 | for(int col = 0; col < MyBitmap->getWidth(); col++) |
lz307 | 1:cb842cdf5858 | 105 | { |
lz307 | 1:cb842cdf5858 | 106 | unsigned c = row_color[col] & 0xffffff; |
lz307 | 1:cb842cdf5858 | 107 | strip.setPixel(col, c & 0xff0000, c & 0xff00, c & 0xff); |
lz307 | 1:cb842cdf5858 | 108 | if ( c > 0xfff ) |
lz307 | 1:cb842cdf5858 | 109 | printf(" "); |
lz307 | 1:cb842cdf5858 | 110 | else |
lz307 | 1:cb842cdf5858 | 111 | printf("*"); |
lz307 | 1:cb842cdf5858 | 112 | } |
lz307 | 1:cb842cdf5858 | 113 | strip.write(); |
lz307 | 1:cb842cdf5858 | 114 | printf("\r\n"); |
lz307 | 1:cb842cdf5858 | 115 | delete [] row_color; |
lz307 | 1:cb842cdf5858 | 116 | } |
lz307 | 1:cb842cdf5858 | 117 | printf("closing\r\n"); |
lz307 | 1:cb842cdf5858 | 118 | MyBitmap->close(); |
lz307 | 1:cb842cdf5858 | 119 | printf("closed\r\n"); |
lz307 | 1:cb842cdf5858 | 120 | free(fn); |
lz307 | 1:cb842cdf5858 | 121 | free(path); |
lz307 | 1:cb842cdf5858 | 122 | } |
lz307 | 1:cb842cdf5858 | 123 | |
lz307 | 1:cb842cdf5858 | 124 | |
mbed_official | 0:cc465aef98cf | 125 | int main() { |
lz307 | 1:cb842cdf5858 | 126 | b1.setAssertValue( 0 ); |
lz307 | 1:cb842cdf5858 | 127 | b2.setAssertValue( 0 ); |
lz307 | 1:cb842cdf5858 | 128 | b3.setAssertValue( 0 ); |
lz307 | 1:cb842cdf5858 | 129 | b4.setAssertValue( 0 ); |
lz307 | 1:cb842cdf5858 | 130 | |
lz307 | 1:cb842cdf5858 | 131 | b1.attach_asserted( &brightnessDown ); |
lz307 | 1:cb842cdf5858 | 132 | b2.attach_asserted( &patternStart ); |
lz307 | 1:cb842cdf5858 | 133 | b3.attach_asserted( &brightnessUp ); |
lz307 | 1:cb842cdf5858 | 134 | |
lz307 | 1:cb842cdf5858 | 135 | b1.attach_asserted_held( &fileDown ); |
lz307 | 1:cb842cdf5858 | 136 | b3.attach_asserted_held( &fileUp ); |
lz307 | 1:cb842cdf5858 | 137 | |
lz307 | 1:cb842cdf5858 | 138 | strip.setBrightness(bright); // set default brightness |
lz307 | 1:cb842cdf5858 | 139 | |
lz307 | 1:cb842cdf5858 | 140 | |
lz307 | 1:cb842cdf5858 | 141 | // build a list of files |
lz307 | 1:cb842cdf5858 | 142 | for (unsigned i=0; i<MAX_FILE; i++) |
lz307 | 1:cb842cdf5858 | 143 | f_list[i] = NULL; |
lz307 | 1:cb842cdf5858 | 144 | |
lz307 | 1:cb842cdf5858 | 145 | unsigned i = 0; // index in the file list; |
lz307 | 1:cb842cdf5858 | 146 | DIR *d = opendir("/local"); // Opens the root directory of the local file system |
lz307 | 1:cb842cdf5858 | 147 | struct dirent *p; |
lz307 | 1:cb842cdf5858 | 148 | while((p = readdir(d)) != NULL) { // Print the names of the files in the local file system |
lz307 | 1:cb842cdf5858 | 149 | int l = strlen(p->d_name); |
lz307 | 1:cb842cdf5858 | 150 | if (strcmp(&(p->d_name[l-3]), "BMP") == 0) { |
lz307 | 1:cb842cdf5858 | 151 | char *tmp_str = (char *) malloc(l+1); |
lz307 | 1:cb842cdf5858 | 152 | strcpy(tmp_str, p->d_name); |
lz307 | 1:cb842cdf5858 | 153 | i++; |
lz307 | 1:cb842cdf5858 | 154 | f_list[i] = tmp_str; |
lz307 | 1:cb842cdf5858 | 155 | printf("f_list: %i:%s\r\n", i, tmp_str); |
lz307 | 1:cb842cdf5858 | 156 | } |
lz307 | 1:cb842cdf5858 | 157 | } |
lz307 | 1:cb842cdf5858 | 158 | closedir(d); |
lz307 | 1:cb842cdf5858 | 159 | |
lz307 | 1:cb842cdf5858 | 160 | max_file = i; |
lz307 | 1:cb842cdf5858 | 161 | b1.setSampleFrequency(); |
lz307 | 1:cb842cdf5858 | 162 | b2.setSampleFrequency(); |
lz307 | 1:cb842cdf5858 | 163 | b3.setSampleFrequency(); |
lz307 | 1:cb842cdf5858 | 164 | b4.setSampleFrequency(); |
lz307 | 1:cb842cdf5858 | 165 | |
lz307 | 1:cb842cdf5858 | 166 | while(1) |
lz307 | 1:cb842cdf5858 | 167 | __WFI(); |
mbed_official | 0:cc465aef98cf | 168 | } |