2020-02-07 12:34:43 +09:00
|
|
|
import Device from "@chirimen/gp2y0e03";
|
|
|
|
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
|
|
|
|
2021-07-24 23:36:22 +09:00
|
|
|
/** Distance (cm) */
|
|
|
|
export type Distance = number;
|
2020-02-07 12:34:43 +09:00
|
|
|
|
|
|
|
export function gp2y0e03(
|
2020-02-07 13:58:09 +09:00
|
|
|
bus: I2C = i2c(),
|
2020-02-07 12:34:43 +09:00
|
|
|
address: number = 0x40
|
|
|
|
): ReadableDevice<number> {
|
|
|
|
const device = new Device(bus, address);
|
|
|
|
|
|
|
|
return {
|
|
|
|
async read(): Promise<Distance> {
|
|
|
|
if (device.i2cSlave == null) await device.init();
|
|
|
|
return device.read();
|
2021-07-24 23:28:23 +09:00
|
|
|
},
|
2020-02-07 12:34:43 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default gp2y0e03;
|