| [3] | 1 | #ifndef OPENTHERM_H |
|---|
| 2 | #define OPENTHERM_H |
|---|
| 3 | |
|---|
| 4 | #include "Arduino.h" |
|---|
| 5 | |
|---|
| 6 | // Messages types |
|---|
| 7 | #define OT_MSGTYPE_READ_DATA B000 |
|---|
| 8 | #define OT_MSGTYPE_READ_ACK B100 |
|---|
| 9 | #define OT_MSGTYPE_WRITE_DATA B001 |
|---|
| 10 | #define OT_MSGTYPE_WRITE_ACK B101 |
|---|
| 11 | #define OT_MSGTYPE_INVALID_DATA B010 |
|---|
| 12 | #define OT_MSGTYPE_DATA_INVALID B110 |
|---|
| 13 | #define OT_MSGTYPE_UNKNOWN_DATAID B111 |
|---|
| 14 | |
|---|
| 15 | // Message IDs |
|---|
| 16 | #define OT_MSGID_STATUS 0 |
|---|
| 17 | #define OT_MSGID_CH_SETPOINT 1 |
|---|
| 18 | #define OT_MSGID_MASTER_CONFIG 2 |
|---|
| 19 | #define OT_MSGID_SLAVE_CONFIG 3 |
|---|
| 20 | #define OT_MSGID_COMMAND_CODE 4 |
|---|
| 21 | #define OT_MSGID_FAULT_FLAGS 5 |
|---|
| 22 | #define OT_MSGID_REMOTE 6 |
|---|
| 23 | #define OT_MSGID_COOLING_CONTROL 7 |
|---|
| 24 | #define OT_MSGID_CONTROL_SETPOINT_CH2 8 |
|---|
| 25 | #define OT_MSGID_CH_SETPOINT_OVERRIDE 9 |
|---|
| 26 | #define OT_MSGID_ROOM_SETPOINT 16 |
|---|
| 27 | #define OT_MSGID_MODULATION_LEVEL 17 |
|---|
| 28 | #define OT_MSGID_CH_WATER_PRESSURE 18 |
|---|
| 29 | #define OT_MSGID_DHW_FLOW_RATE 19 |
|---|
| 30 | #define OT_MSGID_DAY_TIME 20 |
|---|
| 31 | #define OT_MSGID_DATE 21 |
|---|
| 32 | #define OT_MSGID_YEAR 22 |
|---|
| 33 | #define OT_MSGID_ROOM_SETPOINT_CH2 23 |
|---|
| 34 | #define OT_MSGID_ROOM_TEMP 24 |
|---|
| 35 | #define OT_MSGID_FEED_TEMP 25 |
|---|
| 36 | #define OT_MSGID_DHW_TEMP 26 |
|---|
| 37 | #define OT_MSGID_OUTSIDE_TEMP 27 |
|---|
| 38 | #define OT_MSGID_RETURN_WATER_TEMP 28 |
|---|
| 39 | #define OT_MSGID_SOLAR_STORE_TEMP 29 |
|---|
| 40 | #define OT_MSGID_SOLAR_COLLECT_TEMP 30 |
|---|
| 41 | #define OT_MSGID_FEED_TEMP_CH2 31 |
|---|
| 42 | #define OT_MSGID_DHW2_TEMP 32 |
|---|
| 43 | #define OT_MSGID_EXHAUST_TEMP 33 |
|---|
| 44 | #define OT_MSGID_DHW_BOUNDS 48 |
|---|
| 45 | #define OT_MSGID_CH_BOUNDS 49 |
|---|
| 46 | #define OT_MSGID_DHW_SETPOINT 56 |
|---|
| 47 | #define OT_MSGID_MAX_CH_SETPOINT 57 |
|---|
| 48 | #define OT_MSGID_OVERRIDE_FUNC 100 |
|---|
| 49 | #define OT_MSGID_BURNER_STARTS 116 |
|---|
| 50 | #define OT_MSGID_CH_PUMP_STARTS 117 |
|---|
| 51 | #define OT_MSGID_DHW_PUMP_STARTS 118 |
|---|
| 52 | #define OT_MSGID_DHW_BURNER_STARTS 119 |
|---|
| 53 | #define OT_MSGID_BURNER_HOURS 120 |
|---|
| 54 | #define OT_MSGID_CH_PUMP_HOURS 121 |
|---|
| 55 | #define OT_MSGID_DHW_PUMP_HOURS 122 |
|---|
| 56 | #define OT_MSGID_DHW_BURNER_HOURS 123 |
|---|
| 57 | #define OT_MSGID_OT_VERSION_MASTER 124 |
|---|
| 58 | #define OT_MSGID_OT_VERSION_SLAVE 125 |
|---|
| 59 | #define OT_MSGID_VERSION_MASTER 128 |
|---|
| 60 | #define OT_MSGID_VERSION_SLAVE 127 |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Structure to hold Opentherm data packet content. |
|---|
| 64 | * Use f88(), u16() or s16() functions to get appropriate value of data packet accoridng to id of message. |
|---|
| 65 | */ |
|---|
| 66 | struct OpenthermData { |
|---|
| 67 | byte type; |
|---|
| 68 | byte id; |
|---|
| 69 | byte valueHB; |
|---|
| 70 | byte valueLB; |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | * @return float representation of data packet value |
|---|
| 74 | */ |
|---|
| 75 | float f88(); |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * @param float number to set as value of this data packet |
|---|
| 79 | */ |
|---|
| 80 | void f88(float value); |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * @return unsigned 16b integer representation of data packet value |
|---|
| 84 | */ |
|---|
| 85 | uint16_t u16(); |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | * @param unsigned 16b integer number to set as value of this data packet |
|---|
| 89 | */ |
|---|
| 90 | void u16(uint16_t value); |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * @return signed 16b integer representation of data packet value |
|---|
| 94 | */ |
|---|
| 95 | int16_t s16(); |
|---|
| 96 | |
|---|
| 97 | /** |
|---|
| 98 | * @param signed 16b integer number to set as value of this data packet |
|---|
| 99 | */ |
|---|
| 100 | void s16(int16_t value); |
|---|
| 101 | }; |
|---|
| 102 | |
|---|
| 103 | /** |
|---|
| 104 | * Opentherm static class that supports either listening or sending Opentherm data packets in the same time |
|---|
| 105 | */ |
|---|
| 106 | class OPENTHERM { |
|---|
| 107 | public: |
|---|
| 108 | /** |
|---|
| 109 | * Start listening foro Opentherm data packet comming from line connected to given pin. |
|---|
| 110 | * If data packet is received then hasMessage() function returns true and data packet can be retrieved by calling getMessage() function. |
|---|
| 111 | * If timeout > 0 then this function waits for incomming data package for timeout millis and if no data packet is recevived, error state is indicated by isError() function. |
|---|
| 112 | * If either data packet is received or timeout is reached listening is stopped. |
|---|
| 113 | * |
|---|
| 114 | * @param pin digital pin number with support to RISING signal interrupts (typically D2 and D3). |
|---|
| 115 | * @param timeout max time in millis that the listener should wait for data packet. Pass -1 to idicate no timeout. |
|---|
| 116 | * @param callback if provided, callback function is called once data packet is received. |
|---|
| 117 | */ |
|---|
| 118 | static void listen(byte pin, int timeout = -1, void (*callback)() = NULL); |
|---|
| 119 | |
|---|
| 120 | /** |
|---|
| 121 | * Use this function to check whether listen() function already captured a valid data packet. |
|---|
| 122 | * |
|---|
| 123 | * @return true if data packet has been captured from line by listen() function. |
|---|
| 124 | */ |
|---|
| 125 | static bool hasMessage(); |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | * Use this to retrive data packed captured by listen() function. Data packet is ready when hasMessage() function returns true. |
|---|
| 129 | * This function can be called multiple times until stop() is called. |
|---|
| 130 | * |
|---|
| 131 | * @param data reference to data structure to which fill the data packet data. |
|---|
| 132 | * @return true if packet was ready and was filled into data structure passed, false otherwise. |
|---|
| 133 | */ |
|---|
| 134 | static bool getMessage(OpenthermData &data); |
|---|
| 135 | |
|---|
| 136 | /** |
|---|
| 137 | * Immediately send out Opentherm data packet to line connected on given pin. |
|---|
| 138 | * Completed data transfer is indicated by isSent() function. |
|---|
| 139 | * Error state is indicated by isError() function. |
|---|
| 140 | * |
|---|
| 141 | * @param pin digital pin number to send data on. |
|---|
| 142 | * @param data Opentherm data packet. |
|---|
| 143 | * @param callback if provided, callback function is called once data packet is sent. |
|---|
| 144 | */ |
|---|
| 145 | static void send(byte pin, OpenthermData &data, void (*callback)() = NULL); |
|---|
| 146 | |
|---|
| 147 | /** |
|---|
| 148 | * Use this function to check whether send() function already finished sending data packed to line. |
|---|
| 149 | * |
|---|
| 150 | * @return true if data packet has been sent, false otherwise. |
|---|
| 151 | */ |
|---|
| 152 | static bool isSent(); |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | * Stops listening for data packet or sending out data packet and resets internal state of this class. |
|---|
| 156 | * Stops all timers and unattaches all interrupts. |
|---|
| 157 | */ |
|---|
| 158 | static void stop(); |
|---|
| 159 | |
|---|
| 160 | /** |
|---|
| 161 | * Indicates whether listinig or sending is not in progress. |
|---|
| 162 | * That also means that no timers are running and no interrupts are attached. |
|---|
| 163 | * |
|---|
| 164 | * @return true if listening nor sending is in progress. |
|---|
| 165 | */ |
|---|
| 166 | static bool isIdle(); |
|---|
| 167 | |
|---|
| 168 | /** |
|---|
| 169 | * Indicates whether last listen() or send() operation ends up with an error. |
|---|
| 170 | * |
|---|
| 171 | * @return true if last listen() or send() operation ends up with an error. |
|---|
| 172 | */ |
|---|
| 173 | static bool isError(); |
|---|
| 174 | |
|---|
| 175 | static byte getMode(); |
|---|
| 176 | |
|---|
| 177 | /** |
|---|
| 178 | * Helper function to debug content of data packet. |
|---|
| 179 | * It will print whatevet is in given data packet to Serial as formatted string. |
|---|
| 180 | * |
|---|
| 181 | * @param data data packet to print to serial |
|---|
| 182 | */ |
|---|
| 183 | static void printToSerial(OpenthermData &data); |
|---|
| 184 | |
|---|
| 185 | static void _risingSignalISR(); // this function needs to be public since its attached as interrupt handler |
|---|
| 186 | static void _timerISR(); // this function needs to be public since its attached as interrupt handler |
|---|
| 187 | |
|---|
| 188 | private: |
|---|
| 189 | OPENTHERM() {}; // private constructor |
|---|
| 190 | |
|---|
| 191 | static byte _pin; |
|---|
| 192 | static void (*_callback)(); |
|---|
| 193 | |
|---|
| 194 | static volatile byte _mode; |
|---|
| 195 | static volatile unsigned int _capture; |
|---|
| 196 | static volatile byte _clock; |
|---|
| 197 | static volatile unsigned long _data; |
|---|
| 198 | static volatile byte _bitPos; |
|---|
| 199 | static volatile bool _active; |
|---|
| 200 | static volatile int _timeoutMs; // <0 no timeout |
|---|
| 201 | |
|---|
| 202 | static void _listen(); // attach rising interrupt |
|---|
| 203 | static void _stop(); // stop timers and interrupts |
|---|
| 204 | static void _startReadTimer(); // reading timer to sample at 1/5 of manchester code bit length (at 5kHz) |
|---|
| 205 | static void _startWriteTimer(); // writing timer to send manchester code (at 2kHz) |
|---|
| 206 | static void _startTimeoutTimer(); // timeout count down timer (at 1kHz) |
|---|
| 207 | static void _stopTimer(); |
|---|
| 208 | static bool _checkParity(unsigned long val); |
|---|
| 209 | |
|---|
| 210 | static void _bitRead(byte value); |
|---|
| 211 | static bool _verifyStopBit(byte value); |
|---|
| 212 | static void _writeBit(byte high, byte pos); |
|---|
| 213 | static void _callCallback(); |
|---|
| 214 | }; |
|---|
| 215 | |
|---|
| 216 | #endif |
|---|