-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroomba.c
314 lines (233 loc) · 5.44 KB
/
roomba.c
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "roomba.h"
#include "tools.h"
#include "usart.h"
#include <stdio.h>
#include <stdlib.h>
void initializeRoomba(void) {
// Start command
send_byte_roomba(128);
// Save mode
send_byte_roomba(132);
// Store songs
storeSongs();
}
void set_Display(char *data) {
send_byte_roomba(164);
uint8_t a = 0;
while (a < 4) {
send_byte_roomba(data[a]);
a++;
}
}
void clear_Display() {
char result[4] = {' ', ' ', ' ', ' '};
set_Display(result);
}
void set_Display_raw(uint8_t *data_bytes) {
send_byte_roomba(163);
for (uint8_t i = 0; i < 4; i++) {
send_byte_roomba(data_bytes[i]);
}
}
uint8_t read_button(uint8_t button) {
// Get sensor packets
send_byte_roomba(142);
// Buttons
send_byte_roomba(18);
uint8_t result = receive_byte_roomba();
if ((result & button) == button) {
return 1;
}
return 0;
}
void read_values(uint8_t packetid, uint8_t * data, uint8_t length) {
send_byte_roomba(142);
send_byte_roomba(packetid);
for (uint8_t a = 0; a < length; a++) {
data[a] = receive_byte_roomba();
}
}
uint8_t receiveRemote() {
uint8_t data[2];
read_values(17, data, 1);
//showRemoteSignalInDisplay(data[0]);
return data[0];
}
void showRemoteSignalInDisplay(char *data) {
if (data == 0) {
char empty[4] = {' ', ' ', ' ', ' '};
set_Display(empty);
return;
}
char result[4];
intToHex((uint16_t) data, result);
set_Display(result);
}
void intToHex(uint16_t mint, char *result) {
sprintf(result, "%4x", mint);
}
uint16_t read_user_input() {
char awaitInput[4] = {'-', '-', '-', '-'};
set_Display(awaitInput);
// Set 99 as dummy value (we can't have bigger numbers than 9)
uint16_t remoteVal[4] = {99, 99, 99, 99};
// Holds display chars
char toDisplay[4];
// The actual received value from the remote
uint8_t actVal;
// Set display to - - - -
for (uint8_t b = 0; b < 4; b++) {
toDisplay[b] = '-';
}
// 0 is 0x81 = 129
uint8_t finished = 0;
uint16_t counter = 0;
while (counter < 4 && finished == 0) {
uint8_t flag = 1;
while(flag) {
actVal = receiveRemote();
if (actVal != 0) {
// Check if arrow to right
if (actVal == REMOTE_RIGHT && remoteVal[counter] != 99) {
flag = 0;
counter++;
}
// Input finished
else if (actVal == REMOTE_POWER && counter > 0) {
finished = 1;
break;
}
// Check if it is really a number
else if (actVal != REMOTE_RIGHT && actVal != REMOTE_POWER && actVal >= ROOMBA_0 && actVal <= (ROOMBA_0 + 9)) {
remoteVal[counter] = actVal - ROOMBA_0;
toDisplay[counter] = (remoteVal[counter] + ASCI_0);
set_Display(toDisplay);
}
}
}
}
// Calculate number
uint16_t result = 0;
for (uint8_t b = 0; b < counter; b++) {
result = result * 10;
result = result + remoteVal[b];
}
return result;
}
void getSensorQueryList(uint8_t nrPackets, uint8_t* packet_ids, uint8_t* packet_length, uint16_t* data) {
send_byte_roomba(149);
send_byte_roomba(nrPackets);
for (uint8_t a = 0; a < nrPackets; a++) {
send_byte_roomba(packet_ids[a]);
}
for (uint8_t b = 0; b < nrPackets; b++) {
if (packet_length[b] == 1) {
data[b] = receive_byte_roomba();
} else {
uint8_t darray[2];
darray[0] = receive_byte_roomba();
darray[1] = receive_byte_roomba();
data[b] = concat_bytes(darray);
}
}
}
void playSong(uint8_t songNr) {
send_byte_roomba(141);
send_byte_roomba(songNr);
}
void storeSongs() {
// Song 1 -- hit
// Send optcode
send_byte_roomba(140);
// Song nr.
send_byte_roomba(0);
// Length
send_byte_roomba(6);
// Note 1, duration 1
send_byte_roomba(60);
send_byte_roomba(16);
// Note 2, duration 2
send_byte_roomba(65);
send_byte_roomba(32);
// Note 3, duration 3
send_byte_roomba(60);
send_byte_roomba(16);
// Note 4, duration 4
send_byte_roomba(65);
send_byte_roomba(32);
// Note 5, duration 5
send_byte_roomba(60);
send_byte_roomba(16);
// Note 6, duration 6
send_byte_roomba(65);
send_byte_roomba(32);
// Song 2 -- power-up and game over
// Send optcode
send_byte_roomba(140);
// Song nr.
send_byte_roomba(1);
// Length
send_byte_roomba(4);
// Note 1, duration 1
send_byte_roomba(65);
send_byte_roomba(8);
// Note 2, duration 2
send_byte_roomba(70);
send_byte_roomba(8);
// Note 3, duration 3
send_byte_roomba(75);
send_byte_roomba(8);
// Note 4, duration 4
send_byte_roomba(80);
send_byte_roomba(8);
// Song 3 -- Shooting
// Send optcode
send_byte_roomba(140);
// Song nr.
send_byte_roomba(2);
// Length
send_byte_roomba(4);
// Note 1, duration 1
send_byte_roomba(80);
send_byte_roomba(10);
send_byte_roomba(80);
send_byte_roomba(10);
send_byte_roomba(80);
send_byte_roomba(10);
send_byte_roomba(80);
send_byte_roomba(10);
// Song 4 -- No hit, because of mushroom
// Send optcode
send_byte_roomba(140);
// Song nr.
send_byte_roomba(3);
// Length
send_byte_roomba(4);
// Note 1, duration 1
send_byte_roomba(80);
send_byte_roomba(8);
// Note 2, duration 2
send_byte_roomba(75);
send_byte_roomba(8);
// Note 3, duration 3
send_byte_roomba(70);
send_byte_roomba(8);
// Note 4, duration 4
send_byte_roomba(65);
send_byte_roomba(8);
}
uint8_t getRoombaNrFromRemote() {
while (1) {
uint8_t result = read_user_input();
//TODO for testing
//uint16_t result = 1;
// Error, when not 1 or 2
if (result != 1 && result != 2) {
char result[4] = {'1', 'O', 'R', '2'};
set_Display(result);
my_msleep(2000);
} else {
return result;
}
}
}