Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split i2cdriver file #644

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/drivers/src/aht20.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/drivers/src/bme680.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand Down Expand Up @@ -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<TData> 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<TData> {
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<TData>
}
54 changes: 54 additions & 0 deletions packages/drivers/src/i2csensordriver.ts
Original file line number Diff line number Diff line change
@@ -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<TData> 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<TData> {
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<TData>
}
3 changes: 2 additions & 1 deletion packages/drivers/src/index.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion packages/drivers/src/ltr390.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/drivers/src/sh110x.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/drivers/src/sht.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/drivers/src/ssd1306.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down