Toyomasa Watarai
/
mbed_Christmas_tree
mbed Christmas tree using NeoPixel
Fork of JBB_SerialLED_test by
main.cpp@2:435f94e9bb06, 2015-12-05 (annotated)
- Committer:
- MACRUM
- Date:
- Sat Dec 05 05:01:15 2015 +0000
- Revision:
- 2:435f94e9bb06
- Parent:
- 1:789215018659
- Child:
- 3:98b6f181c935
Add led_event() macro using Token-Pasting Operator
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:57df396a2de8 | 1 | /** |
MACRUM | 1:789215018659 | 2 | * X'mas tree LED blinky using NeoPixel |
jksoft | 0:57df396a2de8 | 3 | * |
MACRUM | 1:789215018659 | 4 | * Copyright (C) 2015 Toyomasa Watarai (MACRUM) |
jksoft | 0:57df396a2de8 | 5 | */ |
MACRUM | 1:789215018659 | 6 | |
jksoft | 0:57df396a2de8 | 7 | #include "mbed.h" |
jksoft | 0:57df396a2de8 | 8 | #include "LEDStrip.h" |
jksoft | 0:57df396a2de8 | 9 | |
MACRUM | 1:789215018659 | 10 | #define START_ID 2 |
MACRUM | 1:789215018659 | 11 | #define NUM_LEDS 10 |
MACRUM | 1:789215018659 | 12 | #define MAX_LEVEL 100 |
jksoft | 0:57df396a2de8 | 13 | |
MACRUM | 1:789215018659 | 14 | Ticker blinker0, blinker1, blinker2, blinker3, blinker4, blinker5, blinker6, blinker7, blinker8, blinker9; |
MACRUM | 1:789215018659 | 15 | int led0_val, led1_val, led2_val, led3_val, led4_val, led5_val, led6_val, led7_val, led8_val, led9_val; |
MACRUM | 1:789215018659 | 16 | |
MACRUM | 1:789215018659 | 17 | void blink_1() |
MACRUM | 1:789215018659 | 18 | { |
MACRUM | 1:789215018659 | 19 | for( int i=0 ; i <= MAX_LEVEL ; i++ ) { |
MACRUM | 1:789215018659 | 20 | for (int j = START_ID; j <= (START_ID + NUM_LEDS); j++) { |
MACRUM | 1:789215018659 | 21 | tapeSet(j, i, i, i); // LED No , Red , Green , Blue |
MACRUM | 1:789215018659 | 22 | } |
MACRUM | 1:789215018659 | 23 | tapeSend(); |
MACRUM | 1:789215018659 | 24 | wait_ms(15); |
MACRUM | 1:789215018659 | 25 | } |
MACRUM | 1:789215018659 | 26 | for( int i=MAX_LEVEL ; i >= 0 ; i-- ) { |
MACRUM | 1:789215018659 | 27 | for (int j = START_ID; j <= (START_ID + NUM_LEDS); j++) { |
MACRUM | 1:789215018659 | 28 | tapeSet(j, i, i, i); // LED No , Red , Green , Blue |
MACRUM | 1:789215018659 | 29 | } |
MACRUM | 1:789215018659 | 30 | tapeSend(); |
MACRUM | 1:789215018659 | 31 | wait_ms(15); |
MACRUM | 1:789215018659 | 32 | } |
MACRUM | 1:789215018659 | 33 | } |
MACRUM | 1:789215018659 | 34 | |
MACRUM | 1:789215018659 | 35 | void blink_2() |
MACRUM | 1:789215018659 | 36 | { |
MACRUM | 1:789215018659 | 37 | for( int i=0 ; i <= MAX_LEVEL ; i++ ) { |
MACRUM | 1:789215018659 | 38 | for (int j = START_ID; j <= (START_ID + NUM_LEDS); j++) { |
MACRUM | 1:789215018659 | 39 | tapeSet(j, i, 0, 0); // LED No , Red , Green , Blue |
MACRUM | 1:789215018659 | 40 | } |
MACRUM | 1:789215018659 | 41 | tapeSend(); |
MACRUM | 1:789215018659 | 42 | wait_ms(15); |
MACRUM | 1:789215018659 | 43 | } |
MACRUM | 1:789215018659 | 44 | for( int i=0 ; i <= MAX_LEVEL ; i++ ) { |
MACRUM | 1:789215018659 | 45 | for (int j = START_ID; j <= (START_ID + NUM_LEDS); j++) { |
MACRUM | 1:789215018659 | 46 | tapeSet(j, MAX_LEVEL, i, 0); // LED No , Red , Green , Blue |
MACRUM | 1:789215018659 | 47 | } |
MACRUM | 1:789215018659 | 48 | tapeSend(); |
MACRUM | 1:789215018659 | 49 | wait_ms(15); |
MACRUM | 1:789215018659 | 50 | } |
MACRUM | 1:789215018659 | 51 | for( int i=0 ; i <= MAX_LEVEL ; i++ ) { |
MACRUM | 1:789215018659 | 52 | for (int j = START_ID; j <= (START_ID + NUM_LEDS); j++) { |
MACRUM | 1:789215018659 | 53 | tapeSet(j, MAX_LEVEL, MAX_LEVEL, i); // LED No , Red , Green , Blue |
MACRUM | 1:789215018659 | 54 | } |
MACRUM | 1:789215018659 | 55 | tapeSend(); |
MACRUM | 1:789215018659 | 56 | wait_ms(15); |
MACRUM | 1:789215018659 | 57 | } |
MACRUM | 1:789215018659 | 58 | for( int i=MAX_LEVEL ; i >= 0 ; i-- ) { |
MACRUM | 1:789215018659 | 59 | for (int j = START_ID; j <= (START_ID + NUM_LEDS); j++) { |
MACRUM | 1:789215018659 | 60 | tapeSet(j, i, i, i); // LED No , Red , Green , Blue |
MACRUM | 1:789215018659 | 61 | } |
MACRUM | 1:789215018659 | 62 | tapeSend(); |
MACRUM | 1:789215018659 | 63 | wait_ms(15); |
MACRUM | 1:789215018659 | 64 | } |
MACRUM | 1:789215018659 | 65 | } |
MACRUM | 1:789215018659 | 66 | |
MACRUM | 1:789215018659 | 67 | void blink_3() |
MACRUM | 1:789215018659 | 68 | { |
MACRUM | 1:789215018659 | 69 | for( int i = START_ID ; i <= (START_ID + NUM_LEDS); i++) { |
MACRUM | 1:789215018659 | 70 | for (int j = START_ID; j <= (START_ID + NUM_LEDS); j++) { |
MACRUM | 1:789215018659 | 71 | if ( i == j) { |
MACRUM | 1:789215018659 | 72 | tapeSet(j, MAX_LEVEL, 0, 0); // LED No , Red , Green , Blue |
MACRUM | 1:789215018659 | 73 | } else { |
MACRUM | 1:789215018659 | 74 | tapeSet(j, 0, 0, 0); // LED No , Red , Green , Blue |
MACRUM | 1:789215018659 | 75 | } |
MACRUM | 1:789215018659 | 76 | |
MACRUM | 1:789215018659 | 77 | } |
MACRUM | 1:789215018659 | 78 | tapeSend(); |
MACRUM | 1:789215018659 | 79 | wait_ms(100); |
MACRUM | 1:789215018659 | 80 | } |
MACRUM | 1:789215018659 | 81 | } |
MACRUM | 1:789215018659 | 82 | |
MACRUM | 1:789215018659 | 83 | #define WAIT_TIME0 0.19 |
MACRUM | 1:789215018659 | 84 | #define WAIT_TIME1 0.25 |
MACRUM | 1:789215018659 | 85 | #define WAIT_TIME2 0.3 |
MACRUM | 1:789215018659 | 86 | #define WAIT_TIME3 0.2 |
MACRUM | 1:789215018659 | 87 | #define WAIT_TIME4 0.22 |
MACRUM | 1:789215018659 | 88 | #define WAIT_TIME5 0.3 |
MACRUM | 1:789215018659 | 89 | #define WAIT_TIME6 0.17 |
MACRUM | 1:789215018659 | 90 | #define WAIT_TIME7 0.24 |
MACRUM | 1:789215018659 | 91 | #define WAIT_TIME8 0.18 |
MACRUM | 1:789215018659 | 92 | #define WAIT_TIME9 0.28 |
MACRUM | 1:789215018659 | 93 | |
MACRUM | 1:789215018659 | 94 | #define INTERVAL0 9 |
MACRUM | 1:789215018659 | 95 | #define INTERVAL1 14 |
MACRUM | 1:789215018659 | 96 | #define INTERVAL2 20 |
MACRUM | 1:789215018659 | 97 | #define INTERVAL3 17 |
MACRUM | 1:789215018659 | 98 | #define INTERVAL4 30 |
MACRUM | 1:789215018659 | 99 | #define INTERVAL5 20 |
MACRUM | 1:789215018659 | 100 | #define INTERVAL6 25 |
MACRUM | 1:789215018659 | 101 | #define INTERVAL7 17 |
MACRUM | 1:789215018659 | 102 | #define INTERVAL8 22 |
MACRUM | 1:789215018659 | 103 | #define INTERVAL9 19 |
MACRUM | 1:789215018659 | 104 | |
MACRUM | 1:789215018659 | 105 | extern int wakeup; |
MACRUM | 1:789215018659 | 106 | |
MACRUM | 2:435f94e9bb06 | 107 | #define led_event(i) \ |
MACRUM | 2:435f94e9bb06 | 108 | tapeSet(START_ID + 0, led##i##_val, led##i##_val, led##i##_val); \ |
MACRUM | 2:435f94e9bb06 | 109 | wakeup = 1; \ |
MACRUM | 2:435f94e9bb06 | 110 | if (led##i##_val <= 0) \ |
MACRUM | 2:435f94e9bb06 | 111 | led##i##_val = MAX_LEVEL; \ |
MACRUM | 2:435f94e9bb06 | 112 | else \ |
MACRUM | 2:435f94e9bb06 | 113 | led##i##_val -= INTERVAL##i; \ |
MACRUM | 2:435f94e9bb06 | 114 | if (led##i##_val < 0) \ |
MACRUM | 2:435f94e9bb06 | 115 | led##i##_val = 0; |
MACRUM | 1:789215018659 | 116 | |
MACRUM | 1:789215018659 | 117 | |
MACRUM | 2:435f94e9bb06 | 118 | void led0_event() { |
MACRUM | 2:435f94e9bb06 | 119 | led_event(0); |
MACRUM | 1:789215018659 | 120 | } |
MACRUM | 1:789215018659 | 121 | |
MACRUM | 2:435f94e9bb06 | 122 | void led1_event() { |
MACRUM | 2:435f94e9bb06 | 123 | led_event(1); |
MACRUM | 1:789215018659 | 124 | } |
MACRUM | 1:789215018659 | 125 | |
MACRUM | 2:435f94e9bb06 | 126 | void led2_event() { |
MACRUM | 2:435f94e9bb06 | 127 | led_event(2); |
MACRUM | 1:789215018659 | 128 | } |
MACRUM | 1:789215018659 | 129 | |
MACRUM | 2:435f94e9bb06 | 130 | void led3_event() { |
MACRUM | 2:435f94e9bb06 | 131 | led_event(3); |
MACRUM | 1:789215018659 | 132 | } |
MACRUM | 1:789215018659 | 133 | |
MACRUM | 2:435f94e9bb06 | 134 | void led4_event() { |
MACRUM | 2:435f94e9bb06 | 135 | led_event(4); |
MACRUM | 2:435f94e9bb06 | 136 | } |
MACRUM | 1:789215018659 | 137 | |
MACRUM | 2:435f94e9bb06 | 138 | void led5_event() { |
MACRUM | 2:435f94e9bb06 | 139 | led_event(5); |
MACRUM | 1:789215018659 | 140 | } |
MACRUM | 1:789215018659 | 141 | |
MACRUM | 2:435f94e9bb06 | 142 | void led6_event() { |
MACRUM | 2:435f94e9bb06 | 143 | led_event(6); |
MACRUM | 1:789215018659 | 144 | } |
MACRUM | 1:789215018659 | 145 | |
MACRUM | 2:435f94e9bb06 | 146 | void led7_event() { |
MACRUM | 2:435f94e9bb06 | 147 | led_event(7); |
MACRUM | 1:789215018659 | 148 | } |
MACRUM | 1:789215018659 | 149 | |
MACRUM | 2:435f94e9bb06 | 150 | void led8_event() { |
MACRUM | 2:435f94e9bb06 | 151 | led_event(8); |
MACRUM | 1:789215018659 | 152 | } |
MACRUM | 1:789215018659 | 153 | |
MACRUM | 2:435f94e9bb06 | 154 | void led9_event() { |
MACRUM | 2:435f94e9bb06 | 155 | led_event(9); |
MACRUM | 1:789215018659 | 156 | } |
MACRUM | 1:789215018659 | 157 | |
MACRUM | 1:789215018659 | 158 | void blink_4() |
MACRUM | 1:789215018659 | 159 | { |
MACRUM | 1:789215018659 | 160 | led0_val = MAX_LEVEL; |
MACRUM | 1:789215018659 | 161 | led1_val = MAX_LEVEL; |
MACRUM | 1:789215018659 | 162 | led2_val = MAX_LEVEL; |
MACRUM | 1:789215018659 | 163 | led3_val = MAX_LEVEL; |
MACRUM | 1:789215018659 | 164 | led4_val = MAX_LEVEL; |
MACRUM | 1:789215018659 | 165 | led5_val = MAX_LEVEL; |
MACRUM | 1:789215018659 | 166 | led6_val = MAX_LEVEL; |
MACRUM | 1:789215018659 | 167 | led7_val = MAX_LEVEL; |
MACRUM | 1:789215018659 | 168 | led8_val = MAX_LEVEL; |
MACRUM | 1:789215018659 | 169 | led9_val = MAX_LEVEL; |
MACRUM | 1:789215018659 | 170 | |
MACRUM | 1:789215018659 | 171 | blinker0.attach(&led0_event, WAIT_TIME0); |
MACRUM | 1:789215018659 | 172 | blinker1.attach(&led1_event, WAIT_TIME1); |
MACRUM | 1:789215018659 | 173 | blinker2.attach(&led2_event, WAIT_TIME2); |
MACRUM | 1:789215018659 | 174 | blinker3.attach(&led3_event, WAIT_TIME3); |
MACRUM | 1:789215018659 | 175 | blinker4.attach(&led4_event, WAIT_TIME4); |
MACRUM | 1:789215018659 | 176 | blinker5.attach(&led5_event, WAIT_TIME5); |
MACRUM | 1:789215018659 | 177 | blinker6.attach(&led6_event, WAIT_TIME6); |
MACRUM | 1:789215018659 | 178 | blinker7.attach(&led7_event, WAIT_TIME7); |
MACRUM | 1:789215018659 | 179 | blinker8.attach(&led8_event, WAIT_TIME8); |
MACRUM | 1:789215018659 | 180 | blinker9.attach(&led9_event, WAIT_TIME9); |
MACRUM | 1:789215018659 | 181 | |
MACRUM | 1:789215018659 | 182 | wait(8); |
MACRUM | 1:789215018659 | 183 | |
MACRUM | 1:789215018659 | 184 | blinker0.detach(); |
MACRUM | 1:789215018659 | 185 | blinker1.detach(); |
MACRUM | 1:789215018659 | 186 | blinker2.detach(); |
MACRUM | 1:789215018659 | 187 | blinker3.detach(); |
MACRUM | 1:789215018659 | 188 | blinker4.detach(); |
MACRUM | 1:789215018659 | 189 | blinker5.detach(); |
MACRUM | 1:789215018659 | 190 | blinker6.detach(); |
MACRUM | 1:789215018659 | 191 | blinker7.detach(); |
MACRUM | 1:789215018659 | 192 | blinker8.detach(); |
MACRUM | 1:789215018659 | 193 | blinker9.detach(); |
MACRUM | 1:789215018659 | 194 | } |
MACRUM | 1:789215018659 | 195 | |
MACRUM | 1:789215018659 | 196 | int main() |
MACRUM | 1:789215018659 | 197 | { |
MACRUM | 1:789215018659 | 198 | const int loops = 3; |
MACRUM | 1:789215018659 | 199 | tapeInit(0, START_ID + NUM_LEDS); // freq( 0:default ) , SerialLED num |
MACRUM | 1:789215018659 | 200 | |
MACRUM | 1:789215018659 | 201 | int i = 0; |
MACRUM | 1:789215018659 | 202 | while(1) { |
MACRUM | 1:789215018659 | 203 | if ((i/loops) == 0) { |
MACRUM | 1:789215018659 | 204 | blink_4(); |
MACRUM | 1:789215018659 | 205 | } |
MACRUM | 1:789215018659 | 206 | if ((i/loops) == 1) { |
MACRUM | 1:789215018659 | 207 | blink_2(); |
MACRUM | 1:789215018659 | 208 | } |
MACRUM | 1:789215018659 | 209 | if ((i/loops) == 2) { |
MACRUM | 1:789215018659 | 210 | blink_3(); |
MACRUM | 1:789215018659 | 211 | } |
MACRUM | 1:789215018659 | 212 | if ((i/loops) == 3) { |
MACRUM | 1:789215018659 | 213 | blink_1(); |
MACRUM | 1:789215018659 | 214 | } |
MACRUM | 1:789215018659 | 215 | |
MACRUM | 1:789215018659 | 216 | i++; |
MACRUM | 1:789215018659 | 217 | if (i >= (loops * 4)) { |
MACRUM | 1:789215018659 | 218 | i = 0; |
MACRUM | 1:789215018659 | 219 | } |
MACRUM | 1:789215018659 | 220 | } |
MACRUM | 1:789215018659 | 221 | } |