-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplayFunctions.js
107 lines (89 loc) · 2.71 KB
/
displayFunctions.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
import { exec, spawn } from 'child_process'
const rpiRgbLedMatrixPath = '../rpi-rgb-led-matrix'
const ledRowsCount = 16
const textScrollerPath = '/utils/text-scroller'
const fontPath = rpiRgbLedMatrixPath + '/fonts/9x18.bdf'
const PATH = process.cwd() + '/'
const commandPending = null
const executeCommand = (command, args) =>
{
console.log('Will execute command :', command)
console.log('With args :', args)
if (!PATH) console.error('Path must be defined')
let scriptOutput = ""
const promise = new Promise((resolve, reject) =>
{
commandPending = spawn(command, args, { env: { path: PATH } })
.on('close', function(code)
{
console.log('Command closed : ', code)
resolve({ scriptOutput: scriptOutput, code: code })
})
.on('error', function(error)
{
console.error('Command error : ', error)
reject({ scriptOutput: scriptOutput, code: error })
})
.on('exit', function(data)
{
console.error('Command exited : ', data)
reject({ scriptOutput: scriptOutput, code: data })
})
.on('disconnect', function(data)
{
console.error('Command disconnected : ', data)
reject({ scriptOutput: scriptOutput, code: data })
})
.on('message', function(data)
{
console.error('Command message : ', data)
})
commandPending.stdout.setEncoding('utf8')
commandPending.stdout.on('data', function(data)
{
console.log('stdout: ' + data)
data = data.toString()
scriptOutput += data
})
commandPending.stderr.setEncoding('utf8')
commandPending.stderr.on('data', function(data)
{
console.log('stderr: ' + data)
data = data.toString()
scriptOutput += data
})
})
return promise
.then(res => ({ command: command, status: 'success', result: res }))
.catch(err => ({ command: command, status: 'error', result: err }))
}
// int loopCount | -1 for endless
// int speed | 0 for no scrolling
const displayText = (
text = 'undefined',
speed = 2,
r = 255,
g = 255,
b = 255,
loopCount = 1
) =>
{
reset()
const color = `${r},${g},${b}`
const command = `${rpiRgbLedMatrixPath}${textScrollerPath}`
const args = [ `--led-rows=${ledRowsCount}`, '-f', fontPath, '-s', speed, '-l', loopCount, '-C', color, `${text}` ]
return executeCommand(command, args)
}
// TODO
const displayImage = () =>
{
const commandFilePath = '../led-image-viewer'
}
const reset = () =>
{
if (!commandPending)
return 'No command pending'
commandPending.kill()
return 'Done'
}
export { displayText, displayImage, reset }