![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Microcontroller firmware that uses a simple, yet powerful scripting language to control the timing of input and output events with high temporal resolution. Written by Mattias Karlsson
hardwareInterface.h@0:8dbd6bd9167f, 2015-05-19 (annotated)
- Committer:
- mkarlsso
- Date:
- Tue May 19 15:45:42 2015 +0000
- Revision:
- 0:8dbd6bd9167f
- Child:
- 1:3a050d26d4f6
- Child:
- 2:35266b266eaa
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mkarlsso | 0:8dbd6bd9167f | 1 | #ifndef HARDWARWEINTERFACE_H |
mkarlsso | 0:8dbd6bd9167f | 2 | #define HARDWARWEINTERFACE_H |
mkarlsso | 0:8dbd6bd9167f | 3 | |
mkarlsso | 0:8dbd6bd9167f | 4 | #include <stdint.h> |
mkarlsso | 0:8dbd6bd9167f | 5 | #include <string> |
mkarlsso | 0:8dbd6bd9167f | 6 | |
mkarlsso | 0:8dbd6bd9167f | 7 | #define PENDINGTRIGGERBUFFERSIZE 10 |
mkarlsso | 0:8dbd6bd9167f | 8 | |
mkarlsso | 0:8dbd6bd9167f | 9 | //#define NUMPORTS 8 //the number of ports available on the hardware |
mkarlsso | 0:8dbd6bd9167f | 10 | |
mkarlsso | 0:8dbd6bd9167f | 11 | using namespace std; |
mkarlsso | 0:8dbd6bd9167f | 12 | |
mkarlsso | 0:8dbd6bd9167f | 13 | //for debugging output |
mkarlsso | 0:8dbd6bd9167f | 14 | //#define DEBUGOUTPUT |
mkarlsso | 0:8dbd6bd9167f | 15 | |
mkarlsso | 0:8dbd6bd9167f | 16 | //used to organize digital change events |
mkarlsso | 0:8dbd6bd9167f | 17 | struct changeEvent { |
mkarlsso | 0:8dbd6bd9167f | 18 | uint32_t timeStamp; |
mkarlsso | 0:8dbd6bd9167f | 19 | bool triggered; |
mkarlsso | 0:8dbd6bd9167f | 20 | }; |
mkarlsso | 0:8dbd6bd9167f | 21 | |
mkarlsso | 0:8dbd6bd9167f | 22 | class sDigitalOut |
mkarlsso | 0:8dbd6bd9167f | 23 | { |
mkarlsso | 0:8dbd6bd9167f | 24 | public: |
mkarlsso | 0:8dbd6bd9167f | 25 | sDigitalOut(); |
mkarlsso | 0:8dbd6bd9167f | 26 | |
mkarlsso | 0:8dbd6bd9167f | 27 | virtual void init(int pin) = 0; |
mkarlsso | 0:8dbd6bd9167f | 28 | virtual void write(int value) = 0; |
mkarlsso | 0:8dbd6bd9167f | 29 | virtual int read() = 0; |
mkarlsso | 0:8dbd6bd9167f | 30 | protected: |
mkarlsso | 0:8dbd6bd9167f | 31 | |
mkarlsso | 0:8dbd6bd9167f | 32 | }; |
mkarlsso | 0:8dbd6bd9167f | 33 | |
mkarlsso | 0:8dbd6bd9167f | 34 | class sDigitalIn |
mkarlsso | 0:8dbd6bd9167f | 35 | { |
mkarlsso | 0:8dbd6bd9167f | 36 | public: |
mkarlsso | 0:8dbd6bd9167f | 37 | sDigitalIn(); |
mkarlsso | 0:8dbd6bd9167f | 38 | |
mkarlsso | 0:8dbd6bd9167f | 39 | virtual void init(int pin) = 0; |
mkarlsso | 0:8dbd6bd9167f | 40 | virtual int read() = 0; |
mkarlsso | 0:8dbd6bd9167f | 41 | virtual void interrupt_up_callback() = 0; |
mkarlsso | 0:8dbd6bd9167f | 42 | virtual void interrupt_down_callback() = 0; |
mkarlsso | 0:8dbd6bd9167f | 43 | |
mkarlsso | 0:8dbd6bd9167f | 44 | changeEvent lastUpEvent; |
mkarlsso | 0:8dbd6bd9167f | 45 | changeEvent lastDownEvent; |
mkarlsso | 0:8dbd6bd9167f | 46 | |
mkarlsso | 0:8dbd6bd9167f | 47 | protected: |
mkarlsso | 0:8dbd6bd9167f | 48 | |
mkarlsso | 0:8dbd6bd9167f | 49 | |
mkarlsso | 0:8dbd6bd9167f | 50 | void addStateChange(int newState, uint32_t timeStamp); |
mkarlsso | 0:8dbd6bd9167f | 51 | |
mkarlsso | 0:8dbd6bd9167f | 52 | }; |
mkarlsso | 0:8dbd6bd9167f | 53 | |
mkarlsso | 0:8dbd6bd9167f | 54 | class sSerialPort |
mkarlsso | 0:8dbd6bd9167f | 55 | { |
mkarlsso | 0:8dbd6bd9167f | 56 | public: |
mkarlsso | 0:8dbd6bd9167f | 57 | sSerialPort(); |
mkarlsso | 0:8dbd6bd9167f | 58 | |
mkarlsso | 0:8dbd6bd9167f | 59 | virtual void init() = 0; |
mkarlsso | 0:8dbd6bd9167f | 60 | virtual bool readable() = 0; |
mkarlsso | 0:8dbd6bd9167f | 61 | virtual char readChar() = 0; |
mkarlsso | 0:8dbd6bd9167f | 62 | virtual void writeChar(char s)= 0; |
mkarlsso | 0:8dbd6bd9167f | 63 | protected: |
mkarlsso | 0:8dbd6bd9167f | 64 | |
mkarlsso | 0:8dbd6bd9167f | 65 | |
mkarlsso | 0:8dbd6bd9167f | 66 | }; |
mkarlsso | 0:8dbd6bd9167f | 67 | |
mkarlsso | 0:8dbd6bd9167f | 68 | class sSound |
mkarlsso | 0:8dbd6bd9167f | 69 | { |
mkarlsso | 0:8dbd6bd9167f | 70 | public: |
mkarlsso | 0:8dbd6bd9167f | 71 | sSound(); |
mkarlsso | 0:8dbd6bd9167f | 72 | void setFile(string fileNameIn); |
mkarlsso | 0:8dbd6bd9167f | 73 | void setVolume(int* volumeIn); |
mkarlsso | 0:8dbd6bd9167f | 74 | void setVolume(int volumeIn); |
mkarlsso | 0:8dbd6bd9167f | 75 | void setPlayback(bool playIn); |
mkarlsso | 0:8dbd6bd9167f | 76 | void setReset(); |
mkarlsso | 0:8dbd6bd9167f | 77 | virtual void execute() = 0; |
mkarlsso | 0:8dbd6bd9167f | 78 | |
mkarlsso | 0:8dbd6bd9167f | 79 | protected: |
mkarlsso | 0:8dbd6bd9167f | 80 | char fileName[21]; |
mkarlsso | 0:8dbd6bd9167f | 81 | bool fileNameExists; |
mkarlsso | 0:8dbd6bd9167f | 82 | int* volumePtr; |
mkarlsso | 0:8dbd6bd9167f | 83 | int volume; |
mkarlsso | 0:8dbd6bd9167f | 84 | bool play; |
mkarlsso | 0:8dbd6bd9167f | 85 | bool reset; |
mkarlsso | 0:8dbd6bd9167f | 86 | }; |
mkarlsso | 0:8dbd6bd9167f | 87 | |
mkarlsso | 0:8dbd6bd9167f | 88 | class sSystem |
mkarlsso | 0:8dbd6bd9167f | 89 | { |
mkarlsso | 0:8dbd6bd9167f | 90 | public: |
mkarlsso | 0:8dbd6bd9167f | 91 | sSystem(); |
mkarlsso | 0:8dbd6bd9167f | 92 | virtual void timerinit() = 0; |
mkarlsso | 0:8dbd6bd9167f | 93 | virtual void setStandAloneClock() = 0; |
mkarlsso | 0:8dbd6bd9167f | 94 | virtual void setSlaveClock() = 0; |
mkarlsso | 0:8dbd6bd9167f | 95 | virtual sDigitalOut* getDigitalOutPtr(int portNum) = 0; |
mkarlsso | 0:8dbd6bd9167f | 96 | virtual sDigitalIn* getDigitalInPtr(int portNum) = 0; |
mkarlsso | 0:8dbd6bd9167f | 97 | virtual sSound* createNewSoundAction() = 0; |
mkarlsso | 0:8dbd6bd9167f | 98 | virtual void externalClockReset() = 0; //needs to reset harware timer before calling immediateClockReset(); |
mkarlsso | 0:8dbd6bd9167f | 99 | virtual void incrementClock() = 0; |
mkarlsso | 0:8dbd6bd9167f | 100 | virtual void mainLoopToDo(); |
mkarlsso | 0:8dbd6bd9167f | 101 | virtual void pauseInterrupts(); |
mkarlsso | 0:8dbd6bd9167f | 102 | virtual void resumeInterrupts(); |
mkarlsso | 0:8dbd6bd9167f | 103 | virtual int getPendingFunctionTriggers(uint16_t *bufferPtr); //Returns the number of pending shortcut triggers |
mkarlsso | 0:8dbd6bd9167f | 104 | virtual uint32_t getDigitalOutputChangeFlags(); |
mkarlsso | 0:8dbd6bd9167f | 105 | virtual uint32_t getDigitalInputChangeFlags(); |
mkarlsso | 0:8dbd6bd9167f | 106 | |
mkarlsso | 0:8dbd6bd9167f | 107 | |
mkarlsso | 0:8dbd6bd9167f | 108 | protected: |
mkarlsso | 0:8dbd6bd9167f | 109 | |
mkarlsso | 0:8dbd6bd9167f | 110 | //virtual void timerInterrupt() = 0; |
mkarlsso | 0:8dbd6bd9167f | 111 | |
mkarlsso | 0:8dbd6bd9167f | 112 | void immediateClockReset(); |
mkarlsso | 0:8dbd6bd9167f | 113 | int currentDINPin; |
mkarlsso | 0:8dbd6bd9167f | 114 | int currentDOUTPin; |
mkarlsso | 0:8dbd6bd9167f | 115 | uint32_t currentDigitalOuputStates; |
mkarlsso | 0:8dbd6bd9167f | 116 | uint32_t currentDigitalInputStates; |
mkarlsso | 0:8dbd6bd9167f | 117 | |
mkarlsso | 0:8dbd6bd9167f | 118 | |
mkarlsso | 0:8dbd6bd9167f | 119 | |
mkarlsso | 0:8dbd6bd9167f | 120 | |
mkarlsso | 0:8dbd6bd9167f | 121 | }; |
mkarlsso | 0:8dbd6bd9167f | 122 | |
mkarlsso | 0:8dbd6bd9167f | 123 | |
mkarlsso | 0:8dbd6bd9167f | 124 | //Used to buffer output text. Used mainly for 'display' commands within the script, |
mkarlsso | 0:8dbd6bd9167f | 125 | //and alloed the reset of the block to execute quickly instead. The text is then streamed out |
mkarlsso | 0:8dbd6bd9167f | 126 | //slowly via serial (one character per main loop execution). outputStream is a simple circular |
mkarlsso | 0:8dbd6bd9167f | 127 | //buffer that cannot be resized after initiation. |
mkarlsso | 0:8dbd6bd9167f | 128 | class outputStream { |
mkarlsso | 0:8dbd6bd9167f | 129 | |
mkarlsso | 0:8dbd6bd9167f | 130 | public: |
mkarlsso | 0:8dbd6bd9167f | 131 | outputStream(int bufferSizeIn); |
mkarlsso | 0:8dbd6bd9167f | 132 | ~outputStream(); |
mkarlsso | 0:8dbd6bd9167f | 133 | void setSerial(sSerialPort *s); |
mkarlsso | 0:8dbd6bd9167f | 134 | void send(string outputString); |
mkarlsso | 0:8dbd6bd9167f | 135 | void debug(const char* s); |
mkarlsso | 0:8dbd6bd9167f | 136 | outputStream &operator<<(string outputString); |
mkarlsso | 0:8dbd6bd9167f | 137 | outputStream &operator<<(int outputNum); |
mkarlsso | 0:8dbd6bd9167f | 138 | outputStream &operator<<(uint32_t outputNum); |
mkarlsso | 0:8dbd6bd9167f | 139 | outputStream &operator<<(const char* s); |
mkarlsso | 0:8dbd6bd9167f | 140 | char getNextChar(); |
mkarlsso | 0:8dbd6bd9167f | 141 | bool unsentData; |
mkarlsso | 0:8dbd6bd9167f | 142 | void flush(); |
mkarlsso | 0:8dbd6bd9167f | 143 | |
mkarlsso | 0:8dbd6bd9167f | 144 | private: |
mkarlsso | 0:8dbd6bd9167f | 145 | int readHead; |
mkarlsso | 0:8dbd6bd9167f | 146 | int writeHead; |
mkarlsso | 0:8dbd6bd9167f | 147 | int totalWriteHead; |
mkarlsso | 0:8dbd6bd9167f | 148 | int totalReadHead; |
mkarlsso | 0:8dbd6bd9167f | 149 | int bufferSize; |
mkarlsso | 0:8dbd6bd9167f | 150 | char tmpOut; |
mkarlsso | 0:8dbd6bd9167f | 151 | char* outputBuffer; |
mkarlsso | 0:8dbd6bd9167f | 152 | sSerialPort* serialPtr; |
mkarlsso | 0:8dbd6bd9167f | 153 | }; |
mkarlsso | 0:8dbd6bd9167f | 154 | |
mkarlsso | 0:8dbd6bd9167f | 155 | |
mkarlsso | 0:8dbd6bd9167f | 156 | |
mkarlsso | 0:8dbd6bd9167f | 157 | #endif // HARDWARWEINTERFACE_H |