Philip Walne
/
335_project
Dotstar LED demonstration, with stacker game. Uses 2 buttons, and LPC1768 microcontroller
modes/stacker.h@0:9ab173ff6353, 2017-01-04 (annotated)
- Committer:
- pwalne
- Date:
- Wed Jan 04 20:53:49 2017 +0000
- Revision:
- 0:9ab173ff6353
working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pwalne | 0:9ab173ff6353 | 1 | #ifndef _STACKER_H |
pwalne | 0:9ab173ff6353 | 2 | #define _STACKER_H |
pwalne | 0:9ab173ff6353 | 3 | #include <../light_game.h> |
pwalne | 0:9ab173ff6353 | 4 | class Stacker : public Light_Game { |
pwalne | 0:9ab173ff6353 | 5 | public: |
pwalne | 0:9ab173ff6353 | 6 | Stacker() {} |
pwalne | 0:9ab173ff6353 | 7 | virtual void init() { |
pwalne | 0:9ab173ff6353 | 8 | index_last = -1; |
pwalne | 0:9ab173ff6353 | 9 | index_cur = -1; |
pwalne | 0:9ab173ff6353 | 10 | next_goal = 29; |
pwalne | 0:9ab173ff6353 | 11 | forward = true; |
pwalne | 0:9ab173ff6353 | 12 | isDone = false; |
pwalne | 0:9ab173ff6353 | 13 | for(int i = 0; i < 30; i++) //reset all lights! |
pwalne | 0:9ab173ff6353 | 14 | strip.setPixelColor(i, 0, 0, 0); |
pwalne | 0:9ab173ff6353 | 15 | strip.show(); |
pwalne | 0:9ab173ff6353 | 16 | wait(0.3); |
pwalne | 0:9ab173ff6353 | 17 | } |
pwalne | 0:9ab173ff6353 | 18 | |
pwalne | 0:9ab173ff6353 | 19 | virtual void update() { //dont need to declare this virtual but compiler throws warnings... |
pwalne | 0:9ab173ff6353 | 20 | strip.setPixelColor(index_last, 0, 0, 0); |
pwalne | 0:9ab173ff6353 | 21 | if(forward) |
pwalne | 0:9ab173ff6353 | 22 | strip.setPixelColor(++index_cur, 0, 0, 255); |
pwalne | 0:9ab173ff6353 | 23 | else |
pwalne | 0:9ab173ff6353 | 24 | strip.setPixelColor(--index_cur, 0, 0, 255); |
pwalne | 0:9ab173ff6353 | 25 | index_last = index_cur; |
pwalne | 0:9ab173ff6353 | 26 | strip.show(); |
pwalne | 0:9ab173ff6353 | 27 | if(index_cur >= next_goal || index_cur == 0 && !forward) |
pwalne | 0:9ab173ff6353 | 28 | forward = !forward; |
pwalne | 0:9ab173ff6353 | 29 | wait((0.009 * next_goal)); |
pwalne | 0:9ab173ff6353 | 30 | } |
pwalne | 0:9ab173ff6353 | 31 | |
pwalne | 0:9ab173ff6353 | 32 | virtual void btn1() { |
pwalne | 0:9ab173ff6353 | 33 | if(index_cur != next_goal) { //They lost! |
pwalne | 0:9ab173ff6353 | 34 | isDone = true; |
pwalne | 0:9ab173ff6353 | 35 | for(int i = 0; i < 30; i++) |
pwalne | 0:9ab173ff6353 | 36 | strip.setPixelColor(i, 0, 255, 0); |
pwalne | 0:9ab173ff6353 | 37 | strip.show(); |
pwalne | 0:9ab173ff6353 | 38 | wait(1); //let them see it before resetting. |
pwalne | 0:9ab173ff6353 | 39 | return; |
pwalne | 0:9ab173ff6353 | 40 | } |
pwalne | 0:9ab173ff6353 | 41 | index_last = -1; //reset last index. |
pwalne | 0:9ab173ff6353 | 42 | strip.setPixelColor(index_cur, 0, 255, 0); |
pwalne | 0:9ab173ff6353 | 43 | strip.show(); |
pwalne | 0:9ab173ff6353 | 44 | index_cur = 0; |
pwalne | 0:9ab173ff6353 | 45 | forward = true; |
pwalne | 0:9ab173ff6353 | 46 | wait(0.1); //sometimes butons take time to press. |
pwalne | 0:9ab173ff6353 | 47 | if(--next_goal == 0) {//They win! |
pwalne | 0:9ab173ff6353 | 48 | for(int i = 0; i < 30; i++) { |
pwalne | 0:9ab173ff6353 | 49 | if(i % 2 == 0) |
pwalne | 0:9ab173ff6353 | 50 | strip.setPixelColor(i, 100, 100, 100); |
pwalne | 0:9ab173ff6353 | 51 | else |
pwalne | 0:9ab173ff6353 | 52 | strip.setPixelColor(i, 75, 100, 200); |
pwalne | 0:9ab173ff6353 | 53 | } |
pwalne | 0:9ab173ff6353 | 54 | strip.show(); |
pwalne | 0:9ab173ff6353 | 55 | } |
pwalne | 0:9ab173ff6353 | 56 | } |
pwalne | 0:9ab173ff6353 | 57 | virtual void btn2() { btn1();} //Both buttons do the same thing. |
pwalne | 0:9ab173ff6353 | 58 | virtual bool isFinished() { return isDone; } |
pwalne | 0:9ab173ff6353 | 59 | private: |
pwalne | 0:9ab173ff6353 | 60 | int index_cur, index_last; //This is the index the moving light is currently on! |
pwalne | 0:9ab173ff6353 | 61 | int next_goal; //This is the next required light. |
pwalne | 0:9ab173ff6353 | 62 | bool forward, isDone; |
pwalne | 0:9ab173ff6353 | 63 | }; |
pwalne | 0:9ab173ff6353 | 64 | #endif |