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