-
Notifications
You must be signed in to change notification settings - Fork 1
/
extendbit-2.ts
297 lines (263 loc) · 8.49 KB
/
extendbit-2.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
/*
* @file extendbit-2
* @brief Laplus's extendbit makecode library.
* @support of 4-channel motor drive and 2-channel stepper drive.on board have light,Sound Sensor and 3pcs ws2812.
* @more info see:(http://wiki.labplus.cn/index.php?title=Extend:bitⅡ)
* @copyright [Labplus](http://wiki.labplus.com), 2018
* @copyright GNU Lesser General Public License
* @author [tangliufeng]
* @date 2018.4.12
*/
/**
* Functions for extendbit-2
*/
//% weight=10 color=#FF00FF icon="\uf282" block="extendbit-2"
namespace extendbit_2 {
const PCA9685_ADDRESS = 0x41
const MODE1 = 0x00
const MODE2 = 0x01
const SUBADR1 = 0x02
const SUBADR2 = 0x03
const SUBADR3 = 0x04
const PRESCALE = 0xFE
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 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
// HT16K33 commands
const HT16K33_ADDRESS = 0x70
const HT16K33_BLINK_CMD = 0x80
const HT16K33_BLINK_DISPLAYON = 0x01
const HT16K33_BLINK_OFF = 0
const HT16K33_BLINK_2HZ = 1
const HT16K33_BLINK_1HZ = 2
const HT16K33_BLINK_HALFHZ = 3
const HT16K33_CMD_BRIGHTNESS = 0xE0
//pwm freq
// Servos define
export enum Servos {
S1 = 0x01,
S2 = 0x02,
S3 = 0x03,
S4 = 0x04
}
// Motors define
export enum Motors {
M1 = 0x2,
M2 = 0x1,
M3 = 0x4,
M4 = 0x3
}
// Steppers define
export enum Steppers {
STP_M1 = 0x1,
STP_M2 = 0x2
}
let initialized = false;
let matBuf = pins.createBuffer(17);
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 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_ADDRESS, MODE1);
let newmode = (oldmode & 0x7F) | 0x10; // sleep
i2cwrite(PCA9685_ADDRESS, MODE1, newmode); // go to sleep
i2cwrite(PCA9685_ADDRESS, PRESCALE, prescale); // set the prescaler
i2cwrite(PCA9685_ADDRESS, MODE1, oldmode);
control.waitMicros(5000);
i2cwrite(PCA9685_ADDRESS, MODE1, oldmode | 0xa1);
}
function setPwm(channel: number, on: number, off: number): void {
if (channel < 0 || channel > 15)
return;
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_ADDRESS, buf);
}
function initPCA9685(): void {
i2cwrite(PCA9685_ADDRESS, MODE1, 0x00)
setFreq(50);
for (let idx = 0; idx < 16; idx++) {
setPwm(idx, 0, 0);
}
initialized = true
}
function setStepper(index: number, dir: boolean): void {
if (index == 1) {
if (dir) {
setPwm(0, STP_CHA_L, STP_CHA_H);
setPwm(2, STP_CHB_L, STP_CHB_H);
setPwm(1, STP_CHC_L, STP_CHC_H);
setPwm(3, STP_CHD_L, STP_CHD_H);
} else {
setPwm(3, STP_CHA_L, STP_CHA_H);
setPwm(1, STP_CHB_L, STP_CHB_H);
setPwm(2, STP_CHC_L, STP_CHC_H);
setPwm(0, STP_CHD_L, STP_CHD_H);
}
} else {
if (dir) {
setPwm(4, STP_CHA_L, STP_CHA_H);
setPwm(6, STP_CHB_L, STP_CHB_H);
setPwm(5, STP_CHC_L, STP_CHC_H);
setPwm(7, STP_CHD_L, STP_CHD_H);
} else {
setPwm(7, STP_CHA_L, STP_CHA_H);
setPwm(5, STP_CHB_L, STP_CHB_H);
setPwm(6, STP_CHC_L, STP_CHC_H);
setPwm(4, STP_CHD_L, STP_CHD_H);
}
}
}
function stopMotor(index: number) {
setPwm((index - 1) * 2, 0, 0);
setPwm((index - 1) * 2 + 1, 0, 0);
}
/**
* S1-S4 Servo Execute,0-180 degree
* @param index Servo Channel; eg: S1
* @param degree [0-180] degree of servo; eg: 0,90,180
*/
//% blockId=extendbit_servo block="Servo|%index|degree %degree"
//% weight=70
//% blockGap=35
//% degree.min=0 degree.max=180
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function Servo(index: Servos, degree: number): void {
if (!initialized) {
initPCA9685()
}
// 50hz: 20,000 us
let v_us = (degree * 1800 / 180 + 600) // 0.6 ~ 2.4
let value = v_us * 4096 / 20000
setPwm(index + 7, 0, value)
}
/**
* extendbit M1-M4 Motors driver ,speed:-255~255.
* @param speed -255~255; eg:255
*/
//% blockId=extenbit_motor_run block="MotorRun|%index|speed %speed"
//% weight=88
//% blockGap=15
//% speed.min=-255 speed.max=255
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function MotorRun(index: Motors, speed: number): void {
if (!initialized) {
initPCA9685()
}
speed = speed * 16; // map 255 to 4096
if (speed >= 4096) {
speed = 4095
}
if (speed <= -4096) {
speed = -4095
}
if (index > 4 || index <= 0)
return
let pp = (index - 1) * 2
let pn = (index - 1) * 2 + 1
if (speed >= 0) {
setPwm(pp, 0, speed)
setPwm(pn, 0, 0)
} else {
setPwm(pp, 0, 0)
setPwm(pn, 0, -speed)
}
}
/**
* stepper driver:When the angle is positive, turn clockwise, if negative, turn counterclockwise.
* @param index steppers Channel; eg: 1
* @param degr degree of steppers; eg: 180
*/
//% blockGap=50
//% blockId=stepper_degree block="Stepper 28BYJ-48|%index|degree %degree"
//% weight=69
export function StepperDegree(index: Steppers, degree: number): void {
if (!initialized) {
initPCA9685()
}
setStepper(index, degree > 0);
degree = Math.abs(degree);
basic.pause(10240 * degree / 360);
MotorStopAll()
}
//% blockId=extendbit_motor_stop block="Motor Stop|%index|"
//% weight=85 blockGap=15
export function MotorStop(index: Motors): void {
MotorRun(index, 0);
}
/**
* stop all Motor
*/
//% blockId=extendbit_stop_all block="Motor Stop All"
//% weight=80
//% blockGap=50
export function MotorStopAll(): void {
for (let idx = 1; idx <= 4; idx++) {
stopMotor(idx);
}
}
/**
* Notice:Install the buzzer jump cap before use !
* buzzer Emits a Pulse-width modulation (PWM) signal to the pin0. More advanced application,can use music module.
* @param frequency frequency to modulate in Hz. eg:1000
* @param ms duration of the pitch in milli seconds. eg:1000
*/
//% blockId=buzzer_pitch block="buzzer pitch %frequency|for (ms) %ms"
//% weight=60 blockGap=15
export function BuzzerPitch(frequency: number, ms: number): void {
pins.analogSetPitchPin(AnalogPin.P0);
pins.analogPitch(frequency, ms);
}
/**
* Notice:Install the light sensor jump cap before use !
*/
//% weight=50 blockGap=15
//% blockId=light_Value block="light sensor value"
export function lightValue():number {
return pins.analogReadPin(AnalogPin.P1);
}
/**
* Notice:Install the Sound sensor jump cap before use !
*/
//% weight=40 blockGap=15
//% blockId=Sound_Value block="Sound sensor value"
export function SoundValue():number {
return pins.analogReadPin(AnalogPin.P2);
}
}