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

Dependencies:   GoalCounter WS2812 PixelArray

main.cpp

Committer:
nxf46245
Date:
2019-01-13
Revision:
5:95cb2348af01
Parent:
4:5a11a84fac69
Child:
6:a93ee22232f2

File content as of revision 5:95cb2348af01:

#include "mbed.h"
#include "WS2812.h"
#include "PixelArray.h"
#include "GoalCounter.h"

#define WS2812_BUF 10
#define NUM_COLORS 1
#define NUM_LEDS_PER_COLOR 2
//#define COLOR 0x2f2020 
#define DEBUG 1

//Initialize instances of Timers
Timer timer1;
Timer timer2;

// Initialize instace of a pixel array for LED strip 1 ar 2
PixelArray px1(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);

// For debug use 
Serial pc(USBTX, USBRX);

GoalCounter gc(D2, &timer1);
GoalCounter gc2(D3, &timer2);

float balltime1 = 0;
float speed1 = 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) {
    px->SetAll(0);
    
    for (int i=0; i < score; i++) {
        px->Set(i, 0x2f2020);
    }
    
    px->SetAllI(0x0f);
    ws->write(px->getBuf()); 
}

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();
}


int main()
{
    
    ws1.useII(WS2812::GLOBAL); // use global intensity scaling
    ws2.useII(WS2812::GLOBAL); // use global intensity scaling
      
    pc.printf("Initialization... \n\r");
    while(1) {
        if (gc.is_goal == 1) {
            get_info(&score1, &balltime1, &speed1, &gc);
            set_score(score1, &px1, &ws1);
            print_score(score1, balltime1, speed1);
            gc.is_goal = 0;
        }
        else if (gc2.is_goal == 1) {
            get_info(&score2, &balltime2, &speed2, &gc2);
            set_score(score2, &px2, &ws2);
            print_score(score2, balltime2, speed2);
            gc2.is_goal = 0;     
        }
        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;
        }  

    }
}