add Grove - Thumb Joystick

This commit is contained in:
jerryyip 2018-09-14 14:11:57 +08:00
parent 1154cb2fef
commit fe28f60f66
3 changed files with 197 additions and 44 deletions

7
.gitignore vendored
View File

@ -45,3 +45,10 @@ $RECYCLE.BIN/
Network Trash Folder Network Trash Folder
Temporary Items Temporary Items
.apdisk .apdisk
node_modules/
pxt_modules/
built/
.python-version
package-lock.json
projects/

98
main.ts
View File

@ -61,13 +61,40 @@ enum GroveGesture {
Wave = 9 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. * Functions to operate Grove module.
*/ */
//% weight=10 color=#9F79EE icon="\uf108" block="Grove" //% weight=10 color=#9F79EE icon="\uf108" block="Grove"
namespace grove { namespace grove {
const gestureEventId = 3100; const gestureEventId = 3100;
const joystickEventID = 3101;
let lastGesture = GroveGesture.None; let lastGesture = GroveGesture.None;
let lastJoystick = GroveJoystickKey.None;
let joystick: GroveJoystick = undefined;
let paj7620: PAJ7620 = undefined; let paj7620: PAJ7620 = undefined;
let distanceBackup: number = 0; 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 * Create a new driver of Grove - Ultrasonic Sensor to measure distances in cm
@ -464,4 +516,50 @@ namespace grove {
this.bit(0x7f, 0x03); 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 {
// }
} }

136
test.ts
View File

@ -1,56 +1,104 @@
grove.onGesture(GroveGesture.Up, () => { // grove.onGesture(GroveGesture.Up, () => {
basic.showString("U") // basic.showString("U")
}) // })
grove.onGesture(GroveGesture.Down, () => { // grove.onGesture(GroveGesture.Down, () => {
basic.showString("D") // basic.showString("D")
}) // })
grove.onGesture(GroveGesture.Right, () => { // grove.onGesture(GroveGesture.Right, () => {
basic.showString("R") // basic.showString("R")
}) // })
grove.onGesture(GroveGesture.Left, () => { // grove.onGesture(GroveGesture.Left, () => {
basic.showString("L") // basic.showString("L")
}) // })
grove.onGesture(GroveGesture.Wave, () => { // grove.onGesture(GroveGesture.Wave, () => {
basic.showString("W") // basic.showString("W")
}) // })
grove.onGesture(GroveGesture.Clockwise, () => { // grove.onGesture(GroveGesture.Clockwise, () => {
basic.showString("C") // basic.showString("C")
}) // })
grove.onGesture(GroveGesture.Anticlockwise, () => { // grove.onGesture(GroveGesture.Anticlockwise, () => {
basic.showString("A") // 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 display = grove.createDisplay(DigitalPin.P0, DigitalPin.P1);
let data = 0; // let data = 0;
display.point(true); // display.point(true);
display.clear(); // display.clear();
display.bit(3, 3); // display.bit(3, 3);
basic.pause(500); // basic.pause(500);
display.point(false); // display.point(false);
display.clear(); // display.clear();
display.bit(2, 2); // display.bit(2, 2);
basic.pause(500); // basic.pause(500);
display.point(true); // display.point(true);
display.clear(); // display.clear();
display.bit(1, 1); // display.bit(1, 1);
basic.pause(500); // basic.pause(500);
display.point(false); // display.point(false);
display.clear(); // display.clear();
display.bit(0, 0); // display.bit(0, 0);
basic.pause(500); // basic.pause(500);
display.set(7); // display.set(7);
// let p : grove.PAJ7620;
// p.init();
while(true) while(true)
{ {
display.show(data ++); // display.show(data ++);
let distance = grove.measureInCentimeters(DigitalPin.P0); // let distance = grove.measureInCentimeters(DigitalPin.P0);
basic.showNumber(distance); basic.showNumber(12);
basic.pause(500); basic.pause(2000);
} }
} }