-
Notifications
You must be signed in to change notification settings - Fork 14
/
ev3_scratch.js
1535 lines (1255 loc) · 49.5 KB
/
ev3_scratch.js
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// EV3 ScratchX Plugin
// Copyright 2015 Ken Aspeslagh @massivevector
// Only tested on Mac. On Mac, ev3 brick must be named starting with "serial" if the plugin is to recognize it.
// Rename the brick before pairing it with the Mac or else the name gets cached and the serial port will have the old name
// My bricks are named serialBrick1 (etc)
// Turn off the iPod/iPhone/iPad checkbox on the EV3 Bluetooth settings after pairing or else it will not work at all
function timeStamp()
{
return (new Date).toISOString().replace(/z|t/gi,' ').trim();
}
function console_log(str)
{
console.log(timeStamp() + ": " + str);
}
// scratchX is loading our javascript file again each time a saved SBX file is opened.
// JavaScript is weird and this causes our object to be reloaded and re-registered.
// Prevent this using global variable theEV3Device and EV3Connected that will only initialize to null the first time they are declared.
// This fixes a Windows bug where it would not reconnect.
var waitingCallbacks = waitingCallbacks || [[],[],[],[],[],[],[],[], [], []];
var waitingQueries = waitingQueries || [];
var global_sensor_result = global_sensor_result || [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var thePendingQuery = thePendingQuery || null;
var warnedAboutBattery = warnedAboutBattery || false;
var deviceTimeout = deviceTimeout || 0;
var counter = counter || 0;
var poller = poller || null;
var pingTimeout = pingTimeout || null;
var waitingForPing = waitingForPing || false;
var DIRECT_COMMAND_PREFIX = "800000";
var DIRECT_COMMAND_REPLY_PREFIX = "000100";
var DIRECT_COMMAND_REPLY_SENSOR_PREFIX = "000400";
var DIRECT_COMMAND_REPLY_ALL_TYPES_PREFIX = "001000";
// direct command opcode/prefixes
var SET_MOTOR_SPEED = "A400";
var SET_MOTOR_STOP = "A300";
var SET_MOTOR_START = "A600";
var SET_MOTOR_STEP_SPEED = "AC00";
var NOOP = "0201";
var PLAYTONE = "9401";
var INPUT_DEVICE_READY_SI = "991D";
var INPUT_DEVICE_GET_TYPE_MODE = "9905";
var READ_SENSOR = "9A00";
var UIREAD = "81"; // opUI_READ
var UIREAD_BATTERY = "12"; // GET_LBATT
var UIDRAW = "84";
var UIDRAW_FILLWINDOW = "13";
var UIDRAW_PICTURE = "07";
var UIDRAW_BMPFILE = "1C";
var UIDRAW_UPDATE = "00";
var UIWRITE_INIT_RUN = "19";
var BEGIN_DOWNLOAD = "0192";
var CONTINUE_DOWNLOAD = "8193"
var UIWRITE = "82";
var LED = "1B";
var SYSTEM_REPLY_ERROR = 5;
var mode0 = "00";
var TOUCH_SENSOR = "10";
var COLOR_SENSOR = "1D";
var ULTRASONIC_SENSOR = "1E";
var ULTRSONIC_CM = "00";
var ULTRSONIC_INCH = "01";
var ULTRSONIC_LISTEN = "02";
var ULTRSONIC_SI_CM = "03";
var ULTRSONIC_SI_INCH = "04";
var ULTRSONIC_DC_CM = "05";
var ULTRSONIC_DC_INCH = "06";
var READ_MOTOR_POSITION = "01";
var READ_MOTOR_SPEED = "02";
var GYRO_SENSOR = "20";
var GYRO_ANGLE = "00";
var GYRO_RATE = "01";
var GYRO_FAST = "02";
var GYRO_RATE_AND_ANGLE = "03";
var GYRO_CALIBRATION = "04";
var IR_SENSOR = "21";
var IR_PROX = "00";
var IR_SEEKER = "01";
var IR_REMOTE = "02"
var IR_REMOTE_ADVANCE = "03";
var IR_CALIBRATION = "05";
var REFLECTED_INTENSITY = "00";
var AMBIENT_INTENSITY = "01";
var COLOR_VALUE = "02";
var COLOR_RAW_RGB = "04";
var READ_FROM_MOTOR = "FOOBAR";
var DRIVE_QUERY = "DRIVE_QUERY";
var DRIVE_QUERY_DURATION = "DRIVE_QUERY_DURATION";
var TONE_QUERY = "TONE_QUERY";
var UIDRAW_QUERY = "UIDRAW_QUERY";
var SYSTEM_COMMAND = "SYSTEM_COMMAND";
var frequencies = { "C4" : 262, "D4" : 294, "E4" : 330, "F4" : 349, "G4" : 392, "A4" : 440, "B4" : 494, "C5" : 523, "D5" : 587, "E5" : 659, "F5" : 698, "G5" : 784, "A5" : 880, "B5" : 988, "C6" : 1047, "D6" : 1175, "E6" : 1319, "F6" : 1397, "G6" : 1568, "A6" : 1760, "B6" : 1976, "C#4" : 277, "D#4" : 311, "F#4" : 370, "G#4" : 415, "A#4" : 466, "C#5" : 554, "D#5" : 622, "F#5" : 740, "G#5" : 831, "A#5" : 932, "C#6" : 1109, "D#6" : 1245, "F#6" : 1480, "G#6" : 1661, "A#6" : 1865 };
var colors = [ "none", "black", "blue", "green", "yellow", "red", "white"];
var IRbuttonNames = ['Top Left', 'Bottom Left', 'Top Right', 'Bottom Right', 'Top Bar'];
var IRbuttonCodes = [1, 2, 3, 4, 9];
var sensorPortsNames = [ "1", "2", "3", "4", "A", "B", "C", "D"];
var sensorNames = { "7E" : "None", "7F" : "Port Error", "FF" : "Unknown", "7D" : "Initializing", "07" : "Large Motor", "08" : "Medium Motor", "10" : "Button Sensor", "1D" : "Light Sensor", "1E" : "Ultrasonic Sensor", "20" : "Gyro Sensor", "21" : "Infrared Sensor", "01" : "Button Sensor (NXT)", "02" : "Light Sensor (NXT)", "03" : "Sound Sensor", "04" : "Light/Color Sensor (NXT)", "05" : "Ultrasonic Sensor (NXT)", "06" : "Temperature Sensor (NXT)" };
var port_Assignments = port_Assignments || [0, 0, 0, 0, 0, 0, 0, 0, 0];
var ledColors = {"off" : "00", "green" : "01", "red" : "02", "orange" : "03", "green flashing" : "04", "red flashing" : "05", "orange flashing" : "06", "green pulse" : "07", "red pulse" : "08", "orange pulse" : "09"}
function clearSensorStatuses()
{
var numSensorBlocks = 9;
for (x = 0; x < numSensorBlocks; x++)
{
waitingCallbacks[x] = [];
global_sensor_result[x] = 0;
port_Assignments[x] = 0;
}
}
var lastCommandWeWereTrying = null;
function startupBatteryCheckCallback(result)
{
console_log(timeStamp() + ": got battery level at connect: " + result);
weConnected();
playStartUpTones();
if (result < 11 && !warnedAboutBattery)
{
batteryAlert();
warnedAboutBattery = true;
}
clearScreen();
uploadAndDrawCatFile();
scanPorts();
setupWatchdog();
if (lastCommandWeWereTrying)
{
waitingQueries.push(lastCommandWeWereTrying);
executeQueryQueue();
}
}
function setupWatchdog()
{
if (poller)
clearInterval(poller);
poller = setInterval(pingBatteryWatchdog, 10000);
}
function pingBatteryWatchdog()
{
console_log("pingBatteryWatchdog");
testTheConnection(pingBatteryCheckCallback);
waitingForPing = true;
pingTimeout = setTimeout(pingTimeOutCallback, 3000);
}
function pingTimeOutCallback()
{
if (waitingForPing == true)
{
console_log("Ping timed out");
if (poller)
clearInterval(poller);
disconnected();
}
}
function pingBatteryCheckCallback(result)
{
console_log("pinged battery level: " + result);
if (pingTimeout)
clearTimeout(pingTimeout);
waitingForPing = false;
if (result < 11 && !warnedAboutBattery)
{
batteryAlert();
warnedAboutBattery = true;
}
//scanPorts();
}
function testTheConnection(theCallback)
{
readThatBatteryLevel(theCallback);
}
function playStartUpTones()
{
var tonedelay = 1000;
setTimeout(function()
{
playFreqM2M(262, 100);
}, tonedelay);
setTimeout(function()
{
playFreqM2M(392, 100);
}, tonedelay+150);
setTimeout(function()
{
playFreqM2M(523, 100);
}, tonedelay+300);
}
//// conversion helper routines
// create hex string from bytes
function createHexString(arr)
{
var result = "";
for (i in arr)
{
var str = arr[i].toString(16);
str = str.toUpperCase();
str = str.length == 0 ? "00" :
str.length == 1 ? "0" + str :
str.length == 2 ? str :
str.substring(str.length-2, str.length);
result += str;
}
return result;
}
function stringToHexString(instring)
{
var outString = "";
instring.split('').map(function (c) { var hex = c.charCodeAt(0).toString(16); if (hex.length == 2) { outString += hex; } else { outString += '0' + hex;} });
outString += "00";
return outString;
}
function decimalToLittleEndianHex(d, padding)
{
var hex = Number(d).toString(16);
padding = typeof (padding) === "undefined" || padding === null ? padding = 2 : padding;
while (hex.length < padding)
{
hex = "0" + hex;
}
var a = hex.match(/../g); // split number in groups of two
a.reverse(); // reverse the groups
hex = a.join(""); // join the groups back together
return hex;
}
function getFloatResult(inputData)
{
var a = new ArrayBuffer(4);
var c = new Float32Array(a);
var arr = new Uint8Array(a);
arr[0] = inputData[5];
arr[1] = inputData[6];
arr[2] = inputData[7]
arr[3] = inputData[8]
return c[0];
}
function getIRButtonNameForCode(inButtonCode)
{
for (var i = 0; i < IRbuttonCodes.length; i++)
{
if (inButtonCode == IRbuttonCodes[i])
{
return IRbuttonNames[i];
}
}
return "";
}
function createMessage(str)
{
return str; // yeah
}
// add counter and byte length encoding prefix. return Uint8Array of final message
function packMessageForSending(str)
{
var length = ((str.length / 2) + 2);
var a = new ArrayBuffer(4);
var c = new Uint16Array(a);
var arr = new Uint8Array(a);
c[1] = counter;
c[0] = length;
counter++;
var mess = new Uint8Array((str.length / 2) + 4);
for (var i = 0; i < 4; i ++)
{
mess[i] = arr[i];
}
for (var i = 0; i < str.length; i += 2)
{
mess[(i / 2) + 4] = parseInt(str.substr(i, 2), 16);
}
return mess;
}
// motor port bit field from menu choice string
function getMotorBitsHexString(which)
{
if (which == "A")
return "01";
else if (which == "B")
return "02";
else if (which == "C")
return "04";
else if (which == "D")
return "08";
else if (which == "B+C")
return "06";
else if (which == "A+D")
return "09";
else if (which == "all")
return "0F";
return "00";
}
// motor port bit field from menu choice string
function getSensorBitsHexStringFromIndex(which)
{
if (which == 1)
return "00";
else if (which == 2)
return "01";
else if (which == 3)
return "02";
else if (which == 4)
return "03";
else if (which == 5)
return "10";
else if (which == 6)
return "11";
else if (which == 7)
return "12";
else if (which == 8)
return "13";
return "00";
}
function getMotorIndex(which)
{
if (which == "A")
return 4;
else if (which == "B")
return 5;
else if (which == "C")
return 6;
else if (which == "D")
return 7;
}
// create 8 bit hex couplet
function hexcouplet(num)
{
var str = num.toString(16);
str = str.toUpperCase();
if (str.length == 1)
{
return "0" + str;
}
return str;
}
// int bytes using weird serialization method
function getPackedOutputHexString(num, lc)
{
// nonsensical unsigned byte packing. see cOutputPackParam in c_output-c in EV3 firmware
var a = new ArrayBuffer(4);
var sarr = new Int32Array(a);
var uarr = new Uint8Array(a);
sarr[0] = num;
if (lc == 0)
{
var bits = uarr[0];
bits &= 0x0000003F;
return hexcouplet(bits);
}
else if (lc == 1)
{
return "81" + hexcouplet(uarr[0]);
}
else if (lc == 2)
{
return "82" + hexcouplet(uarr[0]) + hexcouplet(uarr[1]);
}
else if (lc == 3)
{
return "83" + hexcouplet(uarr[0]) + hexcouplet(uarr[1]) + hexcouplet(uarr[2]) + hexcouplet(uarr[3]);
}
return "00";
}
//// command/query queue
function executeQueryQueueAgain(waitABit)
{
setTimeout(
function()
{
executeQueryQueue();
} , waitABit);
}
function executeQueryQueue()
{
if (waitingQueries.length == 0)
return; // nothing to do
if (!checkConnected())
return;
var query_info = waitingQueries[0]; // peek at first in line
var thisCommand = null;
if (query_info.length == 5) // a query with a response
{
var port = query_info[0];
var type = query_info[1];
var mode = query_info[2];
var callback = query_info[3];
var theCommand = query_info[4];
if (thePendingQuery)
{
// we are waiting for a result
if (thePendingQuery[0] == port)
{
// special case: we are actually already in the process of querying this same sensor (should we also compare the type and mode, or maybe just match the command string?)
// so we don't want to bother calling it again
waitingQueries.shift(); // remove it from the queue
if (callback)
waitingCallbacks[port].push(callback);
return;
}
// do nothing. we'll try again after the query finishes
return;
}
waitingQueries.shift(); // remove it from the queue
thePendingQuery = query_info;
// actually go ahead and make the query
var packedCommand = packMessageForSending(theCommand);
sendCommand(packedCommand);
}
else if (query_info.length == 4) // a query with no response
{
if (thePendingQuery) // bail if we're waiting for a response
return;
var type = query_info[0];
var duration = query_info[1];
var callback = query_info[2];
var theCommand = query_info[3];
var waitABit = 1;
if (type == DRIVE_QUERY || type == DRIVE_QUERY_DURATION)
{
clearDriveTimer();
if (type == DRIVE_QUERY_DURATION)
{
var parts = theCommand.split("|");
theCommand = parts[0];
var stopMotors = parts[1];
driveCallback = callback; // save this callback in case timer is cancelled we can call it directly
driveTimer = setTimeout(
function()
{
if (duration > 0) // allow zero duration to run motors asynchronously
{
motorsStop(stopMotors, 'coast'); // xxx
startMotors(stopMotors, 0); // workaround for bug
}
if (callback)
callback();
} , duration*1000);
}
}
else if (type == TONE_QUERY)
{
setTimeout(
function()
{
if (callback)
callback();
} , duration); // duration already in ms
}
else if (type == SYSTEM_COMMAND)
{
if (shouldChunkTranfers())
{
waitABit = 100;
}
}
waitingQueries.shift(); // remove it from the queue
// actually go ahead and make the query
var packedCommand = packMessageForSending(theCommand);
sendCommand(packedCommand);
executeQueryQueueAgain(waitABit); // maybe do the next one
}
}
function addToQueryQueue(query_info)
{
for (var i = 0; i < waitingQueries.length; i++)
{
var next_query = waitingQueries[i];
if (next_query.length == 5) // a query with a response
{
var port = next_query[0];
var type = next_query[1];
var mode = next_query[2];
var callback = next_query[3];
var theCommand = next_query[4];
var this_port = query_info[0];
if (port == this_port)
{
var this_callback = query_info[3]
if (this_callback)
waitingCallbacks[this_port].push(this_callback);
console_log("coalescing query because there's already one in the queue.");
return;
}
}
}
waitingQueries.push(query_info);
executeQueryQueue();
}
function receive_handler(data)
{
var inputData = new Uint8Array(data);
console_log("received: " + createHexString(inputData));
if (!(connectingOrConnected()))
{
console_log("Received Data but not connected or connecting");
return;
}
if (!thePendingQuery)
{
console_log("Received Data and didn't expect it...");
return;
}
var theResult = null;
var port = thePendingQuery[0];
var type = thePendingQuery[1];
var mode = thePendingQuery[2];
var callback = thePendingQuery[3];
var theCommand = thePendingQuery[4];
if (type == TOUCH_SENSOR)
{
var result = inputData[5];
theResult = (result == 100);
}
else if (type == COLOR_SENSOR)
{
var num = Math.floor(getFloatResult(inputData));
if (mode == AMBIENT_INTENSITY || mode == REFLECTED_INTENSITY)
{
theResult = num;
}
else if (mode == COLOR_VALUE)
{
if (num >= 0 && num < 7)
theResult = colors[num];
else
theResult = "none";
}
}
else if (type == IR_SENSOR)
{
if (mode == IR_PROX)
theResult = getFloatResult(inputData);
else if (mode == IR_REMOTE)
theResult = getIRButtonNameForCode(getFloatResult(inputData));
}
else if (type == GYRO_SENSOR)
{
theResult = getFloatResult(inputData);
}
else if (type == READ_FROM_MOTOR)
{
if (mode == READ_MOTOR_POSITION)
theResult = Math.round(getFloatResult(inputData) * 360); // round to nearest degree
else
theResult = getFloatResult(inputData);
}
else if (type == UIREAD)
{
if (mode == UIREAD_BATTERY)
{
theResult = inputData[5];
}
}
else if (type == BEGIN_DOWNLOAD)
{
theResult = inputData[6];
var handle = inputData[7];
if (theResult == 0)
{
console_log("BEGIN_DOWNLOAD status: " + theResult + " handle: " + handle);
var fileData = mode;
continueDownload( decimalToLittleEndianHex(handle, 2), fileData);
}
else
{
console_log("BEGIN_DOWNLOAD non-success status: " + theResult + " handle: " + handle);
}
}
else if (type == INPUT_DEVICE_GET_TYPE_MODE)
{
console_log("INPUT_DEVICE_GET_TYPE_MODE");
for (x = 0; x <= 7; x++)
{
var val = inputData[5+(x*2)];
console_log(sensorPortsNames[x] + ": " + sensorNames[hexcouplet(val)])
port_Assignments = val;
}
}
global_sensor_result[port] = theResult;
// do the callback
console_log("result: " + theResult);
if (callback)
callback(theResult);
while(callback = waitingCallbacks[port].shift())
{
console_log("result (coalesced): " + theResult);
callback(theResult);
}
// done with this query
thePendingQuery = null;
// go look for the next query
executeQueryQueueAgain(1);
}
//// extension callbacks
function capSpeed(speed)
{
if (speed > 100) { speed = 100; }
if (speed < -100) { speed = -100; }
return speed;
}
function motor(which, speed)
{
speed = capSpeed(speed);
var motorBitField = getMotorBitsHexString(which);
var speedBits = getPackedOutputHexString(speed, 1);
var motorsOnCommand = createMessage(DIRECT_COMMAND_PREFIX + SET_MOTOR_SPEED + motorBitField + speedBits + SET_MOTOR_START + motorBitField);
return motorsOnCommand;
}
function motor2(which, speed)
{
speed = capSpeed(speed);
var p = which.split("+");
var motorBitField1 = getMotorBitsHexString(p[0]);
var motorBitField2 = getMotorBitsHexString(p[1]);
var motorBitField = getMotorBitsHexString(which);
var speedBits1 = getPackedOutputHexString(speed, 1);
var speedBits2 = getPackedOutputHexString(speed * -1, 1);
var motorsOnCommand = createMessage(DIRECT_COMMAND_PREFIX
+ SET_MOTOR_SPEED + motorBitField1 + speedBits1
+ SET_MOTOR_SPEED + motorBitField2 + speedBits2
+ SET_MOTOR_START + motorBitField);
return motorsOnCommand;
}
function playFreqM2M(freq, duration)
{
console_log("playFreqM2M duration: " + duration + " freq: " + freq);
var volume = 100;
var volString = getPackedOutputHexString(volume, 1);
var freqString = getPackedOutputHexString(freq, 2);
var durString = getPackedOutputHexString(duration, 2);
var toneCommand = createMessage(DIRECT_COMMAND_PREFIX + PLAYTONE + volString + freqString + durString);
addToQueryQueue([TONE_QUERY, 0, null, toneCommand]);
}
function clearDriveTimer()
{
if (driveTimer)
clearInterval(driveTimer);
driveTimer = 0;
if (driveCallback)
driveCallback();
driveCallback = 0;
}
var driveTimer = 0;
driveCallback = 0;
function howStopCode(how)
{
if (how == 'brake')
return 1;
else
return 0;
}
function motorsStop(which, how)
{
console_log(which + " motor(s) stopped");
var motorBitField = getMotorBitsHexString(which);
var howHex = getPackedOutputHexString(howStopCode(how), 1);
var motorsOffCommand = createMessage(DIRECT_COMMAND_PREFIX + SET_MOTOR_STOP + motorBitField + howHex);
addToQueryQueue([DRIVE_QUERY, 0, null, motorsOffCommand]);
}
/*
function sendNOP()
{
var nopCommand = createMessage(DIRECT_COMMAND_PREFIX + NOOP);
}
*/
function readTouchSensor(portInt, callback)
{
readFromSensor(portInt, TOUCH_SENSOR, mode0, callback);
}
function readIRRemoteSensor(portInt, callback)
{
readFromSensor2(portInt, IR_SENSOR, IR_REMOTE, callback);
}
function readFromColorSensor(portInt, modeCode, callback)
{
readFromSensor2(portInt, COLOR_SENSOR, modeCode, callback);
}
function readThatBatteryLevel(callback)
{
console_log("Going to read battery level");
var portInt = 8; // bogus port number
UIRead(portInt, UIREAD_BATTERY, callback);
}
function readFromSensor(port, type, mode, callback)
{
var theCommand = createMessage(DIRECT_COMMAND_REPLY_PREFIX +
READ_SENSOR +
hexcouplet(port) +
type +
mode + "60");
addToQueryQueue([port, type, mode, callback, theCommand]);
}
function readFromSensor2(port, type, mode, callback)
{
var theCommand = createMessage(DIRECT_COMMAND_REPLY_SENSOR_PREFIX +
INPUT_DEVICE_READY_SI + "00" + // layer
hexcouplet(port) + "00" + // type
mode +
"0160"); // result stuff
addToQueryQueue([port, type, mode, callback, theCommand]);
}
// this routine is awful similar to readFromSensor2...
function readFromAMotor(port, type, mode, callback)
{
var theCommand = createMessage(DIRECT_COMMAND_REPLY_SENSOR_PREFIX +
INPUT_DEVICE_READY_SI + "00" + // layer
hexcouplet(port+12) + "00" + // type
mode +
"0160"); // result stuff
addToQueryQueue([port, type, mode, callback, theCommand]);
}
function UIRead(port, subtype, callback)
{
var theCommand = createMessage(DIRECT_COMMAND_REPLY_PREFIX +
UIREAD + subtype +
"60"); // result stuff
addToQueryQueue([port, UIREAD, subtype, callback, theCommand]);
}
function setLED(pattern, callback)
{
console_log("setting LED to: " + pattern);
var theCommand = createMessage(DIRECT_COMMAND_PREFIX +
UIWRITE + LED + ledColors[pattern]);
addToQueryQueue([UIWRITE, 0, callback, theCommand]);
}
function clearScreen()
{
var theCommand = createMessage(DIRECT_COMMAND_PREFIX +
UIDRAW + UIDRAW_FILLWINDOW +
"000000" + UIDRAW + UIDRAW_UPDATE);
addToQueryQueue([UIDRAW_QUERY, 0, null, theCommand]);
}
// image file code
function uploadAndDrawCatFile()
{
var fileName = "../prjs/tst/cat.rgf";
var catRGFFile = "b28000000000000000000000000000000000000000000000000000000003000000000018000000000000000000000000000000000600000000001c000000000000000000000000000000001e00000000001e000000000000000000000000000000003e00000000001f00000000000000000000000000000000f600000000c01b00000000000000000000000000000000e601000000e039000000000000000000000000000000008603000000f03800000000000000000000000000000000060f0000003838000000000000000000000000000000000e1e0000001c32000000000000000000000000000000004e780000004e30000000000000000000000000000000000ef00000e00771000000000000000000000000000000000ec1c3ffff0374000000000000000000000000000000004e84ffff7f9060000000000000000000000000000000000e007e00800760000000000000000000000000000000000c490400004064000000000000000000000000000000004c0018920410e1000000000000000000000000000000000c9220005004e0000000000000000000000000000000008c0082240141c4010000000000000000000000000000000c480800081081030000000000000000000000000000000c022192200404070000000000000000000000000000009c10800082207f0e0000000000000000000000000000001c4408240888c11d0000000000000000000000000000001c00010041c2003b0000000000000000000000000000008c10fc10044000760000000000000000000000000000001c42cf43906400ec0000000000000000000000000000001c80030e016000d80100000000000000000000000000001c910018482200b00300000000000000000000000000000cc40030012000a00300000000000000000000000000004e60006024610060070000000000000000000000000000076100c00064004006000000000000000000000000000013240080906000c006000000000000000000000000008043200080014200c00e00000000000000000000000000800121000021c800800c00000000000000000000000000c0152400008bc000801d00000000000000000000000000c080600000038401871d00000000000000000000000000e00062000022900187190000000000000000000000000060244800008600038719000000000000000000000000007080c000060624068219000000000000000000000000037012c4000f26010480380000000000000000000000001e304090000702901cc07800000000000000000000000070380481010042023040f0000000000000000000000000c039110403000b00e020c0030000000000000000000000001f8020060021ff871f00074000000000000000000000009c04820c8081ff0700000e60000000000000000000000038100818c000ff0700001c1c0000000000000000000000788020f06000fe070000b80f0000000000000000000000982484821f00fc030000fe010000000000000000000000180040000000f003000066000000000000000000000000184912000000c00000006000000000000000000000000018000000000000000000c000000000000000000000000f7c240900000000000000c00000000000000000000000fe3f000000e00000000000c000000000000000000000000038490200900700000000c000000000000000000000000030000400103c00000000c00000000000000000000000007092000010e001000000c60000000000000000000000007000020010000f000000fc000000000000000000000000602404001800f0000000e03f0000000000000000000000e08000001000801f0000601c0000000000000000000000c0110800100000e007007000000000000000000000000080490200100000001c003000000000000000000000000080031000100000000c00380000000000000000000000000087040030000000060018000000000000000000000000000e20002000000003000c000000000000000000000000009c00004000008001000e0000000000000000000000000038520080000060000007000000000000000000000000007080000003003000800300000000000000000000000000e00800000e000c00e00100000000000000000000000000c023020038800300f0000000000000000000000000000080870800c0ff00003c0000000000000000000000000000001f1000000000001e0000000000000000000000000000c07f400000000080070000000000000000000080030000f0eb0501000000f00300000000000000000000e007000078801b040000007c0000000000000000000000700e00003c00e6010000c01f0000000000000000000000181800001e2406fe0000fe0300000000000000000000001838008087000600ffff3f0000000000000000000000000c3000c023522300f8ff010000000000000000000000000c6000f001000309e000000000000000000000000000000ce000f810894940c201000000000000000000000000000ec0003c40e000048803c00700000000000000000000000e80010e027284882007f00f00000000000000000000000e80018e9038212002063c3c2eae0f843fd739000000000e000306040e1000110e1e303131118ab49810000000000c0007462007080040bc0771a130118a649810000000000c000e0ee1a1000008f943608120110a449010000000000c001c0ec408020018e010629e000f0a44801f000000001800388e8001000088040060a000091e448010000000001850790c1223010000004472a1200911449010000000001000e01c400604004c92103823312911849810000000003004c29f044e00009c00801c1d9eb33b0ecf39000000002090083f200c09003e24091e00000000000000000000006004003c82180000770000070000000000000000000000c0201160081a1200e348c2030000000000000000000000800384e022988080c101f0010000000000000000000000008720c4001908c180077c000000000000000000000000001e0290111822e200df1f000000000000000000000000007c4802034e807400fc0700000000000000000000000000f00320fe17083800f000005c3cc6fd3f781c0e3f1f0000c03f80f93142700000000062428ca4458408042222000000fe7f002000e10000000061818c24450209042242000000f01f00c424c40100000041819420440209040a420000c0e7071201018003000000018194203c0209040e420000f0ff8140104b12070000000181a420240209040a420000383c2008c403400e0000004181a42024028944224200001c000481f08f041c0000006242c420a4848844322200004c0041207c1e10380000003c3c8e70ce78fc7e3f1f00000c4910081e3c42710000000000000000000000000000000c0004c1073808e40000000000000000000000000000009c2421f003f000c00100000000000000000000000000001c00087c00e025810700000000000000000000000000001892801f00c001140e00000000000000000000000000009800fe07008093401e00000000000000000000000000003824f80000000700f8ff010000000000000000000000003001390000001e92f0ff0f000000000000000000000000704870000000bc0002001f000000000000000000000000e000640000003848000078000000000000000000000000c024e1000000f000240970000000000000000000000000c001c0000000e0110120e10000000000000000000000008093c8000000c0439004c40000000000000000000000000003c200000000070490c00100000000000000000000000047c0000000004e2001c8010000000000000000000000001ec8000000001c0824c2010000000000000000000000003ce000000000780080c001000000000000000000000000f87c00000000f0bf00e000000000000000000000000000e03f00000000e0ffffff00000000000000000000000000800f0000000000f8ff3f000000000000000000000000000000000000000000f00700000000000000000000";
uploadAndDrawRGFData(catRGFFile, fileName);
}
function drawFile(fileNameHex)
{
console_log("UIDRAW_BMPFILE");
var theCommand = createMessage(DIRECT_COMMAND_PREFIX +
UIDRAW + UIDRAW_BMPFILE +
"01" + // forground color
getPackedOutputHexString(0,2) + getPackedOutputHexString(0,2) + // location
"84" + fileNameHex + // LCS string encoding
UIDRAW + UIDRAW_UPDATE);
addToQueryQueue([UIDRAW_QUERY, 0, null, theCommand]);
}
function uploadAndDrawRGFData(fileDataHexString, name)
{
var fileLength = fileDataHexString.length / 2;
var lengthString = decimalToLittleEndianHex(fileLength, 8);
var fileNameHex = stringToHexString(name);
var theCommand = createMessage(
BEGIN_DOWNLOAD + lengthString + fileNameHex //"6361742e726766000" // the filename...
);
addToQueryQueue([8, BEGIN_DOWNLOAD, fileNameHex + "|" + fileDataHexString, null, theCommand]);
}
function continueDownload(handle, fileData)
{
console_log("CONTINUE_DOWNLOAD");
var p = fileData.split("|");
var data = p[1];
var nameHex = p[0];
var chunkSize = (shouldChunkTranfers()) ? 256 : 65535;
var totLen = data.length;
var x;
for (x = 0; x < totLen; x += chunkSize)
{
var thisdata = data.substring(x, x + chunkSize);
var theCommand = createMessage(
CONTINUE_DOWNLOAD + handle + thisdata
);
addToQueryQueue([SYSTEM_COMMAND, 0, null, theCommand]);
}
drawFile(nameHex);
}
function startMotors(which, speed)
{
clearDriveTimer();
console_log("motor " + which + " speed: " + speed);
motorCommand = motor(which, speed);
addToQueryQueue([DRIVE_QUERY, 0, null, motorCommand]);
}
function motorDegrees(which, speed, degrees, howStop)
{
speed = capSpeed(speed);
if (degrees < 0)
{
degrees *= -1;
speed *= -1;
}
var motorBitField = getMotorBitsHexString(which);
var speedBits = getPackedOutputHexString(speed, 1);
var stepRampUpBits = getPackedOutputHexString(0, 3);
var stepConstantBits = getPackedOutputHexString(degrees, 3);
var stepRampDownBits = getPackedOutputHexString(0, 3);
var howHex = getPackedOutputHexString(howStopCode(howStop), 1);
var motorsCommand = createMessage(DIRECT_COMMAND_PREFIX + SET_MOTOR_STEP_SPEED + motorBitField + speedBits
+ stepRampUpBits + stepConstantBits + stepRampDownBits + howHex
+ SET_MOTOR_START + motorBitField);
addToQueryQueue([DRIVE_QUERY, 0, null, motorsCommand]);
}
function playTone(tone, duration, callback)
{
var freq = frequencies[tone];
console_log("playTone " + tone + " duration: " + duration + " freq: " + freq);
var volume = 100;
var volString = getPackedOutputHexString(volume, 1);
var freqString = getPackedOutputHexString(freq, 2);
var durString = getPackedOutputHexString(duration, 2);
var toneCommand = createMessage(DIRECT_COMMAND_PREFIX + PLAYTONE + volString + freqString + durString);
addToQueryQueue([TONE_QUERY, duration, callback, toneCommand]);
}
function playFreq(freq, duration, callback)