-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinverter.cpp
156 lines (146 loc) · 3.15 KB
/
inverter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "inverter.h"
Inverter::Inverter(Stream* conn, uint16_t addr) {
_conn = conn;
_addr = addr;
}
void Inverter::send(Frame frm, bool useFrameSrc) {
if (frm._cmd == CMD_ERR) {
#if SUNEZY_DEBUG
__debug(F("error frame, returning"));
#endif
return;
}
uint8_t buf[MAX_SIZE];
if (!useFrameSrc) {
frm._src = _addr;
}
uint8_t len = frm.bytes(buf);
if (len == 0) {
#if SUNEZY_DEBUG
__debug(F("error converting Frame to bytes, returning"));
#endif
return;
}
#if SUNEZY_DEBUG
__debug(F("sending frame"));
#endif
_conn -> write(buf, len);
}
Frame Inverter::receive() {
long start = millis();
#if SUNEZY_DEBUG
__debug(F("receiving frame"));
#endif
uint8_t i = 0;
while (millis() - start < RECV_TIMEOUT) {
yield();
if (_conn -> available() > 0) {
_buf[i++] = _conn -> read();
}
if (i > 8 && i == _buf[8] + 11) {
#if SUNEZY_DEBUG
__debug(F("received frame ok, parsing..."));
#endif
return parseFrame(_buf, _buf[8] + 11);
}
}
#if SUNEZY_DEBUG
__debug(F("timed out while receiving frame"));
#endif
return Frame(CMD_ERR);
}
void Inverter::reset() {
#if SUNEZY_DEBUG
__debug(F("sending reset"));
#endif
send(Frame(CMD_RST));
}
String Inverter::discover() {
#if SUNEZY_DEBUG
__debug(F("sending discover"));
#endif
Frame f(CMD_DSC);
send(f);
f = receive();
if (f._cmd != CMD_DSC_R) {
#if SUNEZY_DEBUG
char dbgMsg[50];
snprintf(dbgMsg, 50, "frame cmd equals %02x:%02x", f._cmd >> 8, f._cmd & 0xff);
__debug(dbgMsg);
__debug(F("invalid response frame, returning"));
#endif
return "";
}
memcpy(_buf, f._payload, f._ploadLen);
_buf[f._ploadLen] = '\0';
return String((char*) _buf);
}
bool Inverter::begin(String& sn, uint16_t addr) {
uint16_t len = sn.length();
memcpy(_buf, sn.c_str(), len);
_buf[len] = addr >> 8;
_buf[len + 1] = addr & 0xff;
Frame f(CMD_REG, _buf, len + 2);
send(f);
f = receive();
return f._cmd == CMD_REG_R;
}
String Inverter::version(uint16_t dst) {
Frame f(CMD_VER);
f._dst = dst;
send(f);
f = receive();
if (f._cmd != CMD_VER_R) {
return "";
}
memcpy(_buf, f._payload, f._ploadLen);
_buf[f._ploadLen] = '\0';
return String((char*) _buf);
}
uint8_t Inverter::_statLayout(uint16_t dst) {
Frame f(CMD_STL);
f._dst = dst;
send(f);
f = receive();
if (f._cmd != CMD_STL_R) {
return 0;
}
memcpy(_buf, f._payload, f._ploadLen);
return f._ploadLen;
}
uint8_t Inverter::_paramLayout(uint16_t dst) {
Frame f(CMD_PRL);
f._dst = dst;
send(f);
f = receive();
if (f._cmd != CMD_PRL_R) {
return 0;
}
memcpy(_buf, f._payload, f._ploadLen);
return f._ploadLen;
}
bool Inverter::status(InverterStatus& status, uint16_t dst) {
if (status.layoutLen == 0) {
uint8_t layoutLen = _statLayout();
if (layoutLen > MAX_LAYOUT_SIZE) {
#if SUNEZY_DEBUG
__debug(F("layout is too long, increase MAX_LAYOUT_SIZE in status.h"));
#endif
return false;
}
if (layoutLen == 0) {
return false;
}
memcpy(status.layout, _buf, layoutLen);
status.layoutLen = layoutLen;
}
Frame f(CMD_STA);
f._dst = dst;
send(f);
f = receive();
if (f._cmd != CMD_STA_R) {
return false;
}
interpretData(status, f._payload, f._ploadLen);
return true;
}