-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
114 lines (102 loc) · 3.35 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
//% weight=100 color=#2b2b2b icon="\uf11b" block="solder:bit Gamepad"
//% groups="['Buttons', 'NeoPixels']"
namespace gamepad {
// Pins setup
let serialOut = DigitalPin.P0;
let parallelLoad = DigitalPin.P1;
let clock = DigitalPin.P2;
// Constants
const NUM_BUTTONS = 8;
const DEBOUNCE_INTERVAL = 50; // Debounce time in milliseconds
// Variables to track button state and debounce
let lastButtonStates = 0;
let lastDebounceTime = control.millis();
export enum GamepadButton {
//% block="right trigger"
RightBumper = 0, // 0b00000001
//% block="left trigger"
LeftBumper = 1, // 0b00000010
//% block="right"
Right = 2, // 0b00000100
//% block="up"
Up = 3, // 0b00001000
//% block="left"
Left = 4, // 0b00010000
//% block="down"
Down = 5, // 0b00100000
//% block="Y"
Y = 6, // 0b01000000
//% block="X"
X = 7, // 0b10000000
}
let strip = neopixel.create(DigitalPin.P1, 5, NeoPixelMode.RGB);
strip.clear();
strip.show();
//% block="Gamepad pixel array"
//% group="NeoPixels"
export function solderbitPixels(): neopixel.Strip {
return strip;
}
//% block="is Gamepad button $button pressed"
//% group="Buttons"
export function isButtonPressed(button: GamepadButton): boolean {
let buttonStates = readShiftRegister();
return (buttonStates & (1 << button)) !== 0;
}
function readShiftRegister(): number {
pins.digitalWritePin(parallelLoad, 0);
control.waitMicros(5);
pins.digitalWritePin(parallelLoad, 1);
let buttonStates = 0;
for (let i = 0; i < NUM_BUTTONS; i++) {
pins.digitalWritePin(clock, 0);
control.waitMicros(2); // Clock pulse width
if (pins.digitalReadPin(serialOut) === 1) {
buttonStates |= 1 << (NUM_BUTTONS - 1 - i);
}
pins.digitalWritePin(clock, 1);
control.waitMicros(2); // Ensure clock high time
}
return buttonStates;
}
// Event handlers storage
let buttonPressHandlers: { [key: number]: () => void } = {};
let buttonReleaseHandlers: { [key: number]: () => void } = {};
//% block="on Gamepad button $button pressed"
//% group="Buttons"
export function onButtonPressed(button: GamepadButton, handler: () => void) {
buttonPressHandlers[button] = handler;
}
//% block="on Gamepad button $button released"
//% group="Buttons"
export function onButtonReleased(button: GamepadButton, handler: () => void) {
buttonReleaseHandlers[button] = handler;
}
// Monitoring button state changes
control.inBackground(() => {
while (true) {
let currentMillis = control.millis();
if (currentMillis - lastDebounceTime > DEBOUNCE_INTERVAL) {
let newButtonStates = readShiftRegister();
if (newButtonStates !== lastButtonStates) {
for (let i = 0; i < NUM_BUTTONS; i++) {
let mask = 1 << i;
if ((newButtonStates & mask) !== (lastButtonStates & mask)) {
if ((newButtonStates & mask) !== 0 && buttonPressHandlers[i]) {
buttonPressHandlers[i]();
} else if (
(newButtonStates & mask) === 0 &&
buttonReleaseHandlers[i]
) {
buttonReleaseHandlers[i]();
}
}
}
lastButtonStates = newButtonStates;
}
lastDebounceTime = currentMillis;
}
basic.pause(10);
}
});
}