Julio G / Mbed 2 deprecated LedStrip

Dependencies:   mbed

Fork of LedStrip_test by Balazs Racz

Revision:
8:77fd54b4864c
Parent:
7:62c9a5483a84
Parent:
4:e3b6f5741d9d
Child:
9:01eb82c2a01b
diff -r 62c9a5483a84 -r 77fd54b4864c main.cpp
--- a/main.cpp	Sat Sep 21 20:22:16 2013 +0000
+++ b/main.cpp	Sat Sep 21 20:49:33 2013 +0000
@@ -219,12 +219,54 @@
     Schedulable* next_;
 };
 
+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;
+        delete this;
+    }
+
+private:
+    bool* done_;
+};
+
+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);
+        }
+        strip_changed = true;
+        new WaitAndSetDone(global_tick + (fast_ ? 256 : 512), done_);
+        delete this;
+    }
+
+private:
+    int start_led_, length_;
+    uint8_t from_color_, to_color_;
+    bool fast_;
+    bool* done_;
+};
+
 /* Keep dropping water drops in a bucket, until it fills up. */
 class DropBucketFill : public Schedulable
 {
 public:
-    DropBucketFill(int start_time, int start_led, int length, int drop_size, uint8_t from_color, uint8_t to_color, bool* done)
-        : start_led_(start_led), length_(length), drop_size_(drop_size), from_color_(from_color), to_color_(to_color), done_(done) {
+    DropBucketFill(int start_time, int start_led, int length, int drop_size, uint8_t from_color, uint8_t to_color, int fade_out_pause, uint8_t fade_out_color, bool* done)
+        : start_led_(start_led), length_(length), original_length_(length), drop_size_(drop_size), from_color_(from_color), to_color_(to_color), fade_out_pause_(fade_out_pause), fade_out_color_(fade_out_color), done_(done) {
         time_ = start_time;
         Schedule(this);
     }
@@ -236,23 +278,24 @@
         if (length_ > 0) {
             // There's still space in the bucket. Drop a new drop.
             for (int i = 0; i < min(drop_size_, length_); ++i) {
-                Schedulable* next_drop = NULL;
-                next_drop = this;
+                Schedulable* next_drop = this;
                 new RegionWalkingFadeInOut(time_ + (256 * i / drop_size_), i, drop_size_, start_led_, length_, from_color_, to_color_, true, false, true, next_drop);
             }
             length_--;
         } else if (length_ == 0) {
             // There's no more space in the bucket. Bail out.
-            if (done_) *done_ = true;
+            new FadeFillRegion(global_tick + fade_out_pause_, start_led_, original_length_, to_color_, fade_out_color_, false, done_);
             delete this;
             return;
         }
     }
 
 private:
-    int start_led_, length_;
+    int start_led_, length_, original_length_;
     int drop_size_;
     uint8_t from_color_, to_color_;
+    int fade_out_pause_;
+    uint8_t fade_out_color_;
     bool* done_;
 };
 
@@ -261,7 +304,9 @@
 class ProgramSupervisor
 {
 public:
-
+    ProgramSupervisor() {
+        next_program_ = 0;
+    }
     // The program should be alive forever. Programs do not delete themselves upon completion. Every scheduling of a program happens with global time set to zero.
     void RegisterProgram(Schedulable* program) {
         programs_.push_back(program);
@@ -282,6 +327,8 @@
         memset(strip, 0x80, sizeof(strip));
         write_strip(strip, sizeof(strip));
         if (programs_.empty()) return;
+        programs_[next_program_]->time_ = 0;
+        Schedule(programs_[next_program_]);
     }
 
 
@@ -318,7 +365,7 @@
         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], done_ + i);
+            new DropBucketFill(time, 10 + i * 20, 20, 4, BLACK, google_colors[i], 2560, BLACK, done_ + i);
         }
         new EndWatcher(done_, kLength);
     }
@@ -396,5 +443,6 @@
     MultiDropBucketFillProgram multi_drop;
     
     supervisor.ScheduleProgram();
+
     run_loop();
 }
\ No newline at end of file