forked from 4tronix/BitBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitbot.ts
More file actions
1852 lines (1733 loc) · 50.5 KB
/
bitbot.ts
File metadata and controls
1852 lines (1733 loc) · 50.5 KB
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
/**
* Eyeball directions
*/
enum eyePos
{
//% block="forward"
Forward,
//% block="down"
Down,
//% block="up"
Up,
//% block="left"
Left,
//% block="right"
Right,
//% block="down-left"
DownLeft,
//% block="down-right"
DownRight,
//% block="up-left"
UpLeft,
//% block="up-right"
UpRight
}
enum eyeSize
{
//% block="small"
Small,
//% block="large"
Large
}
enum bfEyes
{
//% block="left"
Left,
//% block="right"
Right,
//% block="both"
Both
}
enum bfMouth
{
//% block="smile"
Smile,
//% block="grin"
Grin,
//% block="sad"
Sad,
//% block="frown"
Frown,
//% block="straight"
Straight,
//% block="oooh"
Oooh,
//% block="eeeh"
Eeeh
}
/**
* Enumeration of motors.
*/
enum BBMotor
{
//% block="left"
Left,
//% block="right"
Right,
//% block="both"
Both
}
/**
* Enumeration of forward/reverse directions
*/
enum BBDirection
{
//% block="forward"
Forward,
//% block="reverse"
Reverse
}
/**
* Enumeration of directions.
*/
enum BBRobotDirection
{
//% block="left"
Left,
//% block="right"
Right
}
/**
* Stop modes. Coast or Brake
*/
enum BBStopMode
{
//% block="no brake"
Coast,
//% block="brake"
Brake
}
/**
* Enable/Disable for Bluetooth and FireLeds
*/
enum BBBluetooth
{
//% block="Enable"
btEnable,
//% block="Disable"
btDisable
}
/**
* Values for buzzer. On or Off
*/
enum BBBuzz
{
//% block="on"
On,
//% block="off"
Off
}
/**
* Enumeration of line sensors.
*/
enum BBLineSensor
{
//% block="left"
Left,
//% block="right"
Right
}
/**
* Enumeration of light sensors.
*/
enum BBLightSensor
{
//% block="left"
Left,
//% block="right"
Right
}
/**
* Ping unit for sensor.
*/
enum BBPingUnit
{
//% block="cm"
Centimeters,
//% block="inches"
Inches,
//% block="μs"
MicroSeconds
}
enum BBServos
{
P1,
P2
}
/**
* Update mode for LEDs
* setting to Manual requires show LED changes blocks
* setting to Auto will update the LEDs everytime they change
*/
enum BBMode
{
Manual,
Auto
}
/**
* Model Types of BitBot
* Classic or XL
*/
enum BBModel
{
Classic,
XL,
Auto
}
/**
* Pre-Defined LED colours
*/
enum BBColors
{
//% block=red
Red = 0xff0000,
//% block=orange
Orange = 0xffa500,
//% block=yellow
Yellow = 0xffff00,
//% block=green
Green = 0x00ff00,
//% block=blue
Blue = 0x0000ff,
//% block=indigo
Indigo = 0x4b0082,
//% block=violet
Violet = 0x8a2be2,
//% block=purple
Purple = 0xff00ff,
//% block=white
White = 0xffffff,
//% block=black
Black = 0x000000
}
/**
* Custom blocks
*/
//% weight=50 color=#e7660b icon="\uf1b9"
//% groups='["Basic","Advanced","Special","Ultrasonic","Line Sensor","5x5 Matrix","BitFace","OLED 128x64"]'
namespace bitbot
{
let fireBand: fireled.Band;
let _updateMode = BBMode.Auto;
let btDisabled = true;
let matrix5: fireled.Band;
let bitface: fireled.Band;
let mouthSmile: number[] = [0,1,2,3,4,5];
let mouthGrin: number[] = [0,1,2,3,4,5,10,11,12,13];
let mouthSad: number[] = [0,5,6,7,8,9];
let mouthFrown: number[] = [0,5,6,7,8,9,10,11,12,13];
let mouthStraight: number[] = [0,5,10,11,12,13];
let mouthOooh: number[] = [1,2,3,4,6,7,8,9,10,13];
let mouthEeeh: number[] = [0,1,2,3,4,5,6,7,8,9];
let oled: firescreen.Screen;
let leftBias = 0;
let rightBias = 0;
let calibration: number[] = [0, 0, 0];
let leftCalib = 0;
let rightCalib = 0;
let _model = BBModel.Auto;
let i2caddr = 28; // i2c address of I/O Expander
let EEROM = 0x50; // i2c address of EEROM
let versionCode = -1;
let lMotorD0: DigitalPin;
let lMotorD1: DigitalPin;
let lMotorA0: AnalogPin;
let lMotorA1: AnalogPin;
let rMotorD0: DigitalPin;
let rMotorD1: DigitalPin;
let rMotorA0: AnalogPin;
let rMotorA1: AnalogPin;
let _deadband = 2;
let _p1Trim = 0;
let _p2Trim = 0;
function clamp(value: number, min: number, max: number): number
{
return Math.max(Math.min(max, value), min);
}
// Block to enable Bluetooth and disable FireLeds.
/**
* Enable/Disable Bluetooth support by disabling/enabling FireLeds
* @param enable enable or disable Blueetoth
*/
//% blockId="BBEnableBluetooth"
//% block="%enable|Bluetooth"
//% blockGap=8
export function bbEnableBluetooth(enable: BBBluetooth)
{
if (enable == BBBluetooth.btEnable)
btDisabled = false;
else
btDisabled = true;
}
// Blocks for selecting BitBot Model
/**
* Force Model of BitBot (Determines Pins used)
* @param model Model of BitBot; Classic or XL
*/
//% blockId="bitbot_model" block="select BitBot model%model"
//% weight=100
//% subcategory=BitBot_Model
export function select_model(model: BBModel): void
{
if((model==BBModel.Classic) || (model==BBModel.XL) || (model==BBModel.Auto))
{
_model = model;
if (_model == BBModel.Classic)
{
lMotorD0 = DigitalPin.P0;
lMotorD1 = DigitalPin.P8;
lMotorA0 = AnalogPin.P0;
lMotorA1 = AnalogPin.P8;
rMotorD0 = DigitalPin.P1;
rMotorD1 = DigitalPin.P12;
rMotorA0 = AnalogPin.P1;
rMotorA1 = AnalogPin.P12;
}
else
{
lMotorD0 = DigitalPin.P16;
lMotorD1 = DigitalPin.P8;
lMotorA0 = AnalogPin.P16;
lMotorA1 = AnalogPin.P8;
rMotorD0 = DigitalPin.P14;
rMotorD1 = DigitalPin.P12;
rMotorA0 = AnalogPin.P14;
rMotorA1 = AnalogPin.P12;
}
}
}
/**
* get Model of BitBot (Classic or XL)
*/
//% blockId="bb_model" block="BitBot model"
//% weight=90
//% subcategory=BitBot_Model
export function getModel(): BBModel
{
getVersionCode();
if (_model == BBModel.Auto)
{
if (versionCode == 0)
{
select_model(BBModel.Classic);
}
else
{
select_model(BBModel.XL);
pins.digitalWritePin(DigitalPin.P0, 0);
}
}
return _model;
}
/**
* Get numeric value of BitBot Model
*
* @param model BitBot Model eg: BBModel.Classic
*/
//% blockId="bb_models" block=%model
//% weight=80
//% subcategory=BitBot_Model
export function BBModels(model: BBModel): number
{
return model;
}
/**
* Get versionCode
*/
//% blockId="getVersionCode"
//% block="version Code"
//% weight=80
//% subcategory=BitBot_Model
//% deprecated=true
export function getVersionCode(): number
{
if (versionCode == -1) // first time requesting
versionCode = (pins.i2cReadNumber(i2caddr, NumberFormat.Int8LE, false) >> 4) & 0x0f;
return versionCode;
}
// "DRIVE STRAIGHT" BLOCKS
// Uses bottom 3 bytes of EEROM for motor bias data
// Bias values from -100 to +100. Negative values decrease Left speed, Positive decrease right speed
// Byte 0 = Bias at 30, 1 = Bias at 60, 2 = Bias at 90
/**
* Write a byte of data to EEROM at selected address
* @param address Location in EEROM to write to
* @param data Byte of data to write
*/
//% blockId="writeEEROM"
//% block="write%data|to address%address"
//% data.min = -128 data.max = 127
//% weight=100
//% deprecated=true
export function writeEEROM(data: number, address: number): void
{
wrEEROM(data, address);
}
function wrEEROM(data: number, address: number): void
{
if (getVersionCode() == 5)
{
let i2cData = pins.createBuffer(3);
i2cData[0] = address >> 8; // address MSB
i2cData[1] = address & 0xff; // address LSB
i2cData[2] = data & 0xff;
pins.i2cWriteBuffer(EEROM, i2cData, false);
basic.pause(3); // needs a short pause. 3ms ok?
}
}
/**
* Read a byte of data from EEROM at selected address
* @param address Location in EEROM to read from
*/
//% blockId="readEEROM"
//% block="read EEROM address%address"
//% weight=90
//% deprecated=true
export function readEEROM(address: number): number
{
return rdEEROM(address);
}
// Uses bottom 3 bytes of EEROM for motor calibration. No user access
function rdEEROM(address: number): number
{
if (getVersionCode() == 5)
{
let i2cRead = pins.createBuffer(2);
i2cRead[0] = address >> 8; // address MSB
i2cRead[1] = address & 0xff; // address LSB
pins.i2cWriteBuffer(EEROM, i2cRead, false);
basic.pause(1);
return pins.i2cReadNumber(EEROM, NumberFormat.Int8LE);
}
else
return 0;
}
/**
* Load Calibration data from EEROM
*/
//% blockId="loadCalibration"
//% block="Load calibration from EEROM"
//% weight=80
//% deprecated=true
export function loadCalibration(): void
{
for (let i=0; i<3; i++)
calibration[i] = rdEEROM(i);
}
/**
* Save Calibration data to EEROM
*/
//% blockId="saveCalibration"
//% block="Save calibration to EEROM"
//% weight=70
//% deprecated=true
export function saveCalibration(): void
{
for (let i=0; i<3; i++)
wrEEROM(calibration[i],i);
}
/**
* Check Calibration Values for given speed
* @param speed selected speed 0 to 100. eg: 60
* @param side selected motor Left or Right
*/
//% blockId="checkCalibration"
//% block="Check side%side bias for speed%speed"
//% weight=60
//% deprecated=true
export function checkCalibration(side: BBMotor, speed: number): number
{
let calibVal = 0;
leftCalib = 0;
rightCalib = 0;
if (speed < 60)
calibVal = calibration[1] - ((60 - speed)/30) * (calibration[1] - calibration[0]);
else
calibVal = calibration[2] - ((90 - speed)/30) * (calibration[2] - calibration[1]);
if (calibVal < 0)
leftCalib = Math.abs(calibVal);
else
rightCalib = calibVal;
if (side == BBMotor.Left)
return leftCalib;
else
return rightCalib;
}
// New Style Motor Blocks
// slow PWM frequency for slower speeds to improve torque
function setPWM(speed: number): void
{
if (speed < 200)
pins.analogSetPeriod(AnalogPin.P0, 60000);
else if (speed < 300)
pins.analogSetPeriod(AnalogPin.P0, 40000);
else
pins.analogSetPeriod(AnalogPin.P0, 30000);
}
/**
* Move robot forward (or backward) at speed.
* @param direction Move Forward or Reverse
* @param speed speed of motor between 0 and 100. eg: 60
*/
//% blockId="BBGo" block="go%direction|at speed%speed|\\%"
//% speed.min=0 speed.max=100
//% weight=100
//% subcategory=Motors
export function go(direction: BBDirection, speed: number): void
{
move(BBMotor.Both, direction, speed);
}
/**
* Move robot forward (or backward) at speed for milliseconds
* @param direction Move Forward or Reverse
* @param speed speed of motor between 0 and 100. eg: 60
* @param milliseconds duration in milliseconds to drive forward for, then stop. eg: 400
*/
//% blockId="BBGoms" block="go%direction|at speed%speed|\\% for%milliseconds|ms"
//% speed.min=0 speed.max=100
//% weight=90
//% subcategory=Motors
export function goms(direction: BBDirection, speed: number, milliseconds: number): void
{
go(direction, speed);
basic.pause(milliseconds);
stop(BBStopMode.Coast);
}
/**
* Rotate robot in direction at speed
* @param direction direction to turn
* @param speed speed of motors (0 to 100). eg: 60
*/
//% blockId="BBRotate" block="spin%direction|at speed%speed|\\%"
//% speed.min=0 speed.max=100
//% weight=80
//% subcategory=Motors
export function rotate(direction: BBRobotDirection, speed: number): void
{
if (direction == BBRobotDirection.Left)
{
move(BBMotor.Left, BBDirection.Reverse, speed);
move(BBMotor.Right, BBDirection.Forward, speed);
}
else if (direction == BBRobotDirection.Right)
{
move(BBMotor.Left, BBDirection.Forward, speed);
move(BBMotor.Right, BBDirection.Reverse, speed);
}
}
/**
* Rotate robot in direction at speed for milliseconds.
* @param direction direction to spin
* @param speed speed of motor between 0 and 100. eg: 60
* @param milliseconds duration in milliseconds to spin for, then stop. eg: 400
*/
//% blockId="BBRotatems" block="spin%direction|at speed%speed|\\% for%milliseconds|ms"
//% speed.min=0 speed.max=100
//% weight=70
//% subcategory=Motors
export function rotatems(direction: BBRobotDirection, speed: number, milliseconds: number): void
{
rotate(direction, speed);
basic.pause(milliseconds);
stop(BBStopMode.Coast);
}
/**
* Stop robot by coasting slowly to a halt or braking
* @param mode Brakes on or off
*/
//% blockId="BBstop" block="stop with%mode"
//% weight=60
//% subcategory=Motors
export function stop(mode: BBStopMode): void
{
getModel();
let stopMode = 0;
if (mode == BBStopMode.Brake)
stopMode = 1;
pins.digitalWritePin(lMotorD0, stopMode);
pins.digitalWritePin(lMotorD1, stopMode);
pins.digitalWritePin(rMotorD0, stopMode);
pins.digitalWritePin(rMotorD1, stopMode);
}
function createCalib(speed: number): void
{
if (getVersionCode() == 5)
{
let calibVal = 0;
if (speed < 60)
calibVal = calibration[1] - ((60 - speed)/30) * (calibration[1] - calibration[0]);
else
calibVal = calibration[2] - ((90 - speed)/30) * (calibration[2] - calibration[1]);
leftCalib = 0;
rightCalib = 0;
if (calibVal < 0)
leftCalib = Math.abs(calibVal);
else
rightCalib = calibVal;
}
}
/**
* Move individual motors forward or reverse
* @param motor motor to drive
* @param direction select forwards or reverse
* @param speed speed of motor between 0 and 100. eg: 60
*/
//% blockId="BBMove" block="move%motor|motor(s)%direction|at speed%speed|\\%"
//% weight=50
//% speed.min=0 speed.max=100
//% subcategory=Motors
export function move(motor: BBMotor, direction: BBDirection, speed: number): void
{
let lSpeed = 0;
let rSpeed = 0;
getModel();
speed = clamp(speed, 0, 100);
createCalib(speed); // sets bias values for "DriveStraight" if available (versionCode == 5 only)
speed = speed * 10.23
setPWM(speed);
if (getVersionCode() == 5 && leftBias == 0 && rightBias == 0)
{
lSpeed = Math.round(speed * (100 - leftCalib) / 100);
rSpeed = Math.round(speed * (100 - rightCalib) / 100);
}
else
{
lSpeed = Math.round(speed * (100 - leftBias) / 100);
rSpeed = Math.round(speed * (100 - rightBias) / 100);
}
if ((motor == BBMotor.Left) || (motor == BBMotor.Both))
{
if (direction == BBDirection.Forward)
{
pins.analogWritePin(lMotorA0, lSpeed);
pins.analogWritePin(lMotorA1, 0);
}
else
{
pins.analogWritePin(lMotorA0, 0);
pins.analogWritePin(lMotorA1, lSpeed);
}
}
if ((motor == BBMotor.Right) || (motor == BBMotor.Both))
{
if (direction == BBDirection.Forward)
{
pins.analogWritePin(rMotorA0, rSpeed);
pins.analogWritePin(rMotorA1, 0);
}
else
{
pins.analogWritePin(rMotorA0, 0);
pins.analogWritePin(rMotorA1, rSpeed);
}
}
}
/**
* Set left/right bias to match motors
* @param direction direction to turn more (if robot goes right, set this to left)
* @param bias percentage of speed to bias with eg: 10
*/
//% blockId="BBBias" block="bias%direction|by%bias|\\%"
//% bias.min=0 bias.max=80
//% weight=40
//% subcategory=Motors
export function BBBias(direction: BBRobotDirection, bias: number): void
{
bias = clamp(bias, 0, 80);
if (direction == BBRobotDirection.Left)
{
leftBias = bias;
rightBias = 0;
}
else
{
leftBias = 0;
rightBias = bias;
}
}
// Old Motor Blocks - kept for compatibility
/**
* Drive motor(s) forward or reverse.
* @param motor motor to drive.
* @param speed speed of motor (-1023 to 1023). eg: 600
*/
//% blockId="bitbot_motor" block="drive%motor|motor(s) at speed%speed"
//% speed.min=-1023 speed.max=1023
//% weight=80
//% subcategory=Motors
//% group="Old style blocks"
//% blockGap=8
//% deprecated=true
export function motor(motor: BBMotor, speed: number): void
{
speed = clamp(speed, -1023, 1023);
let speed0 = 0;
let speed1 = 0;
setPWM(Math.abs(speed));
if (speed > 0)
{
speed0 = speed;
speed1 = 0;
}
else
{
speed0 = 0;
speed1 = 0 - speed;
}
if ((motor == BBMotor.Left) || (motor == BBMotor.Both))
{
if (getModel() == BBModel.Classic)
{
pins.analogWritePin(AnalogPin.P0, speed0);
pins.analogWritePin(AnalogPin.P8, speed1);
}
else
{
pins.analogWritePin(AnalogPin.P16, speed0);
pins.analogWritePin(AnalogPin.P8, speed1);
}
}
if ((motor == BBMotor.Right) || (motor == BBMotor.Both))
{
if (getModel() == BBModel.Classic)
{
pins.analogWritePin(AnalogPin.P1, speed0);
pins.analogWritePin(AnalogPin.P12, speed1);
}
else
{
pins.analogWritePin(AnalogPin.P14, speed0);
pins.analogWritePin(AnalogPin.P12, speed1);
}
}
}
/**
* Drive robot forward (or backward) at speed.
* @param speed speed of motor between -1023 and 1023. eg: 600
*/
//% blockId="bitbot_motor_forward" block="drive at speed%speed"
//% speed.min=-1023 speed.max=1023
//% weight=100
//% subcategory=Motors
//% group="Old style blocks"
//% blockGap=8
//% deprecated=true
export function drive(speed: number): void
{
motor(BBMotor.Both, speed);
}
/**
* Drive robot forward (or backward) at speed for milliseconds.
* @param speed speed of motor between -1023 and 1023. eg: 600
* @param milliseconds duration in milliseconds to drive forward for, then stop. eg: 400
*/
//% blockId="bitbot_motor_forward_milliseconds" block="drive at speed%speed| for%milliseconds|ms"
//% speed.min=-1023 speed.max=1023
//% weight=95
//% subcategory=Motors
//% group="Old style blocks"
//% blockGap=8
//% deprecated=true
export function driveMilliseconds(speed: number, milliseconds: number): void
{
drive(speed);
basic.pause(milliseconds);
stop(BBStopMode.Coast);
}
/**
* Turn robot in direction at speed.
* @param direction direction to turn.
* @param speed speed of motor between 0 and 1023. eg: 600
*/
//% blockId="bitbot_turn" block="spin%direction|at speed%speed"
//% speed.min=0 speed.max=1023
//% weight=90
//% subcategory=Motors
//% group="Old style blocks"
//% blockGap=8
//% deprecated=true
export function driveTurn(direction: BBRobotDirection, speed: number): void
{
if (speed < 0)
speed = 0;
if (direction == BBRobotDirection.Left)
{
motor(BBMotor.Left, -speed);
motor(BBMotor.Right, speed);
}
else if (direction == BBRobotDirection.Right)
{
motor(BBMotor.Left, speed);
motor(BBMotor.Right, -speed);
}
}
/**
* Spin robot in direction at speed for milliseconds.
* @param direction direction to turn.
* @param speed speed of motor between 0 and 1023. eg: 600
* @param milliseconds duration in milliseconds to turn for, then stop. eg: 400
*/
//% blockId="bitbot_turn_milliseconds" block="spin%direction|at speed%speed| fo %milliseconds|ms"
//% speed.min=0 speed.max=1023
//% weight=85
//% subcategory=Motors
//% group="Old style blocks"
//% blockGap=8
//% deprecated=true
export function driveTurnMilliseconds(direction: BBRobotDirection, speed: number, milliseconds: number): void
{
driveTurn(direction, speed)
basic.pause(milliseconds)
stop(BBStopMode.Coast);
}
// Inbuilt FireLed Blocks
// create a FireLed band if not got one already. Default to brightness 40
function fire(): fireled.Band
{
if (!fireBand)
{
fireBand = fireled.newBand(DigitalPin.P13, 12);
fireBand.setBrightness(40);
}
return fireBand;
}
// update FireLeds if _updateMode set to Auto
function updateLEDs(): void
{
if (_updateMode == BBMode.Auto)
ledShow();
}
/**
* Sets all LEDs to a given color (range 0-255 for r, g, b).
* @param rgb RGB color of the LED
*/
//% blockId="bitbot_set_led_color" block="set all LEDs to%rgb=bb_colours"
//% weight=100
//% subcategory=FireLeds
//% group=Basic
//% blockGap=8
export function setLedColor(rgb: number)
{
fire().setBand(rgb);
updateLEDs();
}
/**
* Clear all leds.
*/
//% blockId="bitbot_led_clear" block="clear all LEDs"
//% weight=90
//% subcategory=FireLeds
//% group=Basic
//% blockGap=8
export function ledClear(): void
{
fire().clearBand();
updateLEDs();
}
/**
* Set single LED to a given color (range 0-255 for r, g, b).
*
* @param ledId position of the LED (0 to 11)
* @param rgb RGB color of the LED
*/
//% blockId="bitbot_set_pixel_color" block="set LED at%ledId|to%rgb=bb_colours"
//% weight=80
//% subcategory=FireLeds
//% group=Basic
//% blockGap=8
export function setPixelColor(ledId: number, rgb: number): void
{
fire().setPixel(ledId, rgb);
updateLEDs();
}
/**
* Shows a rainbow pattern on all LEDs.
*/
//% blockId="bitbot_rainbow" block="set LED rainbow"
//% weight=70
//% subcategory=FireLeds
//% group=Basic
//% blockGap=8
export function ledRainbow(): void
{
fire().setRainbow();
updateLEDs()
}
/**
* Shift LEDs forward and clear with zeros.
*/
//% blockId="bitbot_led_shift" block="shift LEDs"
//% weight=60
//% subcategory=FireLeds
//% group=Basic
//% blockGap=8
export function ledShift(): void
{
fire().shiftBand();
updateLEDs()
}
/**
* Rotate LEDs forward.
*/
//% blockId="bitbot_led_rotate" block="rotate LEDs"
//% weight=50
//% subcategory=FireLeds
//% group=Basic
//% blockGap=8
export function ledRotate(): void
{
fire().rotateBand();
updateLEDs()
}
// Advanced blocks
/**
* Set the brightness of the LEDs
* @param brightness a measure of LED brightness in 0-255. eg: 40
*/
//% blockId="bitbot_led_brightness" block="set LED brightness%brightness"
//% brightness.min=0 brightness.max=255
//% weight=100
//% subcategory=FireLeds
//% group=Advanced
//% blockGap=8
export function ledBrightness(brightness: number): void
{
fire().setBrightness(brightness);
updateLEDs();
}
/**
* Set LED update mode (Manual or Automatic)
* @param updateMode setting automatic will show LED changes automatically
*/
//% blockId="bitbot_set_updateMode" block="set%updateMode|update mode"
//% weight=90
//% subcategory=FireLeds
//% group=Advanced
//% blockGap=8
export function setUpdateMode(updateMode: BBMode): void
{
_updateMode = updateMode;
}
/**
* Show LED changes
*/
//% blockId="BBledShow" block="show FireLed changes"
//% weight=80
//% subcategory=FireLeds
//% group=Advanced
//% blockGap=8
export function ledShow(): void
{
if (btDisabled)
fire().updateBand();
}
/**
* Get numeric value of colour
* @param color Standard RGB Led Colours eg: #ff0000
*/
//% blockId="bb_colours" block=%color
//% blockHidden=false
//% weight=70
//% subcategory=FireLeds
//% group=Advanced
//% blockGap=8