From 2401fa65e79c1a37d719e351b4c22581415e1bfa Mon Sep 17 00:00:00 2001 From: pelikhan Date: Tue, 10 Oct 2023 21:47:52 -0700 Subject: [PATCH] split i2cdriver file --- packages/drivers/src/aht20.ts | 2 +- packages/drivers/src/bme680.ts | 2 +- .../drivers/src/{driver.ts => i2cdriver.ts} | 56 +------------------ packages/drivers/src/i2csensordriver.ts | 54 ++++++++++++++++++ packages/drivers/src/index.ts | 3 +- packages/drivers/src/ltr390.ts | 2 +- packages/drivers/src/sh110x.ts | 2 +- packages/drivers/src/sht.ts | 2 +- packages/drivers/src/ssd1306.ts | 4 +- 9 files changed, 65 insertions(+), 62 deletions(-) rename packages/drivers/src/{driver.ts => i2cdriver.ts} (70%) create mode 100644 packages/drivers/src/i2csensordriver.ts diff --git a/packages/drivers/src/aht20.ts b/packages/drivers/src/aht20.ts index ad8c11d2c6..3c2724957d 100644 --- a/packages/drivers/src/aht20.ts +++ b/packages/drivers/src/aht20.ts @@ -1,7 +1,7 @@ import * as ds from "@devicescript/core" import { DriverError } from "./core" import { startTemperatureHumidity } from "./servers" -import { I2CSensorDriver } from "./driver" +import { I2CSensorDriver } from "./i2csensordriver" import { sleep } from "@devicescript/core" const AHT20_ADDRESS = 0x38 diff --git a/packages/drivers/src/bme680.ts b/packages/drivers/src/bme680.ts index 39b763ba5c..6178284f52 100644 --- a/packages/drivers/src/bme680.ts +++ b/packages/drivers/src/bme680.ts @@ -6,7 +6,7 @@ import { } from "./servers" import { delay } from "@devicescript/core" import { DriverError } from "./core" -import { I2CSensorDriver } from "./driver" +import { I2CSensorDriver } from "./i2csensordriver" // based on https://github.com/adafruit/Adafruit_CircuitPython_BME680/blob/main/adafruit_bme680.py diff --git a/packages/drivers/src/driver.ts b/packages/drivers/src/i2cdriver.ts similarity index 70% rename from packages/drivers/src/driver.ts rename to packages/drivers/src/i2cdriver.ts index de447ad655..2a65c93a0c 100644 --- a/packages/drivers/src/driver.ts +++ b/packages/drivers/src/i2cdriver.ts @@ -1,6 +1,6 @@ -import { AsyncValue, isSimulator, millis, sleep } from "@devicescript/core" +import { isSimulator } from "@devicescript/core" import { i2c, I2C } from "@devicescript/i2c" -import { DriverError, throttle } from "./core" +import { DriverError } from "./core" export interface I2CDriverOptions { /** @@ -122,55 +122,3 @@ export abstract class I2CDriver { return await this.client.writeBuf(this.devAddr, b) } } - -export interface I2CSensorDriverOptions extends I2CDriverOptions { - /** - * Data read throttle time in milliseconds - */ - readCacheTime?: number -} - -/** - * A helper class to implement I2C sensor drivers - */ -export abstract class I2CSensorDriver extends I2CDriver { - _data: TData - _dataTime = 0 - _dataCacheTime: number - - /** - * Instantiate a driver - * @param devAddr a 7 bit i2c address - * @param options - */ - constructor(devAddr: number, options?: I2CSensorDriverOptions) { - super(devAddr, options) - const { readCacheTime } = options || {} - this._dataCacheTime = readCacheTime || 50 - } - - /** - * Throttled read of the sensor data - * @returns - */ - async read(): Promise { - if (isSimulator()) return {} as any - - // lock on reading data - while (this._dataTime === -1) await sleep(5) - - // cache hit - if (millis() - this._dataTime < this._dataCacheTime) return this._data - - // query sensor again, read data is throttled - this._dataTime = -1 - this._data = await this.readData() - this._dataTime = millis() - return this._data - } - - /** - * Perform the I2C transaction to read the sensor data - */ - protected abstract readData(): AsyncValue -} diff --git a/packages/drivers/src/i2csensordriver.ts b/packages/drivers/src/i2csensordriver.ts new file mode 100644 index 0000000000..a0fe4eea5e --- /dev/null +++ b/packages/drivers/src/i2csensordriver.ts @@ -0,0 +1,54 @@ +import { AsyncValue, isSimulator, millis, sleep } from "@devicescript/core" +import { I2CDriver, I2CDriverOptions } from "./i2cdriver" + +export interface I2CSensorDriverOptions extends I2CDriverOptions { + /** + * Data read throttle time in milliseconds + */ + readCacheTime?: number +} + +/** + * A helper class to implement I2C sensor drivers + */ +export abstract class I2CSensorDriver extends I2CDriver { + _data: TData + _dataTime = 0 + _dataCacheTime: number + + /** + * Instantiate a driver + * @param devAddr a 7 bit i2c address + * @param options + */ + constructor(devAddr: number, options?: I2CSensorDriverOptions) { + super(devAddr, options) + const { readCacheTime } = options || {} + this._dataCacheTime = readCacheTime || 50 + } + + /** + * Throttled read of the sensor data + * @returns + */ + async read(): Promise { + if (isSimulator()) return {} as any + + // lock on reading data + while (this._dataTime === -1) await sleep(5) + + // cache hit + if (millis() - this._dataTime < this._dataCacheTime) return this._data + + // query sensor again, read data is throttled + this._dataTime = -1 + this._data = await this.readData() + this._dataTime = millis() + return this._data + } + + /** + * Perform the I2C transaction to read the sensor data + */ + protected abstract readData(): AsyncValue +} diff --git a/packages/drivers/src/index.ts b/packages/drivers/src/index.ts index 891c5b2dfc..cf26a33f5a 100644 --- a/packages/drivers/src/index.ts +++ b/packages/drivers/src/index.ts @@ -1,6 +1,7 @@ import { configureHardware } from "@devicescript/servers" -export * from "./driver" +export * from "./i2cdriver" +export * from "./i2csensordriver" export * from "./core" export * from "./shtc3" export * from "./sht30" diff --git a/packages/drivers/src/ltr390.ts b/packages/drivers/src/ltr390.ts index 6b6b4d97a7..1657354e30 100644 --- a/packages/drivers/src/ltr390.ts +++ b/packages/drivers/src/ltr390.ts @@ -1,6 +1,6 @@ import * as ds from "@devicescript/core" import { DriverError } from "./core" -import { I2CSensorDriver } from "./driver" +import { I2CSensorDriver } from "./i2csensordriver" import { startSimpleServer } from "./servers" const LTR390UV_ADDR = 0x53 diff --git a/packages/drivers/src/sh110x.ts b/packages/drivers/src/sh110x.ts index cf5ab9a8e9..ae7987f0c8 100644 --- a/packages/drivers/src/sh110x.ts +++ b/packages/drivers/src/sh110x.ts @@ -1,5 +1,5 @@ import { Display, Image, Palette } from "@devicescript/graphics" -import { I2CDriver, I2CDriverOptions } from "./driver" +import { I2CDriver, I2CDriverOptions } from "./i2cdriver" import { delay, isSimulator } from "@devicescript/core" // inspired by https://github.com/adafruit/Adafruit_SH110x diff --git a/packages/drivers/src/sht.ts b/packages/drivers/src/sht.ts index 3366fee7ad..d5bcde9845 100644 --- a/packages/drivers/src/sht.ts +++ b/packages/drivers/src/sht.ts @@ -1,6 +1,6 @@ import * as ds from "@devicescript/core" import { DriverError } from "./core" -import { I2CSensorDriver, I2CSensorDriverOptions } from "./driver" +import { I2CSensorDriver, I2CSensorDriverOptions } from "./i2csensordriver" export abstract class SHTDriver extends I2CSensorDriver<{ humidity: number diff --git a/packages/drivers/src/ssd1306.ts b/packages/drivers/src/ssd1306.ts index e9ede7ded1..f9f1b0064f 100644 --- a/packages/drivers/src/ssd1306.ts +++ b/packages/drivers/src/ssd1306.ts @@ -1,6 +1,6 @@ import { Image, Display, Palette } from "@devicescript/graphics" -import { I2CDriver } from "./driver" -import { I2CDriverOptions } from "./driver" +import { I2CDriver } from "./i2cdriver" +import { I2CDriverOptions } from "./i2cdriver" import { isSimulator } from "@devicescript/core" // inspired by https://github.com/adafruit/Adafruit_CircuitPython_SSD1306/blob/main/adafruit_ssd1306.py