diff --git a/.gitignore b/.gitignore index cd2946a..cbee7a5 100644 --- a/.gitignore +++ b/.gitignore @@ -45,3 +45,10 @@ $RECYCLE.BIN/ Network Trash Folder Temporary Items .apdisk + +node_modules/ +pxt_modules/ +built/ +.python-version +package-lock.json +projects/ \ No newline at end of file diff --git a/main.ts b/main.ts index a252386..d30e154 100644 --- a/main.ts +++ b/main.ts @@ -61,13 +61,40 @@ enum GroveGesture { Wave = 9 } +enum GroveJoystickKey { + //% block=None + None = 0, + //% block=Right + Right = 1, + //% block=Left + Left = 2, + //% block=Up + Up = 3, + //% block=Down + Down = 4, + //% block=Upper left + UL = 5, + //% block=Upper right + UR = 6, + //% block=Lower left + LL = 7, + //% block=Lower right + LR = 8, + //% block=press + Press = 9 +} + + /** * Functions to operate Grove module. */ //% weight=10 color=#9F79EE icon="\uf108" block="Grove" namespace grove { const gestureEventId = 3100; + const joystickEventID = 3101; let lastGesture = GroveGesture.None; + let lastJoystick = GroveJoystickKey.None; + let joystick: GroveJoystick = undefined; let paj7620: PAJ7620 = undefined; let distanceBackup: number = 0; /** @@ -92,6 +119,31 @@ namespace grove { }) } } + + + /** + * 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); + } + }) + + } + /** * Create a new driver of Grove - Ultrasonic Sensor to measure distances in cm @@ -464,4 +516,50 @@ namespace grove { this.bit(0x7f, 0x03); } } + + + export class GroveJoystick + { + /** + * Detect position from Grove - Thumb Joystick + * @param xPin + * @param yPin + */ + //% blockId=grove_joystick_read block="%strip|read position of joystick" + //% advanced=true + read(xPin: AnalogPin, yPin: AnalogPin): number { + let xdata = 0, ydata = 0, result = 0; + if (xPin && yPin) { + xdata = pins.analogReadPin(xPin); + ydata = pins.analogReadPin(yPin); + if (xdata > 1000) { + result = GroveJoystickKey.Press; + } + else if (xdata > 600) { + if (ydata > 600) result = GroveJoystickKey.UR; + else if (ydata < 400) result = GroveJoystickKey.LR; + else result = GroveJoystickKey.Right; + } + else if (xdata < 400) { + if (ydata > 600) result = GroveJoystickKey.UL; + else if (ydata < 400) result = GroveJoystickKey.LL; + else result = GroveJoystickKey.Left; + } + else { + if (ydata > 600) result = GroveJoystickKey.Up; + else if (ydata < 400) result = GroveJoystickKey.Down; + else result = GroveJoystickKey.None; + } + } + else { + result = GroveJoystickKey.None; + } + return result; + } + } + + + // export class GroveHicell { + + // } } \ No newline at end of file diff --git a/test.ts b/test.ts index 0c3c7fe..1044a81 100644 --- a/test.ts +++ b/test.ts @@ -1,56 +1,104 @@ -grove.onGesture(GroveGesture.Up, () => { - basic.showString("U") -}) -grove.onGesture(GroveGesture.Down, () => { - basic.showString("D") -}) -grove.onGesture(GroveGesture.Right, () => { - basic.showString("R") -}) -grove.onGesture(GroveGesture.Left, () => { - basic.showString("L") -}) -grove.onGesture(GroveGesture.Wave, () => { - basic.showString("W") -}) -grove.onGesture(GroveGesture.Clockwise, () => { - basic.showString("C") -}) -grove.onGesture(GroveGesture.Anticlockwise, () => { - basic.showString("A") -}) +// grove.onGesture(GroveGesture.Up, () => { +// basic.showString("U") +// }) +// grove.onGesture(GroveGesture.Down, () => { +// basic.showString("D") +// }) +// grove.onGesture(GroveGesture.Right, () => { +// basic.showString("R") +// }) +// grove.onGesture(GroveGesture.Left, () => { +// basic.showString("L") +// }) +// grove.onGesture(GroveGesture.Wave, () => { +// basic.showString("W") +// }) +// grove.onGesture(GroveGesture.Clockwise, () => { +// basic.showString("C") +// }) +// grove.onGesture(GroveGesture.Anticlockwise, () => { +// basic.showString("A") +// }) + +// grove.onJoystick(GroveJoystickKey.Right, AnalogPin.P0,AnalogPin.P1, () => { +// // basic.showArrow(ArrowNames.East); +// basic.showString("1"); +// }) + +// grove.onJoystick(GroveJoystickKey.Left, AnalogPin.P0,AnalogPin.P1, () => { +// // basic.showArrow(ArrowNames.West); +// basic.showString("2"); +// }) + +// grove.onJoystick(GroveJoystickKey.Up, AnalogPin.P0,AnalogPin.P1, () => { +// // basic.showArrow(ArrowNames.North); +// basic.showString("3"); +// }) + +// grove.onJoystick(GroveJoystickKey.Down, AnalogPin.P0,AnalogPin.P1, () => { +// // basic.showArrow(ArrowNames.South); +// basic.showString("4"); +// }) + +// grove.onJoystick(GroveJoystickKey.UL, AnalogPin.P0,AnalogPin.P1, () => { +// // basic.showArrow(ArrowNames.NorthEast); +// basic.showString("5"); +// }) + +// grove.onJoystick(GroveJoystickKey.UR, AnalogPin.P0,AnalogPin.P1, () => { +// // basic.showArrow(ArrowNames.NorthWest); +// basic.showString("6"); +// }) + +// grove.onJoystick(GroveJoystickKey.LL, AnalogPin.P0,AnalogPin.P1, () => { +// // basic.showArrow(ArrowNames.SouthWest); +// basic.showString("7"); +// }) + +// grove.onJoystick(GroveJoystickKey.LR, AnalogPin.P0,AnalogPin.P1, () => { +// // basic.showArrow(ArrowNames.SouthEast); +// basic.showString("8"); +// }) + +// grove.onJoystick(GroveJoystickKey.Press, AnalogPin.P0,AnalogPin.P1, () => { +// basic.showString("9"); +// }) + + { - let display = grove.createDisplay(DigitalPin.P0, DigitalPin.P1); - let data = 0; + // let display = grove.createDisplay(DigitalPin.P0, DigitalPin.P1); + // let data = 0; - display.point(true); - display.clear(); - display.bit(3, 3); - basic.pause(500); + // display.point(true); + // display.clear(); + // display.bit(3, 3); + // basic.pause(500); - display.point(false); - display.clear(); - display.bit(2, 2); - basic.pause(500); + // display.point(false); + // display.clear(); + // display.bit(2, 2); + // basic.pause(500); - display.point(true); - display.clear(); - display.bit(1, 1); - basic.pause(500); + // display.point(true); + // display.clear(); + // display.bit(1, 1); + // basic.pause(500); - display.point(false); - display.clear(); - display.bit(0, 0); - basic.pause(500); + // display.point(false); + // display.clear(); + // display.bit(0, 0); + // basic.pause(500); - display.set(7); + // display.set(7); + // let p : grove.PAJ7620; + // p.init(); while(true) { - display.show(data ++); - let distance = grove.measureInCentimeters(DigitalPin.P0); - basic.showNumber(distance); - basic.pause(500); + // display.show(data ++); + // let distance = grove.measureInCentimeters(DigitalPin.P0); + basic.showNumber(12); + basic.pause(2000); } } \ No newline at end of file