-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
181 lines (153 loc) · 4.43 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const express = require('express')
const app = express()
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const path = require('path')
const bodyParser = require('body-parser')
const morgan = require('morgan')
const ip = require('ip')
const PORT = 3000
const serverIP = `${ip.address()}:${PORT}`
const moment = require('moment')
const FadeCandy = require('node-fadecandy')
const getRandomColors = require('./getRandomColors')
const fill = require('./utils/fill')
const five = require('johnny-five');
const Raspi = require('raspi-io');
const board = new five.Board({
io: new Raspi(),
repl: false
});
// express setup
app.set('view engine', 'pug')
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(morgan('tiny'))
app.use(express.static(path.join(__dirname, 'public')))
app.set('views', path.join(__dirname, 'views'))
// route for user interface
app.get('/', (req, res, next) => {
res.render('index')
next()
})
let RED = [255, 0, 83]
let GREEN = [78, 236, 108]
let BLUE = [0, 167, 255]
let BLACK = [0, 0, 0]
let WHITE = [255, 255, 255]
let HALF_WHITE = [125, 125, 125]
let YELLOW = [255, 235, 52]
let ORANGE = [255, 97, 0]
let PURPLE = [172, 43, 185]
let DARK_BLUE = [45, 41, 135]
let TAN = [238, 190, 138]
let OFF_WHITE = [60, 60, 30]
let HOURS_COLOR = PURPLE
let MINUTES_COLOR = GREEN
let SECONDS_COLOR = BLUE
let BACKGROUND_COLOR = BLACK
let TIMER_DURATION = 30000
let sleep = false
let alwaysOn = false
let DURATION = 1000
const PIXEL_COUNT = 60
let colorArray = new Array(PIXEL_COUNT)
// fill lights with background color
fill.withColor(colorArray, BACKGROUND_COLOR, PIXEL_COUNT)
setInterval(() => {
if (sleep && !alwaysOn) {
fill.withColor(colorArray, BLACK, PIXEL_COUNT)
return
}
let mTime = moment(new Date()).subtract(6, 'hours')
let minutes = mTime.minutes()
let seconds = mTime.seconds()
let hours = Math.floor((mTime.hours()%12+(minutes/60))*5)
fill.withColor(colorArray, BACKGROUND_COLOR, PIXEL_COUNT)
fill.hours(colorArray, hours, HOURS_COLOR)
fill.minutes(colorArray, minutes, MINUTES_COLOR)
fill.seconds(colorArray, seconds, SECONDS_COLOR)
}, DURATION)
// FADE CANDY
let fc = new FadeCandy()
fc.on(FadeCandy.events.READY, function () {
console.log('FadeCandy is ready...')
fc.clut.create()
fc.config.set(fc.Configuration.schema.DISABLE_DITHERING, 0)
fc.config.set(fc.Configuration.schema.DISABLE_KEYFRAME_INTERPOLATION, 0)
})
fc.on(FadeCandy.events.COLOR_LUT_READY, function () {
console.log('FaceCandy says color lut ready')
let frame = 0
// start clock
let clock_interval = setInterval(function () {
fc.send([].concat.apply([], colorArray))
frame++
}, DURATION)
})
function emitColorChange(socket, allSockets) {
// broadcast to all clients
io.sockets.emit('color-changed', {
background: BACKGROUND_COLOR,
hours: HOURS_COLOR,
minutes: MINUTES_COLOR,
seconds: SECONDS_COLOR
})
}
io.on('connection', socket => {
socket.emit('color-changed', {
background: BACKGROUND_COLOR,
hours: HOURS_COLOR,
minutes: MINUTES_COLOR,
seconds: SECONDS_COLOR
})
socket.emit('always-on', alwaysOn)
// user has selected new color
socket.on('color-selected', color => {
BACKGROUND_COLOR = color.background
HOURS_COLOR = color.hours
MINUTES_COLOR = color.minutes
SECONDS_COLOR = color.seconds
// broadcast back to any other clients
emitColorChange(socket)
})
// user selected pick random colors
socket.on('pick-random-colors', () => {
getRandomColors()
.then(colors => {
HOURS_COLOR = colors[0]
MINUTES_COLOR = colors[1]
SECONDS_COLOR = colors[2]
emitColorChange(socket)
})
.catch(error => {
console.log(error)
})
})
// always on bypass
socket.on('always-on', value => {
console.log('always-on was fired')
alwaysOn = value
// broadcast back to any other clients
socket.broadcast.emit('always-on', alwaysOn)
})
})
// Johnny-five motion sensor feature
let timerId = null
let motionTimer = function (timerId) {
clearTimeout(timerId)
sleep = false
// No motion so lets start a timer and clear the clock when it ends
timerId = setTimeout(() => {
sleep = true
}, TIMER_DURATION)
}
board.on("ready", function() {
let motion = new five.Motion(0)
motion.on("motionstart", function() {
console.log('motion detected!')
motionTimer(timerId)
})
})
server.listen(PORT, () => console.log(`Listening on ${serverIP}`))