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

Add option to round scaled sensor values #1804

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
7 changes: 5 additions & 2 deletions lib/sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Sensor extends Withinable {
this.limit = options.limit || null;
this.threshold = options.threshold === undefined ? 1 : options.threshold;
this.isScaled = false;
this.isScaledRounded = !!options.isScaledRounded;

this.io[`${options.type}Read`](this.pin, data => {
raw = data;
Expand Down Expand Up @@ -195,8 +196,7 @@ class Sensor extends Withinable {
},
scaled: {
get() {
let mapped;
let constrain;
let mapped, constrain;

if (state.scale && raw !== null) {
if (options.type === "digital") {
Expand All @@ -206,6 +206,9 @@ class Sensor extends Withinable {
}

mapped = Fn.fmap(raw, this.range[0], this.range[1], state.scale[0], state.scale[1]);
if (this.isScaledRounded) {
mapped = Math.round(mapped);
}
constrain = Fn.constrain(mapped, state.scale[0], state.scale[1]);

return constrain;
Expand Down
3 changes: 2 additions & 1 deletion test/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ exports["Board"] = {
},

snapshot(test) {
test.expect(83);
test.expect(84);

new Multi({
controller: "BME280",
Expand Down Expand Up @@ -650,6 +650,7 @@ exports["Board"] = {
limit: null,
threshold: 1,
isScaled: false,
isScaledRounded: false,
raw: null,
analog: 0,
constrained: 0,
Expand Down
20 changes: 16 additions & 4 deletions test/sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ exports["Sensor - Resolution"] = {

this.sensor = new Sensor({
pin: "A1",
board: this.board
board: this.board,
});

test.equal(this.sensor.resolution, 1023);
Expand Down Expand Up @@ -79,7 +79,7 @@ exports["Sensor - Analog"] = {
this.analogRead = this.sandbox.spy(MockFirmata.prototype, "analogRead");
this.sensor = new Sensor({
pin: "A1",
board: this.board
board: this.board,
});

// Complete visible property information expected for the above sensor instance,
Expand Down Expand Up @@ -144,6 +144,9 @@ exports["Sensor - Analog"] = {
isScaled: {
type: "boolean"
},
isScaledRounded: {
type: "boolean"
},
raw: {
type: "object"
}, // defined property that returns var inited to null
Expand Down Expand Up @@ -1092,7 +1095,7 @@ exports["Sensor - Analog"] = {
scale(test) {
const callback = this.analogRead.args[0][1];

test.expect(3);
test.expect(4);

// Scale the expected 0-1023 to a value between 50-100 (~75)
this.sensor.scale(50, 100);
Expand All @@ -1117,6 +1120,15 @@ exports["Sensor - Analog"] = {
callback(12);
this.clock.tick(25);

// Ensure sensors round scaled values if isScaledRounded = true
this.sensor.isScaledRounded = true;
this.sensor.scale(0, 25);
this.sensor.once("change", function() {
test.equal(this.value, 6);
});
callback(250);
this.clock.tick(25);

test.done();
}, // ./scale: function(test)

Expand Down Expand Up @@ -1351,7 +1363,7 @@ exports["Sensor - Digital"] = {
this.sensor = new Sensor({
type: "digital",
pin: 3,
board: this.board
board: this.board,
});

this.proto = [{
Expand Down