Skip to content

Commit

Permalink
Updates per support.microbit.org/support/tickets/55867
Browse files Browse the repository at this point in the history
  • Loading branch information
bsiever committed Aug 19, 2022
1 parent a16acf5 commit 3880a50
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 74 deletions.
48 changes: 34 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Clicks
# Button Clicks

This extension allows expands the behaviors supported by the A & B buttons. It supports (mutually exclusive) detection of a single click of a button, a double click of a button, or holding a button down.

Expand All @@ -10,27 +10,47 @@ They are different than ``[input.onButtonPressed()]``, which detects when a butt
# Single Button Clicks

```sig
clicks.onButtonSingleClicked(button: Button, body: Action) : void
buttonClicks.onButtonSingleClicked(button: buttonClicks.AorB, body: Action) : void
```

Set the actions to do on a single click. *NOTE: Only button A and button B are supported separately. Button AB will not work.*

# Double Button Clicks

```sig
clicks.onButtonDoubleClicked(button: Button, body: Action) : void
buttonClicks.onButtonDoubleClicked(button: buttonClicks.AorB, body: Action) : void
```

Set the actions to do on a double click. *NOTE: Only button A and button B are supported separately. Button AB will not work.*

# Holding buttons (Long Clicks)

```sig
clicks.onButtonHeld(button: Button, body: Action) : void
buttonClicks.onButtonHeld(button: buttonClicks.AorB, body: Action) : void
```

Set the actions to do while the button is held down.

**Hold the button will cause this event to happen repeated while the button is held**. Set the actions to do on a long click. *NOTE: Only button A and button B are supported separately. Button AB will not work.*


# Button Down

```sig
buttonClicks.onButtonDown(button: buttonClicks.AorB, body: Action) : void
```

Set the actions to do when the button first makes contact when being pressed. This will run before other events, like single click, double click, and long click.


# Button Up

```sig
buttonClicks.onButtonUp(button: buttonClicks.AorB, body: Action) : void
```

Set the actions to do when the button is released. This will run before before events, like single click, double click, and long click.

# Example

The following program will show the behavior on both the LED grid and the serial console.
Expand All @@ -47,7 +67,7 @@ The following program will show the behavior on both the LED grid and the serial
* Holding a button will be shown by lighting all five LEDs.

```block
buttonClicks.onButtonSingleClicked(Button.A, function () {
buttonClicks.onButtonSingleClicked(buttonClicks.AorB.A, function () {
serial.writeLine("A single")
basic.showLeds(`
# . . . .
Expand All @@ -59,7 +79,7 @@ buttonClicks.onButtonSingleClicked(Button.A, function () {
showClear()
})
buttonClicks.onButtonDoubleClicked(Button.A, function () {
buttonClicks.onButtonDoubleClicked(buttonClicks.AorB.A, function () {
serial.writeLine("A double")
basic.showLeds(`
# . . . .
Expand All @@ -71,7 +91,7 @@ buttonClicks.onButtonDoubleClicked(Button.A, function () {
showClear()
})
buttonClicks.onButtonHeld(Button.A, function () {
buttonClicks.onButtonHeld(buttonClicks.AorB.A, function () {
serial.writeLine("A held")
basic.showLeds(`
# . . . .
Expand All @@ -83,18 +103,18 @@ buttonClicks.onButtonHeld(Button.A, function () {
showClear()
})
buttonClicks.onButtonDown(Button.A, function () {
buttonClicks.onButtonDown(buttonClicks.AorB.A, function () {
serial.writeLine("A down")
led.toggle(0, 1)
})
buttonClicks.onButtonUp(Button.A, function () {
buttonClicks.onButtonUp(buttonClicks.AorB.A, function () {
serial.writeLine("A up")
led.toggle(0, 1)
})
buttonClicks.onButtonSingleClicked(Button.B, function () {
buttonClicks.onButtonSingleClicked(buttonClicks.AorB.B, function () {
serial.writeLine("B single")
basic.showLeds(`
. . . . #
Expand All @@ -110,7 +130,7 @@ function showClear() {
basic.clearScreen()
}
buttonClicks.onButtonDoubleClicked(Button.B, function () {
buttonClicks.onButtonDoubleClicked(buttonClicks.AorB.B, function () {
serial.writeLine("B double")
basic.showLeds(`
. . . . #
Expand All @@ -122,7 +142,7 @@ buttonClicks.onButtonDoubleClicked(Button.B, function () {
showClear()
})
buttonClicks.onButtonHeld(Button.B, function () {
buttonClicks.onButtonHeld(buttonClicks.AorB.B, function () {
serial.writeLine("B held")
basic.showLeds(`
. . . . #
Expand All @@ -134,11 +154,11 @@ buttonClicks.onButtonHeld(Button.B, function () {
showClear()
})
buttonClicks.onButtonDown(Button.B, function () {
buttonClicks.onButtonDown(buttonClicks.AorB.B, function () {
serial.writeLine("B down")
led.toggle(4, 1)
})
buttonClicks.onButtonUp(Button.B, function () {
buttonClicks.onButtonUp(buttonClicks.AorB.B, function () {
serial.writeLine("B up")
led.toggle(4, 1)
})
Expand Down
95 changes: 45 additions & 50 deletions clicks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ let lastClickEnd = [0, 0, 0, 0]
let lastPressedStart = [0, 0, 0, 0]
let inLongClick = [false, false, false, false]

export enum AorB { // Thanks Martin Williams / https://support.microbit.org/support/tickets/55867
A = 1,
B = 2
}

// Array of handlers
let actions : [[Action]] = [
null,
Expand Down Expand Up @@ -90,59 +95,49 @@ loops.everyInterval(singleClickCheckTime, function() {
}
}
})
// Register Handlers
control.onEvent(EventBusSource.MICROBIT_ID_BUTTON_A,
EventBusValue.MICROBIT_BUTTON_EVT_DOWN, () => button(Button.A))
control.onEvent(EventBusSource.MICROBIT_ID_BUTTON_A,
EventBusValue.MICROBIT_BUTTON_EVT_UP, () => button(Button.A))
control.onEvent(EventBusSource.MICROBIT_ID_BUTTON_B,
EventBusValue.MICROBIT_BUTTON_EVT_DOWN, () => button(Button.B))
control.onEvent(EventBusSource.MICROBIT_ID_BUTTON_B,
EventBusValue.MICROBIT_BUTTON_EVT_UP, () => button(Button.B))

//% blockId=onButtonSingleClicked block="on button |%NAME single clicked"
//% weight=50
export function onButtonSingleClicked(button: Button, body: Action) {
if (button < Button.AB) {
let buttonHandlers = actions.get(button)
buttonHandlers.set(SINGLECLICK, body)
}
}
// Register Handlers
control.onEvent(EventBusSource.MICROBIT_ID_BUTTON_A,
EventBusValue.MICROBIT_BUTTON_EVT_DOWN, () => button(Button.A))
control.onEvent(EventBusSource.MICROBIT_ID_BUTTON_A,
EventBusValue.MICROBIT_BUTTON_EVT_UP, () => button(Button.A))
control.onEvent(EventBusSource.MICROBIT_ID_BUTTON_B,
EventBusValue.MICROBIT_BUTTON_EVT_DOWN, () => button(Button.B))
control.onEvent(EventBusSource.MICROBIT_ID_BUTTON_B,
EventBusValue.MICROBIT_BUTTON_EVT_UP, () => button(Button.B))

//% blockId=onButtonSingleClicked block="on button |%NAME single clicked"
//% weight=100
export function onButtonSingleClicked(button: AorB, body: Action) {
let buttonHandlers = actions.get(button)
buttonHandlers.set(SINGLECLICK, body)
}

//% blockId=onButtonDoubleClicked block="on button |%NAME double clicked "
//% weight=25
export function onButtonDoubleClicked(button: Button, body: Action) {
if(button < Button.AB) {
let buttonHandlers = actions.get(button)
buttonHandlers.set(DOUBLECLICK, body)
}
}
//% blockId=onButtonDoubleClicked block="on button |%NAME double clicked "
//% weight=75
export function onButtonDoubleClicked(button: AorB, body: Action) {
let buttonHandlers = actions.get(button)
buttonHandlers.set(DOUBLECLICK, body)
}

//% blockId=onButtonHeld block="on button |%NAME held"
//% weight=10
export function onButtonHeld(button: Button, body: Action) {
if (button < Button.AB) {
let buttonHandlers = actions.get(button)
buttonHandlers.set(LONGCLICK, body)
}
}
//% blockId=onButtonHeld block="on button |%NAME held"
//% weight=50
export function onButtonHeld(button: AorB, body: Action) {
let buttonHandlers = actions.get(button)
buttonHandlers.set(LONGCLICK, body)
}


//% blockId=onButtonDown block="on button |%NAME down "
//% weight=25
export function onButtonDown(button: Button, body: Action) {
if (button < Button.AB) {
let buttonHandlers = actions.get(button)
buttonHandlers.set(BUTTONDOWN, body)
}
}
//% blockId=onButtonDown block="on button |%NAME down "
//% weight=25
export function onButtonDown(button: AorB, body: Action) {
let buttonHandlers = actions.get(button)
buttonHandlers.set(BUTTONDOWN, body)
}

//% blockId=onButtonUp block="on button |%NAME up "
//% weight=25
export function onButtonUp(button: Button, body: Action) {
if (button < Button.AB) {
let buttonHandlers = actions.get(button)
buttonHandlers.set(BUTTONUP, body)
}
}
//% blockId=onButtonUp block="on button |%NAME up "
//% weight=10
export function onButtonUp(button: AorB, body: Action) {
let buttonHandlers = actions.get(button)
buttonHandlers.set(BUTTONUP, body)
}
}
20 changes: 10 additions & 10 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
buttonClicks.onButtonSingleClicked(Button.A, function () {
buttonClicks.onButtonSingleClicked(buttonClicks.AorB.A, function () {
serial.writeLine("A single")
basic.showLeds(`
# . . . .
Expand All @@ -10,7 +10,7 @@ buttonClicks.onButtonSingleClicked(Button.A, function () {
showClear()
})

buttonClicks.onButtonDoubleClicked(Button.A, function () {
buttonClicks.onButtonDoubleClicked(buttonClicks.AorB.A, function () {
serial.writeLine("A double")
basic.showLeds(`
# . . . .
Expand All @@ -22,7 +22,7 @@ buttonClicks.onButtonDoubleClicked(Button.A, function () {
showClear()
})

buttonClicks.onButtonHeld(Button.A, function () {
buttonClicks.onButtonHeld(buttonClicks.AorB.A, function () {
serial.writeLine("A held")
basic.showLeds(`
# . . . .
Expand All @@ -34,18 +34,18 @@ buttonClicks.onButtonHeld(Button.A, function () {
showClear()
})

buttonClicks.onButtonDown(Button.A, function () {
buttonClicks.onButtonDown(buttonClicks.AorB.A, function () {
serial.writeLine("A down")
led.toggle(0, 1)
})
buttonClicks.onButtonUp(Button.A, function () {
buttonClicks.onButtonUp(buttonClicks.AorB.A, function () {
serial.writeLine("A up")
led.toggle(0, 1)
})



buttonClicks.onButtonSingleClicked(Button.B, function () {
buttonClicks.onButtonSingleClicked(buttonClicks.AorB.B, function () {
serial.writeLine("B single")
basic.showLeds(`
. . . . #
Expand All @@ -61,7 +61,7 @@ function showClear() {
basic.clearScreen()
}

buttonClicks.onButtonDoubleClicked(Button.B, function () {
buttonClicks.onButtonDoubleClicked(buttonClicks.AorB.B, function () {
serial.writeLine("B double")
basic.showLeds(`
. . . . #
Expand All @@ -73,7 +73,7 @@ buttonClicks.onButtonDoubleClicked(Button.B, function () {
showClear()
})

buttonClicks.onButtonHeld(Button.B, function () {
buttonClicks.onButtonHeld(buttonClicks.AorB.B, function () {
serial.writeLine("B held")
basic.showLeds(`
. . . . #
Expand All @@ -85,11 +85,11 @@ buttonClicks.onButtonHeld(Button.B, function () {
showClear()
})

buttonClicks.onButtonDown(Button.B, function () {
buttonClicks.onButtonDown(buttonClicks.AorB.B, function () {
serial.writeLine("B down")
led.toggle(4, 1)
})
buttonClicks.onButtonUp(Button.B, function () {
buttonClicks.onButtonUp(buttonClicks.AorB.B, function () {
serial.writeLine("B up")
led.toggle(4, 1)
})
Expand Down

0 comments on commit 3880a50

Please sign in to comment.