Skip to content

Commit

Permalink
split i2cdriver file
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Oct 11, 2023
1 parent 07b74a7 commit 2401fa6
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 62 deletions.
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

0 comments on commit 2401fa6

Please sign in to comment.