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
GoalCounter.cpp@4:fc48ef79f484, 2019-01-14 (annotated)
- Committer:
- nxf46245
- Date:
- Mon Jan 14 17:48:02 2019 +0000
- Revision:
- 4:fc48ef79f484
- Parent:
- 3:ce69ad70270a
Code cleaning, added timeout between two goals
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nxf46245 | 0:d00bd73d08f8 | 1 | #include "GoalCounter.h" |
nxf46245 | 0:d00bd73d08f8 | 2 | |
nxf46245 | 1:eb4ee5706587 | 3 | GoalCounter::GoalCounter(PinName pin, Timer * t) : _t(t) { |
nxf46245 | 0:d00bd73d08f8 | 4 | |
nxf46245 | 4:fc48ef79f484 | 5 | InterruptIn *_interrupt = new InterruptIn(pin); // Initialize instace of InterruptIn |
nxf46245 | 4:fc48ef79f484 | 6 | // It has to be done dynamically because of non-copyable class |
nxf46245 | 1:eb4ee5706587 | 7 | |
nxf46245 | 4:fc48ef79f484 | 8 | _interrupt->fall(callback(this, &GoalCounter::tstart)); // Set callbacks to timer |
nxf46245 | 1:eb4ee5706587 | 9 | _interrupt->rise(callback(this, &GoalCounter::tstop)); |
nxf46245 | 0:d00bd73d08f8 | 10 | |
nxf46245 | 1:eb4ee5706587 | 11 | _score = 0; |
nxf46245 | 1:eb4ee5706587 | 12 | _time = 0; |
nxf46245 | 4:fc48ef79f484 | 13 | is_goal = 0; |
nxf46245 | 4:fc48ef79f484 | 14 | _begin = 0; |
nxf46245 | 4:fc48ef79f484 | 15 | _end = 0; |
nxf46245 | 4:fc48ef79f484 | 16 | _last_end = 0; |
nxf46245 | 4:fc48ef79f484 | 17 | _time_between = 0; |
nxf46245 | 4:fc48ef79f484 | 18 | |
nxf46245 | 4:fc48ef79f484 | 19 | _t->start(); |
nxf46245 | 4:fc48ef79f484 | 20 | } |
nxf46245 | 4:fc48ef79f484 | 21 | |
nxf46245 | 4:fc48ef79f484 | 22 | GoalCounter::~GoalCounter() |
nxf46245 | 4:fc48ef79f484 | 23 | { |
nxf46245 | 4:fc48ef79f484 | 24 | delete[] _interrupt; // free up dynamically allocated memory |
nxf46245 | 0:d00bd73d08f8 | 25 | } |
nxf46245 | 0:d00bd73d08f8 | 26 | |
nxf46245 | 1:eb4ee5706587 | 27 | void GoalCounter::tstart() { |
nxf46245 | 4:fc48ef79f484 | 28 | _begin = _t->read(); // read value of the timer |
nxf46245 | 0:d00bd73d08f8 | 29 | } |
nxf46245 | 0:d00bd73d08f8 | 30 | |
nxf46245 | 1:eb4ee5706587 | 31 | void GoalCounter::tstop() { |
nxf46245 | 4:fc48ef79f484 | 32 | _end = _t->read(); |
nxf46245 | 4:fc48ef79f484 | 33 | _time = _end - _begin; |
nxf46245 | 4:fc48ef79f484 | 34 | _time_between = _begin - _last_end; |
nxf46245 | 4:fc48ef79f484 | 35 | |
nxf46245 | 4:fc48ef79f484 | 36 | if ( _time > 0 && _time < 2 && _score < 10 && _time_between > 1) { |
nxf46245 | 1:eb4ee5706587 | 37 | _score++; |
nxf46245 | 1:eb4ee5706587 | 38 | _balltimes[_score] = _time; |
nxf46245 | 4:fc48ef79f484 | 39 | _last_end = _end; |
nxf46245 | 1:eb4ee5706587 | 40 | is_goal = 1; |
nxf46245 | 0:d00bd73d08f8 | 41 | } |
nxf46245 | 0:d00bd73d08f8 | 42 | } |
nxf46245 | 0:d00bd73d08f8 | 43 | |
nxf46245 | 0:d00bd73d08f8 | 44 | |
nxf46245 | 0:d00bd73d08f8 | 45 | uint8_t GoalCounter::get_score() { |
nxf46245 | 1:eb4ee5706587 | 46 | return _score; |
nxf46245 | 0:d00bd73d08f8 | 47 | } |
nxf46245 | 0:d00bd73d08f8 | 48 | |
nxf46245 | 0:d00bd73d08f8 | 49 | float GoalCounter::get_balltime(uint8_t score) { |
nxf46245 | 4:fc48ef79f484 | 50 | if (score <= _score && score > 0) |
nxf46245 | 1:eb4ee5706587 | 51 | return _balltimes[score]; |
nxf46245 | 0:d00bd73d08f8 | 52 | else |
nxf46245 | 0:d00bd73d08f8 | 53 | return -1; |
nxf46245 | 0:d00bd73d08f8 | 54 | } |
nxf46245 | 0:d00bd73d08f8 | 55 | |
nxf46245 | 0:d00bd73d08f8 | 56 | float GoalCounter::get_balltime() { |
nxf46245 | 1:eb4ee5706587 | 57 | return _balltimes[_score]; |
nxf46245 | 0:d00bd73d08f8 | 58 | |
nxf46245 | 0:d00bd73d08f8 | 59 | } |
nxf46245 | 0:d00bd73d08f8 | 60 | |
nxf46245 | 0:d00bd73d08f8 | 61 | float GoalCounter::get_ballspeed(uint8_t score) { |
nxf46245 | 4:fc48ef79f484 | 62 | if (score <= _score && score > 0) { |
nxf46245 | 1:eb4ee5706587 | 63 | float speed = 0.034f/_balltimes[score]*3.6f; |
nxf46245 | 0:d00bd73d08f8 | 64 | return speed; |
nxf46245 | 0:d00bd73d08f8 | 65 | } |
nxf46245 | 0:d00bd73d08f8 | 66 | else |
nxf46245 | 0:d00bd73d08f8 | 67 | return -1; |
nxf46245 | 0:d00bd73d08f8 | 68 | } |
nxf46245 | 0:d00bd73d08f8 | 69 | |
nxf46245 | 0:d00bd73d08f8 | 70 | float GoalCounter::get_ballspeed() { |
nxf46245 | 1:eb4ee5706587 | 71 | float speed = 0.034f/_balltimes[_score]*3.6f; |
nxf46245 | 0:d00bd73d08f8 | 72 | return speed; |
nxf46245 | 0:d00bd73d08f8 | 73 | } |