Move button code
This commit is contained in:
parent
768e8c60f5
commit
36050de253
@ -15,6 +15,106 @@ const enum LightsPattern {
|
||||
OrangePulse = 9, // LED_ORANGE_PULSE
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* User interaction on buttons
|
||||
*/
|
||||
const enum ButtonEvent {
|
||||
//% block="click"
|
||||
Click = 1,
|
||||
//% block="long click"
|
||||
LongClick = 2,
|
||||
//% block="up"
|
||||
Up = 3,
|
||||
//% block="down"
|
||||
Down = 4,
|
||||
}
|
||||
|
||||
namespace input {
|
||||
/**
|
||||
* Generic button class, for device buttons and sensors.
|
||||
*/
|
||||
//% fixedInstances
|
||||
export class Button extends control.Component {
|
||||
private downTime: number;
|
||||
private _isPressed: boolean;
|
||||
private _wasPressed: boolean;
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
this.downTime = 0
|
||||
this._isPressed = false
|
||||
this._wasPressed = false
|
||||
}
|
||||
|
||||
//% hidden
|
||||
update(curr: boolean) {
|
||||
if (this._isPressed == curr) return
|
||||
this._isPressed = curr
|
||||
if (curr) {
|
||||
this.downTime = control.millis()
|
||||
control.raiseEvent(this._id, ButtonEvent.Down)
|
||||
} else {
|
||||
control.raiseEvent(this._id, ButtonEvent.Up)
|
||||
let delta = control.millis() - this.downTime
|
||||
control.raiseEvent(this._id, delta > 500 ? ButtonEvent.LongClick : ButtonEvent.Click)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if button is currently pressed or not.
|
||||
* @param button the button to query the request
|
||||
*/
|
||||
//% help=input/button/is-pressed weight=79
|
||||
//% block="%button|is pressed"
|
||||
//% blockId=buttonIsPressed
|
||||
//% blockGap=8
|
||||
//% parts="buttonpair"
|
||||
//% blockNamespace=input
|
||||
//% button.fieldEditor="gridpicker"
|
||||
//% button.fieldOptions.width=220
|
||||
//% button.fieldOptions.columns=3
|
||||
isPressed() {
|
||||
return this._isPressed
|
||||
}
|
||||
|
||||
/**
|
||||
* See if the button was pressed again since the last time you checked.
|
||||
* @param button the button to query the request
|
||||
*/
|
||||
//% help=input/button/was-pressed weight=78
|
||||
//% block="%button|was pressed"
|
||||
//% blockId=buttonWasPressed
|
||||
//% parts="buttonpair" blockGap=8
|
||||
//% blockNamespace=input advanced=true
|
||||
//% button.fieldEditor="gridpicker"
|
||||
//% button.fieldOptions.width=220
|
||||
//% button.fieldOptions.columns=3
|
||||
wasPressed() {
|
||||
const r = this._wasPressed
|
||||
this._wasPressed = false
|
||||
return r
|
||||
}
|
||||
|
||||
/**
|
||||
* Do something when a button or sensor is clicked, double clicked, etc...
|
||||
* @param button the button that needs to be clicked or used
|
||||
* @param event the kind of button gesture that needs to be detected
|
||||
* @param body code to run when the event is raised
|
||||
*/
|
||||
//% help=input/button/on-event weight=99 blockGap=8
|
||||
//% blockId=buttonEvent block="on %button|%event"
|
||||
//% parts="buttonpair"
|
||||
//% blockNamespace=input
|
||||
//% button.fieldEditor="gridpicker"
|
||||
//% button.fieldOptions.width=220
|
||||
//% button.fieldOptions.columns=3
|
||||
onEvent(ev: ButtonEvent, body: () => void) {
|
||||
control.onEvent(this._id, ev, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace input {
|
||||
let btnsMM: MMap
|
||||
let buttons: DevButton[]
|
||||
|
@ -422,100 +422,3 @@ namespace input.internal {
|
||||
TST_UART_WRITE = 0xc048740a,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* User interaction on buttons
|
||||
*/
|
||||
const enum ButtonEvent {
|
||||
//% block="click"
|
||||
Click = 1,
|
||||
//% block="long click"
|
||||
LongClick = 2,
|
||||
//% block="up"
|
||||
Up = 3,
|
||||
//% block="down"
|
||||
Down = 4,
|
||||
}
|
||||
|
||||
namespace input {
|
||||
//% fixedInstances
|
||||
export class Button extends control.Component {
|
||||
private downTime: number;
|
||||
private _isPressed: boolean;
|
||||
private _wasPressed: boolean;
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
this.downTime = 0
|
||||
this._isPressed = false
|
||||
this._wasPressed = false
|
||||
}
|
||||
|
||||
//% hidden
|
||||
update(curr: boolean) {
|
||||
if (this._isPressed == curr) return
|
||||
this._isPressed = curr
|
||||
if (curr) {
|
||||
this.downTime = control.millis()
|
||||
control.raiseEvent(this._id, ButtonEvent.Down)
|
||||
} else {
|
||||
control.raiseEvent(this._id, ButtonEvent.Up)
|
||||
let delta = control.millis() - this.downTime
|
||||
control.raiseEvent(this._id, delta > 500 ? ButtonEvent.LongClick : ButtonEvent.Click)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if button is currently pressed or not.
|
||||
* @param button the button to query the request
|
||||
*/
|
||||
//% help=input/button/is-pressed weight=79
|
||||
//% block="%button|is pressed"
|
||||
//% blockId=buttonIsPressed
|
||||
//% blockGap=8
|
||||
//% parts="buttonpair"
|
||||
//% blockNamespace=input
|
||||
//% button.fieldEditor="gridpicker"
|
||||
//% button.fieldOptions.width=220
|
||||
//% button.fieldOptions.columns=3
|
||||
isPressed() {
|
||||
return this._isPressed
|
||||
}
|
||||
|
||||
/**
|
||||
* See if the button was pressed again since the last time you checked.
|
||||
* @param button the button to query the request
|
||||
*/
|
||||
//% help=input/button/was-pressed weight=78
|
||||
//% block="%button|was pressed"
|
||||
//% blockId=buttonWasPressed
|
||||
//% parts="buttonpair" blockGap=8
|
||||
//% blockNamespace=input advanced=true
|
||||
//% button.fieldEditor="gridpicker"
|
||||
//% button.fieldOptions.width=220
|
||||
//% button.fieldOptions.columns=3
|
||||
wasPressed() {
|
||||
const r = this._wasPressed
|
||||
this._wasPressed = false
|
||||
return r
|
||||
}
|
||||
|
||||
/**
|
||||
* Do something when a button or sensor is clicked, double clicked, etc...
|
||||
* @param button the button that needs to be clicked or used
|
||||
* @param event the kind of button gesture that needs to be detected
|
||||
* @param body code to run when the event is raised
|
||||
*/
|
||||
//% help=input/button/on-event weight=99 blockGap=8
|
||||
//% blockId=buttonEvent block="on %button|%event"
|
||||
//% parts="buttonpair"
|
||||
//% blockNamespace=input
|
||||
//% button.fieldEditor="gridpicker"
|
||||
//% button.fieldOptions.width=220
|
||||
//% button.fieldOptions.columns=3
|
||||
onEvent(ev: ButtonEvent, body: () => void) {
|
||||
control.onEvent(this._id, ev, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user