diff --git a/docs/button_index.js.html b/docs/button_index.js.html index 37fd534..8d27e80 100644 --- a/docs/button_index.js.html +++ b/docs/button_index.js.html @@ -166,10 +166,10 @@

button/index.js

const Provider = await getProvider(options, "builtin/digital"); let mode = Provider.Input; - if (!options.isPullup) { + if (typeof options.isPullup !== "undefined") { mode = Provider.InputPullUp; } - if (!options.isPulldown) { + if (typeof options.isPulldown !== "undefined") { mode = Provider.InputPullDown; } @@ -195,12 +195,12 @@

button/index.js

}, downValue: { get: () => { - return 1 ^ this.isPullup ^ this.invert; + return 1 ^ this.#state.isPullup ^ this.invert; } }, upValue: { get: () => { - return 0 ^ this.isPullup ^ this.invert; + return 0 ^ this.#state.isPullup ^ this.invert; } }, holdtime: { @@ -241,7 +241,6 @@

button/index.js

intialize(options, callback) { } processRead() { - if (this.isOpen) { this.emit("open"); timer.clearTimeout(this.#state.interval); diff --git a/lib/button/index.js b/lib/button/index.js index a8c02cd..cbdafa8 100644 --- a/lib/button/index.js +++ b/lib/button/index.js @@ -70,10 +70,10 @@ class Button extends Emitter { const Provider = await getProvider(options, "builtin/digital"); let mode = Provider.Input; - if (!options.isPullup) { + if (typeof options.isPullup !== "undefined") { mode = Provider.InputPullUp; } - if (!options.isPulldown) { + if (typeof options.isPulldown !== "undefined") { mode = Provider.InputPullDown; } @@ -99,12 +99,12 @@ class Button extends Emitter { }, downValue: { get: () => { - return 1 ^ this.isPullup ^ this.invert; + return 1 ^ this.#state.isPullup ^ this.invert; } }, upValue: { get: () => { - return 0 ^ this.isPullup ^ this.invert; + return 0 ^ this.#state.isPullup ^ this.invert; } }, holdtime: { @@ -145,7 +145,6 @@ class Button extends Emitter { intialize(options, callback) { } processRead() { - if (this.isOpen) { this.emit("open"); timer.clearTimeout(this.#state.interval); diff --git a/package.json b/package.json index 14555f3..98c88d3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "j5e", - "version": "0.4.2", + "version": "0.4.3", "description": "j5e is a device framework built for ECMA TC-53's IO pattern", "main": "index.js", "exports": { diff --git a/test/button.js b/test/button.js index cd92510..5284857 100644 --- a/test/button.js +++ b/test/button.js @@ -1,5 +1,6 @@ import assert from "assert"; import sinon from "sinon"; +import chai from "chai"; import { Digital } from "@dtex/mock-io"; import Button from "j5e/button"; @@ -16,6 +17,103 @@ describe("Button", function() { assert.equal(button instanceof Button, true); assert.equal(button.io instanceof Digital, true); }); + + describe("Options", function() { + + describe("invert", function() { + + it("should invert state when the invert option is true", async function() { + const button = await new Button({ + pin: 13, + io: Digital, + invert: true + }); + + button.io.write(0); + assert.equal(button.isClosed, true); + assert.equal(button.isOpen, false); + + button.io.write(1); + assert.equal(button.isClosed, false); + assert.equal(button.isOpen, true); + + }); + }); + + describe("isPullup", function() { + + it("should invert state when the isPullup option is true", async function() { + const button = await new Button({ + pin: 13, + io: Digital, + isPullup: true + }); + + button.io.write(0); + assert.equal(button.isClosed, true); + assert.equal(button.isOpen, false); + + button.io.write(1); + assert.equal(button.isClosed, false); + assert.equal(button.isOpen, true); + + }); + + it("should not invert state when the isPullup and invert options are true", async function() { + const button = await new Button({ + pin: 13, + io: Digital, + isPullup: true, + invert: true + }); + + button.io.write(1); + assert.equal(button.isClosed, true); + assert.equal(button.isOpen, false); + + button.io.write(0); + assert.equal(button.isClosed, false); + assert.equal(button.isOpen, true); + + }); + + }); + + describe("isPulldown", function() { + + it("should not invert state when the isPulldown option is true", async function() { + const button = await new Button({ + pin: 13, + io: Digital, + isPulldown: true + }); + + button.io.write(1); + assert.equal(button.isClosed, true); + assert.equal(button.isOpen, false); + + button.io.write(0); + assert.equal(button.isClosed, false); + assert.equal(button.isOpen, true); + + }); + }); + + describe("holdtime", function() { + + it("should set the holdtime state when passed a value in options", async function() { + const button = await new Button({ + pin: 13, + io: Digital, + holdtime: 2000 + }); + + assert.equal(button.holdtime, 2000); + + }); + }); + + }); }); describe("Properties", function() { @@ -70,6 +168,198 @@ describe("Button", function() { assert.equal(button.isOpen, true); }); }); + + describe("holdtime", function() { + + it("should be settable and getable", async function() { + const button = await new Button({ + pin: 13, + io: Digital + }); + + assert.equal(button.holdtime, 500); + button.holdtime = 1000; + assert.equal(button.holdtime, 1000); + + }); + + it("should emit hold event after 500ms", async function() { + + const clock = sinon.useFakeTimers(); + const holdSpy = sinon.spy(); + const button = await new Button({ + pin: 13, + io: Digital + }); + + button.on("hold", holdSpy); + + clock.tick(1); + button.io.write(1); + assert.equal(holdSpy.callCount, 0); + clock.tick(499); + assert.equal(holdSpy.callCount, 0); + clock.tick(10); + assert.equal(holdSpy.callCount, 1); + + clock.restore(); + + }); + + it("should emit hold event after 2000ms", async function() { + + const clock = sinon.useFakeTimers(); + const holdSpy = sinon.spy(); + const button = await new Button({ + pin: 13, + io: Digital, + holdtime: 2000 + }); + + button.on("hold", holdSpy); + + clock.tick(1); + button.io.write(1); + assert.equal(holdSpy.callCount, 0); + clock.tick(1999); + assert.equal(holdSpy.callCount, 0); + clock.tick(10); + assert.equal(holdSpy.callCount, 1); + + clock.restore(); + + }); + + + + }); + + describe("downValue", function() { + + it("should have the correct default value", async function() { + + const button = await new Button({ + pin: 13, + io: Digital + }); + + assert.equal(button.downValue, 1); + + }); + + it("should have the correct value when invert is true", async function() { + + const button = await new Button({ + pin: 13, + io: Digital, + invert: true + }); + + assert.equal(button.downValue, 0); + + }); + + it("should have the correct value when isPullup is true", async function() { + + const button = await new Button({ + pin: 13, + io: Digital, + isPullup: true + }); + + assert.equal(button.downValue, 0); + + }); + + it("should have the correct value when isPullup and invert are true", async function() { + + const button = await new Button({ + pin: 13, + io: Digital, + isPullup: true, + invert: true + }); + + assert.equal(button.downValue, 1); + + }); + + it("should not be settable", async function() { + const button = await new Button({ + pin: 13, + io: Digital + }); + + chai.expect(() => { + button.downValue = 1; + }).to.throw(TypeError); + }); + + }); + + describe("upValue", function() { + + it("should have the correct default value", async function() { + + const button = await new Button({ + pin: 13, + io: Digital + }); + + assert.equal(button.upValue, 0); + + }); + + it("should have the correct value when invert is true", async function() { + + const button = await new Button({ + pin: 13, + io: Digital, + invert: true + }); + + assert.equal(button.upValue, 1); + + }); + + it("should have the correct value when isPullup is true", async function() { + + const button = await new Button({ + pin: 13, + io: Digital, + isPullup: true + }); + + assert.equal(button.upValue, 1); + + }); + + it("should have the correct value when isPullup and invert are true", async function() { + + const button = await new Button({ + pin: 13, + io: Digital, + isPullup: true, + invert: true + }); + + assert.equal(button.upValue, 0); + + }); + + it("should not be settable", async function() { + const button = await new Button({ + pin: 13, + io: Digital + }); + + chai.expect(() => { + button.upValue = 1; + }).to.throw(TypeError); + }); + + }); + }); describe("Events", function() { @@ -167,5 +457,4 @@ describe("Button", function() { }); }); - });