Simple implementation of goal counter for table football using laser diode and phototransistor.
Dependencies: GoalCounter WS2812 PixelArray
main.cpp
00001 #include "mbed.h" 00002 #include "WS2812.h" 00003 #include "PixelArray.h" 00004 #include "GoalCounter.h" 00005 00006 #define WS2812_BUF 10 00007 #define NUM_COLORS 6 00008 #define NUM_LEDS_PER_COLOR 2 00009 #define COLOR 0x2f2020 00010 00011 //Initialize instances of Timers for goal counters 00012 Timer timer1; 00013 Timer timer2; 00014 00015 // Initialize instace of a pixel array for LED strip 1 and 2 00016 PixelArray px1(WS2812_BUF); 00017 PixelArray px2(WS2812_BUF); 00018 00019 // The given numbers are calculated for the K64F MCU 00020 WS2812 ws1(D4, WS2812_BUF, 0, 5, 5, 0); 00021 WS2812 ws2(D5, WS2812_BUF, 0, 5, 5, 0); 00022 00023 // For printing information to serial console 00024 Serial pc(USBTX, USBRX); 00025 00026 GoalCounter gc1(D2, &timer1); 00027 GoalCounter gc2(D3, &timer2); 00028 00029 float balltime1 = 0; 00030 float speed1 = 0; 00031 00032 float balltime2 = 0; 00033 float speed2 = 0; 00034 00035 uint8_t score1 = 0; 00036 uint8_t score2 = 0; 00037 00038 // This function sets 1 - 10 to ws2812 led strip 00039 void set_score(uint8_t score, PixelArray * px, WS2812 * ws) { 00040 px->SetAll(0); // null pixel array 00041 00042 for (int i=0; i < score; i++) { 00043 px->Set(i, COLOR); // set pixels to correspondig score 00044 } 00045 00046 px->SetAllI(0x0f); // set intensity 00047 ws->write(px->getBuf()); // write buffer to the led strip 00048 } 00049 00050 void print_score(uint8_t score, float balltime, float speed) { 00051 pc.printf("Score : %d \n\r", score); 00052 pc.printf("Time of ball pass : %f seconds\n\r", balltime); 00053 pc.printf("Speed of ball (34 mm diameter) : %f kph\n\r", speed); 00054 } 00055 00056 // get information about score, balltime and speed from the GoalCounter 00057 void get_info(uint8_t * score, float * balltime, float * speed, GoalCounter * gc) { 00058 *score = gc->get_score(); 00059 *balltime = gc->get_balltime(); 00060 *speed = gc->get_ballspeed(); 00061 } 00062 00063 // Play animation for the winner, use pixel array 1, target strip passed as pointer 00064 void winner_animation(WS2812 * ws) { 00065 // This function is modified and taken from the Brian Daniels WS2812 example 00066 // set up the colours we want to draw with 00067 int colorbuf[NUM_COLORS] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f}; 00068 00069 // for each of the colours (j) write out 10 of them 00070 // the pixels are written at the colour*10, plus the colour position 00071 // all modulus 60 so it wraps around 00072 for (int i = 0; i < WS2812_BUF; i++) { 00073 px1.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]); 00074 } 00075 00076 // now all the colours are computed, add a fade effect using intensity scaling 00077 // compute and write the II value for each pixel 00078 for (int j=0; j<WS2812_BUF; j++) { 00079 // px.SetI(pixel position, II value) 00080 px1.SetI(j%WS2812_BUF, 0xf+(0xf*(j%NUM_LEDS_PER_COLOR))); 00081 } 00082 00083 00084 // Now the buffer is written, rotate it 00085 // by writing it out with an increasing offset 00086 // 00087 for (int i=0; i < 1000 ; i++) { 00088 for (int z=WS2812_BUF; z >= 0 ; z--) { 00089 ws->write_offsets(px1.getBuf(),z,z,z); 00090 wait(0.075); 00091 } 00092 } 00093 00094 } 00095 00096 int main() 00097 { 00098 // use global intensity scaling 00099 ws1.useII(WS2812::GLOBAL); 00100 ws2.useII(WS2812::GLOBAL); 00101 00102 pc.printf("Initialization of the game... \n\r"); 00103 00104 while(1) { 00105 // polling - checking if goal was detected 00106 if (gc1.is_goal == 1) { 00107 get_info(&score1, &balltime1, &speed1, &gc1); 00108 set_score(score1, &px1, &ws1); 00109 pc.printf("Side 1 score \n\r"); 00110 print_score(score1, balltime1, speed1); 00111 gc1.is_goal = 0; 00112 } 00113 else if (gc2.is_goal == 1) { 00114 get_info(&score2, &balltime2, &speed2, &gc2); 00115 set_score(score2, &px2, &ws2); 00116 pc.printf("Side 2 score \n\r"); 00117 print_score(score2, balltime2, speed2); 00118 gc2.is_goal = 0; 00119 } 00120 // when game is over, print game replay and winning animation 00121 if (score1 >= 10 || score2 >= 10) { 00122 pc.printf("Game over \n\r"); 00123 pc.printf(((score1 > score2) ? "Team 1 is winner" : "Team 2 is winner")); 00124 00125 pc.printf("\n Game score Team 1 : \n\r"); 00126 00127 for (int i=1; i<= score1; i++) { 00128 balltime1 = gc1.get_balltime(i); 00129 speed1 = gc1.get_ballspeed(i); 00130 print_score(i, balltime1, speed1); 00131 } 00132 00133 pc.printf("Game score Team 2 : \n\r"); 00134 00135 for (int i=1; i<= score2; i++) { 00136 balltime2 = gc2.get_balltime(i); 00137 speed2 = gc2.get_ballspeed(i); 00138 print_score(i, balltime2, speed2); 00139 } 00140 00141 // play winner animation on winning team's LED strip 00142 winner_animation(((score1 > score2) ? &ws1 : &ws2)); 00143 00144 return 0; 00145 } 00146 00147 } 00148 }
Generated on Wed Jul 13 2022 14:15:12 by 1.7.2