Big Mouth Billy Bass automation library
action.hpp@4:f009306756b3, 2013-06-18 (annotated)
- Committer:
- bikeNomad
- Date:
- Tue Jun 18 14:10:34 2013 +0000
- Revision:
- 4:f009306756b3
- Parent:
- 2:eaba75af0f0d
- Child:
- 6:ea8136eb6976
working. re-used one Song for memory reduction.
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 | 0:84aaade0de8f | 17 | : actionTime(_time), desiredState(_state) |
bikeNomad | 4:f009306756b3 | 18 | , output(_out) { |
bikeNomad | 2:eaba75af0f0d | 19 | } |
bikeNomad | 2:eaba75af0f0d | 20 | |
bikeNomad | 2:eaba75af0f0d | 21 | bool operator < (Action const &other) const { |
bikeNomad | 2:eaba75af0f0d | 22 | return actionTime < other.actionTime; |
bikeNomad | 0:84aaade0de8f | 23 | } |
bikeNomad | 4:f009306756b3 | 24 | |
bikeNomad | 4:f009306756b3 | 25 | // return <0 if *p1 is before *p2 |
bikeNomad | 4:f009306756b3 | 26 | static int compare(const void* p1, const void* p2) { |
bikeNomad | 4:f009306756b3 | 27 | return static_cast<Action const *>(p1)->actionTime - static_cast<Action const *>(p2)->actionTime; |
bikeNomad | 4:f009306756b3 | 28 | } |
bikeNomad | 0:84aaade0de8f | 29 | |
bikeNomad | 2:eaba75af0f0d | 30 | bool isValid() const { |
bikeNomad | 2:eaba75af0f0d | 31 | return actionTime >= 0.0 && output != 0; |
bikeNomad | 2:eaba75af0f0d | 32 | } |
bikeNomad | 0:84aaade0de8f | 33 | |
bikeNomad | 2:eaba75af0f0d | 34 | void act() { |
bikeNomad | 2:eaba75af0f0d | 35 | output->write(desiredState ? 1 : 0); |
bikeNomad | 2:eaba75af0f0d | 36 | } |
bikeNomad | 4:f009306756b3 | 37 | |
bikeNomad | 2:eaba75af0f0d | 38 | bool actIfPast(float _now) { |
bikeNomad | 2:eaba75af0f0d | 39 | if (_now >= actionTime) { |
bikeNomad | 2:eaba75af0f0d | 40 | act(); |
bikeNomad | 2:eaba75af0f0d | 41 | return true; |
bikeNomad | 2:eaba75af0f0d | 42 | } else return false; |
bikeNomad | 2:eaba75af0f0d | 43 | } |
bikeNomad | 4:f009306756b3 | 44 | void set(float _time, int _state, DigitalOut* _out) { |
bikeNomad | 4:f009306756b3 | 45 | actionTime = _time; |
bikeNomad | 4:f009306756b3 | 46 | desiredState = _state; |
bikeNomad | 4:f009306756b3 | 47 | output = _out; |
bikeNomad | 4:f009306756b3 | 48 | } |
bikeNomad | 0:84aaade0de8f | 49 | |
bikeNomad | 0:84aaade0de8f | 50 | float actionTime; |
bikeNomad | 4:f009306756b3 | 51 | int desiredState; |
bikeNomad | 0:84aaade0de8f | 52 | DigitalOut *output; |
bikeNomad | 0:84aaade0de8f | 53 | }; |
bikeNomad | 0:84aaade0de8f | 54 | |
bikeNomad | 0:84aaade0de8f | 55 | #endif |