This repository has been archived by the owner on Mar 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
122 lines (98 loc) · 2.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
module.exports = (function() {
'use strict';
const exec = require('node-exec-promise').exec;
class OmegaOled {
constructor() {
this._chainMode = false;
this._commandChain = [];
this._is = {
poweredOn: false,
inverted: false,
dimmed: false
};
}
/**
* execute command line oled-exp binary with given command
* @param command
* @param isExecuteChain
*/
_executeCommand(command, isExecuteChain) {
if (this._chainMode && !isExecuteChain) {
this._commandChain.push(command);
return Promise.resolve(this._commandChain);
}
return exec('oled-exp ' + command);
}
_getFlag(property) {
return this._is[property];
}
_setFlag(carry, value, property) {
this._is[property] = value;
return carry;
}
init() {
return this._executeCommand('-i')
.then(result => this._setFlag(result, true, 'poweredOn'));
}
clear() {
return this._executeCommand('-c');
}
power(on) {
if (typeof on === 'undefined') {
return this._getFlag('poweredOn');
}
return this._executeCommand('power ' + (on ? 'on' : 'off'))
.then(result => this._setFlag(result, !!on, 'poweredOn'));
}
invert(on) {
if (typeof on === 'undefined') {
return this._getFlag('inverted');
}
return this._executeCommand('invert ' + (on ? 'on' : 'off'))
.then(result => this._setFlag(result, !!on, 'inverted'));
}
dim(on) {
if (typeof on === 'undefined') {
return this._getFlag('dimmed');
}
return this._executeCommand('dim ' + (on ? 'on' : 'off'))
.then(result => this._setFlag(result, !!on, 'dimmed'));
}
cursor(row, column) {
return this._executeCommand('cursor ' + [row, column].join(','));
}
cursorPixel(row, pixel) {
return this._executeCommand('cursorPixel ' + [row, pixel].join(','));
}
write(string) {
return this._executeCommand("write '" + string + "'");
}
writeByte(byte) {
return this._executeCommand('writeByte ' + byte);
}
scroll(direction) {
return this._executeCommand('scroll ' + direction);
}
draw(imageFile) {
return this._executeCommand('draw ' + imageFile);
}
/**
* turn on/off chain mode
* chain mode will collect commands until executeChain() is triggered
* and then execute all commands in one call
* @param on
*/
chainMode(on) {
if (typeof on === 'undefined') {
return this._chainMode;
}
this._chainMode = on;
}
executeChain() {
const executionResult = this._executeCommand(this._commandChain.join(' '), true);
this._commandChain = [];
return executionResult;
}
}
return new OmegaOled();
})();