-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMotorEncoder.ts
84 lines (78 loc) · 2.54 KB
/
MotorEncoder.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
let encA: DigitalPin;
let encB: DigitalPin;
let position = 0;
/**
* Użyj tego pliku, aby zdefiniować niestandardowe funkcje i bloki.
* Czytaj więcej na https://makecode.microbit.org/blocks/custom
*/
enum MyEnum {
//% block="one"
One,
//% block="two"
Two
}
/**
* Custom blocks
*/
//% weight=100 color=#0fbc11 icon=""
namespace MotorEncoder {
/**
* TODO: describe your function here
* @param n describe parameter here, eg: 5
* @param s describe parameter here, eg: "Hello"
* @param e describe parameter here
*/
//% block
export function foo(n: number, s: string, e: MyEnum): void {
// Add code here
}
/**
* TODO: describe your function here
* @param value describe value here, eg: 5
*/
//% block
export function fib(value: number): number {
return value <= 1 ? value : fib(value - 1) + fib(value - 2);
}
/**
* rotary encoder was rotated.
*/
//% blockId=rotary_ky_rotated_left_event
//% block="on rotated |%dir"
export function onRotateEvent(dir: RotationDirection, body: () => void): void {
serial.setBaudRate(115200);
if (dir == RotationDirection.Left) control.onEvent(rotatedLeftID, dir, body);
if (dir == RotationDirection.Right) control.onEvent(rotatedRightID, dir, body);
control.inBackground(() => {
while (true) {
const riValue = pins.digitalReadPin(encA);
const dvValue = pins.digitalReadPin(encB);
if (riValue == 1 && dvValue == 1) rotateReady = true;
else if (rotateReady) {
if (riValue == 1 && dvValue == 0) {
serial.writeLine("Right!");
position += 1;
rotateReady = false;
control.raiseEvent(rotatedRightID, RotationDirection.Right);
}
else if (riValue == 0 && dvValue == 1) {
serial.writeLine("Left!")
position -= 1;
rotateReady = false;
control.raiseEvent(rotatedLeftID, RotationDirection.Left);
}
}
basic.pause(5);
}
})
}
/**
* initialises local variables and enables the rotary encoder.
*/
//% block="Connect Encoder Pin A %pinA|Pin B %pinB"
//% icon="\uf1ec"
export function init(pinA: DigitalPin, pinB: DigitalPin,): void {
encA = pinA;
encB = pinB;
}
}