FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
VeDirectFrameHandler/VeDirectFrameHandler.cpp at master · cterwilliger/VeDirectFrameHandler · GitHub
cterwilliger
/
VeDirectFrameHandler
Public
Notifications
You must be signed in to change notification settings
Fork
11
Star
33
Code
Issues
3
Pull requests
2
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
VeDirectFrameHandler
/
VeDirectFrameHandler.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
209 lines (199 loc) · 6 KB
Breadcrumbs
VeDirectFrameHandler
/
VeDirectFrameHandler.cpp
Copy path
File metadata and controls
209 lines (199 loc) · 6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
framehandler.cpp
*
* Arduino library to read from Victron devices using VE.Direct protocol.
* Derived from Victron framehandler reference implementation.
*
* The MIT License
*
* Copyright (c) 2019 Victron Energy BV
* Portions Copyright (C) 2020 Chris Terwilliger
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* 2020.05.05 - 0.2 - initial release
* 2020.06.21 - 0.2 - add MIT license, no code changes
* 2020.08.20 - 0.3 - corrected #include reference
*
*/
#
include
<
Arduino.h
>
#
include
"
VeDirectFrameHandler.h
"
#
define
MODULE
"
VE.Frame
"
//
Victron seems to use this to find out where logging messages were generated
//
The name of the record that contains the checksum.
static
constexpr
char
checksumTagName[] =
"
CHECKSUM
"
;
VeDirectFrameHandler::VeDirectFrameHandler
() :
//
mStop(false), // don't know what Victron uses this for, not using
mState(
IDLE
),
mChecksum(
0
),
mTextPointer(
0
),
tempName(),
tempValue(),
frameIndex(
0
),
veName(),
veValue(),
veEnd(
0
)
{
}
/*
* rxData
* This function is called by the application which passes a byte of serial data
* It is unchanged from Victron's example code
*/
void
VeDirectFrameHandler::rxData
(
uint8_t
inbyte)
{
//
if (mStop) return;
if
( (inbyte ==
'
:
'
) && (
mState
!=
CHECKSUM
) ) {
mState
=
RECORD_HEX
;
}
if
(
mState
!=
RECORD_HEX
) {
mChecksum
+= inbyte;
}
inbyte =
toupper
(inbyte);
switch
(
mState
) {
case
IDLE
:
/*
wait for \n of the start of an record
*/
switch
(inbyte) {
case
'
\n
'
:
mState
=
RECORD_BEGIN
;
break
;
case
'
\r
'
:
/*
Skip
*/
default
:
break
;
}
break
;
case
RECORD_BEGIN
:
mTextPointer
=
mName
;
*
mTextPointer
++ = inbyte;
mState
=
RECORD_NAME
;
break
;
case
RECORD_NAME
:
//
The record name is being received, terminated by a \t
switch
(inbyte) {
case
'
\t
'
:
//
the Checksum record indicates a EOR
if
(
mTextPointer
< (
mName
+
sizeof
(
mName
)) ) {
*
mTextPointer
=
0
;
/*
Zero terminate
*/
if
(
strcmp
(
mName
, checksumTagName) ==
0
) {
mState
=
CHECKSUM
;
break
;
}
}
mTextPointer
=
mValue
;
/*
Reset value pointer
*/
mState
=
RECORD_VALUE
;
break
;
default
:
//
add byte to name, but do no overflow
if
(
mTextPointer
< (
mName
+
sizeof
(
mName
)) )
*
mTextPointer
++ = inbyte;
break
;
}
break
;
case
RECORD_VALUE
:
//
The record value is being received. The \r indicates a new record.
switch
(inbyte) {
case
'
\n
'
:
//
forward record, only if it could be stored completely
if
(
mTextPointer
< (
mValue
+
sizeof
(
mValue
)) ) {
*
mTextPointer
=
0
;
//
make zero ended
textRxEvent
(
mName
,
mValue
);
}
mState
=
RECORD_BEGIN
;
break
;
case
'
\r
'
:
/*
Skip
*/
break
;
default
:
//
add byte to value, but do no overflow
if
(
mTextPointer
< (
mValue
+
sizeof
(
mValue
)) )
*
mTextPointer
++ = inbyte;
break
;
}
break
;
case
CHECKSUM
:
{
bool
valid =
mChecksum
==
0
;
if
(!valid)
logE
(
MODULE
,
"
[CHECKSUM] Invalid frame
"
);
mChecksum
=
0
;
mState
=
IDLE
;
frameEndEvent
(valid);
break
;
}
case
RECORD_HEX
:
if
(
hexRxEvent
(inbyte)) {
mChecksum
=
0
;
mState
=
IDLE
;
}
break
;
}
}
/*
* textRxEvent
* This function is called every time a new name/value is successfully parsed. It writes the values to the temporary buffer.
*/
void
VeDirectFrameHandler::textRxEvent
(
char
*
mName
,
char
*
mValue
) {
strcpy
(tempName[frameIndex],
mName
);
//
copy name to temporary buffer
strcpy
(tempValue[frameIndex],
mValue
);
//
copy value to temporary buffer
frameIndex++;
}
/*
* frameEndEvent
* This function is called at the end of the received frame. If the checksum is valid, the temp buffer is read line by line.
* If the name exists in the public buffer, the new value is copied to the public buffer. If not, a new name/value entry
* is created in the public buffer.
*/
void
VeDirectFrameHandler::frameEndEvent
(
bool
valid) {
if
( valid ) {
for
(
int
i =
0
; i < frameIndex; i++ ) {
//
read each name already in the temp buffer
bool
nameExists =
false
;
for
(
int
j =
0
; j <= veEnd; j++ ) {
//
compare to existing names in the public buffer
if
(
strcmp
(tempName[i], veName[j]) ==
0
) {
strcpy
(veValue[j], tempValue[i]);
//
overwrite tempValue in the public buffer
nameExists =
true
;
break
;
}
}
if
( !nameExists ) {
strcpy
(veName[veEnd], tempName[i]);
//
write new Name to public buffer
strcpy
(veValue[veEnd], tempValue[i]);
//
write new Value to public buffer
veEnd++;
//
increment end of public buffer
if
( veEnd >= buffLen ) {
//
stop any buffer overrun
veEnd = buffLen -
1
;
}
}
}
}
frameIndex =
0
;
//
reset frame
}
/*
* logE
* This function included for continuity and possible future use.
*/
void
VeDirectFrameHandler::logE
(
char
*
module
,
char
* error) {
//
Serial.print("MODULE: ");
//
Serial.println(module);
//
Serial.print("ERROR: ");
//
Serial.println(error);
return
;
}
/*
* hexRxEvent
* This function included for continuity and possible future use.
*/
bool
VeDirectFrameHandler::hexRxEvent
(
uint8_t
inbyte) {
return
true
;
//
stubbed out for future
}
Back
|
FazBrowse Home
|
New Git URL