-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandParser.cpp
More file actions
175 lines (156 loc) · 2.83 KB
/
CommandParser.cpp
File metadata and controls
175 lines (156 loc) · 2.83 KB
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
#include "CommandParser.h"
bool CommandParser::Run()
{
while (true)
{
SendChangedData();
Parse();
}
}
bool CommandParser::Parse()
{
if (!uart.DataAvailable())
{
return true;
}
unsigned char command;
bool ret = uart.Receive(&command);
if (!ret)
{
LED_STOP_ON;
return false;
}
switch (command)
{
case 'v':
Version();
break;
case 't':
TerminalMode();
break;
case 's':
SetModules();
break;
case 'm':
ReturnData('m');
break;
default:
return false;
}
unsigned char cr = uart.WaitReceive();
if (cr != '\r')
{
LED_STOP_ON;
return false;
}
return true;
}
void CommandParser::SendChangedData()
{
unsigned char modules = s88.DataAvailable();
if (modules == 0)
{
return;
}
uart.Send('i');
WriteNumber(modules);
for (unsigned char module = 0; module < modules; ++module)
{
S88::UpdateQueueData data = s88.GetData();
if (data.module > S88::MaxModules16)
{
LED_STOP_ON;
return;
}
WriteNumber(data.module + 1);
WriteNumber(data.data2);
WriteNumber(data.data1);
}
uart.Send('\r');
}
unsigned char CommandParser::ReadNumber()
{
if (terminalMode)
{
unsigned char nibble1 = uart.WaitReceive();
nibble1 -= '0';
if (nibble1 >= 10)
{
nibble1 -= 7;
}
unsigned char nibble2 = uart.WaitReceive();
nibble2 -= '0';
if (nibble2 >= 10)
{
nibble2 -= 7;
}
unsigned char number = (nibble1 << 4) + nibble2;
return number;
}
else
{
unsigned char number = uart.WaitReceive();
return number;
}
}
void CommandParser::WriteNumber(const unsigned char number)
{
if (terminalMode)
{
unsigned char nibble1 = number >> 4;
if (nibble1 >= 10)
{
nibble1 += 7;
}
unsigned char nibble2 = number & 0x0F;
if (nibble2 >= 10)
{
nibble2 += 7;
}
uart.Send(nibble1);
uart.Send(nibble2);
}
else
{
uart.Send(number);
}
}
void CommandParser::Version()
{
const unsigned char* data = reinterpret_cast<const unsigned char*>("1.0.0 / 24.01.20 / HSI-88-Clone by Teddy\r");
uart.Send(data, 41);
}
void CommandParser::TerminalMode()
{
terminalMode = !terminalMode;
uart.Send('t');
uart.Send(terminalMode + '0');
uart.Send('\r');
}
void CommandParser::SetModules()
{
uart.Send('s');
unsigned char modules1 = ReadNumber();
unsigned char modules2 = ReadNumber();
unsigned char modules3 = ReadNumber();
unsigned char modulesTotal = modules1 + modules2 + modules3;
WriteNumber(modulesTotal);
uart.Send('\r');
s88.SetModules(modules1, modules2, modules3);
ReturnData('i');
}
void CommandParser::ReturnData(const unsigned char command)
{
uart.Send(command);
unsigned char* data;
unsigned char modules = s88.GetAllData(&data);
WriteNumber(modules);
for (unsigned char module = 0; module < modules; ++module)
{
WriteNumber(module + 1);
unsigned char byte = module << 1;
WriteNumber(data[byte + 1]);
WriteNumber(data[byte]);
}
uart.Send('\r');
}