This demo shows: - how to write to the multiple outputs/leds using a bus - how to send (debug) info to the pc - how to persist values
No extra hardware needed. The Led Show uses the 4 build in mbed leds.
main.cpp@0:d5f986c1b759, 2012-11-01 (annotated)
- Committer:
- gj_schoneveld
- Date:
- Thu Nov 01 20:49:54 2012 +0000
- Revision:
- 0:d5f986c1b759
Initial version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gj_schoneveld | 0:d5f986c1b759 | 1 | /* |
gj_schoneveld | 0:d5f986c1b759 | 2 | LedShow |
gj_schoneveld | 0:d5f986c1b759 | 3 | |
gj_schoneveld | 0:d5f986c1b759 | 4 | This demo shows: |
gj_schoneveld | 0:d5f986c1b759 | 5 | - how to write to the multiple outputs/leds using a bus |
gj_schoneveld | 0:d5f986c1b759 | 6 | - how to send (debug) info to the pc |
gj_schoneveld | 0:d5f986c1b759 | 7 | - how to persist values |
gj_schoneveld | 0:d5f986c1b759 | 8 | |
gj_schoneveld | 0:d5f986c1b759 | 9 | */ |
gj_schoneveld | 0:d5f986c1b759 | 10 | |
gj_schoneveld | 0:d5f986c1b759 | 11 | #include "mbed.h" |
gj_schoneveld | 0:d5f986c1b759 | 12 | |
gj_schoneveld | 0:d5f986c1b759 | 13 | BusOut leds(LED1, LED2, LED3, LED4); |
gj_schoneveld | 0:d5f986c1b759 | 14 | Serial pc(USBTX, USBRX); |
gj_schoneveld | 0:d5f986c1b759 | 15 | LocalFileSystem local("local"); |
gj_schoneveld | 0:d5f986c1b759 | 16 | |
gj_schoneveld | 0:d5f986c1b759 | 17 | #define delay 0.25 |
gj_schoneveld | 0:d5f986c1b759 | 18 | typedef struct { |
gj_schoneveld | 0:d5f986c1b759 | 19 | int value; |
gj_schoneveld | 0:d5f986c1b759 | 20 | int steps; |
gj_schoneveld | 0:d5f986c1b759 | 21 | } show; |
gj_schoneveld | 0:d5f986c1b759 | 22 | show shows[] = {{1, 4}, {3, 3}, {7, 2}, {5, 2}, {15, 5}}; |
gj_schoneveld | 0:d5f986c1b759 | 23 | char *show_file = "/local/show.txt"; |
gj_schoneveld | 0:d5f986c1b759 | 24 | char *format = "%d\n"; |
gj_schoneveld | 0:d5f986c1b759 | 25 | |
gj_schoneveld | 0:d5f986c1b759 | 26 | int main() |
gj_schoneveld | 0:d5f986c1b759 | 27 | { |
gj_schoneveld | 0:d5f986c1b759 | 28 | pc.printf("*** The Ultimate Led Show ***\n"); |
gj_schoneveld | 0:d5f986c1b759 | 29 | |
gj_schoneveld | 0:d5f986c1b759 | 30 | int show_index = 0; |
gj_schoneveld | 0:d5f986c1b759 | 31 | |
gj_schoneveld | 0:d5f986c1b759 | 32 | FILE *fp = fopen(show_file, "r"); |
gj_schoneveld | 0:d5f986c1b759 | 33 | if (fp) { |
gj_schoneveld | 0:d5f986c1b759 | 34 | fscanf(fp, format, &show_index); |
gj_schoneveld | 0:d5f986c1b759 | 35 | fclose(fp); |
gj_schoneveld | 0:d5f986c1b759 | 36 | } |
gj_schoneveld | 0:d5f986c1b759 | 37 | |
gj_schoneveld | 0:d5f986c1b759 | 38 | fp = fopen(show_file, "w"); |
gj_schoneveld | 0:d5f986c1b759 | 39 | if (fp) { |
gj_schoneveld | 0:d5f986c1b759 | 40 | fprintf(fp, format, (show_index + 1) % (sizeof(shows) / sizeof(show))); |
gj_schoneveld | 0:d5f986c1b759 | 41 | fclose(fp); |
gj_schoneveld | 0:d5f986c1b759 | 42 | } |
gj_schoneveld | 0:d5f986c1b759 | 43 | |
gj_schoneveld | 0:d5f986c1b759 | 44 | pc.printf("Show %d out of %d.\n", show_index + 1, (sizeof(shows) / sizeof(show))); |
gj_schoneveld | 0:d5f986c1b759 | 45 | pc.printf("Press the reset button to move to the next one.\n"); |
gj_schoneveld | 0:d5f986c1b759 | 46 | |
gj_schoneveld | 0:d5f986c1b759 | 47 | show current = shows[show_index]; |
gj_schoneveld | 0:d5f986c1b759 | 48 | |
gj_schoneveld | 0:d5f986c1b759 | 49 | int step = 0; |
gj_schoneveld | 0:d5f986c1b759 | 50 | bool step_forward = true; |
gj_schoneveld | 0:d5f986c1b759 | 51 | while(1) { |
gj_schoneveld | 0:d5f986c1b759 | 52 | leds = current.value << step; |
gj_schoneveld | 0:d5f986c1b759 | 53 | wait(delay); |
gj_schoneveld | 0:d5f986c1b759 | 54 | |
gj_schoneveld | 0:d5f986c1b759 | 55 | if (step == 0) |
gj_schoneveld | 0:d5f986c1b759 | 56 | step_forward = true; |
gj_schoneveld | 0:d5f986c1b759 | 57 | else if (step == current.steps - 1) |
gj_schoneveld | 0:d5f986c1b759 | 58 | step_forward = false; |
gj_schoneveld | 0:d5f986c1b759 | 59 | |
gj_schoneveld | 0:d5f986c1b759 | 60 | if (step_forward) |
gj_schoneveld | 0:d5f986c1b759 | 61 | step++; |
gj_schoneveld | 0:d5f986c1b759 | 62 | else |
gj_schoneveld | 0:d5f986c1b759 | 63 | step--; |
gj_schoneveld | 0:d5f986c1b759 | 64 | } |
gj_schoneveld | 0:d5f986c1b759 | 65 | } |