-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSerial_Event.ino
167 lines (142 loc) · 4.11 KB
/
Serial_Event.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
162
163
164
165
166
167
/**
@file Serial_Event.ino
@brief Serial Event functions for SATLLA0.
This file contains functionality to maintain serial events via PC communication for the SATLLA0.
Copyright (C) 2023 @author Rony Ronen
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* ============ */
/* Serial Event */
/* ============ */
#define MAX_BUFFER_SIZE 1024 // Max buffer size of input
char ser_buffer_g[MAX_BUFFER_SIZE];
void serial_event_setup()
{
PRINTLN("***********************");
PRINTLN("Settings and Commands:");
PRINTLN("Send HEX Command: ");
PRINTLN("r: Receive, s: S-BAND, u: UHF, p: Print");
PRINTLN("{cmd}{dest}{code}{payload}");
PRINTLN("***********************");
}
void serial_event()
{
PRINTLN("Func: serial_event()");
int num_bytes = Serial.available();
char c = 0;
PRINT("num_bytes:\t");
PRINTLN(num_bytes);
if (num_bytes > MAX_BUFFER_SIZE)
{
num_bytes = MAX_BUFFER_SIZE;
}
if (num_bytes > 0)
{
c = (char)Serial.read();
PRINT("Command:\t");
PRINTLN(c);
switch (c)
{
case '\n':
case '\r':
case '\r\n':
{
// PRINTLN("CR LF");
}
break;
case 'H':
case 'h':
{
serial_event_setup();
}
default:
bool send_flag = false;
delay(SEC_1);
num_bytes = Serial.available() - 2; // CR
PRINT("num_bytes:\t");
PRINTLN(num_bytes);
if (num_bytes > 0)
{
parse_command(c, num_bytes);
}
Serial.flush();
break;
}
}
}
void parse_command(char cmd, int num_bytes)
{
PRINTLN("Func: parse_command()");
uint8_t val = 0;
uint8_t idx = 0;
uint8_t b = 0;
char c[2];
String hex;
lorafreq_e freq = lora_none;
memset(ser_buffer_g, 0x00, MAX_BUFFER_SIZE);
for (int i = 0; i < num_bytes; i += 2) // avoid eol
{
c[0] = (char)Serial.read();
c[1] = (char)Serial.read();
if (c[0] == -1 || c[1] == -1)
{
PRINTLN("Error: Exit");
return;
}
hex = String(c[0]) + String(c[1]);
val = (int)strtol(hex.c_str(), NULL, 16);
ser_buffer_g[idx] = val;
idx++;
}
#ifdef DEBUG
print_buffer((uint8_t *)ser_buffer_g, idx);
#endif
memset(command_data_g.packet, 0x00, COMMAND_PACKET_LENGTH);
uint8_t destination = ser_buffer_g[0];
command_data_g.cmd_code = ser_buffer_g[1];
command_data_g.cmd_type = cmd_type_param;
command_data_g.cmd_size = idx - 2;
memcpy(command_data_g.cmd_payload, ser_buffer_g + 2, idx - 2);
// print_command(&command_data_g);
if (cmd == 'p')
{
memcpy(lora_data_g.packet, ser_buffer_g, idx);
print_message(lora_data_g.packet, LORA_HEADER_LENGTH + lora_data_g.msg_size);
if (lora_data_g.msg_type == type_command)
{
memcpy(command_data_g.packet, lora_data_g.msg_payload, lora_data_g.msg_size);
parse_and_activate_command(&command_data_g);
}
}
else
{
if (cmd == 'r')
{
cmd_receive(&command_data_g);
}
else
{
if (cmd == 's')
{
freq = lora_24;
}
else if (cmd == 'u')
{
freq = lora_433;
}
if (destination != 0x00)
{
cmd_send(&command_data_g, freq);
}
}
}
}