source: trunk/Arduino/libraries/ot_slave_2/ot_slave_2.cpp @ 10

Last change on this file since 10 was 10, checked in by markus, 7 years ago

Changed: Using of serial is optional and non-blocking

File size: 4.9 KB
Line 
1#include "ot_slave_2.h"
2
3#define THERMOSTAT_IN 2
4#define THERMOSTAT_OUT 4
5
6OpenthermData  OT_V2R2_Slave::message;
7
8OT_V2R2_Slave::OT_V2R2_Slave (OT_Data_CB cb)
9:
10  cbPostReq(cb),
11  chEnable(false),
12  chActive(false),
13  chTemperature(0),
14  dhwEnable(false),
15  dhwActive(false),
16  dhwTemperature(0)
17{
18}
19
20void  OT_V2R2_Slave::Setup ()
21{
22  pinMode(THERMOSTAT_IN, INPUT);
23  digitalWrite(THERMOSTAT_OUT, HIGH);
24  pinMode(THERMOSTAT_OUT, OUTPUT); // low output = high current, high output = low current
25}
26
27/**
28 * Loop will act as boiler (slave) connected to Opentherm thermostat.
29 * It will wait for requests from thermostat and response with unknown data id as a fake response.
30 */
31void  OT_V2R2_Slave::Loop ()
32{
33  if (OPENTHERM::getMessage(message)) {
34    HandleRequest(message);
35  } else if (OPENTHERM::isSent() || OPENTHERM::isIdle() || OPENTHERM::isError()) { // wait for request from thermostat
36    OPENTHERM::listen(THERMOSTAT_IN, -1);
37  }
38}
39
40void  OT_V2R2_Slave::HandleRequest (OpenthermData& message) 
41{
42  if (Serial) {
43    Serial.print("-> ");
44    OPENTHERM::printToSerial(message);
45    Serial.println();
46  }
47  delay(100); // Opentherm defines delay between request and response
48  switch (message.id) {
49    case OT_MSGID_STATUS: // 0  (Master and slave status flags) [M:ReadAck]
50      StatusReq(message);
51      break; 
52    case OT_MSGID_CH_SETPOINT:  // 1  (Control setpoint ie CH water temp. setpoint) [M:WriteAck]
53      message.type = OT_MSGTYPE_WRITE_ACK;
54      CH_Temperature(message.f88());
55      break;
56    case OT_MSGID_SLAVE_CONFIG: // 3  (Slave configuration flags) [M:ReadAck]
57      ConfigReq(message);
58      break; 
59    case OT_MSGID_FAULT_FLAGS:  // 5  (Application-specific fault flags / OEM fault code)
60      FaultReq(message);
61      break; 
62    case 14:  // 14 (Maximum relative modulation level setting) [M:WriteAck] 
63      message.type = OT_MSGTYPE_WRITE_ACK;
64      break;
65    case OT_MSGID_MODULATION_LEVEL: // 17 (Relative modulation level)  [M:ReadAck|DataInvalid]
66      message.type = OT_MSGTYPE_DATA_INVALID;
67      break;
68    case OT_MSGID_FEED_TEMP:  // 25 (Boiler temperature)  [M:ReadAck|DataInvalid]
69      message.type = OT_MSGTYPE_DATA_INVALID;
70      break;
71    case OT_MSGID_DHW_SETPOINT: // 56 (DHW setpoint)
72      if (message.type == OT_MSGTYPE_READ_DATA) {
73        message.type = OT_MSGTYPE_READ_ACK;
74        message.f88(dhwTemperature);
75        break;
76      } else if (message.type == OT_MSGTYPE_WRITE_DATA) {
77        message.type = OT_MSGTYPE_WRITE_ACK;
78        DHW_Temperature(message.f88());
79      } else {
80        message.type = OT_MSGTYPE_UNKNOWN_DATAID; // construct fake response
81      }
82      break; 
83    case OT_MSGID_VERSION_SLAVE:  // 127  (Slave product version number and type)
84      message.type = OT_MSGTYPE_READ_ACK;
85      message.valueHB = 0;  //  product type
86      message.valueLB = 0;  //  product version
87      break; 
88    default:
89      message.type = OT_MSGTYPE_UNKNOWN_DATAID; // construct fake response
90      message.valueHB = 0;
91      message.valueLB = 0;
92  }
93  if (cbPostReq) {
94    (*cbPostReq)(message);
95  }
96  OPENTHERM::send(THERMOSTAT_OUT, message, OT_V2R2_Slave::ListenAfterResponse); // send some response and wait for antoher request from thermostat set by callback
97}
98
99void  OT_V2R2_Slave::ConfigReq (OpenthermData& message)
100{
101  message.type = OT_MSGTYPE_READ_ACK;
102  message.valueHB = 0; // 0x11;  //  slave configuration:
103  message.valueLB = 0;  //  Slave MemberID code
104}
105
106void  OT_V2R2_Slave::StatusReq (OpenthermData& message)
107{
108  message.type = OT_MSGTYPE_READ_ACK;
109  CH_Enable(message.valueHB & 0x01);
110  DHW_Enable(message.valueHB & 0x02);
111  message.valueLB = 0;  //  Slave status
112  if (chActive) {
113    message.valueLB |= 0x02;
114  } else {
115    message.valueLB &= ~0x02;
116  }
117  if (dhwActive) {
118    message.valueLB |= 0x04;
119  } else {
120    message.valueLB &= ~0x04;
121  }
122}
123
124void  OT_V2R2_Slave::FaultReq (OpenthermData& message)
125{
126  message.type = OT_MSGTYPE_READ_ACK;
127  message.valueHB = 0;  //  Application-specific fault flags
128  message.valueLB = 0;  //  OEM fault code
129}
130
131bool  OT_V2R2_Slave::CH_Enable (bool enable)
132{
133  chEnable = enable;
134  chActive = enable;
135  return chEnable;
136}
137
138bool  OT_V2R2_Slave::CH_Disable ()
139{
140  chEnable = false;
141  chActive = false;
142  return chEnable;
143}
144
145void  OT_V2R2_Slave::CH_Temperature (float temp)
146{
147  chTemperature = temp;
148}
149
150bool  OT_V2R2_Slave::DHW_Enable (bool enable)
151{
152  dhwEnable = enable;
153  dhwActive = enable;
154  return dhwEnable;
155}
156
157bool  OT_V2R2_Slave::DHW_Disable ()
158{
159  dhwEnable = false;
160  dhwActive = false;
161  return dhwEnable;
162}
163
164void  OT_V2R2_Slave::DHW_Temperature (float temp)
165{
166  dhwTemperature = temp;
167}
168
169void  OT_V2R2_Slave::ListenAfterResponse () 
170{
171  if (Serial) {
172    Serial.print("<- ");
173    OPENTHERM::printToSerial(message);
174    Serial.println();
175    Serial.println();
176  }
177  OPENTHERM::listen(THERMOSTAT_IN, -1);
178}
Note: See TracBrowser for help on using the repository browser.