source: trunk/Arduino/libraries/OT_Slave/OT_Slave.cpp @ 12

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

Update: OT_Data conversion

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