Big Mouth Billy Bass automation library
action.hpp@6:ea8136eb6976, 2013-06-20 (annotated)
- Committer:
- bikeNomad
- Date:
- Thu Jun 20 03:04:36 2013 +0000
- Revision:
- 6:ea8136eb6976
- Parent:
- 4:f009306756b3
- Child:
- 7:dba9221acf48
Added diagnostic output and manual (keyboard) testing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bikeNomad | 0:84aaade0de8f | 1 | #ifndef __included_action_hpp |
bikeNomad | 0:84aaade0de8f | 2 | #define __included_action_hpp |
bikeNomad | 0:84aaade0de8f | 3 | |
bikeNomad | 0:84aaade0de8f | 4 | #include "billybass.hpp" |
bikeNomad | 0:84aaade0de8f | 5 | |
bikeNomad | 0:84aaade0de8f | 6 | // 0.000000\t5.646050\thead |
bikeNomad | 0:84aaade0de8f | 7 | // 0.064498\t1.069177\ttail |
bikeNomad | 0:84aaade0de8f | 8 | // 0.357219\t0.580481\tmouth |
bikeNomad | 0:84aaade0de8f | 9 | |
bikeNomad | 0:84aaade0de8f | 10 | #define MAX_ACTION_LINE_LENGTH 30 |
bikeNomad | 0:84aaade0de8f | 11 | |
bikeNomad | 2:eaba75af0f0d | 12 | struct Action { |
bikeNomad | 0:84aaade0de8f | 13 | Action(float _time = -1.0, |
bikeNomad | 2:eaba75af0f0d | 14 | bool _state = false, |
bikeNomad | 2:eaba75af0f0d | 15 | DigitalOut *_out = 0, |
bikeNomad | 2:eaba75af0f0d | 16 | char const *_outName = 0) |
bikeNomad | 6:ea8136eb6976 | 17 | : actionTime(_time) |
bikeNomad | 6:ea8136eb6976 | 18 | , output(_out) |
bikeNomad | 6:ea8136eb6976 | 19 | , desiredState(_state) |
bikeNomad | 6:ea8136eb6976 | 20 | , code('.') { |
bikeNomad | 6:ea8136eb6976 | 21 | if (_outName) code = _outName[0]; |
bikeNomad | 2:eaba75af0f0d | 22 | } |
bikeNomad | 2:eaba75af0f0d | 23 | |
bikeNomad | 2:eaba75af0f0d | 24 | bool operator < (Action const &other) const { |
bikeNomad | 2:eaba75af0f0d | 25 | return actionTime < other.actionTime; |
bikeNomad | 0:84aaade0de8f | 26 | } |
bikeNomad | 6:ea8136eb6976 | 27 | |
bikeNomad | 4:f009306756b3 | 28 | // return <0 if *p1 is before *p2 |
bikeNomad | 4:f009306756b3 | 29 | static int compare(const void* p1, const void* p2) { |
bikeNomad | 4:f009306756b3 | 30 | return static_cast<Action const *>(p1)->actionTime - static_cast<Action const *>(p2)->actionTime; |
bikeNomad | 4:f009306756b3 | 31 | } |
bikeNomad | 0:84aaade0de8f | 32 | |
bikeNomad | 2:eaba75af0f0d | 33 | bool isValid() const { |
bikeNomad | 2:eaba75af0f0d | 34 | return actionTime >= 0.0 && output != 0; |
bikeNomad | 2:eaba75af0f0d | 35 | } |
bikeNomad | 0:84aaade0de8f | 36 | |
bikeNomad | 2:eaba75af0f0d | 37 | void act() { |
bikeNomad | 2:eaba75af0f0d | 38 | output->write(desiredState ? 1 : 0); |
bikeNomad | 2:eaba75af0f0d | 39 | } |
bikeNomad | 4:f009306756b3 | 40 | |
bikeNomad | 2:eaba75af0f0d | 41 | bool actIfPast(float _now) { |
bikeNomad | 2:eaba75af0f0d | 42 | if (_now >= actionTime) { |
bikeNomad | 2:eaba75af0f0d | 43 | act(); |
bikeNomad | 2:eaba75af0f0d | 44 | return true; |
bikeNomad | 2:eaba75af0f0d | 45 | } else return false; |
bikeNomad | 2:eaba75af0f0d | 46 | } |
bikeNomad | 6:ea8136eb6976 | 47 | |
bikeNomad | 6:ea8136eb6976 | 48 | void set(float _time, int _state, DigitalOut* _out, char _code = '.') { |
bikeNomad | 4:f009306756b3 | 49 | actionTime = _time; |
bikeNomad | 4:f009306756b3 | 50 | desiredState = _state; |
bikeNomad | 4:f009306756b3 | 51 | output = _out; |
bikeNomad | 6:ea8136eb6976 | 52 | code = _code; |
bikeNomad | 4:f009306756b3 | 53 | } |
bikeNomad | 0:84aaade0de8f | 54 | |
bikeNomad | 0:84aaade0de8f | 55 | float actionTime; |
bikeNomad | 0:84aaade0de8f | 56 | DigitalOut *output; |
bikeNomad | 6:ea8136eb6976 | 57 | uint8_t desiredState; |
bikeNomad | 6:ea8136eb6976 | 58 | char code; |
bikeNomad | 0:84aaade0de8f | 59 | }; |
bikeNomad | 0:84aaade0de8f | 60 | |
bikeNomad | 0:84aaade0de8f | 61 | #endif |