This library provides simple interface for the table football goal counter based on a IR LED or laser diode and phototransistor.
Dependents: goal_counter_project
Diff: GoalCounter.cpp
- Revision:
- 4:fc48ef79f484
- Parent:
- 3:ce69ad70270a
--- a/GoalCounter.cpp Sun Jan 13 16:43:27 2019 +0000 +++ b/GoalCounter.cpp Mon Jan 14 17:48:02 2019 +0000 @@ -2,28 +2,41 @@ GoalCounter::GoalCounter(PinName pin, Timer * t) : _t(t) { - InterruptIn *_interrupt = new InterruptIn(pin); + InterruptIn *_interrupt = new InterruptIn(pin); // Initialize instace of InterruptIn + // It has to be done dynamically because of non-copyable class - _interrupt->fall(callback(this, &GoalCounter::tstart)); + _interrupt->fall(callback(this, &GoalCounter::tstart)); // Set callbacks to timer _interrupt->rise(callback(this, &GoalCounter::tstop)); _score = 0; _time = 0; - is_goal = 0; + is_goal = 0; + _begin = 0; + _end = 0; + _last_end = 0; + _time_between = 0; + + _t->start(); +} + +GoalCounter::~GoalCounter() +{ + delete[] _interrupt; // free up dynamically allocated memory } void GoalCounter::tstart() { - _t->start(); + _begin = _t->read(); // read value of the timer } void GoalCounter::tstop() { - _t->stop(); - _time = _t->read(); - _t->reset(); - - if ( _time > 0 && _time < 2 && _score < 10) { + _end = _t->read(); + _time = _end - _begin; + _time_between = _begin - _last_end; + + if ( _time > 0 && _time < 2 && _score < 10 && _time_between > 1) { _score++; _balltimes[_score] = _time; + _last_end = _end; is_goal = 1; } } @@ -34,7 +47,7 @@ } float GoalCounter::get_balltime(uint8_t score) { - if (score <= 10 && score > 0) + if (score <= _score && score > 0) return _balltimes[score]; else return -1; @@ -46,7 +59,7 @@ } float GoalCounter::get_ballspeed(uint8_t score) { - if (score <= 10 && score > 0) { + if (score <= _score && score > 0) { float speed = 0.034f/_balltimes[score]*3.6f; return speed; }