...
 
Commits (4)
const { Kymera } = require('./src/kymera.js');
const { Display } = require('./src/display.js');
module.exports = { Kymera, Display };
......@@ -2,7 +2,7 @@
"name": "@kymera/core",
"version": "1.0.0",
"description": "Kymera core application",
"main": "src/kymera.js",
"main": "index.js",
"bin": {
"kymera": "bin/kymera"
},
......
......@@ -2,7 +2,8 @@ Kymera Core
===========
Kymera core library.
For more information, see [the main Kymera repo][About]
This repository is part of the [Kymera project][Kymera Website]. For more
information, see [the main Kymera repo][About].
[About]: https://gitlab.com/chameleoid/kymera/about
[Kymera Website]: https://kymera.tech
......@@ -8,10 +8,10 @@ liblouis.enableOnDemandTableLoading(path.join(appRoot, 'node_modules', 'liblouis
class Display {
/**
* @constructs
* @param {Gpio[]} pins An array of GPIO pins
* @param {Gpio[]} gpio An array of GPIO objects
*/
constructor(pins) {
this.pins = pins;
constructor(gpio) {
this.gpio = gpio;
this.tables = 'tables/unicode.dis,tables/en-us-comp6.ctb';
}
......@@ -40,8 +40,8 @@ class Display {
* @returns {Promise} Promise to be resolved when done
*/
writeBitArray(bitArray) {
return Promise.all(this.pins.map((point, index) =>
point.write(bitArray[index]),
return Promise.all(this.gpio.map((pin, index) =>
pin.write(bitArray[index]),
));
}
......@@ -74,7 +74,7 @@ class Display {
* @returns {Promise} Promise to be resolved when done
*/
clear() {
return Promise.all(this.pins.map(point => point.write(0)));
return Promise.all(this.gpio.map(pin => pin.write(0)));
}
}
......
const { Display } = require('./display');
module.exports = { Display };
class Kymera {
constructor(options) {
this.paused = options.paused || false;
this.display = new Display(options.displayGPIO);
this.main();
setInterval(() => this.main(), 60 * 1000);
}
main() {
if (this.paused) {
return;
}
this.display.write(
new Date().toLocaleTimeString(undefined, {
hour12: false,
hour: '2-digit',
minute: '2-digit',
})
);
}
}
module.exports = { Kymera };
......@@ -8,7 +8,7 @@ chai.use(require('sinon-chai'));
describe('Display', () => {
before(() => {
points = [
gpio = [
{ write: sinon.stub() },
{ write: sinon.stub() },
{ write: sinon.stub() },
......@@ -22,7 +22,7 @@ describe('Display', () => {
beforeEach(() => {
clock = sinon.useFakeTimers();
display = new Display(points);
display = new Display(gpio);
});
afterEach(() => {
......@@ -41,14 +41,14 @@ describe('Display', () => {
it('should write a bit array to the display', async () => {
await display.writeBitArray([ 1, 1, 0, 1, 1, 0, 0, 0 ]);
expect(points[0].write).to.have.been.calledOnceWith(1);
expect(points[1].write).to.have.been.calledOnceWith(1);
expect(points[2].write).to.have.been.calledOnceWith(0);
expect(points[3].write).to.have.been.calledOnceWith(1);
expect(points[4].write).to.have.been.calledOnceWith(1);
expect(points[5].write).to.have.been.calledOnceWith(0);
expect(points[6].write).to.have.been.calledOnceWith(0);
expect(points[7].write).to.have.been.calledOnceWith(0);
expect(gpio[0].write).to.have.been.calledOnceWith(1);
expect(gpio[1].write).to.have.been.calledOnceWith(1);
expect(gpio[2].write).to.have.been.calledOnceWith(0);
expect(gpio[3].write).to.have.been.calledOnceWith(1);
expect(gpio[4].write).to.have.been.calledOnceWith(1);
expect(gpio[5].write).to.have.been.calledOnceWith(0);
expect(gpio[6].write).to.have.been.calledOnceWith(0);
expect(gpio[7].write).to.have.been.calledOnceWith(0);
});
});
......@@ -89,25 +89,25 @@ describe('Display', () => {
it('should clear the display', async () => {
await display.clear();
expect(points[0].write).to.have.been.calledOnceWith(0);
expect(points[1].write).to.have.been.calledOnceWith(0);
expect(points[2].write).to.have.been.calledOnceWith(0);
expect(points[3].write).to.have.been.calledOnceWith(0);
expect(points[4].write).to.have.been.calledOnceWith(0);
expect(points[5].write).to.have.been.calledOnceWith(0);
expect(points[6].write).to.have.been.calledOnceWith(0);
expect(points[7].write).to.have.been.calledOnceWith(0);
expect(gpio[0].write).to.have.been.calledOnceWith(0);
expect(gpio[1].write).to.have.been.calledOnceWith(0);
expect(gpio[2].write).to.have.been.calledOnceWith(0);
expect(gpio[3].write).to.have.been.calledOnceWith(0);
expect(gpio[4].write).to.have.been.calledOnceWith(0);
expect(gpio[5].write).to.have.been.calledOnceWith(0);
expect(gpio[6].write).to.have.been.calledOnceWith(0);
expect(gpio[7].write).to.have.been.calledOnceWith(0);
});
it('should resolve', done => {
points[0].write.resolves();
points[1].write.resolves();
points[2].write.resolves();
points[3].write.resolves();
points[4].write.resolves();
points[5].write.resolves();
points[6].write.resolves();
points[7].write.resolves();
gpio[0].write.resolves();
gpio[1].write.resolves();
gpio[2].write.resolves();
gpio[3].write.resolves();
gpio[4].write.resolves();
gpio[5].write.resolves();
gpio[6].write.resolves();
gpio[7].write.resolves();
display.clear().then(() => done());
});
......
const chai = require('chai');
const sinon = require('sinon');
const { Display } = require('../src/display');
const { Kymera } = require('../src/kymera');
const { expect } = chai;
chai.use(require('chai-as-promised'));
chai.use(require('sinon-chai'));
describe('Kymera', () => {
before(() => {
gpio = [
{ write: sinon.stub() },
{ write: sinon.stub() },
{ write: sinon.stub() },
{ write: sinon.stub() },
{ write: sinon.stub() },
{ write: sinon.stub() },
{ write: sinon.stub() },
{ write: sinon.stub() },
];
});
beforeEach(() => {
clock = sinon.useFakeTimers();
kymera = new Kymera({
displayGPIO: gpio,
paused: true,
});
});
afterEach(() => {
sinon.resetHistory();
clock.restore();
});
it('should display a time value', () => {
sinon.spy(kymera.display, 'write');
kymera.paused = false;
kymera.main();
let spyCall = kymera.display.write.getCall(0);
expect(spyCall.args[0]).to.equal('00:00');
});
it('should update once a minute', () => {
sinon.spy(kymera.display, 'write');
kymera.paused = false;
clock.tick(1000 * 60);
clock.tick(1000 * 60);
let spyCall0 = kymera.display.write.getCall(0);
let spyCall1 = kymera.display.write.getCall(1);
expect(spyCall0.args[0]).to.equal('00:01');
expect(spyCall1.args[0]).to.equal('00:02');
});
});