![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
自由の旗が永遠にたなびき続けますように。 flag of freedom, forever.
main.cpp@0:61c89e1770be, 2015-11-15 (annotated)
- Committer:
- TareObjects
- Date:
- Sun Nov 15 10:29:31 2015 +0000
- Revision:
- 0:61c89e1770be
solidarite.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TareObjects | 0:61c89e1770be | 1 | #include "mbed.h" |
TareObjects | 0:61c89e1770be | 2 | #include "neopixel.h" |
TareObjects | 0:61c89e1770be | 3 | |
TareObjects | 0:61c89e1770be | 4 | // This must be an SPI MOSI pin. |
TareObjects | 0:61c89e1770be | 5 | #define DATA_PIN dp2 |
TareObjects | 0:61c89e1770be | 6 | |
TareObjects | 0:61c89e1770be | 7 | #define N_COL 8 |
TareObjects | 0:61c89e1770be | 8 | #define N_ROW 8 |
TareObjects | 0:61c89e1770be | 9 | #define N_LED (N_COL * N_ROW) |
TareObjects | 0:61c89e1770be | 10 | |
TareObjects | 0:61c89e1770be | 11 | #define N_WAVE 128 |
TareObjects | 0:61c89e1770be | 12 | |
TareObjects | 0:61c89e1770be | 13 | float wave[N_WAVE]; |
TareObjects | 0:61c89e1770be | 14 | int gSpeed; |
TareObjects | 0:61c89e1770be | 15 | |
TareObjects | 0:61c89e1770be | 16 | void generate(neopixel::Pixel * out, uint32_t index, uintptr_t extra) |
TareObjects | 0:61c89e1770be | 17 | { |
TareObjects | 0:61c89e1770be | 18 | uint8_t red, green, blue; |
TareObjects | 0:61c89e1770be | 19 | |
TareObjects | 0:61c89e1770be | 20 | int x = index % N_COL; |
TareObjects | 0:61c89e1770be | 21 | |
TareObjects | 0:61c89e1770be | 22 | red = green = blue = 0; |
TareObjects | 0:61c89e1770be | 23 | uint8_t val = 8.0 * wave[((x+extra)*gSpeed) % N_WAVE] + 16.0 * wave[extra % N_WAVE] + 16.0; |
TareObjects | 0:61c89e1770be | 24 | val = val / 2; |
TareObjects | 0:61c89e1770be | 25 | if (x <= 2) { |
TareObjects | 0:61c89e1770be | 26 | blue = val; |
TareObjects | 0:61c89e1770be | 27 | } else if (x <= 5) { |
TareObjects | 0:61c89e1770be | 28 | red = green = blue = val / 2; |
TareObjects | 0:61c89e1770be | 29 | } else { |
TareObjects | 0:61c89e1770be | 30 | red = val; |
TareObjects | 0:61c89e1770be | 31 | } |
TareObjects | 0:61c89e1770be | 32 | |
TareObjects | 0:61c89e1770be | 33 | out->red = red; |
TareObjects | 0:61c89e1770be | 34 | out->green = green; |
TareObjects | 0:61c89e1770be | 35 | out->blue = blue; |
TareObjects | 0:61c89e1770be | 36 | } |
TareObjects | 0:61c89e1770be | 37 | |
TareObjects | 0:61c89e1770be | 38 | int main() |
TareObjects | 0:61c89e1770be | 39 | { |
TareObjects | 0:61c89e1770be | 40 | // Create a temporary DigitalIn so we can configure the pull-down resistor. |
TareObjects | 0:61c89e1770be | 41 | // (The mbed API doesn't provide any other way to do this.) |
TareObjects | 0:61c89e1770be | 42 | // An alternative is to connect an external pull-down resistor. |
TareObjects | 0:61c89e1770be | 43 | DigitalIn(DATA_PIN, PullDown); |
TareObjects | 0:61c89e1770be | 44 | |
TareObjects | 0:61c89e1770be | 45 | // The pixel array control class. |
TareObjects | 0:61c89e1770be | 46 | neopixel::PixelArray array(DATA_PIN); |
TareObjects | 0:61c89e1770be | 47 | |
TareObjects | 0:61c89e1770be | 48 | for (int i = 0; i < N_WAVE; i++) { |
TareObjects | 0:61c89e1770be | 49 | wave[i] = sin((float)i / (float)N_WAVE * 3.14159265 * 2.0) * 0.5 + 0.5; |
TareObjects | 0:61c89e1770be | 50 | } |
TareObjects | 0:61c89e1770be | 51 | |
TareObjects | 0:61c89e1770be | 52 | int offset = 0; |
TareObjects | 0:61c89e1770be | 53 | gSpeed = 8; |
TareObjects | 0:61c89e1770be | 54 | |
TareObjects | 0:61c89e1770be | 55 | while(1) { |
TareObjects | 0:61c89e1770be | 56 | array.update(generate, N_LED, offset++); |
TareObjects | 0:61c89e1770be | 57 | if (offset >= N_WAVE) { |
TareObjects | 0:61c89e1770be | 58 | offset = 0; |
TareObjects | 0:61c89e1770be | 59 | gSpeed = (rand() % 8) + 4; |
TareObjects | 0:61c89e1770be | 60 | } |
TareObjects | 0:61c89e1770be | 61 | |
TareObjects | 0:61c89e1770be | 62 | wait_ms(20); |
TareObjects | 0:61c89e1770be | 63 | } |
TareObjects | 0:61c89e1770be | 64 | } |