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

Adding SEN0322 O2 Sensor Driver Support #1075

Open
wants to merge 2 commits into
base: public
Choose a base branch
from

Conversation

dabdoue
Copy link
Contributor

@dabdoue dabdoue commented Mar 27, 2023

Created sensor driver as well as example project

@dabdoue dabdoue closed this Mar 27, 2023
@dabdoue dabdoue reopened this Mar 27, 2023
Copy link
Collaborator

@phoddie phoddie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Here are some notes on changes to get it ready to merge.

hz: 100_000,
...options.sensor
});
this.#onError = options.onError
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#onError is unused

{
#io;
#key;
#onError;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#onError is unused

const mv = options.mv ?? 0;
if (vol < 0 || vol > 25)
throw new RangeError("invalid O2 concentration (must be 0-25)");
else
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

}
close()
{
this.#io.close();
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?.close() here instead.

sample()
{
const io = this.#io;
let ret = {};
Copy link
Collaborator

Choose a reason for hiding this comment

The 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

@@ -0,0 +1,80 @@
import Timer from "timer";
Copy link
Collaborator

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:

  • Copyright notice
  • Full name of part
  • Link to relevant manufacturer data sheets
  • Link to any implementations used as a reference

io.write(Uint8Array.of(Register.OXYGEN_DATA));
Timer.delay(100);
const rxbuf = new Uint8Array(io.read(3));
const oxygenPercent = ((this.#key) * ((rxbuf[0]) + (rxbuf[1] / 10.0) + (rxbuf[2] / 100.0)));
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 (mv < 0.000001 && mv > (-0.000001)). More consistent style with this repository would be ((mv < 0.000001( && (mv > -0.000001))) or a better way to express between, (-0.000001 < mv) && (mv < 0.000001)). That could apply to line 34 as well.

const io = this.#io;
this.#setKey();
io.write(Uint8Array.of(Register.OXYGEN_DATA));
Timer.delay(100);
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 sample().

Because sample()1 is allowed to return undefined`, the driver could clear the cached sample after it is read once. The benefit is that a caller will know when they have a truly new reading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants