Simple implementation of goal counter for table football using laser diode and phototransistor.
Dependencies: GoalCounter WS2812 PixelArray
main.cpp@2:f04959a6fd61, 2019-01-11 (annotated)
- Committer:
- nxf46245
- Date:
- Fri Jan 11 18:29:58 2019 +0000
- Revision:
- 2:f04959a6fd61
- Parent:
- 1:ffac56d434b3
- Child:
- 3:d74c47d2e933
Code cleaning, NonCopyable warning needs to be fixed
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 | 0:24f846b68c77 | 9 | #define COLOR 0x2f2020 |
nxf46245 | 2:f04959a6fd61 | 10 | #define DEBUG 1 |
nxf46245 | 0:24f846b68c77 | 11 | |
nxf46245 | 1:ffac56d434b3 | 12 | // Initialize instace of a pixel array for LED strip 1 ar 2 |
nxf46245 | 1:ffac56d434b3 | 13 | PixelArray px1(WS2812_BUF); |
nxf46245 | 1:ffac56d434b3 | 14 | PixelArray px2(WS2812_BUF); |
nxf46245 | 0:24f846b68c77 | 15 | |
nxf46245 | 1:ffac56d434b3 | 16 | // The given numbers are calculated for the K64F MCU |
nxf46245 | 1:ffac56d434b3 | 17 | WS2812 ws1(D4, WS2812_BUF, 0, 5, 5, 0); |
nxf46245 | 2:f04959a6fd61 | 18 | WS2812 ws2(D5, WS2812_BUF, 0, 5, 5, 0); |
nxf46245 | 0:24f846b68c77 | 19 | |
nxf46245 | 1:ffac56d434b3 | 20 | // Initialize instances of GoalCounter classes for counting goals |
nxf46245 | 1:ffac56d434b3 | 21 | GoalCounter gc1(D2); |
nxf46245 | 1:ffac56d434b3 | 22 | GoalCounter gc2(D3); |
nxf46245 | 1:ffac56d434b3 | 23 | |
nxf46245 | 1:ffac56d434b3 | 24 | // Initialize instances of Ticker classes for handling goal counters |
nxf46245 | 2:f04959a6fd61 | 25 | //Ticker bank1; |
nxf46245 | 2:f04959a6fd61 | 26 | //Ticker bank2; |
nxf46245 | 1:ffac56d434b3 | 27 | |
nxf46245 | 1:ffac56d434b3 | 28 | // For debug use |
nxf46245 | 0:24f846b68c77 | 29 | Serial pc(USBTX, USBRX); |
nxf46245 | 1:ffac56d434b3 | 30 | |
nxf46245 | 2:f04959a6fd61 | 31 | float balltime1 = 0; |
nxf46245 | 2:f04959a6fd61 | 32 | float speed1 = 0; |
nxf46245 | 1:ffac56d434b3 | 33 | |
nxf46245 | 2:f04959a6fd61 | 34 | float balltime2 = 0; |
nxf46245 | 2:f04959a6fd61 | 35 | float speed2 = 0; |
nxf46245 | 2:f04959a6fd61 | 36 | |
nxf46245 | 2:f04959a6fd61 | 37 | uint8_t score1 = 0; |
nxf46245 | 2:f04959a6fd61 | 38 | uint8_t score2 = 0; |
nxf46245 | 1:ffac56d434b3 | 39 | |
nxf46245 | 1:ffac56d434b3 | 40 | void set_score(uint8_t score, PixelArray px, WS2812 ws) { |
nxf46245 | 1:ffac56d434b3 | 41 | for (int i=0; i < score; i++) { |
nxf46245 | 1:ffac56d434b3 | 42 | px.Set(i, COLOR); |
nxf46245 | 1:ffac56d434b3 | 43 | } |
nxf46245 | 1:ffac56d434b3 | 44 | for (int i = score; i <= 10; i++) { |
nxf46245 | 1:ffac56d434b3 | 45 | px.Set(i, 0); |
nxf46245 | 1:ffac56d434b3 | 46 | } |
nxf46245 | 1:ffac56d434b3 | 47 | |
nxf46245 | 1:ffac56d434b3 | 48 | px.SetAllI(0x0f); |
nxf46245 | 1:ffac56d434b3 | 49 | ws.write(px.getBuf()); |
nxf46245 | 0:24f846b68c77 | 50 | } |
nxf46245 | 1:ffac56d434b3 | 51 | |
nxf46245 | 2:f04959a6fd61 | 52 | void print_score(uint8_t score, float balltime, float speed) { |
nxf46245 | 2:f04959a6fd61 | 53 | pc.printf("Score : %d \n\r", score); |
nxf46245 | 2:f04959a6fd61 | 54 | pc.printf("Time of ball pass : %f seconds\n\r", balltime); |
nxf46245 | 2:f04959a6fd61 | 55 | pc.printf("Speed of ball (34 mm diameter) : %f kph\n\r", speed); |
nxf46245 | 2:f04959a6fd61 | 56 | } |
nxf46245 | 2:f04959a6fd61 | 57 | |
nxf46245 | 2:f04959a6fd61 | 58 | void get_info(uint8_t * score, float * balltime, float * speed, GoalCounter gc) { |
nxf46245 | 2:f04959a6fd61 | 59 | *score = gc.get_score(); |
nxf46245 | 2:f04959a6fd61 | 60 | *balltime = gc.get_balltime(); |
nxf46245 | 2:f04959a6fd61 | 61 | *speed = gc.get_ballspeed(); |
nxf46245 | 2:f04959a6fd61 | 62 | gc.is_goal = 0; |
nxf46245 | 2:f04959a6fd61 | 63 | } |
nxf46245 | 0:24f846b68c77 | 64 | |
nxf46245 | 0:24f846b68c77 | 65 | int main() |
nxf46245 | 0:24f846b68c77 | 66 | { |
nxf46245 | 1:ffac56d434b3 | 67 | |
nxf46245 | 1:ffac56d434b3 | 68 | ws1.useII(WS2812::GLOBAL); // use global intensity scaling |
nxf46245 | 1:ffac56d434b3 | 69 | ws2.useII(WS2812::GLOBAL); // use global intensity scaling |
nxf46245 | 0:24f846b68c77 | 70 | |
nxf46245 | 1:ffac56d434b3 | 71 | // Attach Ticker to functions |
nxf46245 | 1:ffac56d434b3 | 72 | |
nxf46245 | 1:ffac56d434b3 | 73 | // bank1.attach(&print_info, 0.5); |
nxf46245 | 1:ffac56d434b3 | 74 | // bank2.attach(&print_info, 0.5); |
nxf46245 | 0:24f846b68c77 | 75 | |
nxf46245 | 0:24f846b68c77 | 76 | while(1) { |
nxf46245 | 2:f04959a6fd61 | 77 | if (gc1.is_goal) { |
nxf46245 | 2:f04959a6fd61 | 78 | get_info(&score1, &balltime1, &speed1, gc1); |
nxf46245 | 2:f04959a6fd61 | 79 | set_score(score1, px1, ws1); |
nxf46245 | 2:f04959a6fd61 | 80 | if (DEBUG) |
nxf46245 | 2:f04959a6fd61 | 81 | print_score(score1, balltime1, speed1); |
nxf46245 | 2:f04959a6fd61 | 82 | } |
nxf46245 | 2:f04959a6fd61 | 83 | else if (gc2.is_goal) { |
nxf46245 | 2:f04959a6fd61 | 84 | get_info(&score2, &balltime2, &speed2, gc2); |
nxf46245 | 2:f04959a6fd61 | 85 | set_score(score2, px2, ws2); |
nxf46245 | 2:f04959a6fd61 | 86 | if (DEBUG) |
nxf46245 | 2:f04959a6fd61 | 87 | print_score(score2, balltime2, speed2); |
nxf46245 | 2:f04959a6fd61 | 88 | } |
nxf46245 | 0:24f846b68c77 | 89 | } |
nxf46245 | 0:24f846b68c77 | 90 | } |