From 43674e0dc5498ffbc546be1298a5515c54d66ce3 Mon Sep 17 00:00:00 2001 From: Baozhu Zuo Date: Wed, 31 Jul 2019 16:52:21 +0800 Subject: [PATCH] add onGesture and onGesture --- README.md | 10 ++++++++++ main.ts | 45 +++++++++++++++++++++++++++++++++++++++++++++ pxt.json | 2 +- 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e0569e1..f56193a 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,16 @@ A Microsoft MakeCode package for for Seeed Studio Grove module. get gesture model. ```blocks + + +grove.onGesture(GroveGesture.Up, () => { + basic.showString("Up"); +}) +grove.onGesture(GroveGesture.Down, () => { + basic.showString("Down"); +}) + + grove.initGesture() basic.forever(function () { if (grove.getGestureModel() == 1) { diff --git a/main.ts b/main.ts index 542b0fc..7f72883 100644 --- a/main.ts +++ b/main.ts @@ -538,4 +538,49 @@ namespace grove { 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); + } + }) + + } } \ No newline at end of file diff --git a/pxt.json b/pxt.json index cada6f1..869fea7 100644 --- a/pxt.json +++ b/pxt.json @@ -1,6 +1,6 @@ { "name": "Grove", - "version": "0.1.4", + "version": "0.1.5", "description": "A Microsoft MakeCode package for Seeed Studio Grove module", "license": "MIT", "dependencies": {