-
Notifications
You must be signed in to change notification settings - Fork 239
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
Adding SEN0322 O2 Sensor Driver Support #1075
base: public
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import Sensor from "embedded:sensor/OxygenGasSensor-OSensor/SEN0322"; | ||
import Timer from "timer"; | ||
|
||
const knownOxygenVal = 20.9; // Known concentration of oxygen in the air, for calibration | ||
const oxygenMV = 0; // The value marked on the sensor, set to 0 unless otherwise required | ||
|
||
const sensor = new Sensor({ | ||
sensor: { | ||
...device.I2C.default, | ||
io: device.io.I2C | ||
} | ||
}); | ||
|
||
sensor.configure({ | ||
vol: knownOxygenVal, | ||
mv: oxygenMV | ||
}); | ||
|
||
Timer.repeat(() => | ||
{ | ||
const sample = sensor.sample(); | ||
trace(`O2: ${sample.O} ppm\n`); | ||
}, 500); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"include": [ | ||
"$(MODDABLE)/modules/io/manifest.json", | ||
"$(MODDABLE)/modules/drivers/sensors/sen0322/manifest.json" | ||
], | ||
"modules": { | ||
"*": "./main" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"modules": { | ||
"embedded:sensor/OxygenGasSensor-OSensor/SEN0322": "$(MODDABLE)/modules/drivers/sensors/sen0322/sen0322" | ||
}, | ||
"preload": [ | ||
"embedded:sensor/OxygenGasSensor-OSensor/SEN0322" | ||
] | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import Timer from "timer"; | ||
|
||
const Register = Object.freeze({ | ||
OXYGEN_DATA: 0x03, ///< register for oxygen data | ||
USER_SET: 0x08, ///< register for users to configure key value manually | ||
AUTUAL_SET: 0x09, ///< register that automatically configure key value | ||
GET_KEY: 0x0A ///< register for obtaining key value | ||
}); | ||
|
||
const I2C_ADDR = 0x73; | ||
|
||
class SEN0322 | ||
{ | ||
#io; | ||
#key; | ||
#onError; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #onError is unused |
||
|
||
constructor(options) { | ||
this.#io = new options.sensor.io({ | ||
address: I2C_ADDR, | ||
hz: 100_000, | ||
...options.sensor | ||
}); | ||
this.#onError = options.onError | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #onError is unused |
||
|
||
} | ||
|
||
configure(options) | ||
{ | ||
if (undefined !== options.vol) | ||
{ | ||
const vol = options.vol; | ||
const mv = options.mv ?? 0; | ||
if (vol < 0 || vol > 25) | ||
throw new RangeError("invalid O2 concentration (must be 0-25)"); | ||
else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The throw above will always, so the content of this else clause can be directly inline below. |
||
{ | ||
const io = this.#io; | ||
let keyValue = vol * 10; | ||
|
||
if (mv < 0.000001 && mv > (-0.000001)) | ||
io.write(Uint8Array.of(Register.USER_SET, keyValue)); | ||
else | ||
{ | ||
keyValue = (vol / mv) * 1000; | ||
io.write(Uint8Array.of(Register.AUTUAL_SET, keyValue)); | ||
} | ||
} | ||
} | ||
} | ||
#setKey() | ||
{ | ||
const io = this.#io; | ||
io.write(Uint8Array.of(Register.GET_KEY)); | ||
const curKey = (new Uint8Array(io.read(1)))[0]; | ||
if (curKey) | ||
this.#key = curKey / 1000.0; | ||
else | ||
this.#key = 20.9 / 120.0; | ||
} | ||
close() | ||
{ | ||
this.#io.close(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. close is defined to be safe to call more than once. This implementation will throw after the first time it is called. We typically use |
||
this.#io = undefined; | ||
} | ||
sample() | ||
{ | ||
const io = this.#io; | ||
let ret = {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is allocated very far from when it is used. Maybe simplify to: return {O: oxygenPercent * 10_000}; // O2 concentration in PPM |
||
this.#setKey(); | ||
io.write(Uint8Array.of(Register.OXYGEN_DATA)); | ||
Timer.delay(100); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, this pauses the VM. I would not use this driver for this alone. Would be nice to find a different way to code this. At the very least document it at the top or readme. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. A simple solution would be to have a timer in the driver to poll the value every 100ms. The driver would store the most recent value and return it from Because |
||
const rxbuf = new Uint8Array(io.read(3)); | ||
const oxygenPercent = ((this.#key) * ((rxbuf[0]) + (rxbuf[1] / 10.0) + (rxbuf[2] / 100.0))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a diversity of approaches to parenthesis in this source file. For example, this line is very careful about parenthesizing just about everything (including this.#key and the entire expression). Compare to line 42 which only parenthesizes a number literal ( |
||
ret.O = oxygenPercent * 10000; // O2 concentration in PPM | ||
return ret; | ||
} | ||
|
||
} | ||
export default SEN0322; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our ECMA-419 sensor drivers usually include a header with: