Simple implementation of goal counter for table football using laser diode and phototransistor.

Dependencies:   GoalCounter WS2812 PixelArray

Files at this revision

API Documentation at this revision

Comitter:
nxf46245
Date:
Fri Jan 11 18:29:58 2019 +0000
Parent:
1:ffac56d434b3
Child:
3:d74c47d2e933
Commit message:
Code cleaning, NonCopyable warning needs to be fixed

Changed in this revision

GoalCounter.cpp Show annotated file Show diff for this revision Revisions of this file
GoalCounter.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/GoalCounter.cpp	Thu Jan 10 15:48:39 2019 +0000
+++ b/GoalCounter.cpp	Fri Jan 11 18:29:58 2019 +0000
@@ -1,10 +1,11 @@
 #include "GoalCounter.h"
 
 GoalCounter::GoalCounter(PinName pin) : _interrupt(pin) {
-    _interrupt.fall(this, &GoalCounter::tstart);
-    _interrupt.rise(this, &GoalCounter::tstop);
+    // Set callback functions to timers
+    _interrupt.fall(callback(this, &GoalCounter::tstart));
+    _interrupt.rise(callback(this, &GoalCounter::tstop));
 //    _balltimes = {0};
-    _score= 0;   
+    _score= 0;
 }
          
 void GoalCounter::tstart() {
@@ -18,11 +19,15 @@
     
     if ( _time > 0 && _time < 2 && _score < 10) {
        _balltimes[++_score] = _time;
-       goal = 1;
+       is_goal = 1;
 //       wait(1);
         }     
 }
 
+void GoalCounter::goal() {
+    is_goal = 1;
+}
+
 uint8_t GoalCounter::get_score() {
     return _score;
 }
@@ -53,6 +58,12 @@
     return speed;
 }
 
+//void GoalCounter:print_info() {
+//    pc.printf("Score : %d \n\r", _score);
+//    pc.printf("Time of ball pass : %f seconds\n\r", this.get_balltime());
+//    pc.printf("Speed of ball (34 mm diameter) : %f kph\n\r", this.get_ballspeed());
+//}
+
 
     
 
--- a/GoalCounter.h	Thu Jan 10 15:48:39 2019 +0000
+++ b/GoalCounter.h	Fri Jan 11 18:29:58 2019 +0000
@@ -10,7 +10,8 @@
     float get_balltime();
     float get_ballspeed(uint8_t score);
     float get_ballspeed();
-    char goal;
+    void goal();
+    char is_goal;
 
 
 private:
--- a/main.cpp	Thu Jan 10 15:48:39 2019 +0000
+++ b/main.cpp	Fri Jan 11 18:29:58 2019 +0000
@@ -7,6 +7,7 @@
 #define NUM_COLORS 1
 #define NUM_LEDS_PER_COLOR 2
 #define COLOR 0x2f2020 
+#define DEBUG 1
 
 // Initialize instace of a pixel array for LED strip 1 ar 2
 PixelArray px1(WS2812_BUF);
@@ -14,24 +15,27 @@
 
 // The given numbers are calculated for the K64F MCU
 WS2812 ws1(D4, WS2812_BUF, 0, 5, 5, 0);
-WS2812 ws2(D4, WS2812_BUF, 0, 5, 5, 0);
+WS2812 ws2(D5, WS2812_BUF, 0, 5, 5, 0);
 
 // Initialize instances of GoalCounter classes for counting goals
 GoalCounter gc1(D2);
 GoalCounter gc2(D3);
 
 // Initialize instances of Ticker classes for handling goal counters
-Ticker bank1;
-Ticker bank2; 
+//Ticker bank1;
+//Ticker bank2; 
 
 // For debug use 
 Serial pc(USBTX, USBRX);
 
-float balltime = 0;
-float speed = 0;
+float balltime1 = 0;
+float speed1 = 0;
 
-uint8_t score_1 = 0;
-uint8_t score_2 = 0;
+float balltime2 = 0;
+float speed2 = 0;
+
+uint8_t score1 = 0;
+uint8_t score2 = 0;
 
 void set_score(uint8_t score, PixelArray px, WS2812 ws) {
     for (int i=0; i < score; i++) {
@@ -45,17 +49,18 @@
     ws.write(px.getBuf()); 
 }
 
-void print_info() {
-     if (gc1.goal) {
-            score_1 = gc1.get_score();
-            balltime = gc1.get_balltime();
-            speed = gc1.get_ballspeed();
-            pc.printf("Score : %d \n\r", score_1);
-            pc.printf("Time of ball pass : %f seconds\n\r", balltime);
-            pc.printf("Speed of ball (34 mm diameter) : %f kph\n\r", speed);
-            gc1.goal = 0;
-            }
-    }
+void print_score(uint8_t score, float balltime, float speed) {
+    pc.printf("Score : %d \n\r", score);
+    pc.printf("Time of ball pass : %f seconds\n\r", balltime);
+    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_goal = 0;
+}
 
 int main()
 {
@@ -69,7 +74,17 @@
 //    bank2.attach(&print_info, 0.5);
     
     while(1) {
-        print_info();
-        wait(0.5);
+        if (gc1.is_goal) {
+            get_info(&score1, &balltime1, &speed1, gc1);
+            set_score(score1, px1, ws1);
+            if (DEBUG)
+                print_score(score1, balltime1, speed1);
+        }
+        else if (gc2.is_goal) {
+            get_info(&score2, &balltime2, &speed2, gc2);
+            set_score(score2, px2, ws2);
+            if (DEBUG)
+                print_score(score2, balltime2, speed2);
+        }
     }
 }