Balazs Racz / Mbed 2 deprecated LedStrip_test

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
bracz
Date:
Tue Sep 24 10:52:07 2013 +0000
Parent:
14:2f2f9eb5532f
Commit message:
production version.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Sep 22 15:21:24 2013 +0000
+++ b/main.cpp	Tue Sep 24 10:52:07 2013 +0000
@@ -14,6 +14,7 @@
 DigitalOut strobe(p17);
 
 Ticker g_ticker;
+void set_frequency(float f);
 
 
 #define LENGTH 160
@@ -221,14 +222,15 @@
     Schedulable* next_;
 };
 
-class WaitAndSetDone : public Schedulable {
+class WaitAndSetDone : public Schedulable
+{
 public:
     WaitAndSetDone(int start_time, bool* done)
         : done_(done) {
         time_ = start_time;
         Schedule(this);
     }
-    
+
     virtual void Run() {
         strip_changed = true;
         if (done_) *done_ = true;
@@ -239,14 +241,15 @@
     bool* done_;
 };
 
-class FadeFillRegion : public Schedulable {
+class FadeFillRegion : public Schedulable
+{
 public:
     FadeFillRegion(int start_time, int start_led, int length, uint8_t from_color, uint8_t to_color, bool fast, bool* done)
         : start_led_(start_led), length_(length), from_color_(from_color), to_color_(to_color), fast_(fast), done_(done) {
         time_ = start_time;
         Schedule(this);
     }
-    
+
     virtual void Run() {
         for (int i = start_led_; i < start_led_ + length_; ++i) {
             strip[i] = getcolor(from_color_, to_color_) | (fast_ ? FAST : 0);
@@ -324,6 +327,7 @@
 
     void ScheduleProgram() {
         global_tick = 0;
+        set_frequency(1000);
         while (!task_list.empty()) task_list.pop();
         Schedule(g_watchdog);
         memset(strip, 0x80, sizeof(strip));
@@ -370,11 +374,11 @@
         fade_out_time_ = fast_ ? 256 : 512;
         supervisor.RegisterProgram(this);
     }
-    
+
     virtual void Run() {
         strip[100] = RED;
         strip_changed = true;
-        
+
         int next_cycle_start_time = global_tick;
         for (int region = 0; region < sizeof(CycleColorsRegions) / sizeof(CycleColorsRegions[0]); ++region) {
             int region_start_led = CycleColorsRegions[region][0];
@@ -405,6 +409,9 @@
 {
 public:
     MultiDropBucketFillProgram() {
+        // We repeat this program as it is three times each time it gets scheduled.
+        supervisor.RegisterProgram(this);
+        supervisor.RegisterProgram(this);
         supervisor.RegisterProgram(this);
     }
 
@@ -413,38 +420,39 @@
         const int kLength = sizeof(google_colors);
         memset(done_, 0, sizeof(done_));
         for (int i = 0; i < kLength; i++) {
-            new DropBucketFill(time, 10 + i * 20, 20, 4, BLACK, google_colors[i], 2560, BLACK, done_ + i);
+            new DropBucketFill(time, 1 + i * 26, 26, 4, BLACK, google_colors[i], 2560, BLACK, done_ + i);
         }
         new EndWatcher(done_, kLength);
     }
 
 private:
-    class EndWatcher : public Schedulable {
+    class EndWatcher : public Schedulable
+    {
     public:
         EndWatcher(bool* done_array, int len) : done_(done_array), len_(len) {
             time_ = 0;
             Schedule(this);
         }
-    
+
         virtual void Run() {
             int i;
             for (i = 0; i < len_ && done_[i]; i++);
             if (i < len_) {
                 // not done yet.
-                time_ = global_tick + 2;    
+                time_ = global_tick + 2;
                 Schedule(this);
             } else {
                 supervisor.CurrentProgramDone();
-                delete this;    
+                delete this;
             }
         }
-    
+
     private:
         bool* done_;
         int len_;
     };
-    
-    
+
+
     bool done_[6];
 };
 
@@ -452,7 +460,7 @@
 {
 public:
     WalkingFade(int start_time, int led, int stride, int end, uint8_t a, uint8_t b, bool fast)
-        : led_(led), stride_(stride), end_(end), a_(a), b_(b), fast_(fast) {
+        : led_(led), start_(led), stride_(stride), end_(end), a_(a), b_(b), fast_(fast) {
         time_ = start_time;
         Schedule(this);
     }
@@ -475,22 +483,25 @@
     }
 
 private:
-    int led_, stride_, end_;
+    int led_, start_, stride_, end_;
     uint8_t a_,b_;
     bool fast_, step_;
 };
 
 
-class MorseGoogleProgram : public Schedulable {
+class MorseGoogleProgram : public Schedulable
+{
 public:
     MorseGoogleProgram() {
         supervisor.RegisterProgram(this);
     }
 
     virtual void Run() {
-        const string code = "W.... .- .--. .--. -.-- / -... .. .-. - .... -.. .- -.-- --..-- / B--. R--- Y--- B--. G.-.. R.   ";
+        //const string t_code = "Y---";
+        const string t_code = "W.... .- .--. .--. -.-- / -... .. .-. - .... -.. .- -.-- --..-- / B--. R--- Y--- B--. G.-.. R.";
+        const string code = (t_code + "            " + t_code + "            " + t_code);
         const int kSpaceTime = 512;
-        const int kLedStart = 150;
+        const int kLedStart = 159;
         const int kStride = -2;
         const int kHalfStrideTime = 128;
         const int kDotSpaceTime = 256;
@@ -498,43 +509,104 @@
         uint8_t color = WHITE;
         for (int i = 0; i < code.size(); i++) {
             switch (code[i]) {
-                case 'R': color = RED ; break;
-                case 'G': color = GREEN ; break;
-                case 'B': color = BLUE ; break;
-                case 'Y': color = YELLOW ; break;
-                case 'W': color = WHITE ; break;
-                case 'C': color = CYAN ; break;
-                case 'M': color = MAGENTA ; break;
+                case 'R':
+                    color = RED ;
+                    break;
+                case 'G':
+                    color = GREEN ;
+                    break;
+                case 'B':
+                    color = BLUE ;
+                    break;
+                case 'Y':
+                    color = YELLOW ;
+                    break;
+                case 'W':
+                    color = WHITE ;
+                    break;
+                case 'C':
+                    color = CYAN ;
+                    break;
+                case 'M':
+                    color = MAGENTA ;
+                    break;
                 case '.': {
-                    new WalkingFade(time, kLedStart, kStride, 160, BLACK, color, false); time += kHalfStrideTime;
-                    new WalkingFade(time, kLedStart - 1, kStride, 160, BLACK, color, false); time += kHalfStrideTime;
-                    new WalkingFade(time, kLedStart, kStride / 2, 160, color, BLACK, true); time += kHalfStrideTime;
+                    new WalkingFade(time, kLedStart, kStride, 160, BLACK, color, false);
+                    time += kHalfStrideTime;
+                    new WalkingFade(time, kLedStart - 1, kStride, 160, BLACK, color, false);
+                    time += kHalfStrideTime;
+                    new WalkingFade(time, kLedStart, kStride / 2, 160, color, BLACK, true);
+                    time += kHalfStrideTime;
                     //new WalkingFade(time, kLedStart - 1, kStride, 160, color, BLACK, true); time += kHalfStrideTime;
                     time += kDotSpaceTime;
                     break;
                 }
                 case '-': {
-                    new WalkingFade(time, kLedStart, kStride, 160, BLACK, color, false); time += kHalfStrideTime;
-                    new WalkingFade(time, kLedStart - 1, kStride, 160, BLACK, color, false); time += kHalfStrideTime;
+                    new WalkingFade(time, kLedStart, kStride, 160, BLACK, color, false);
+                    time += kHalfStrideTime;
+                    new WalkingFade(time, kLedStart - 1, kStride, 160, BLACK, color, false);
+                    time += kHalfStrideTime;
                     time += 3 * kHalfStrideTime;  // should create three complete pixels
-                    new WalkingFade(time, kLedStart, kStride, 160, color, BLACK, false); time += kHalfStrideTime;
-                    new WalkingFade(time, kLedStart - 1, kStride, 160, color, BLACK, false); time += kHalfStrideTime;
+                    new WalkingFade(time, kLedStart, kStride, 160, color, BLACK, false);
+                    time += kHalfStrideTime;
+                    new WalkingFade(time, kLedStart - 1, kStride, 160, color, BLACK, false);
+                    time += kHalfStrideTime;
                     time += kDotSpaceTime;
                     break;
                 }
                 case '/': // fall-through
-                case ' ': time += kSpaceTime; break;
+                case ' ':
+                    time += kSpaceTime;
+                    break;
             }
         }
-        finaliser_.time_ = time + kLedStart * kHalfStrideTime + 2000;
+        finaliser_.time_ = time + kLedStart * 128 + 1000;
         Schedule(&finaliser_);
     }
 
 private:
-    ProgramWatchdog finaliser_;        
+    ProgramWatchdog finaliser_;
 };
 
-class PrintFProgram : public Schedulable {
+
+class GoogleColorMarquee : public Schedulable
+{
+public:
+    GoogleColorMarquee(int stride = 7, int offset = 0, int total_size = 159)
+        : stride_(stride), offset_(offset), total_size_(total_size) {
+        supervisor.RegisterProgram(this);
+    }
+
+    virtual void Run() {
+        set_frequency(500);
+        vector<uint8_t> colors;
+        colors.push_back(BLACK);
+        colors.insert(colors.end(), google_colors, google_colors + sizeof(google_colors));
+        colors.push_back(BLACK);
+        int time = global_tick;
+        const int kColorWaitTime = 2*512;
+        for (int count = 0; count < 4; count++) {
+            for (int i = 0; i < colors.size() - 1; i++) {
+                for (int j = 0; j < stride_; j++) {
+                    new WalkingFade(time + j * 256 / stride_, offset_ + total_size_ - j, -stride_, offset_ + total_size_ + 1, colors[i], colors[i+1], false);
+                }
+                time += kColorWaitTime;
+            }
+            time += -kColorWaitTime + (total_size_ * 256 / stride_ ) / 2;
+        }
+        finaliser_.time_ = time + (total_size_ * 256 / stride_ ) / 2 + 1000;
+        Schedule(&finaliser_);
+    }
+
+private:
+    ProgramWatchdog finaliser_;
+    int stride_;
+    int offset_;
+    int total_size_;
+};
+
+class PrintFProgram : public Schedulable
+{
 public:
     PrintFProgram() {
         supervisor.RegisterProgram(this);
@@ -548,6 +620,11 @@
     }
 };
 
+void set_frequency(float freq) {
+    g_ticker.detach();
+    g_ticker.attach(&tick_cb, 1.0/freq);
+}
+
 void init_board()
 {
     pc.baud(115200);
@@ -561,7 +638,7 @@
     myled = 1;
     memset(strip, BLACK, sizeof(strip));
     write_strip(strip, sizeof(strip));
-    g_ticker.attach(&tick_cb, 1.0/1000);
+    set_frequency(1000);
 
     memset(strip, 0x0, sizeof(strip));
 }
@@ -586,12 +663,13 @@
 {
     init_board();
     g_watchdog = &g_watchdog_impl;
-    
-    // MultiDropBucketFillProgram multi_drop;
+
+    MultiDropBucketFillProgram multi_drop;
     //CycleColors cycle_colors(false, 5);
     MorseGoogleProgram morse;
+    GoogleColorMarquee marquee;
     //PrintFProgram pr;
-    
+
     supervisor.ScheduleProgram();
 
     run_loop();