-
Notifications
You must be signed in to change notification settings - Fork 0
/
ENSABrewSerial.ino
161 lines (139 loc) · 4.83 KB
/
ENSABrewSerial.ino
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
//*******************************************************************************************
// Arquivo C/C++ do Projeto Controlador de Patamares -- ENSABrewControll
//-------------------------------------------------------------------------------------------
//
// Arquivo: ENSABrewSerial.ino
//
//
// Autor : Jânio Anselmo, Eng. Me.
// Autor : Thiago Anselmo, Tecgº
// Autor : Gabriel da Silva Caetano
//
// CPU : ATMega2561 - MegaCore
// Clock : 16MHz
//
//*******************************************************************************************
//WWWWWWWWWW*********************************************************************************
// Função: startSerial
// Data : 04/04/2017 13:45
//WWWWWWWWWW*********************************************************************************
void startSerial(void )
{
Serial.begin(SERIAL_BAUD_RATE); //Para debug
while (!Serial) {;}
Serial1.begin(SERIAL_BAUD_RATE); //Para bluetooth
while (!Serial1) {;}
}
//WWWWWWWWWW*********************************************************************************
// Função: serialReceiver
// Data : 04/04/2017 13:45
//WWWWWWWWWW*********************************************************************************
void serialReceiver(void )
{
char startMarker = '{';
char endMarker = '}';
while (Serial1.available() > 0 && !_newDataSerial)
{
dataChar = Serial1.read();
if (dataChar == startMarker)
{
_recvInProgress = true;
disableInterrupts(); // Disable interrupts
}
if (_recvInProgress)
receivedSerial[idx++] = dataChar;
if (dataChar == endMarker)
{
receivedSerial[idx] = '\0'; // Terminate the "String"
idx = 0;
_recvInProgress = false;
_newDataSerial = true;
enableInterrupts(); // Enable interrupts
}
}
}
//WWWWWWWWWW*********************************************************************************
// Função: analyzesDataSerial
// Data : 04/04/2017 13:45
//WWWWWWWWWW*********************************************************************************
void analyzesDataSerial(void )
{
if (_newDataSerial)
{
// https://arduinojson.org/v5/faq/how-to-reuse-a-jsonbuffer/
_newDataSerial = false;
const size_t bufferSize = JSON_OBJECT_SIZE(7) + 90;
DynamicJsonDocument jsonReceivedBuffer(bufferSize);
DeserializationError jsonError = deserializeJson(jsonReceivedBuffer, receivedSerial);
JsonObject dataSerialJson = jsonReceivedBuffer.as<JsonObject>();
switch (jsonError)
{ // https://arduinojson.org/v6/api/misc/deserializationerror/
case DeserializationError::Ok:
code = F("A1"); // Deserialization succeeded
break;
case DeserializationError::IncompleteInput:
code = F("E1"); // Incomplete Input
break;
case DeserializationError::InvalidInput:
code = F("E2"); // Invalid input
break;
case DeserializationError::NoMemory:
code = F("E3"); // Not enough memory
break;
case DeserializationError::NotSupported:
code = F("E4"); /// Not Supported
break;
case DeserializationError::TooDeep:
code = F("E5"); // Too Deep
break;
default:
code = F("E6"); // Deserialization failed
break;
}
int typeCommand = RESET;
byte command = RESET;
if (code == F("A1"))
{
if (dataSerialJson[F("header")] == F("4E") &&
dataSerialJson[F("product")] == F("0002"))
{
// Recarrega o envio do keep alive
controlKeepAlive(_LOAD);
typeCommand = hexToDec(dataSerialJson[F("typeCmd")]);
command = hexToDec(dataSerialJson[F("cmd")]);
if(typeCommand != 0xFF && command <= 0xFF)
{
// Garante que o getId seja executado
String auxIdModule = dataSerialJson[F("id")].as<String>();
char idModuleChar[10];
auxIdModule.toCharArray(idModuleChar, 10);
// Verifica o número de série
if((strcmp(idModuleChar, configGeral.idModule) != 0))
{
memset(receivedSerial, 0, sizeof(receivedSerial));
code = F("E15"); // Número de série inválido
setJsonData(0x00, 0x00);
return;
}
}
switch (typeCommand)
{
case 0xCC:
requestCommand(typeCommand, command, dataSerialJson);
break;
case 0xFF:
replyCommand(typeCommand, command, dataSerialJson);
break;
case 0xEE: break;
default:
passoMaquina = mSTANDBY;
code = F("E7"); // typeCommand: Unknown
break;
}
}
}
memset(receivedSerial, 0, sizeof(receivedSerial));
dataSerialJson.clear();
jsonReceivedBuffer.clear();
}
}