add onGesture and onGesture

This commit is contained in:
Baozhu Zuo 2019-07-31 16:52:21 +08:00
parent 2ee4f55c1c
commit 43674e0dc5
3 changed files with 56 additions and 1 deletions

View File

@ -9,6 +9,16 @@ A Microsoft MakeCode package for for Seeed Studio Grove module.
get gesture model. get gesture model.
```blocks ```blocks
grove.onGesture(GroveGesture.Up, () => {
basic.showString("Up");
})
grove.onGesture(GroveGesture.Down, () => {
basic.showString("Down");
})
grove.initGesture() grove.initGesture()
basic.forever(function () { basic.forever(function () {
if (grove.getGestureModel() == 1) { if (grove.getGestureModel() == 1) {

45
main.ts
View File

@ -538,4 +538,49 @@ namespace grove {
return joystick.read(xpin, ypin); return joystick.read(xpin, ypin);
} }
/**
* Do something when a gesture is detected by Grove - Gesture
* @param gesture type of gesture to detect
* @param handler code to run
*/
//% blockId=grove_gesture_create_event block="on Gesture|%gesture"
export function onGesture(gesture: GroveGesture, handler: () => void) {
control.onEvent(gestureEventId, gesture, handler);
paj7620.init();
control.inBackground(() => {
while(true) {
const gesture = paj7620.read();
if (gesture != lastGesture) {
lastGesture = gesture;
control.raiseEvent(gestureEventId, lastGesture);
}
basic.pause(50);
}
})
}
/**
* Do something when a key is detected by Grove - Thumb Joystick
* @param key type of joystick to detect
* @param xpin
* @param ypin
* @param handler code to run
*/
//% blockId=grove_joystick_create_event block="on Key|%key"
export function onJoystick(key: GroveJoystickKey, xpin: AnalogPin, ypin: AnalogPin, handler: () => void) {
control.onEvent(joystickEventID, key, handler);
control.inBackground(() => {
while(true) {
const key = joystick.read(xpin, ypin);
if (key != lastJoystick) {
lastJoystick = key;
control.raiseEvent(joystickEventID, lastJoystick);
}
basic.pause(50);
}
})
}
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "Grove", "name": "Grove",
"version": "0.1.4", "version": "0.1.5",
"description": "A Microsoft MakeCode package for Seeed Studio Grove module", "description": "A Microsoft MakeCode package for Seeed Studio Grove module",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {