2016-04-06 00:59:25 +02:00
|
|
|
/**
|
|
|
|
* Events and data from sensors
|
|
|
|
*/
|
2016-10-28 01:15:44 +02:00
|
|
|
//% color=#C90072 weight=99
|
2016-03-10 23:01:04 +01:00
|
|
|
namespace input {
|
|
|
|
/**
|
|
|
|
* Attaches code to run when the screen is facing up.
|
|
|
|
* @param body TODO
|
|
|
|
*/
|
2016-03-22 06:13:39 +01:00
|
|
|
//% help=input/on-screen-up
|
2019-12-02 05:58:26 +01:00
|
|
|
export function onScreenUp(body: () => void): void {
|
2016-03-24 16:26:55 +01:00
|
|
|
onGesture(Gesture.ScreenUp, body);
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attaches code to run when the screen is facing down.
|
|
|
|
* @param body TODO
|
|
|
|
*/
|
2016-03-22 06:13:39 +01:00
|
|
|
//% help=input/on-screen-down
|
2019-12-02 05:58:26 +01:00
|
|
|
export function onScreenDown(body: () => void): void {
|
2016-03-24 16:26:55 +01:00
|
|
|
onGesture(Gesture.ScreenDown, body);
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attaches code to run when the device is shaken.
|
|
|
|
* @param body TODO
|
|
|
|
*/
|
2020-09-08 11:04:25 +02:00
|
|
|
//% deprecated=true
|
2016-03-22 06:13:39 +01:00
|
|
|
//% help=input/on-shake
|
2019-12-02 05:58:26 +01:00
|
|
|
export function onShake(body: () => void): void {
|
2016-03-24 16:26:55 +01:00
|
|
|
onGesture(Gesture.Shake, body);
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attaches code to run when the logo is oriented upwards and the board is vertical.
|
|
|
|
* @param body TODO
|
|
|
|
*/
|
2016-03-22 06:13:39 +01:00
|
|
|
//% help=input/on-logo-up
|
2019-12-02 05:58:26 +01:00
|
|
|
export function onLogoUp(body: () => void): void {
|
2016-03-24 16:26:55 +01:00
|
|
|
onGesture(Gesture.LogoUp, body);
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attaches code to run when the logo is oriented downwards and the board is vertical.
|
|
|
|
* @param body TODO
|
|
|
|
*/
|
2016-03-22 06:13:39 +01:00
|
|
|
//% help=input/on-logo-down
|
2019-12-02 05:58:26 +01:00
|
|
|
export function onLogoDown(body: () => void): void {
|
2016-03-24 16:26:55 +01:00
|
|
|
onGesture(Gesture.LogoDown, body);
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|
2019-12-02 05:58:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Obsolete, use input.calibrateCompass instead.
|
|
|
|
*/
|
|
|
|
//% weight=0 help=input/calibrate-compass
|
|
|
|
export function calibrate() {
|
|
|
|
input.calibrateCompass();
|
|
|
|
}
|
2020-05-08 18:35:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the number of milliseconds elapsed since power on.
|
|
|
|
*/
|
|
|
|
//% help=input/running-time weight=50 blockGap=8
|
|
|
|
//% blockId=device_get_running_time block="running time (ms)"
|
|
|
|
//% advanced=true
|
|
|
|
export function runningTime() {
|
|
|
|
return control.millis();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the number of microseconds elapsed since power on.
|
|
|
|
*/
|
|
|
|
//% help=input/running-time-micros weight=49
|
|
|
|
//% blockId=device_get_running_time_micros block="running time (micros)"
|
|
|
|
//% advanced=true
|
|
|
|
export function runningTimeMicros() {
|
|
|
|
return control.micros();
|
|
|
|
}
|
2020-09-08 11:04:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gets the level of loudness in 0-100%
|
|
|
|
*/
|
|
|
|
//% blockId="loudness"
|
|
|
|
//% block="Loudness"
|
|
|
|
|
|
|
|
export function soundLevel(): number {
|
|
|
|
let value = 0
|
|
|
|
let max = 0
|
|
|
|
let min = 1023
|
|
|
|
for (let index = 0; index < 32; index++) {
|
|
|
|
value = pins.analogReadPin(AnalogPin.MIC)
|
|
|
|
if (value > max) {
|
|
|
|
max = value
|
|
|
|
} else if (value < min) {
|
|
|
|
min = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
value = (max - min) * 977 / 10000
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|