A neopixel light painting strip

Dependencies:   mbed

Fork of LocalFileSystem_HelloWorld by mbed official

Revision:
1:cb842cdf5858
Parent:
0:cc465aef98cf
Child:
2:5021f3ba2043
--- a/main.cpp	Wed Feb 13 16:36:26 2013 +0000
+++ b/main.cpp	Mon Jul 06 20:50:42 2015 +0000
@@ -1,9 +1,168 @@
 #include "mbed.h"
- 
+#include "BitmapFile.h"
+#include <string>
+#include "PinDetect.h"
+#include "NeoStrip.h"
+
+# define MAX_FILE 30
+
+#define N 32
+NeoStrip strip(p5, N);
+float bright = 0.2; // 20% is plenty for indoor use
+
+PinDetect b1(p25, PullUp);
+PinDetect b2(p29, PullUp);
+PinDetect b3(p22, PullUp);
+PinDetect b4(p27, PullUp);
+
 LocalFileSystem local("local");               // Create the local filesystem under the name "local"
- 
+Serial pc(USBTX, USBRX); // tx, rx
+BitmapFile *MyBitmap;
+
+char *f_list[MAX_FILE];
+int file_index = 0; // get the index of file being displayed
+unsigned max_file = 0; // register how many files there are
+
+char * get_file_name() {
+    char *fn = NULL;
+    unsigned i = 0; // index in the file list;
+    DIR *d = opendir("/local");               // Opens the root directory of the local file system
+    struct dirent *p;
+    while((p = readdir(d)) != NULL) {         // Print the names of the files in the local file system
+        int l = strlen(p->d_name);
+        if (strcmp(&(p->d_name[l-3]), "BMP") == 0) {
+            i++;
+            if (file_index == i) {
+                fn = (char *) malloc(50);
+                strcpy(fn, p->d_name);
+                break;
+            }
+        }
+    }
+    closedir(d);
+
+    return fn;
+}
+
+
+void brightnessUp(void) {
+    if (bright < 1)
+    {
+        bright += 0.01;
+        if (bright > 1)
+            bright = 1;
+        printf("increase brightness\r\n");
+        strip.setBrightness(bright);
+    }
+}
+
+void brightnessDown(void) {
+    if (bright > 0)
+    {
+        bright -= 0.01;
+        if (bright < 0)
+            bright = 0;
+        printf("decrease brightness\r\n");
+        strip.setBrightness(bright);
+    }
+}
+
+void fileDown(void) {
+    file_index--;
+    if (file_index < 0)
+        file_index = max_file;
+    printf("fileDown to %i:%s\r\n", file_index, f_list[file_index]);
+}
+
+void fileUp(void) {
+    file_index++;
+    if (file_index > max_file)
+        file_index = 0;
+    printf("fileUp to %i:%s\r\n", file_index, f_list[file_index]);
+}
+
+void patternStart(void) {    
+    printf("file_index: %i\r\n", file_index);
+    if (file_index == 0)
+        return;
+    
+    printf("fn: %s\r\n", f_list[file_index]);
+    
+    char *fn = get_file_name();
+    
+    unsigned l = strlen(fn);
+    char *path = (char *) malloc(l+7);
+    path[0] = 0;
+    strcat(path, "/local/");
+    strcat(path, fn);
+    printf("path: %s\r\n", path);
+    
+    MyBitmap = new BitmapFile(path);
+    for(int row = 0; row < MyBitmap->getHeight(); row++)
+    {
+        int *row_color = MyBitmap->getRow(row, false);
+        for(int col = 0; col < MyBitmap->getWidth(); col++)
+        {
+            unsigned c = row_color[col] & 0xffffff;
+            strip.setPixel(col, c & 0xff0000, c & 0xff00, c & 0xff);
+            if ( c > 0xfff )
+                printf(" ");
+            else
+                printf("*");
+        }
+        strip.write();
+        printf("\r\n");
+        delete [] row_color;
+    }
+    printf("closing\r\n");
+    MyBitmap->close();
+    printf("closed\r\n");
+    free(fn);
+    free(path);
+}
+
+
 int main() {
-    FILE *fp = fopen("/local/out.txt", "w");  // Open "out.txt" on the local file system for writing
-    fprintf(fp, "Hello World!");
-    fclose(fp);
+    b1.setAssertValue( 0 );
+    b2.setAssertValue( 0 );
+    b3.setAssertValue( 0 );
+    b4.setAssertValue( 0 );
+    
+    b1.attach_asserted( &brightnessDown );
+    b2.attach_asserted( &patternStart );
+    b3.attach_asserted( &brightnessUp );
+    
+    b1.attach_asserted_held( &fileDown );
+    b3.attach_asserted_held( &fileUp );
+
+    strip.setBrightness(bright);    // set default brightness
+
+    
+    // build a list of files
+    for (unsigned i=0; i<MAX_FILE; i++)
+        f_list[i] = NULL;
+
+    unsigned i = 0; // index in the file list;
+    DIR *d = opendir("/local");               // Opens the root directory of the local file system
+    struct dirent *p;
+    while((p = readdir(d)) != NULL) {         // Print the names of the files in the local file system
+        int l = strlen(p->d_name);
+        if (strcmp(&(p->d_name[l-3]), "BMP") == 0) {
+            char *tmp_str = (char *) malloc(l+1);
+            strcpy(tmp_str, p->d_name);
+            i++;
+            f_list[i] = tmp_str;
+            printf("f_list: %i:%s\r\n", i, tmp_str);
+        }
+    }
+    closedir(d);
+    
+    max_file = i;
+    b1.setSampleFrequency();
+    b2.setSampleFrequency();
+    b3.setSampleFrequency();
+    b4.setSampleFrequency();
+    
+    while(1)
+        __WFI();
 }
\ No newline at end of file