-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRotaryEncoder.ts
106 lines (89 loc) · 3.23 KB
/
RotaryEncoder.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
const enum JoyPiAdvancedDirection {
//% block="Right"
clockwise = 2,
//% block="Left"
counterclockwise = 4
}
namespace JoyPiAdvanced {
const rotaryDTPin = DigitalPin.P2;
const rotaryCLKPin = DigitalPin.P3;
const rotarySWPin = DigitalPin.P4;
const KYEventID = 3100;
let directionIndicator = 1;
let currentCLK = 0;
let currentDT = 0;
let lastCLK = 0;
let EvCounter = 1;
let lastPressed = 1;
let pressedID = 5600;
// Function to decide the direction in which the Encoder is being turned
function RotaryEncoder() {
if (currentCLK != lastCLK) {
if (currentDT != currentCLK) {
directionIndicator = 1;
}
else {
directionIndicator = 0;
}
EvCounter += 1;
if (EvCounter % 2 == 0) { // kill every second Event
if (directionIndicator == 1) {
control.raiseEvent(KYEventID + JoyPiAdvancedDirection .clockwise, JoyPiAdvancedDirection .clockwise);
}
else {
control.raiseEvent(KYEventID + JoyPiAdvancedDirection .counterclockwise, JoyPiAdvancedDirection .counterclockwise);
}
}
lastCLK = currentCLK;
}
}
/**
* Initializes the rotary encoder
*/
//% block="initialize Rotary Encoder"
//% subcategory="Rotary Encoder"
//% weight=100
export function initializeRotaryEncoder() {
led.enable(false)
pins.setPull(rotaryDTPin, PinPullMode.PullUp);
pins.setPull(rotarySWPin, PinPullMode.PullUp);
// Interrupt the code on a rising edge on the rotaryCLKPin to execute the RotaryEncoder() function
pins.onPulsed(rotaryCLKPin, PulseValue.High, function () {
currentCLK = 1
RotaryEncoder()
})
// Interrupt the code on a falling edge on the rotaryCLKPin to execute the RotaryEncoder() function
pins.onPulsed(rotaryCLKPin, PulseValue.Low, function () {
currentCLK = 0
RotaryEncoder()
})
pins.onPulsed(rotaryDTPin, PulseValue.High, function () {
currentDT = 1
})
// Interrupt the code on a falling edge on the rotaryCLKPin to execute the RotaryEncoder() function
pins.onPulsed(rotaryDTPin, PulseValue.Low, function () {
currentDT = 0
})
}
/**
* Event that is executed as soon as the rotary encoder is turned in the corresponding direction
* @param JoyPiAdvancedDirection Direction to be listened to
*/
//% block="on rotary encoder turned in direction %direction"
//% subcategory="Rotary Encoder"
//% weight=100
export function rotaryEncoderonTurned(direction: JoyPiAdvancedDirection , handler: () => void) {
control.onEvent(KYEventID + direction, direction, handler);
}
/**
* Event that is executed as soon as the rotary encoder is pressed
*/
//% block="on rotary encoder pressed"
//% subcategory="Rotary Encoder"
//% weight=90
export function rotaryEncoderonPressEvent(handler: () => void) {
pins.onPulsed(rotarySWPin, PulseValue.Low, function () {
handler()
})
}
}