Skip to content

Commit

Permalink
Add brightness
Browse files Browse the repository at this point in the history
  • Loading branch information
mac-aron committed Jul 11, 2024
1 parent a4391e4 commit 687985e
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ namespace gamepad {
// Enum for button mapping
export enum Button {
//% block="right trigger"
RightBumper = 1 << 0,
RightBumper = 1 << 0, // 0b00000001
//% block="left trigger"
LeftBumper = 1 << 1,
LeftBumper = 1 << 1, // 0b00000010
//% block="right"
Right = 1 << 2,
Right = 1 << 2, // 0b00000100
//% block="up"
Up = 1 << 3,
Up = 1 << 3, // 0b00001000
//% block="left"
Left = 1 << 4,
Left = 1 << 4, // 0b00010000
//% block="down"
Down = 1 << 5,
Down = 1 << 5, // 0b00100000
//% block="Y"
Y = 1 << 6,
Y = 1 << 6, // 0b01000000
//% block="X"
X = 1 << 7
X = 1 << 7, // 0b10000000
}

const strip = neopixel.create(DigitalPin.P1, 5, NeoPixelMode.RGB);
Expand All @@ -44,7 +44,19 @@ namespace gamepad {
return strip;
}

//% block="button $button is pressed"
/**
* Clears all color data for the pixels, and turns them off
*/
//% block="turn off all Gamepad NeoPixels"
//% group="NeoPixels
export function clearAllGamepadPixels() {
led.enable(false);
strip.clear();
strip.show();
led.enable(true);
}

//% block="is Gamepad button $button pressed"
//% group="Buttons"
export function isButtonPressed(button: Button): boolean {
let buttonStates = readShiftRegister();
Expand All @@ -61,7 +73,7 @@ namespace gamepad {
pins.digitalWritePin(clock, 0);
control.waitMicros(2); // Clock pulse width
if (pins.digitalReadPin(serialOut) === 1) {
buttonStates |= (1 << i);
buttonStates |= 1 << i;
}
pins.digitalWritePin(clock, 1);
control.waitMicros(2); // Ensure clock high time
Expand All @@ -73,13 +85,13 @@ namespace gamepad {
let buttonPressHandlers: { [key: number]: () => void } = {};
let buttonReleaseHandlers: { [key: number]: () => void } = {};

//% block="on button $button pressed"
//% block="on Gamepad button $button pressed"
//% group="Buttons"
export function onButtonPressed(button: Button, handler: () => void) {
buttonPressHandlers[button] = handler;
}

//% block="on button $button released"
//% block="on Gamepad button $button released"
//% group="Buttons"
export function onButtonReleased(button: Button, handler: () => void) {
buttonReleaseHandlers[button] = handler;
Expand Down

0 comments on commit 687985e

Please sign in to comment.