2020-02-07 12:34:43 +09:00
|
|
|
import Device from "@chirimen/grove-gesture";
|
|
|
|
import { ReadableDevice } from "./Device";
|
2020-02-07 13:58:09 +09:00
|
|
|
import { I2C, i2c } from "./i2c";
|
2020-02-07 12:34:43 +09:00
|
|
|
|
|
|
|
type Direction =
|
|
|
|
| ""
|
|
|
|
| "right"
|
|
|
|
| "left"
|
|
|
|
| "up"
|
|
|
|
| "down"
|
|
|
|
| "forward"
|
|
|
|
| "back"
|
|
|
|
| "clockwise"
|
|
|
|
| "counterclockwise";
|
|
|
|
|
|
|
|
export function paj7620(
|
2020-02-07 13:58:09 +09:00
|
|
|
bus: I2C = i2c(),
|
2020-02-07 12:34:43 +09:00
|
|
|
address: number = 0x73
|
|
|
|
): ReadableDevice<Direction> {
|
|
|
|
const device = new Device(bus, address);
|
|
|
|
|
|
|
|
return {
|
|
|
|
async read(): Promise<Direction> {
|
|
|
|
if (device.i2cSlave == null) await device.init();
|
|
|
|
const direction = await device.read();
|
|
|
|
switch (direction) {
|
|
|
|
case "right":
|
|
|
|
case "left":
|
|
|
|
case "up":
|
|
|
|
case "down":
|
|
|
|
case "forward":
|
|
|
|
case "back":
|
|
|
|
case "clockwise":
|
|
|
|
return direction;
|
|
|
|
case "count clockwise":
|
|
|
|
return "counterclockwise";
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default paj7620;
|