pxt-ev3/libs/core/buttons.ts

280 lines
7.5 KiB
TypeScript
Raw Normal View History

2017-07-10 15:16:31 +02:00
/**
* Patterns for lights under the buttons.
*/
const enum StatusLight {
2018-01-11 22:43:11 +01:00
//% block=off enumval=0
2017-07-10 16:11:21 +02:00
Off = 0,
2018-01-11 22:43:11 +01:00
//% block=green enumval=1
2017-07-10 16:11:21 +02:00
Green = 1,
2018-01-11 22:43:11 +01:00
//% block=red enumval=2
2017-07-10 16:11:21 +02:00
Red = 2,
2018-01-11 22:43:11 +01:00
//% block=orange enumval=3
2017-07-10 16:11:21 +02:00
Orange = 3,
2018-01-11 22:43:11 +01:00
//% block="green flash" enumval=4
2017-07-10 16:11:21 +02:00
GreenFlash = 4,
2018-01-11 22:43:11 +01:00
//% block="red flash" enumval=5
2017-07-10 16:11:21 +02:00
RedFlash = 5,
2018-01-11 22:43:11 +01:00
//% block="orange flash" enumval=6
2017-07-10 16:11:21 +02:00
OrangeFlash = 6,
2018-01-11 22:43:11 +01:00
//% block="green pulse" enumval=7
2017-07-10 16:11:21 +02:00
GreenPulse = 7,
2018-01-11 22:43:11 +01:00
//% block="red pulse" enumval=8
2017-07-10 16:11:21 +02:00
RedPulse = 8,
2018-01-11 22:43:11 +01:00
//% block="orange pulse" enumval=9
2017-07-10 16:11:21 +02:00
OrangePulse = 9,
2017-07-10 15:16:31 +02:00
}
2017-07-10 15:18:00 +02:00
/**
* User interaction on buttons
*/
const enum ButtonEvent {
//% block="pressed"
Pressed = 4,
//% block="bumped"
Bumped = 1,
//% block="released"
Released = 3,
2017-07-10 15:18:00 +02:00
}
2017-10-28 18:13:02 +02:00
namespace brick {
2017-07-10 15:18:00 +02:00
/**
* 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
}
protected poke() {
}
2017-07-10 15:18:00 +02:00
//% hidden
2017-10-27 06:10:37 +02:00
_update(curr: boolean) {
if (this == null) return
2017-07-10 15:18:00 +02:00
if (this._isPressed == curr) return
this._isPressed = curr
if (curr) {
2017-11-29 01:02:04 +01:00
this._wasPressed = true;
2017-07-10 15:18:00 +02:00
this.downTime = control.millis()
control.raiseEvent(this._id, ButtonEvent.Pressed)
2017-07-10 15:18:00 +02:00
} else {
control.raiseEvent(this._id, ButtonEvent.Released)
2017-11-29 01:02:04 +01:00
const delta = control.millis() - this.downTime;
if (delta < 500)
control.raiseEvent(this._id, ButtonEvent.Bumped)
2017-07-10 15:18:00 +02:00
}
}
/**
* Check if button is currently pressed or not.
* @param button the button to query the request
*/
//% help=brick/button/is-pressed
2017-12-20 00:10:13 +01:00
//% block="%button|is pressed"
2017-07-10 15:18:00 +02:00
//% blockId=buttonIsPressed
2017-10-25 01:25:03 +02:00
//% parts="brick"
2017-10-28 18:13:02 +02:00
//% blockNamespace=brick
//% weight=81
2017-10-28 18:20:34 +02:00
//% group="Buttons"
//% button.fieldEditor="brickbuttons"
2017-07-10 15:18:00 +02:00
isPressed() {
this.poke();
2017-07-10 15:18:00 +02:00
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=brick/button/was-pressed
2017-12-20 00:10:13 +01:00
//% block="%button|was pressed"
2017-07-10 15:18:00 +02:00
//% blockId=buttonWasPressed
2018-04-03 13:10:35 +02:00
//% blockHidden=true
2017-10-25 01:25:03 +02:00
//% parts="brick"
2017-10-28 18:13:02 +02:00
//% blockNamespace=brick
//% weight=80
2017-10-28 18:20:34 +02:00
//% group="Buttons"
//% button.fieldEditor="brickbuttons"
2017-07-10 15:18:00 +02:00
wasPressed() {
this.poke();
2017-07-10 15:18:00 +02:00
const r = this._wasPressed
this._wasPressed = false
return r
}
/**
2017-10-27 06:10:37 +02:00
* Do something when a button or sensor is clicked, up or down.
2017-07-10 15:18:00 +02:00
* @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=brick/button/on-event
2017-12-20 00:10:13 +01:00
//% blockId=buttonEvent block="on %button|%event"
2017-10-25 01:25:03 +02:00
//% parts="brick"
2017-10-28 18:13:02 +02:00
//% blockNamespace=brick
//% weight=99 blockGap=16
2017-10-28 18:20:34 +02:00
//% group="Buttons"
//% button.fieldEditor="brickbuttons"
2017-07-10 15:18:00 +02:00
onEvent(ev: ButtonEvent, body: () => void) {
control.onEvent(this._id, ev, body)
}
2017-11-30 19:05:00 +01:00
/**
* Waits until the event is raised
* @param ev the event to wait for
*/
//% help=brick/button/pause-until
2017-12-20 00:10:13 +01:00
//% blockId=buttonWaitUntil block="pause until %button|%event"
2017-11-30 19:05:00 +01:00
//% parts="brick"
//% blockNamespace=brick
//% weight=98 blockGap=8
//% group="Buttons"
//% button.fieldEditor="brickbuttons"
pauseUntil(ev: ButtonEvent) {
2017-11-30 19:05:00 +01:00
control.waitForEvent(this._id, ev);
}
2017-07-10 15:18:00 +02:00
}
}
2017-10-28 18:13:02 +02:00
namespace brick {
2017-07-10 15:16:31 +02:00
let btnsMM: MMap
let buttons: DevButton[]
let buttonPoller: sensors.internal.Poller;
2017-07-10 15:16:31 +02:00
export namespace internal {
export function getBtnsMM() {
initBtns()
return btnsMM;
}
}
function readButtons() {
let sl = btnsMM.slice(0, DAL.NUM_BUTTONS)
2017-07-10 15:16:31 +02:00
let ret = 0
for (let i = 0; i < sl.length; ++i) {
if (sl[i])
ret |= 1 << i
}
return ret
}
function initBtns() {
if (btnsMM) return
btnsMM = control.mmap("/dev/lms_ui", DAL.NUM_BUTTONS, 0)
2017-07-10 15:16:31 +02:00
if (!btnsMM) control.fail("no buttons?")
buttons = []
buttonPoller = new sensors.internal.Poller(50, readButtons, (prev, curr) => {
2017-07-10 15:16:31 +02:00
for (let b of buttons)
2017-10-27 06:10:37 +02:00
b._update(!!(curr & b.mask))
2017-07-10 15:16:31 +02:00
})
control.dmesg("runtime started, " + control.deviceFirmwareVersion())
}
class DevButton extends Button {
mask: number
constructor(mask: number) {
super()
this.mask = mask
initBtns()
buttons.push(this)
}
protected poke() {
buttonPoller.poke();
}
2018-01-06 07:56:11 +01:00
}
2017-07-10 15:16:31 +02:00
initBtns() // always ON as it handles ESCAPE button
/**
* Enter button on the EV3 Brick.
*/
2018-01-11 17:28:25 +01:00
//% whenUsed block="button enter" weight=95 fixedInstance
export const buttonEnter: Button = new DevButton(DAL.BUTTON_ID_ENTER)
2017-07-10 15:16:31 +02:00
/**
2017-10-25 01:25:03 +02:00
* Left button on the EV3 Brick.
2017-07-10 15:16:31 +02:00
*/
2018-01-11 17:28:25 +01:00
//% whenUsed block="button left" weight=95 fixedInstance
export const buttonLeft: Button = new DevButton(DAL.BUTTON_ID_LEFT)
2017-07-10 15:16:31 +02:00
/**
2017-10-25 01:25:03 +02:00
* Right button on the EV3 Brick.
2017-07-10 15:16:31 +02:00
*/
2018-01-11 17:28:25 +01:00
//% whenUsed block="button right" weight=94 fixedInstance
export const buttonRight: Button = new DevButton(DAL.BUTTON_ID_RIGHT)
2017-07-10 15:16:31 +02:00
/**
2017-10-25 01:25:03 +02:00
* Up button on the EV3 Brick.
2017-07-10 15:16:31 +02:00
*/
2018-01-11 17:28:25 +01:00
//% whenUsed block="button up" weight=95 fixedInstance
export const buttonUp: Button = new DevButton(DAL.BUTTON_ID_UP)
2017-07-10 15:16:31 +02:00
/**
2017-10-25 01:25:03 +02:00
* Down button on the EV3 Brick.
2017-07-10 15:16:31 +02:00
*/
2018-01-11 17:28:25 +01:00
//% whenUsed block="button down" weight=95 fixedInstance
export const buttonDown: Button = new DevButton(DAL.BUTTON_ID_DOWN)
2017-07-10 15:16:31 +02:00
}
namespace control {
/**
* Determine the version of system software currently running.
*/
2018-01-06 07:56:11 +01:00
//%
2017-07-10 15:16:31 +02:00
export function deviceFirmwareVersion(): string {
let buf = output.createBuffer(6)
2017-10-28 18:13:02 +02:00
brick.internal.getBtnsMM().read(buf)
2017-07-10 15:16:31 +02:00
let r = ""
for (let i = 0; i < buf.length; ++i) {
let c = buf[i]
if (c == 0) break
r += String.fromCharCode(c)
}
return r
}
}
2017-10-28 18:13:02 +02:00
namespace brick {
2018-01-11 22:43:11 +01:00
// the brick starts with the red color
let currPattern: StatusLight = StatusLight.Off;
2017-07-10 15:16:31 +02:00
/**
* Gets the current light pattern.
*/
//% weight=99 group="Buttons"
//% help=brick/status-light
export function statusLight() {
return currPattern;
}
2017-07-10 15:16:31 +02:00
/**
* Set lights.
2018-02-15 22:49:04 +01:00
* @param pattern the lights pattern to use. eg: StatusLight.Orange
2017-07-10 15:16:31 +02:00
*/
//% blockId=setLights block="set status light to %pattern"
2017-12-20 00:10:13 +01:00
//% weight=100 group="Buttons"
//% help=brick/set-status-light
export function setStatusLight(pattern: StatusLight): void {
2017-07-10 15:16:31 +02:00
if (currPattern === pattern)
return
2018-01-11 22:43:11 +01:00
currPattern = pattern;
const cmd = output.createBuffer(2)
2017-07-10 15:16:31 +02:00
cmd[0] = pattern + 48
2017-10-28 18:13:02 +02:00
brick.internal.getBtnsMM().write(cmd)
2017-07-10 15:16:31 +02:00
}
}