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
|
#include <stdlib.h>
#include <string.h>
#include "../shared/bin.h"
#include "../shared/serial_parse.h"
#include "errcatch.h"
#include "hypervisor.h"
#include "io.h"
#include "mode_dirc.h"
#include "modes.h"
#include "orangutan_shim.h"
#include "sercomm.h"
w2_s_bin *g_w2_sercomm_buffer[W2_SERCOMM_BUFFER_SIZE] = {0};
uint8_t g_w2_sercomm_index = 0;
uint8_t g_w2_sercomm_offset = 0;
uint8_t g_w2_sercomm_buffer_full = 0;
char g_w2_serial_buffer[W2_SERIAL_READ_BUFFER_SIZE] = {0};
uint8_t g_w2_serial_buffer_index = 0;
uint8_t g_w2_serial_buffer_head = 0;
void w2_sercomm_main() {
#ifdef W2_SIM
simprintfunc("w2_sercomm_main", "");
#endif
// read and parse data
while (serial_get_received_bytes() != g_w2_serial_buffer_index) {
if (!w2_serial_parse(g_w2_serial_buffer[g_w2_serial_buffer_index]))
w2_errcatch_throw(W2_E_WARN_SERIAL_NOISY);
g_w2_serial_buffer_index = (g_w2_serial_buffer_index + 1) % W2_SERIAL_READ_BUFFER_SIZE;
}
// send data
while (g_w2_sercomm_offset != g_w2_sercomm_index) {
w2_s_bin *data = g_w2_sercomm_buffer[g_w2_sercomm_offset];
#ifdef W2_SIM
w2_sim_print_serial(data);
#endif
for (uint8_t i = 0; i < data->bytes; i++) {
uint8_t byte = data->data[i];
byte == 0xff ? serial_send("\xff\xff", 2) : serial_send((char *)&byte, 1);
}
g_w2_sercomm_offset = (g_w2_sercomm_offset + 1) % W2_SERCOMM_BUFFER_SIZE;
}
}
void w2_sercomm_append_msg(w2_s_bin *data) {
#ifdef W2_SIM
simprintfunc("w2_sercomm_append_msg", "");
#endif
uint8_t next_index = (g_w2_sercomm_index + 1) % W2_SERCOMM_BUFFER_SIZE;
g_w2_sercomm_buffer_full = next_index == g_w2_sercomm_offset;
free(g_w2_sercomm_buffer[g_w2_sercomm_index]);
g_w2_sercomm_buffer[g_w2_sercomm_index] = w2_bin_s_alloc(data->bytes, data->data);
if (g_w2_sercomm_buffer_full) return;
g_w2_sercomm_index = next_index;
}
void w2_cmd_handler(uint8_t data[W2_SERIAL_READ_BUFFER_SIZE], uint8_t data_length) {
w2_s_bin *copy = w2_bin_s_alloc(data_length, data);
void (*handler)(w2_s_bin *) = g_w2_cmd_handlers[data[0]];
if (handler == NULL) {
#ifdef W2_SIM
// TODO throw warning
simwarn("unknown serial message with code 0x%02x\n", data[0]);
#endif
w2_errcatch_throw(W2_E_WARN_SERIAL_NOISY);
} else {
#ifdef W2_SIM
w2_sim_print_serial(copy);
#endif
handler(copy);
}
free(copy);
}
void w2_cmd_ping_rx(w2_s_bin *data) {
w2_s_cmd_ping_rx *message = malloc(w2_cmd_sizeof(data->data, data->bytes));
memcpy(message, data->data, data->bytes);
size_t return_size = sizeof(w2_s_cmd_ping_tx);
w2_s_cmd_ping_tx *return_message = malloc(return_size);
return_message->opcode = W2_CMD_PING | W2_CMDDIR_TX;
return_message->id = message->id;
w2_s_bin *return_message_bin = w2_bin_s_alloc(return_size, (uint8_t *)return_message);
w2_sercomm_append_msg(return_message_bin);
free(message);
free(return_message);
free(return_message_bin);
}
void w2_cmd_mode_rx(w2_s_bin *data) {
w2_s_cmd_mode_rx *message = malloc(w2_cmd_sizeof(data->data, data->bytes));
memcpy(message, data->data, data->bytes);
w2_modes_swap(message->mode);
}
void w2_cmd_sped_rx(w2_s_bin *data) { return; }
void w2_cmd_dirc_rx(w2_s_bin *data) {
w2_s_cmd_dirc_rx *message = malloc(w2_cmd_sizeof(data->data, data->bytes));
memcpy(message, data->data, data->bytes);
g_w2_mode_dirc_motor_l = w2_bin_ntoh16(message->left);
g_w2_mode_dirc_motor_r = w2_bin_ntoh16(message->right);
}
void w2_cmd_cord_rx(w2_s_bin *data) { return; }
void w2_cmd_bomd_rx(w2_s_bin *data) { return; }
void w2_cmd_sres_rx(w2_s_bin *data) { return; }
void w2_cmd_mcfg_rx(w2_s_bin *data) { return; }
void w2_cmd_sens_rx(w2_s_bin *data) {
w2_s_cmd_sens_rx *message = malloc(w2_cmd_sizeof(data->data, data->bytes));
memcpy(message, data->data, data->bytes);
size_t return_size = sizeof(w2_s_cmd_sens_tx);
w2_s_cmd_sens_tx *return_message = malloc(return_size);
return_message->opcode = W2_CMD_SENS | W2_CMDDIR_TX;
memcpy((uint8_t *)&return_message->io, (uint8_t *)&g_w2_io, sizeof(w2_s_io_all));
for (int i = 0; i < 5; i++) w2_bin_repl_hton16(&return_message->io.qtr[i].range);
w2_bin_repl_hton16(&return_message->io.front_distance.detection);
w2_bin_repl_hton16(&return_message->io.side_distance.detection);
w2_bin_repl_hton16(&return_message->io.battery.charge_level);
w2_bin_repl_hton16((uint16_t *)&return_message->io.motor_left.speed);
w2_bin_repl_hton16((uint16_t *)&return_message->io.motor_right.speed);
w2_s_bin *return_message_bin = w2_bin_s_alloc(return_size, (uint8_t *)return_message);
w2_sercomm_append_msg(return_message_bin);
free(message);
free(return_message);
free(return_message_bin);
}
void w2_cmd_info_rx(w2_s_bin *data) {
w2_s_cmd_info_rx *message = malloc(w2_cmd_sizeof(data->data, data->bytes));
memcpy(message, data->data, data->bytes);
size_t return_size = sizeof(w2_s_cmd_info_tx);
w2_s_cmd_info_tx *return_message = malloc(return_size);
return_message->opcode = W2_CMD_INFO | W2_CMDDIR_TX;
strncpy((char *)return_message->build_str, W2_BUILD_STR, sizeof(return_message->build_str));
return_message->errcatch_ms = (uint8_t)g_w2_hypervisor_ema_errcatch_ms;
return_message->io_ms = (uint8_t)g_w2_hypervisor_ema_io_ms;
return_message->sercomm_ms = (uint8_t)g_w2_hypervisor_ema_sercomm_ms;
return_message->mode_ms = (uint8_t)g_w2_hypervisor_ema_mode_ms;
return_message->uptime_s = w2_bin_hton32((uint32_t)(g_w2_hypervisor_uptime_ms / 1e3));
w2_s_bin *return_message_bin = w2_bin_s_alloc(return_size, (uint8_t *)return_message);
w2_sercomm_append_msg(return_message_bin);
free(message);
free(return_message);
free(return_message_bin);
}
void w2_cmd_disp_rx(w2_s_bin *data) { return; }
void w2_cmd_play_rx(w2_s_bin *data) { return; }
void w2_cmd_cled_rx(w2_s_bin *data) { return; }
void w2_cmd_ping_tx(w2_s_bin *data) {}
void w2_cmd_expt_tx(w2_s_bin *data) {}
void w2_cmd_mode_tx(w2_s_bin *data) {}
void w2_cmd_cord_tx(w2_s_bin *data) {}
void w2_cmd_bomd_tx(w2_s_bin *data) {}
void w2_cmd_sens_tx(w2_s_bin *data) {}
void w2_cmd_info_tx(w2_s_bin *data) {}
|