forked from lzty634158/SuperBit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
429 lines (372 loc) · 13.5 KB
/
main.ts
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
/*
Copyright (C): 2010-2019, Shenzhen Yahboom Tech
modified from liusen
load dependency
"SuperBit": "file:../pxt-Superbit"
*/
//% color="#ECA40D" weight=20 icon="\uf085"
namespace SuperBit {
const PCA9685_ADD = 0x40
const MODE1 = 0x00
const MODE2 = 0x01
const SUBADR1 = 0x02
const SUBADR2 = 0x03
const SUBADR3 = 0x04
const LED0_ON_L = 0x06
const LED0_ON_H = 0x07
const LED0_OFF_L = 0x08
const LED0_OFF_H = 0x09
const ALL_LED_ON_L = 0xFA
const ALL_LED_ON_H = 0xFB
const ALL_LED_OFF_L = 0xFC
const ALL_LED_OFF_H = 0xFD
const PRESCALE = 0xFE
const STP_CHA_L = 2047
const STP_CHA_H = 4095
const STP_CHB_L = 1
const STP_CHB_H = 2047
const STP_CHC_L = 1023
const STP_CHC_H = 3071
const STP_CHD_L = 3071
const STP_CHD_H = 1023
let initialized = false
let yahStrip: neopixel.Strip;
export enum enMusic {
dadadum = 0,
entertainer,
prelude,
ode,
nyan,
ringtone,
funk,
blues,
birthday,
wedding,
funereal,
punchline,
baddy,
chase,
ba_ding,
wawawawaa,
jump_up,
jump_down,
power_up,
power_down
}
export enum enSteppers {
B1 = 0x1,
B2 = 0x2
}
export enum enPos {
//% blockId="forward" block="forward"
forward = 1,
//% blockId="reverse" block="reverse"
reverse = 2,
//% blockId="stop" block="stop"
stop = 3
}
export enum enTurns {
//% blockId="T1B4" block="1/4"
T1B4 = 90,
//% blockId="T1B2" block="1/2"
T1B2 = 180,
//% blockId="T1B0" block="1"
T1B0 = 360,
//% blockId="T2B0" block="2"
T2B0 = 720,
//% blockId="T3B0" block="3"
T3B0 = 1080,
//% blockId="T4B0" block="4"
T4B0 = 1440,
//% blockId="T5B0" block="5"
T5B0 = 1800
}
export enum enServo {
S1 = 0,
S2,
S3,
S4,
S5,
S6,
S7,
S8
}
export enum enMotors {
M1 = 8,
M2 = 10,
M3 = 12,
M4 = 14
}
function i2cwrite(addr: number, reg: number, value: number) {
let buf = pins.createBuffer(2)
buf[0] = reg
buf[1] = value
pins.i2cWriteBuffer(addr, buf)
}
function i2ccmd(addr: number, value: number) {
let buf = pins.createBuffer(1)
buf[0] = value
pins.i2cWriteBuffer(addr, buf)
}
function i2cread(addr: number, reg: number) {
pins.i2cWriteNumber(addr, reg, NumberFormat.UInt8BE);
let val = pins.i2cReadNumber(addr, NumberFormat.UInt8BE);
return val;
}
function initPCA9685(): void {
i2cwrite(PCA9685_ADD, MODE1, 0x00)
setFreq(50);
initialized = true
}
function setFreq(freq: number): void {
// Constrain the frequency
let prescaleval = 25000000;
prescaleval /= 4096;
prescaleval /= freq;
prescaleval -= 1;
let prescale = prescaleval; //Math.Floor(prescaleval + 0.5);
let oldmode = i2cread(PCA9685_ADD, MODE1);
let newmode = (oldmode & 0x7F) | 0x10; // sleep
i2cwrite(PCA9685_ADD, MODE1, newmode); // go to sleep
i2cwrite(PCA9685_ADD, PRESCALE, prescale); // set the prescaler
i2cwrite(PCA9685_ADD, MODE1, oldmode);
control.waitMicros(5000);
i2cwrite(PCA9685_ADD, MODE1, oldmode | 0xa1);
}
function setPwm(channel: number, on: number, off: number): void {
if (channel < 0 || channel > 15)
return;
if (!initialized) {
initPCA9685();
}
let buf = pins.createBuffer(5);
buf[0] = LED0_ON_L + 4 * channel;
buf[1] = on & 0xff;
buf[2] = (on >> 8) & 0xff;
buf[3] = off & 0xff;
buf[4] = (off >> 8) & 0xff;
pins.i2cWriteBuffer(PCA9685_ADD, buf);
}
function setStepper(index: number, dir: boolean): void {
if (index == enSteppers.B1) {
if (dir) {
setPwm(11, STP_CHA_L, STP_CHA_H);
setPwm(9, STP_CHB_L, STP_CHB_H);
setPwm(10, STP_CHC_L, STP_CHC_H);
setPwm(8, STP_CHD_L, STP_CHD_H);
} else {
setPwm(8, STP_CHA_L, STP_CHA_H);
setPwm(10, STP_CHB_L, STP_CHB_H);
setPwm(9, STP_CHC_L, STP_CHC_H);
setPwm(11, STP_CHD_L, STP_CHD_H);
}
} else {
if (dir) {
setPwm(12, STP_CHA_L, STP_CHA_H);
setPwm(14, STP_CHB_L, STP_CHB_H);
setPwm(13, STP_CHC_L, STP_CHC_H);
setPwm(15, STP_CHD_L, STP_CHD_H);
} else {
setPwm(15, STP_CHA_L, STP_CHA_H);
setPwm(13, STP_CHB_L, STP_CHB_H);
setPwm(14, STP_CHC_L, STP_CHC_H);
setPwm(12, STP_CHD_L, STP_CHD_H);
}
}
}
function stopMotor(index: number) {
setPwm(index, 0, 0);
setPwm(index + 1, 0, 0);
}
/**
* *****************************************************************
* @param index
*/
//% blockId=SuperBit_RGB_Program block="RGB_Program"
//% weight=99
//% blockGap=10
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function RGB_Program(): neopixel.Strip {
if (!yahStrip) {
yahStrip = neopixel.create(DigitalPin.P12, 4, NeoPixelMode.RGB);
}
return yahStrip;
}
//% blockId=SuperBit_Music block="Music|%index"
//% weight=98
//% blockGap=10
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function Music(index: enMusic): void {
switch (index) {
case enMusic.dadadum: music.beginMelody(music.builtInMelody(Melodies.Dadadadum), MelodyOptions.Once); break;
case enMusic.birthday: music.beginMelody(music.builtInMelody(Melodies.Birthday), MelodyOptions.Once); break;
case enMusic.entertainer: music.beginMelody(music.builtInMelody(Melodies.Entertainer), MelodyOptions.Once); break;
case enMusic.prelude: music.beginMelody(music.builtInMelody(Melodies.Prelude), MelodyOptions.Once); break;
case enMusic.ode: music.beginMelody(music.builtInMelody(Melodies.Ode), MelodyOptions.Once); break;
case enMusic.nyan: music.beginMelody(music.builtInMelody(Melodies.Nyan), MelodyOptions.Once); break;
case enMusic.ringtone: music.beginMelody(music.builtInMelody(Melodies.Ringtone), MelodyOptions.Once); break;
case enMusic.funk: music.beginMelody(music.builtInMelody(Melodies.Funk), MelodyOptions.Once); break;
case enMusic.blues: music.beginMelody(music.builtInMelody(Melodies.Blues), MelodyOptions.Once); break;
case enMusic.wedding: music.beginMelody(music.builtInMelody(Melodies.Wedding), MelodyOptions.Once); break;
case enMusic.funereal: music.beginMelody(music.builtInMelody(Melodies.Funeral), MelodyOptions.Once); break;
case enMusic.punchline: music.beginMelody(music.builtInMelody(Melodies.Punchline), MelodyOptions.Once); break;
case enMusic.baddy: music.beginMelody(music.builtInMelody(Melodies.Baddy), MelodyOptions.Once); break;
case enMusic.chase: music.beginMelody(music.builtInMelody(Melodies.Chase), MelodyOptions.Once); break;
case enMusic.ba_ding: music.beginMelody(music.builtInMelody(Melodies.BaDing), MelodyOptions.Once); break;
case enMusic.wawawawaa: music.beginMelody(music.builtInMelody(Melodies.Wawawawaa), MelodyOptions.Once); break;
case enMusic.jump_up: music.beginMelody(music.builtInMelody(Melodies.JumpUp), MelodyOptions.Once); break;
case enMusic.jump_down: music.beginMelody(music.builtInMelody(Melodies.JumpDown), MelodyOptions.Once); break;
case enMusic.power_up: music.beginMelody(music.builtInMelody(Melodies.PowerUp), MelodyOptions.Once); break;
case enMusic.power_down: music.beginMelody(music.builtInMelody(Melodies.PowerDown), MelodyOptions.Once); break;
}
}
//% blockId=SuperBit_Servo block="Servo(180°)|num %num|value %value"
//% weight=97
//% blockGap=10
//% num.min=1 num.max=4 value.min=0 value.max=180
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=20
export function Servo(num: enServo, value: number): void {
// 50hz: 20,000 us
let us = (value * 1800 / 180 + 600); // 0.6 ~ 2.4
let pwm = us * 4096 / 20000;
setPwm(num, 0, pwm);
}
//% blockId=SuperBit_Servo2 block="Servo(270°)|num %num|value %value"
//% weight=96
//% blockGap=10
//% num.min=1 num.max=4 value.min=0 value.max=270
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=20
export function Servo2(num: enServo, value: number): void {
// 50hz: 20,000 us
let newvalue = Math.map(value, 0, 270, 0, 180);
let us = (newvalue * 1800 / 180 + 600); // 0.6 ~ 2.4
let pwm = us * 4096 / 20000;
setPwm(num, 0, pwm);
}
//% blockId=SuperBit_Servo3 block="Servo(360°)|num %num|pos %pos|value %value"
//% weight=96
//% blockGap=10
//% num.min=1 num.max=4 value.min=0 value.max=90
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=20
export function Servo3(num: enServo, pos: enPos, value: number): void {
// 50hz: 20,000 us
if (pos == enPos.stop) {
let us = (86 * 1800 / 180 + 600); // 0.6 ~ 2.4
let pwm = us * 4096 / 20000;
setPwm(num, 0, pwm);
}
else if(pos == enPos.forward){ //0-90 -> 90 - 0
let us = ((90-value) * 1800 / 180 + 600); // 0.6 ~ 2.4
let pwm = us * 4096 / 20000;
setPwm(num, 0, pwm);
}
else if(pos == enPos.reverse){ //0-90 -> 90 -180
let us = ((90+value) * 1800 / 180 + 600); // 0.6 ~ 2.4
let pwm = us * 4096 / 20000;
setPwm(num, 0, pwm);
}
}
//% blockId=SuperBit_MotorRun block="Motor|%index|speed(-255~255) %speed"
//% weight=93
//% speed.min=-255 speed.max=255
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function MotorRun(index: enMotors, speed: number): void {
if (!initialized) {
initPCA9685()
}
speed = speed * 16; // map 255 to 4096
if (speed >= 4096) {
speed = 4095
}
if (speed <= -4096) {
speed = -4095
}
let a = index
let b = index + 1
if (a > 10)
{
if (speed >= 0) {
setPwm(a, 0, speed)
setPwm(b, 0, 0)
} else {
setPwm(a, 0, 0)
setPwm(b, 0, -speed)
}
}
else {
if (speed >= 0) {
setPwm(b, 0, speed)
setPwm(a, 0, 0)
} else {
setPwm(b, 0, 0)
setPwm(a, 0, -speed)
}
}
}
//% blockId=SuperBit_MotorRunDual block="Motor|%motor1|speed %speed1|%motor2|speed %speed2"
//% weight=92
//% blockGap=50
//% speed1.min=-255 speed1.max=255
//% speed2.min=-255 speed2.max=255
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=2
export function MotorRunDual(motor1: enMotors, speed1: number, motor2: enMotors, speed2: number): void {
MotorRun(motor1, speed1);
MotorRun(motor2, speed2);
}
//% blockId=SuperBit_StepperDegree block="Stepper Motor(28BYJ-48) |%index|degree %degree"
//% weight=90
export function StepperDegree(index: enSteppers, degree: number): void {
if (!initialized) {
initPCA9685()
}
setStepper(index, degree > 0);
degree = Math.abs(degree);
basic.pause(10240 * degree / 360);
MotorStopAll()
}
//% blockId=SuperBit_MotorStopAll block="Motor Stop All"
//% weight=91
//% blockGap=50
export function MotorStopAll(): void {
if (!initialized) {
initPCA9685()
}
stopMotor(enMotors.M1);
stopMotor(enMotors.M2);
stopMotor(enMotors.M3);
stopMotor(enMotors.M4);
}
//% blockId=SuperBit_StepperTurn block="Stepper Motor(28BYJ-48) |%index|turn %turn|circle"
//% weight=89
export function StepperTurn(index: enSteppers, turn: enTurns): void {
let degree = turn;
StepperDegree(index, degree);
}
//% blockId=SuperBit_StepperDual block="Dual Stepper Motor(Degree) |M1 %degree1| M2 %degree2"
//% weight=88
export function StepperDual(degree1: number, degree2: number): void {
if (!initialized) {
initPCA9685()
}
setStepper(1, degree1 > 0);
setStepper(2, degree2 > 0);
degree1 = Math.abs(degree1);
degree2 = Math.abs(degree2);
basic.pause(10240 * Math.min(degree1, degree2) / 360);
if (degree1 > degree2) {
stopMotor(enMotors.M3);
stopMotor(enMotors.M4);
basic.pause(10240 * (degree1 - degree2) / 360);
} else {
stopMotor(enMotors.M1);
stopMotor(enMotors.M2);
basic.pause(10240 * (degree2 - degree1) / 360);
}
MotorStopAll()
}
//% blockId=SuperBit_PWMOFF block="PWM OFF|%index"
//% weight=87
export function PWMOFF(index: number): void {
setPwm(index, 0, 0);
}
}