Simple implementation of goal counter for table football using laser diode and phototransistor.
Dependencies: GoalCounter WS2812 PixelArray
main.cpp@4:5a11a84fac69, 2019-01-13 (annotated)
- Committer:
- nxf46245
- Date:
- Sun Jan 13 14:23:30 2019 +0000
- Revision:
- 4:5a11a84fac69
- Parent:
- 3:d74c47d2e933
- Child:
- 5:95cb2348af01
Fix non copyable issue;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nxf46245 | 0:24f846b68c77 | 1 | #include "mbed.h" |
nxf46245 | 0:24f846b68c77 | 2 | #include "WS2812.h" |
nxf46245 | 0:24f846b68c77 | 3 | #include "PixelArray.h" |
nxf46245 | 0:24f846b68c77 | 4 | #include "GoalCounter.h" |
nxf46245 | 0:24f846b68c77 | 5 | |
nxf46245 | 0:24f846b68c77 | 6 | #define WS2812_BUF 10 |
nxf46245 | 0:24f846b68c77 | 7 | #define NUM_COLORS 1 |
nxf46245 | 0:24f846b68c77 | 8 | #define NUM_LEDS_PER_COLOR 2 |
nxf46245 | 4:5a11a84fac69 | 9 | //#define COLOR 0x2f2020 |
nxf46245 | 2:f04959a6fd61 | 10 | #define DEBUG 1 |
nxf46245 | 0:24f846b68c77 | 11 | |
nxf46245 | 3:d74c47d2e933 | 12 | //Initialize instances of Timers |
nxf46245 | 4:5a11a84fac69 | 13 | Timer timer1; |
nxf46245 | 3:d74c47d2e933 | 14 | //Timer t2; |
nxf46245 | 3:d74c47d2e933 | 15 | |
nxf46245 | 1:ffac56d434b3 | 16 | // Initialize instace of a pixel array for LED strip 1 ar 2 |
nxf46245 | 1:ffac56d434b3 | 17 | PixelArray px1(WS2812_BUF); |
nxf46245 | 4:5a11a84fac69 | 18 | //PixelArray px2(WS2812_BUF); |
nxf46245 | 0:24f846b68c77 | 19 | |
nxf46245 | 1:ffac56d434b3 | 20 | // The given numbers are calculated for the K64F MCU |
nxf46245 | 1:ffac56d434b3 | 21 | WS2812 ws1(D4, WS2812_BUF, 0, 5, 5, 0); |
nxf46245 | 4:5a11a84fac69 | 22 | //WS2812 ws2(D5, WS2812_BUF, 0, 5, 5, 0); |
nxf46245 | 0:24f846b68c77 | 23 | |
nxf46245 | 4:5a11a84fac69 | 24 | // For debug use |
nxf46245 | 4:5a11a84fac69 | 25 | Serial pc(USBTX, USBRX); |
nxf46245 | 3:d74c47d2e933 | 26 | |
nxf46245 | 4:5a11a84fac69 | 27 | GoalCounter gc(D2, &timer1); |
nxf46245 | 1:ffac56d434b3 | 28 | |
nxf46245 | 1:ffac56d434b3 | 29 | // Initialize instances of Ticker classes for handling goal counters |
nxf46245 | 2:f04959a6fd61 | 30 | //Ticker bank1; |
nxf46245 | 2:f04959a6fd61 | 31 | //Ticker bank2; |
nxf46245 | 1:ffac56d434b3 | 32 | |
nxf46245 | 2:f04959a6fd61 | 33 | float balltime1 = 0; |
nxf46245 | 2:f04959a6fd61 | 34 | float speed1 = 0; |
nxf46245 | 1:ffac56d434b3 | 35 | |
nxf46245 | 2:f04959a6fd61 | 36 | float balltime2 = 0; |
nxf46245 | 2:f04959a6fd61 | 37 | float speed2 = 0; |
nxf46245 | 2:f04959a6fd61 | 38 | |
nxf46245 | 2:f04959a6fd61 | 39 | uint8_t score1 = 0; |
nxf46245 | 2:f04959a6fd61 | 40 | uint8_t score2 = 0; |
nxf46245 | 1:ffac56d434b3 | 41 | |
nxf46245 | 4:5a11a84fac69 | 42 | void set_score(uint8_t score, PixelArray * px, WS2812 * ws) { |
nxf46245 | 4:5a11a84fac69 | 43 | px->SetAll(0); |
nxf46245 | 4:5a11a84fac69 | 44 | |
nxf46245 | 1:ffac56d434b3 | 45 | for (int i=0; i < score; i++) { |
nxf46245 | 4:5a11a84fac69 | 46 | px->Set(i, 0x2f2020); |
nxf46245 | 1:ffac56d434b3 | 47 | } |
nxf46245 | 1:ffac56d434b3 | 48 | |
nxf46245 | 4:5a11a84fac69 | 49 | px->SetAllI(0x0f); |
nxf46245 | 4:5a11a84fac69 | 50 | ws->write(px->getBuf()); |
nxf46245 | 0:24f846b68c77 | 51 | } |
nxf46245 | 1:ffac56d434b3 | 52 | |
nxf46245 | 2:f04959a6fd61 | 53 | void print_score(uint8_t score, float balltime, float speed) { |
nxf46245 | 2:f04959a6fd61 | 54 | pc.printf("Score : %d \n\r", score); |
nxf46245 | 2:f04959a6fd61 | 55 | pc.printf("Time of ball pass : %f seconds\n\r", balltime); |
nxf46245 | 2:f04959a6fd61 | 56 | pc.printf("Speed of ball (34 mm diameter) : %f kph\n\r", speed); |
nxf46245 | 2:f04959a6fd61 | 57 | } |
nxf46245 | 2:f04959a6fd61 | 58 | |
nxf46245 | 4:5a11a84fac69 | 59 | void get_info(uint8_t * score, float * balltime, float * speed, GoalCounter * gc) { |
nxf46245 | 4:5a11a84fac69 | 60 | *score = gc->get_score(); |
nxf46245 | 4:5a11a84fac69 | 61 | *balltime = gc->get_balltime(); |
nxf46245 | 4:5a11a84fac69 | 62 | *speed = gc->get_ballspeed(); |
nxf46245 | 2:f04959a6fd61 | 63 | } |
nxf46245 | 0:24f846b68c77 | 64 | |
nxf46245 | 4:5a11a84fac69 | 65 | |
nxf46245 | 0:24f846b68c77 | 66 | int main() |
nxf46245 | 0:24f846b68c77 | 67 | { |
nxf46245 | 1:ffac56d434b3 | 68 | |
nxf46245 | 1:ffac56d434b3 | 69 | ws1.useII(WS2812::GLOBAL); // use global intensity scaling |
nxf46245 | 4:5a11a84fac69 | 70 | // ws2.useII(WS2812::GLOBAL); // use global intensity scaling |
nxf46245 | 4:5a11a84fac69 | 71 | |
nxf46245 | 4:5a11a84fac69 | 72 | pc.printf("Initialization... \n\r"); |
nxf46245 | 0:24f846b68c77 | 73 | while(1) { |
nxf46245 | 4:5a11a84fac69 | 74 | if (gc.is_goal == 1) { |
nxf46245 | 4:5a11a84fac69 | 75 | get_info(&score1, &balltime1, &speed1, &gc); |
nxf46245 | 4:5a11a84fac69 | 76 | set_score(score1, &px1, &ws1); |
nxf46245 | 4:5a11a84fac69 | 77 | // score1 = gc.get_score(); |
nxf46245 | 4:5a11a84fac69 | 78 | // balltime1 = gc.get_balltime(); |
nxf46245 | 4:5a11a84fac69 | 79 | // speed1 = gc.get_ballspeed(); |
nxf46245 | 4:5a11a84fac69 | 80 | // pc.printf("Score : %d \n\r", score1); |
nxf46245 | 4:5a11a84fac69 | 81 | // pc.printf("Time of ball pass : %f seconds\n\r", balltime1); |
nxf46245 | 4:5a11a84fac69 | 82 | // pc.printf("Speed of ball (34 mm diameter) : %f kph\n\r", speed1); |
nxf46245 | 4:5a11a84fac69 | 83 | print_score(score1, balltime1, speed1); |
nxf46245 | 4:5a11a84fac69 | 84 | gc.is_goal = 0; |
nxf46245 | 2:f04959a6fd61 | 85 | } |
nxf46245 | 4:5a11a84fac69 | 86 | if (score1 >= 10) { |
nxf46245 | 4:5a11a84fac69 | 87 | pc.printf("Game over \n\r"); |
nxf46245 | 4:5a11a84fac69 | 88 | for (int i=1; i<= 10; i++) { |
nxf46245 | 4:5a11a84fac69 | 89 | pc.printf("Game score: \n\r"); |
nxf46245 | 4:5a11a84fac69 | 90 | balltime1 = gc.get_balltime(i); |
nxf46245 | 4:5a11a84fac69 | 91 | speed1 = gc.get_ballspeed(i); |
nxf46245 | 4:5a11a84fac69 | 92 | print_score(i, balltime1, speed1); |
nxf46245 | 4:5a11a84fac69 | 93 | } |
nxf46245 | 4:5a11a84fac69 | 94 | return 0; |
nxf46245 | 4:5a11a84fac69 | 95 | } |
nxf46245 | 4:5a11a84fac69 | 96 | |
nxf46245 | 0:24f846b68c77 | 97 | } |
nxf46245 | 0:24f846b68c77 | 98 | } |