-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
test.js
153 lines (144 loc) · 3.76 KB
/
test.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { PigpioClient } from './lib/PigpioClient.js'
import { PigpioLedChain } from './lib/PigpioLedChain.js'
import './lib/PigpioLedChain/Blinkt.js'
import './lib/PigpioLedChain/P9813.js'
async function delay (ms = 100) {
return new Promise((resolve, reject) => {
setTimeout(() => { resolve() }, ms)
})
}
// const host = 'pi10'
// const config = { // FanSHIM
// gpioClock: 14,
// gpioData: 15,
// nLeds: 1
// }
// const host = 'pi10'
// const config = { // P9813
// gpioClock: 10,
// gpioData: 11,
// nLeds: 2,
// type: 'p9813'
// }
const host = 'pi6'
const config = { // Blinkt!
gpioClock: 24,
gpioData: 23,
nLeds: 8
}
class Test {
constructor () {
this.pi = new PigpioClient({ host })
// this.pi
// .on('request', (request) => { console.log('request: %j', request) })
this.blinkt = config.type === 'p9818'
? new PigpioLedChain.P9813(this.pi, config)
: new PigpioLedChain.Blinkt(this.pi, config)
// this.blinkt
// .on('led', (id, led) => { console.log('led %d: %j', id, led) })
process.on('SIGINT', async (signal) => {
console.log('Got %s', signal)
this.interrupted = true
})
}
async colourLoop (bri = 0x08) {
// let d
let r = 0
let g = 1
let b = 255
while (true) {
if (r < 255 && g === 0 && b === 255) {
// if (r === 0) {
// const now = new Date()
// if (d != null) {
// console.log('cycle took %d ms', now - d)
// }
// d = now
// console.log('red in')
// }
r++
} else if (r === 255 && g === 0 && b > 0) {
// if (b === 255) {
// console.log('blue out')
// }
b--
} else if (r === 255 && g < 255 && b === 0) {
// if (g === 0) {
// console.log('green in')
// }
g++
} else if (r > 0 && g === 255 && b === 0) {
// if (r === 255) {
// console.log('red out')
// }
r--
} else if (r === 0 && g === 255 && b < 255) {
// if (b === 0) {
// console.log('blue in')
// }
b++
} else if (r === 0 && g > 0 && b === 255) {
// if (g === 255) {
// console.log('green out')
// }
g--
}
this.blinkt.setAllLeds(bri, r, g, b)
await this.blinkt.update()
if (this.interrupted) {
return
}
}
}
// Inspired by: https://github.com/pimoroni/blinkt/blob/master/examples/larson.py
async cylon (bri = 0xFF) {
const values = [0, 0, 0, 0, 0, 0, 0, 16, 64, 255, 64, 16, 0, 0, 0, 0, 0, 0, 0]
const delays = [
1 + Math.sin(0.5 * Math.PI),
1 + Math.sin(0.4 * Math.PI),
1 + Math.sin(0.3 * Math.PI),
1 + Math.sin(0.2 * Math.PI),
1 + Math.sin(0.1 * Math.PI),
1 + Math.sin(0.0 * Math.PI),
1 + Math.sin(0.1 * Math.PI),
1 + Math.sin(0.2 * Math.PI),
1 + Math.sin(0.3 * Math.PI),
1 + Math.sin(0.4 * Math.PI),
1 + Math.sin(0.5 * Math.PI)
]
let offset = 11
let up = true
while (true) {
for (let i = 0; i < config.nLeds; i++) {
this.blinkt.setLed(i, bri, values[offset + i], 0, 0)
}
await this.blinkt.update()
await delay(40 * delays[offset])
offset += up ? -1 : 1
if (offset === 11) {
up = true
} else if (offset === 0) {
up = false
}
if (this.interrupted) {
return
}
}
}
async main () {
try {
await this.blinkt.init(true)
await this.colourLoop()
this.interrupted = false
await this.cylon()
console.log('Exiting...')
await this.blinkt.disconnect(true)
await this.pi.disconnect()
} catch (error) {
console.log(error)
} finally {
await this.pi.disconnect()
}
}
}
new Test().main()