This repository has been archived by the owner on Dec 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cheesecake.js
125 lines (108 loc) · 2.75 KB
/
cheesecake.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
123
124
125
const chalk = require('chalk');
const { existsSync, watchFile, unwatchFile } = require('fs');
const { execSync } = require('child_process');
const GPIO_ROOT_PATH = '/sys/class/gpio';
function __delay(value) {
execSync(`sleep ${value / 1000}`);
}
function __GpioExport(gpio) {
execSync(`echo "${gpio}" > ${GPIO_ROOT_PATH}/export`);
console.log(
chalk.green(`Cheesecake ${new Date().toJSON()} - Gpio${gpio}: exported ✔`),
);
__delay(100);
}
function __GpioUnexport(gpio) {
execSync(`echo "${gpio}" > ${GPIO_ROOT_PATH}/unexport`);
console.log(
chalk.green(
`Cheesecake ${new Date().toJSON()} - Gpio${gpio}: unexported ✔`,
),
);
}
function __GpioDirection(gpio, direction) {
execSync(`echo "${direction}" > ${GPIO_ROOT_PATH}/gpio${gpio}/direction`);
}
class Gpio {
/**
* Constructor method
* @param {Number} gpio
* @param {String} direction
*/
constructor(gpio, direction) {
this.gpio = Object.freeze(gpio);
this.direction = Object.freeze(direction);
this.gpioPath = Object.freeze(`${GPIO_ROOT_PATH}/gpio${gpio}`);
this.gpioValuePath = Object.freeze(`${this.gpioPath}/value`);
if (!existsSync(this.gpioPath)) {
__GpioExport(this.gpio);
}
__GpioDirection(this.gpio, this.direction);
}
/**
* Retrieve the current GPIO value
*/
getValue() {
try {
return Number(execSync(`cat ${this.gpioValuePath}`).toString().trim());
} catch (_err) {
console.log(
chalk.yellow(
`\r\nCheesecake ${new Date().toJSON()} - Gpio${
this.gpio
} is not available`,
),
);
return Number();
}
}
/**
* Define a new value for GPIO
* @param {Number} value
*/
setValue(value) {
try {
execSync(`echo "${value}" > ${this.gpioValuePath}`);
} catch (_err) {
console.log(
chalk.yellow(
`\r\nCheesecake ${new Date().toJSON()} - Gpio${
this.gpio
} is not available`,
),
);
}
}
/**
* Reverses the effect of exporting GPIO to user space
*/
dispose() {
unwatchFile(this.gpioValuePath);
__GpioUnexport(this.gpio);
}
/**
* The callback will be called when gpio value changed
* @param {function(Number)} callback
*/
onValueChanged(callback) {
if (typeof callback === 'function') {
let currentValue = this.getValue();
watchFile(this.gpioValuePath, { interval: 150 }, () => {
const value = this.getValue();
if (value !== currentValue) {
currentValue = value;
callback(value);
}
});
}
}
}
const GpioValue = Object.freeze({
HIGH: 1,
LOW: 0,
});
const GpioDirection = Object.freeze({
IN: 'in',
OUT: 'out',
});
module.exports = { Gpio, GpioValue, GpioDirection };