Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 2:590e99f4a313
- Parent:
- 1:1a0d5a780e57
- Child:
- 3:42efa00ffef4
diff -r 1a0d5a780e57 -r 590e99f4a313 main.cpp
--- a/main.cpp Sat Sep 21 15:21:53 2013 +0000
+++ b/main.cpp Sat Sep 21 19:10:43 2013 +0000
@@ -10,6 +10,9 @@
DigitalOut latch(p16);
DigitalOut strobe(p17);
+Ticker g_ticker;
+
+
#define LENGTH 160
uint8_t strip[160];
@@ -23,6 +26,8 @@
#define MAGENTA (RED | BLUE)
#define WHITE (RED | GREEN | BLUE)
+#define FAST 0x40
+
uint8_t fade_result[] =
{0, 2, 3, 1};
@@ -91,7 +96,7 @@
virtual void Run() {
strip[led_] = getcolor(a_, b_);
if (fast_) {
- strip[led_] |= 0x40;
+ strip[led_] |= FAST;
time_ += 128;
} else {
time_ += 256;
@@ -100,7 +105,7 @@
swap(a_,b_);
Schedule(this);
}
-
+
private:
int led_;
uint8_t a_,b_;
@@ -126,7 +131,7 @@
}
strip[led_] = getcolor(a_, b_);
if (fast_) {
- strip[led_] |= 0x40;
+ strip[led_] |= FAST;
time_ += 128;
} else {
time_ += 256;
@@ -135,7 +140,7 @@
swap(a_,b_);
Schedule(this);
}
-
+
private:
int led_, stride_;
uint8_t a_,b_;
@@ -143,9 +148,98 @@
};
+class RegionWalkingFadeInOut : public Schedulable {
+public:
+ RegionWalkingFadeInOut(int start_time, int led, int stride, int start_led, int length, uint8_t a, uint8_t b, bool fast, bool repeat, bool drop, Schedulable* next)
+ : led_(led - stride), stride_(stride), start_led_(start_led), length_(length), a_(a), b_(b), fast_(fast), step_(true), repeat_(repeat), drop_(drop), next_(next) {
+ time_ = start_time;
+ Schedule(this);
+ }
-int main() {
- Ticker t;
+ virtual void Run() {
+ if (step_) {
+ step_ = false;
+ if (led_ >= 0) strip[led_ + start_led_] = a_;
+ led_ += stride_;
+ if (repeat_) {
+ led_ %= length_;
+ } else {
+ if (led_ > length_) {
+ if (next_ && led_ == (length_ + stride_)) {
+ next_->time_ = global_tick + 1;
+ Schedule(next_);
+ }
+ delete this;
+ return;
+ }
+ }
+ } else {
+ if (led_ == length_ && drop_ && !repeat_) {
+ if (next_) {
+ next_->time_ = global_tick + 1;
+ Schedule(next_);
+ }
+ delete this;
+ return;
+ }
+ step_ = true;
+ }
+ strip[led_ + start_led_] = getcolor(a_, b_);
+ if (fast_) {
+ strip[led_ + start_led_] |= FAST;
+ time_ += 128;
+ } else {
+ time_ += 256;
+ }
+ strip_changed = true;
+ swap(a_,b_);
+ Schedule(this);
+ }
+
+private:
+ int led_, stride_;
+ int start_led_, length_;
+ uint8_t a_,b_;
+ bool fast_, step_;
+ bool repeat_, drop_;
+ Schedulable* next_;
+};
+
+/* 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)
+ : start_led_(start_led), length_(length), drop_size_(drop_size), from_color_(from_color), to_color_(to_color) {
+ time_ = start_time;
+ Schedule(this);
+ }
+
+ virtual void Run() {
+ // The bucket starts with a drop at its end.
+ //strip[start_led_ + length_] = to_color_;
+ //strip[start_led_ + length_] = RED;
+ if (length_ > 0) {
+ // There's still space in the bucket. Drop a new drop.
+ for (int i = 0; i < drop_size_; ++i) {
+ Schedulable* next_drop = NULL;
+ next_drop = this;
+ new RegionWalkingFadeInOut(time_ + (256 * i / drop_size_), i, drop_size_, start_led_, length_ - 1, from_color_, to_color_, true, false, true, next_drop);
+ }
+ length_--;
+ } else {
+ // There's no more space in the bucket. Bail out.
+ delete this;
+ return;
+ }
+ }
+
+private:
+ int start_led_, length_;
+ int drop_size_;
+ uint8_t from_color_, to_color_;
+};
+
+void init_board() {
pc.baud(115200);
myled = 0;
@@ -155,21 +249,14 @@
spi.frequency(300000);
wait_ms(500);
myled = 1;
- memset(strip, 0x80, sizeof(strip));
+ memset(strip, BLACK, sizeof(strip));
write_strip(strip, sizeof(strip));
- t.attach(&tick_cb, 1.0/1000);
+ g_ticker.attach(&tick_cb, 1.0/1000);
memset(strip, 0x0, sizeof(strip));
-
- /*new RepeatedFadeInOut(0, 0, BLACK, RED, false);
- new RepeatedFadeInOut(171, 1, BLACK, RED, false);
- new RepeatedFadeInOut(341, 2, BLACK, RED, false);
-*/
- int stride = 7;
- for (int i = 0; i < stride; i++) {
- new WalkingFadeInOut((256 * i / stride), i, stride, BLACK, GREEN, true);
- }
-
+}
+
+void run_loop() {
while(1) {
while (task_list.empty() || global_tick < task_list.top()->time_) {
if (strip_changed) {
@@ -182,50 +269,19 @@
task_list.pop();
action->Run();
}
- strip[0] = BLACK;
- strip[1] = GREEN;
- strip[2] = BLUE;
- strip[3] = getcolor(BLACK, RED);
+}
- (strip, sizeof(strip));
+int main() {
+ init_board();
- myled = 1;
- int count = 0;
- memset(strip, 0, sizeof(strip));
- while(1) {
- ++count;
- wait_us(300);
- strobe = !strobe;
- // count = 256 => one thing finished.
- if (count > 511) count = 0;
- bool flush = 0;
- if (count == 0) {
- strip[0] = getcolor(BLACK, RED);
- flush = true;
- }
- if (count== 256) {
- strip[0] = getcolor(RED, BLACK);
- flush = true;
- }
- if (count == 256+83) {
- strip[1] = getcolor(BLACK, RED);
- flush = true;
- }
- if (count== 83) {
- strip[1] = getcolor(RED, BLACK);
- flush = true;
- }
- if (count == 166) {
- strip[2] = getcolor(BLACK, RED);
- flush = true;
- }
- if (count== 256+166) {
- strip[2] = getcolor(RED, BLACK);
- flush = true;
- }
- if (flush) {
- write_strip(strip, sizeof(strip));
- memset(strip, 0, sizeof(strip));
- }
+ int stride = 7;
+ for (int i = 0; i < stride; i++) {
+ //new WalkingFadeInOut((256 * i / stride), i, stride, BLACK, RED, true);
+ /* new RepeatedFadeInOut(0, i, BLACK, RED, false); */
+ //new RegionWalkingFadeInOut((256 * i / stride), i, stride, 5, 30, BLACK, BLUE, true, false);
}
-}
+
+ new DropBucketFill(0, 10, 20, 3, BLACK, BLUE);
+
+ run_loop();
+}
\ No newline at end of file
