Big Mouth Billy Bass automation library

Dependents:   BillyBass_with_SD

Committer:
bikeNomad
Date:
Thu Jun 20 15:03:49 2013 +0000
Revision:
8:ad0c038ebfc1
Parent:
7:dba9221acf48
Made main loop repeat forever; eliminated time shifting

Who changed what in which revision?

UserRevisionLine numberNew 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 7:dba9221acf48 25 if (actionTime < other.actionTime) return true;
bikeNomad 7:dba9221acf48 26 if (actionTime > other.actionTime) return false;
bikeNomad 7:dba9221acf48 27 return (code < other.code);
bikeNomad 0:84aaade0de8f 28 }
bikeNomad 6:ea8136eb6976 29
bikeNomad 4:f009306756b3 30 // return <0 if *p1 is before *p2
bikeNomad 4:f009306756b3 31 static int compare(const void* p1, const void* p2) {
bikeNomad 7:dba9221acf48 32 float tdiff = static_cast<Action const *>(p1)->actionTime - static_cast<Action const *>(p2)->actionTime;
bikeNomad 7:dba9221acf48 33 if (tdiff != 0.0) return tdiff;
bikeNomad 7:dba9221acf48 34 return static_cast<Action const *>(p1)->code - static_cast<Action const *>(p2)->code;
bikeNomad 4:f009306756b3 35 }
bikeNomad 0:84aaade0de8f 36
bikeNomad 2:eaba75af0f0d 37 bool isValid() const {
bikeNomad 2:eaba75af0f0d 38 return actionTime >= 0.0 && output != 0;
bikeNomad 2:eaba75af0f0d 39 }
bikeNomad 0:84aaade0de8f 40
bikeNomad 2:eaba75af0f0d 41 void act() {
bikeNomad 2:eaba75af0f0d 42 output->write(desiredState ? 1 : 0);
bikeNomad 2:eaba75af0f0d 43 }
bikeNomad 4:f009306756b3 44
bikeNomad 7:dba9221acf48 45 bool isPast(float _now) {
bikeNomad 7:dba9221acf48 46 return _now >= actionTime;
bikeNomad 7:dba9221acf48 47 }
bikeNomad 7:dba9221acf48 48
bikeNomad 2:eaba75af0f0d 49 bool actIfPast(float _now) {
bikeNomad 7:dba9221acf48 50 if (isPast(_now)) {
bikeNomad 2:eaba75af0f0d 51 act();
bikeNomad 2:eaba75af0f0d 52 return true;
bikeNomad 2:eaba75af0f0d 53 } else return false;
bikeNomad 2:eaba75af0f0d 54 }
bikeNomad 6:ea8136eb6976 55
bikeNomad 6:ea8136eb6976 56 void set(float _time, int _state, DigitalOut* _out, char _code = '.') {
bikeNomad 4:f009306756b3 57 actionTime = _time;
bikeNomad 4:f009306756b3 58 desiredState = _state;
bikeNomad 4:f009306756b3 59 output = _out;
bikeNomad 6:ea8136eb6976 60 code = _code;
bikeNomad 4:f009306756b3 61 }
bikeNomad 0:84aaade0de8f 62
bikeNomad 0:84aaade0de8f 63 float actionTime;
bikeNomad 0:84aaade0de8f 64 DigitalOut *output;
bikeNomad 6:ea8136eb6976 65 uint8_t desiredState;
bikeNomad 6:ea8136eb6976 66 char code;
bikeNomad 0:84aaade0de8f 67 };
bikeNomad 0:84aaade0de8f 68
bikeNomad 0:84aaade0de8f 69 #endif