-
Notifications
You must be signed in to change notification settings - Fork 8
/
thymio_natives.c
617 lines (507 loc) · 12.8 KB
/
thymio_natives.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
Thymio-II Firmware
Copyright (C) 2011 Philippe Retornaz <philippe dot retornaz at epfl dot ch>,
Mobots group (http://mobots.epfl.ch), Robotics system laboratory (http://lsro.epfl.ch)
EPFL Ecole polytechnique federale de Lausanne (http://www.epfl.ch)
See authors.txt for more details about other contributors.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, version 3 of the License.
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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <types/types.h>
#include <skel-usb.h>
#include "leds.h"
#include "sd.h"
#include "playback.h"
#include "behavior.h"
#include "tone.h"
#include "ir_prox.h"
#include "rf.h"
AsebaNativeFunctionDescription AsebaNativeDescription_set_led = {
"_leds.set",
"Set the led",
{
{1,"led"},
{1,"brightness"},
{0,0}
}
};
void set_led(AsebaVMState *vm) {
int led = vm->variables[AsebaNativePopArg(vm)];
int b = vm->variables[AsebaNativePopArg(vm)];
if(led == SOUND_ON || led < 0 || led > 39)
return;
leds_set(led,b);
}
AsebaNativeFunctionDescription AsebaNativeDescription_record = {
"sound.record",
"Start recording of rN.wav",
{
{1,"N"},
{0,0},
}
};
AsebaNativeFunctionDescription AsebaNativeDescription_play = {
"sound.play",
"Start playback of pN.wav",
{
{1,"N"},
{0,0},
}
};
AsebaNativeFunctionDescription AsebaNativeDescription_replay = {
"sound.replay",
"Start playback of rN.wav",
{
{1,"N"},
{0,0},
}
};
AsebaNativeFunctionDescription AsebaNativeDescription_duration = {
"sound.duration",
"Give duration in 1/10s of rN.wav",
{
{1,"N"},
{1,"duration"},
{0,0},
}
};
AsebaNativeFunctionDescription AsebaNativeDescription_sound_system = {
"sound.system",
"Start playback of system sound N",
{
{1,"N"},
{0,0},
}
};
static char * _prepare_name(unsigned int n, char * buf) {
unsigned int div;
unsigned int z = 0;
for(div=10000; div > 0; div /= 10) {
unsigned int disp = n / div;
n %=div;
if((disp != 0) || (z) || (div == 1)) {
z = 1;
*buf++ = '0' + disp;
}
}
return buf;
}
static void prepare_name(unsigned int n, char * buf) {
buf = _prepare_name(n, buf);
*buf++ = '.';
*buf++ = 'w';
*buf++ = 'a';
*buf++ = 'v';
*buf++ = 0;
}
void sound_playback(AsebaVMState *vm) {
char name[13] = {'p'};
int number = vm->variables[AsebaNativePopArg(vm)];
behavior_stop(B_SOUND_BUTTON);
playback_enable_event();
if(number == -1)
play_user_sound(NULL);
else {
prepare_name(number, &name[1]);
play_user_sound(name);
}
}
void sound_record(AsebaVMState *vm) {
char name[13] = {'r'};
int number = vm->variables[AsebaNativePopArg(vm)];
if(number == -1) {
sd_stop_record();
return;
}
behavior_stop(B_SOUND_BUTTON);
prepare_name(number, &name[1]);
sd_start_record(name);
}
void sound_replay(AsebaVMState *vm) {
char name[13] = {'r'};
int number = vm->variables[AsebaNativePopArg(vm)];
behavior_stop(B_SOUND_BUTTON);
playback_enable_event();
if(number == -1)
play_user_sound(NULL);
else {
prepare_name(number, &name[1]);
play_user_sound(name);
}
}
void sound_duration(AsebaVMState *vm) {
char name[13] = {'r'};
int number = vm->variables[AsebaNativePopArg(vm)];
unsigned int durationIndex = AsebaNativePopArg(vm);
prepare_name(number, &name[1]);
vm->variables[durationIndex] = sd_read_duration(name);
}
void sound_system(AsebaVMState *vm) {
char name[13] = {'s'};
int number = vm->variables[AsebaNativePopArg(vm)];
behavior_stop(B_SOUND_BUTTON);
playback_enable_event();
if(play_sound(number)) {
prepare_name(number,&name[1]);
play_user_sound(name);
}
}
AsebaNativeFunctionDescription AsebaNativeDescription_set_led_circle = {
"leds.circle",
"Set circular ring leds",
{
{1,"l0"},
{1,"l1"},
{1,"l2"},
{1,"l3"},
{1,"l4"},
{1,"l5"},
{1,"l6"},
{1,"l7"},
{0,0},
}
};
void set_led_circle(AsebaVMState *vm) {
int l1 = vm->variables[AsebaNativePopArg(vm)];
int l2 = vm->variables[AsebaNativePopArg(vm)];
int l3 = vm->variables[AsebaNativePopArg(vm)];
int l4 = vm->variables[AsebaNativePopArg(vm)];
int l5 = vm->variables[AsebaNativePopArg(vm)];
int l6 = vm->variables[AsebaNativePopArg(vm)];
int l7 = vm->variables[AsebaNativePopArg(vm)];
int l8 = vm->variables[AsebaNativePopArg(vm)];
behavior_stop(B_LEDS_ACC);
leds_set_circle(l1,l2,l3,l4,l5,l6,l7,l8);
vmVariables.led_circle[0]=l1;
vmVariables.led_circle[1]=l2;
vmVariables.led_circle[2]=l3;
vmVariables.led_circle[3]=l4;
vmVariables.led_circle[4]=l5;
vmVariables.led_circle[5]=l6;
vmVariables.led_circle[6]=l7;
vmVariables.led_circle[7]=l8;
}
AsebaNativeFunctionDescription AsebaNativeDescription_set_led_rgb_top = {
"leds.top",
"Set RGB top led",
{
{1,"red"},
{1,"green"},
{1,"blue"},
{0,0},
}
};
void set_rgb_top(AsebaVMState *vm) {
int r = vm->variables[AsebaNativePopArg(vm)];
int g = vm->variables[AsebaNativePopArg(vm)];
int b = vm->variables[AsebaNativePopArg(vm)];
leds_set_top(r,g,b);
vmVariables.led_top_rgb[0]=r;
vmVariables.led_top_rgb[1]=g;
vmVariables.led_top_rgb[2]=b;
}
AsebaNativeFunctionDescription AsebaNativeDescription_set_led_rgb_br = {
"leds.bottom.right",
"Set RGB botom right led",
{
{1,"red"},
{1,"green"},
{1,"blue"},
{0,0},
}
};
void set_rgb_br(AsebaVMState *vm) {
int r = vm->variables[AsebaNativePopArg(vm)];
int g = vm->variables[AsebaNativePopArg(vm)];
int b = vm->variables[AsebaNativePopArg(vm)];
leds_set_br(r,g,b);
vmVariables.led_bottom_right_rgb[0]=r;
vmVariables.led_bottom_right_rgb[1]=g;
vmVariables.led_bottom_right_rgb[2]=b;
}
AsebaNativeFunctionDescription AsebaNativeDescription_set_led_rgb_bl = {
"leds.bottom.left",
"Set RGB botom left led",
{
{1,"red"},
{1,"green"},
{1,"blue"},
{0,0},
}
};
void set_rgb_bl(AsebaVMState *vm) {
int r = vm->variables[AsebaNativePopArg(vm)];
int g = vm->variables[AsebaNativePopArg(vm)];
int b = vm->variables[AsebaNativePopArg(vm)];
leds_set_bl(r,g,b);
vmVariables.led_bottom_left_rgb[0]=r;
vmVariables.led_bottom_left_rgb[1]=g;
vmVariables.led_bottom_left_rgb[2]=b;
}
AsebaNativeFunctionDescription AsebaNativeDescription_set_led_buttons = {
"leds.buttons",
"Set buttons leds",
{
{1,"l0"},
{1,"l1"},
{1,"l2"},
{1,"l3"},
{0,0},
}
};
void set_buttons_leds(AsebaVMState *vm) {
int l1 = vm->variables[AsebaNativePopArg(vm)];
int l2 = vm->variables[AsebaNativePopArg(vm)];
int l3 = vm->variables[AsebaNativePopArg(vm)];
int l4 = vm->variables[AsebaNativePopArg(vm)];
behavior_stop(B_LEDS_BUTTON);
leds_set(LED_BUTTON_0, l1);
leds_set(LED_BUTTON_1, l2);
leds_set(LED_BUTTON_2, l3);
leds_set(LED_BUTTON_3, l4);
}
AsebaNativeFunctionDescription AsebaNativeDescription_set_hprox_leds = {
"leds.prox.h",
"Set horizontal proximity leds",
{
{1,"l0"},
{1,"l1"},
{1,"l2"},
{1,"l3"},
{1,"l4"},
{1,"l5"},
{1,"l6"},
{1,"l7"},
{0,0},
}
};
void set_hprox_leds(AsebaVMState *vm) {
int l1 = vm->variables[AsebaNativePopArg(vm)];
int l2 = vm->variables[AsebaNativePopArg(vm)];
int l3 = vm->variables[AsebaNativePopArg(vm)];
int l4 = vm->variables[AsebaNativePopArg(vm)];
int l5 = vm->variables[AsebaNativePopArg(vm)];
int l6 = vm->variables[AsebaNativePopArg(vm)];
int l7 = vm->variables[AsebaNativePopArg(vm)];
int l8 = vm->variables[AsebaNativePopArg(vm)];
behavior_stop(B_LEDS_PROX);
leds_set_prox_h(l1,l2,l3,l4,l5,l6,l7,l8);
}
AsebaNativeFunctionDescription AsebaNativeDescription_set_vprox_leds = {
"leds.prox.v",
"Set vertical proximity leds",
{
{1,"l0"},
{1,"l1"},
{0,0},
}
};
void set_vprox_leds(AsebaVMState *vm) {
int l1 = vm->variables[AsebaNativePopArg(vm)];
int l2 = vm->variables[AsebaNativePopArg(vm)];
behavior_stop(B_LEDS_PROX);
leds_set(LED_GROUND_IR_0,l1);
leds_set(LED_GROUND_IR_1,l2);
}
AsebaNativeFunctionDescription AsebaNativeDescription_set_rc_leds = {
"leds.rc",
"Set rc led",
{
{1,"led"},
{0,0},
}
};
void set_rc_leds(AsebaVMState *vm) {
int l1 = vm->variables[AsebaNativePopArg(vm)];
behavior_stop(B_LEDS_RC5);
leds_set(LED_RC,l1);
}
AsebaNativeFunctionDescription AsebaNativeDescription_set_sound_leds = {
"leds.sound",
"Set sound led",
{
{1,"led"},
{0,0},
}
};
void set_sound_leds(AsebaVMState *vm) {
int l1 = vm->variables[AsebaNativePopArg(vm)];
behavior_stop(B_LEDS_MIC);
leds_set(LED_SOUND,l1);
}
AsebaNativeFunctionDescription AsebaNativeDescription_set_ntc_leds = {
"leds.temperature",
"Set ntc led",
{
{1,"red"},
{1,"blue"},
{0,0},
}
};
void set_ntc_leds(AsebaVMState *vm) {
int l1 = vm->variables[AsebaNativePopArg(vm)];
int l2 = vm->variables[AsebaNativePopArg(vm)];
behavior_stop(B_LEDS_NTC);
leds_set(LED_TEMP_RED,l1);
leds_set(LED_TEMP_BLUE,l2);
}
AsebaNativeFunctionDescription AsebaNativeDescription_play_freq = {
"sound.freq",
"Play frequency",
{
{1,"Hz"},
{1,"1/60s"},
{0,0},
}
};
void play_freq(AsebaVMState * vm) {
int freq = vm->variables[AsebaNativePopArg(vm)];
int time = vm->variables[AsebaNativePopArg(vm)];
behavior_stop(B_SOUND_BUTTON);
playback_enable_event();
play_frequency(freq, time);
}
AsebaNativeFunctionDescription AsebaNativeDescription_set_wave = {
"sound.wave",
"Set the primary wave of the tone generator",
{
{WAVEFORM_SIZE, "wave"},
{0,0},
}
};
void set_wave(AsebaVMState * vm) {
int *wave = (int *) vm->variables + AsebaNativePopArg(vm);
int i;
for(i = 0; i < WAVEFORM_SIZE; i++) {
if(wave[i] > 127 || wave[i] < -128) {
AsebaVMEmitNodeSpecificError(vm, "Error: samples must be in [-128,127] range");
return;
}
}
tone_set_waveform(wave);
}
AsebaNativeFunctionDescription AsebaNativeDescription_prox_network = {
"prox.comm.enable",
"Enable or disable the proximity communication",
{
{1, "state"},
{0,0},
}
};
void prox_network(AsebaVMState * vm) {
int enable = vm->variables[AsebaNativePopArg(vm)];
if(enable)
prox_enable_network();
else
prox_disable_network();
}
AsebaNativeFunctionDescription AsebaNativeDescription_sd_open = {
"sd.open",
"Open a file on the SD card",
{
{1, "number"},
{1, "status"},
{0,0},
}
};
void thymio_native_sd_open(AsebaVMState * vm) {
int no = vm->variables[AsebaNativePopArg(vm)];
unsigned int status = AsebaNativePopArg(vm);
char name[13] = {'u'};
char * p;
if(no == -1) {
sd_user_open(NULL);
vm->variables[status] = 0;
} else {
p = _prepare_name(no, &name[1]);
*p++ = '.';
*p++ = 'd';
*p++ = 'a';
*p++ = 't';
*p++ = 0;
vm->variables[status] = sd_user_open(name);
}
}
AsebaNativeFunctionDescription AsebaNativeDescription_sd_write = {
"sd.write",
"Write data to the opened file",
{
{-1, "data"},
{1, "written"},
{0,0},
}
};
void thymio_native_sd_write(AsebaVMState * vm) {
// variable pos
unsigned char * data = (unsigned char *) (vm->variables + AsebaNativePopArg(vm));
uint16_t status = AsebaNativePopArg(vm);
// variable size
uint16_t length = AsebaNativePopArg(vm) * 2;
vm->variables[status] = sd_user_write(data,length) / 2;
}
AsebaNativeFunctionDescription AsebaNativeDescription_sd_read = {
"sd.read",
"Read data from the opened file",
{
{-1, "data"},
{1, "read"},
{0,0},
}
};
void thymio_native_sd_read(AsebaVMState * vm) {
// variable pos
unsigned char * data = (unsigned char *) (vm->variables + AsebaNativePopArg(vm));
uint16_t status = AsebaNativePopArg(vm);
// variable size
uint16_t length = AsebaNativePopArg(vm) * 2;
vm->variables[status] = sd_user_read(data,length) / 2;
}
AsebaNativeFunctionDescription AsebaNativeDescription_sd_seek = {
"sd.seek",
"Seek the opened file",
{
{1, "position"},
{1, "status"},
{0,0},
}
};
void thymio_native_sd_seek(AsebaVMState * vm) {
unsigned long seek = vm->variables[AsebaNativePopArg(vm)];
unsigned int status = AsebaNativePopArg(vm);
vm->variables[status] = sd_user_seek(seek * 2);
}
AsebaNativeFunctionDescription AsebaNativeDescription_rf_nodeid = {
"_rf.nodeid",
"Set Wireless Node ID",
{
{1, "nodeID"},
{0,0},
}
};
void set_rf_nodeid(AsebaVMState * vm) {
unsigned int nodeid = vm->variables[AsebaNativePopArg(vm)];
rf_set_node_id(nodeid);
rf_flash_setting();
}
AsebaNativeFunctionDescription AsebaNativeDescription_rf_setup = {
"_rf.setup",
"Set Wireless Channel and Network",
{
{1, "channel"},
{1, "networkID"},
{0,0},
}
};
void set_rf_setup(AsebaVMState * vm) {
unsigned int channel = vm->variables[AsebaNativePopArg(vm)];
unsigned int networkID = vm->variables[AsebaNativePopArg(vm)];
rf_set_conf(channel,networkID);
rf_flash_setting();
}