Simple implementation of goal counter for table football using laser diode and phototransistor.
Dependencies: GoalCounter WS2812 PixelArray
Revision 4:5a11a84fac69, committed 2019-01-13
- Comitter:
- nxf46245
- Date:
- Sun Jan 13 14:23:30 2019 +0000
- Parent:
- 3:d74c47d2e933
- Child:
- 5:95cb2348af01
- Commit message:
- Fix non copyable issue;
Changed in this revision
--- a/GoalCounter.cpp Sat Jan 12 18:50:49 2019 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,91 +0,0 @@
-#include "GoalCounter.h"
-
-GoalCounter::GoalCounter(PinName pin1, PinName pin2) : _interrupt1(pin1) {
- _interrupt1.fall(callback(this, &GoalCounter::tstart1));
- _interrupt1.rise(callback(this, &GoalCounter::tstop1));
-
-// _interrupt2.fall(callback(this, &GoalCounter::tstart2));
-// _interrupt2.rise(callback(this, &GoalCounter::tstop2));
-
- Timer t1;
- Timer t2;
-
- _score1 = 0;
- _score2 = 0;
-
- _time1 = 0;
- _time2 = 0;
-
-// _balltimes1 = {0};
-// _balltimes2 = {0};
-
- is_goal1 = 0;
- is_goal2 = 0;
-
-}
-
-void GoalCounter::tstart1() {
- t1.start();
-}
-
-void GoalCounter::tstop1() {
- t1.stop();
- _time1 = t1.read();
- t1.reset();
-
- if ( _time1 > 0 && _time1 < 2 && _score1 < 10) {
- _balltimes1[++_score1] = _time1;
- is_goal1 = 1;
- }
-}
-
-void GoalCounter::tstart2() {
- t2.start();
-}
-
-void GoalCounter::tstop2() {
- t2.stop();
- _time2 = t2.read();
- t2.reset();
-
- if ( _time2 > 0 && _time2 < 2 && _score2 < 10) {
- _balltimes2[++_score2] = _time2;
- is_goal2 = 1;
- }
-}
-
-uint8_t GoalCounter::get_score() {
- return _score1;
-}
-
-float GoalCounter::get_balltime(uint8_t score) {
- if (score <= 10 && score > 0)
- return _balltimes1[score];
- else
- return -1;
-}
-
-float GoalCounter::get_balltime() {
- return _balltimes1[_score1];
-
-}
-
-float GoalCounter::get_ballspeed(uint8_t score) {
- if (score <= 10 && score > 0) {
- float speed = 0.034f/_balltimes1[score]*3.6f;
- return speed;
- }
- else
- return -1;
-}
-
-float GoalCounter::get_ballspeed() {
- float speed = 0.034f/_balltimes1[_score1]*3.6f;
- return speed;
-}
-
-
-
-
-
-
--- a/GoalCounter.h Sat Jan 12 18:50:49 2019 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,32 +0,0 @@
-#include "mbed.h"
-
-class GoalCounter {
-public:
- GoalCounter(PinName pin1, PinName pin2);
- void tstart1();
- void tstop1();
- void tstart2();
- void tstop2();
- uint8_t get_score();
- float get_balltime(uint8_t score);
- float get_balltime();
- float get_ballspeed(uint8_t score);
- float get_ballspeed();
- uint8_t is_goal1;
- uint8_t is_goal2;
-
-
-private:
- InterruptIn _interrupt1;
-// InterruptIn _interrupt2;
- volatile uint8_t _score1;
- volatile uint8_t _score2;
- Timer t1;
- Timer t2;
- float _balltimes1[11];
- float _time1;
- float _balltimes2[11];
- float _time2;
-
-};
-
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GoalCounter.lib Sun Jan 13 14:23:30 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/nxf46245/code/GoalCounter/#eb4ee5706587
Binary file goal_counter_project.K64F.bin has changed
--- a/main.cpp Sat Jan 12 18:50:49 2019 +0000
+++ b/main.cpp Sun Jan 13 14:23:30 2019 +0000
@@ -6,39 +6,30 @@
#define WS2812_BUF 10
#define NUM_COLORS 1
#define NUM_LEDS_PER_COLOR 2
-#define COLOR 0x2f2020
+//#define COLOR 0x2f2020
#define DEBUG 1
-#define MBED_CONF_PLATFORM_FORCE_NON_COPYABLE_ERROR 0
-
//Initialize instances of Timers
-//Timer t1;
+Timer timer1;
//Timer t2;
// Initialize instace of a pixel array for LED strip 1 ar 2
PixelArray px1(WS2812_BUF);
-PixelArray px2(WS2812_BUF);
+//PixelArray px2(WS2812_BUF);
// The given numbers are calculated for the K64F MCU
WS2812 ws1(D4, WS2812_BUF, 0, 5, 5, 0);
-WS2812 ws2(D5, WS2812_BUF, 0, 5, 5, 0);
+//WS2812 ws2(D5, WS2812_BUF, 0, 5, 5, 0);
-// Initialize interrupt classes
-//InterruptIn i1(D2);
-//InterruptIn i2(D3);
-// Initialize instances of GoalCounter classes for counting goals
-//GoalCounter gc1(D2, i1);
-//GoalCounter gc2(D3, i2);
+// For debug use
+Serial pc(USBTX, USBRX);
-GoalCounter gc(D2, D3);
+GoalCounter gc(D2, &timer1);
// Initialize instances of Ticker classes for handling goal counters
//Ticker bank1;
//Ticker bank2;
-// For debug use
-Serial pc(USBTX, USBRX);
-
float balltime1 = 0;
float speed1 = 0;
@@ -48,16 +39,15 @@
uint8_t score1 = 0;
uint8_t score2 = 0;
-void set_score(uint8_t score, PixelArray px, WS2812 ws) {
+void set_score(uint8_t score, PixelArray * px, WS2812 * ws) {
+ px->SetAll(0);
+
for (int i=0; i < score; i++) {
- px.Set(i, COLOR);
- }
- for (int i = score; i <= 10; i++) {
- px.Set(i, 0);
+ px->Set(i, 0x2f2020);
}
- px.SetAllI(0x0f);
- ws.write(px.getBuf());
+ px->SetAllI(0x0f);
+ ws->write(px->getBuf());
}
void print_score(uint8_t score, float balltime, float speed) {
@@ -66,36 +56,43 @@
pc.printf("Speed of ball (34 mm diameter) : %f kph\n\r", speed);
}
-void get_info(uint8_t * score, float * balltime, float * speed, GoalCounter gc) {
- *score = gc.get_score();
- *balltime = gc.get_balltime();
- *speed = gc.get_ballspeed();
- gc.is_goal1 = 0;
+void get_info(uint8_t * score, float * balltime, float * speed, GoalCounter * gc) {
+ *score = gc->get_score();
+ *balltime = gc->get_balltime();
+ *speed = gc->get_ballspeed();
}
+
int main()
{
ws1.useII(WS2812::GLOBAL); // use global intensity scaling
- ws2.useII(WS2812::GLOBAL); // use global intensity scaling
-
- // Attach Ticker to functions
-
-// bank1.attach(&print_info, 0.5);
-// bank2.attach(&print_info, 0.5);
-
+// ws2.useII(WS2812::GLOBAL); // use global intensity scaling
+
+ pc.printf("Initialization... \n\r");
while(1) {
- if (gc.is_goal1) {
- get_info(&score1, &balltime1, &speed1, gc);
- set_score(score1, px1, ws1);
- if (DEBUG)
- print_score(score1, balltime1, speed1);
+ if (gc.is_goal == 1) {
+ get_info(&score1, &balltime1, &speed1, &gc);
+ set_score(score1, &px1, &ws1);
+// score1 = gc.get_score();
+// balltime1 = gc.get_balltime();
+// speed1 = gc.get_ballspeed();
+// pc.printf("Score : %d \n\r", score1);
+// pc.printf("Time of ball pass : %f seconds\n\r", balltime1);
+// pc.printf("Speed of ball (34 mm diameter) : %f kph\n\r", speed1);
+ print_score(score1, balltime1, speed1);
+ gc.is_goal = 0;
}
-// else if (gc2.is_goal) {
-// get_info(&score2, &balltime2, &speed2, gc2);
-// set_score(score2, px2, ws2);
-// if (DEBUG)
-// print_score(score2, balltime2, speed2);
-// }
+ if (score1 >= 10) {
+ pc.printf("Game over \n\r");
+ for (int i=1; i<= 10; i++) {
+ pc.printf("Game score: \n\r");
+ balltime1 = gc.get_balltime(i);
+ speed1 = gc.get_ballspeed(i);
+ print_score(i, balltime1, speed1);
+ }
+ return 0;
+ }
+
}
}