Mais conteúdo relacionado Acenda a sua luz com Javascript1. ACENDA A SUA LUZ
COM JS
+ um despacito
@ ABCDev - São Caetano do Sul 22/09/18
2. CAIQUE MITSUOKA Sorocabano
Ruby / JS / Elixir
1ª vez em São Caetano do Sul
Eu sou legal no twitter, segue lá
Sommelier de litrão
twitter.com/caiquemitsuoka
github.com/caiquemitsuoka
15. Essa é a história de como
acender um led com JS.
23. Socket @ Server
const SocketServer = {
setupClient: () => {
SocketServer.server.on('connection', (client) => {
client.on(
'register',
SocketServer.handleRegister(client)
).on(
'disconnect',
SocketServer.handleDisconnect(client)
)
})
},
...
}
24. Socket @ Server
const SocketServer = {
...
handleRegister: (client) => {
return (data) => {
client.lightId = getMd5(data)
SocketServer.lights[client.lightId] = {
client: client
}
client.emit('registered', client.lightId)
}
},
...
}
25. Socket @ Server
const SocketServer = {
setupClient: () => {
SocketServer.server.on('connection', (client) => {
client.on(
'register',
SocketServer.handleRegister(client)
).on(
'disconnect',
SocketServer.handleDisconnect(client)
)
})
},
...
}
26. Socket @ Server
const SocketServer = {
...
handleDisconnect: (client) => {
return () => {
delete SocketServer.lights[client.lightId]
}
}
}
27. const lightsController = {
edit: (request, response) =>
lights[request.params.id].client.emit('status.update')
response.redirect('/')
}
}
Controller @ Server
29. const state = {
mainLight: false
}
const toggleMainLight = () => {
if(state.mainLight) {
LightLighter.off()
} else {
LightLighter.on()
}
state.mainLight = !state.mainLight
}
Toggle @ Client
37. Mas a parte boa é que vc
não precisa! Vc só
precisa entender o
suficiente
43. const gpio = require('rpi-gpio')
const lightLighter = {
io4: 7,
off: () => {
gpio.destroy();
},
on: () => {
gpio.setup(lightLighter.io4, gpio.DIR_OUT, console.log)
}
}
LightLighter @ Pi
70. const gp = require('gamepad')
const ACTIONS = {
0:'yellow',
1:'green',
2:'red',
3:'blue',
4:'orange',
}
VigilantGuitar @ Client
71. module.exports = VigilantGuitar = {
init: () => {
gp.init()
VigilantGuitar.CONTROLLERS = []
for (var i = 0, l = gp.numDevices(); i < l; i++) {
VigilantGuitar.CONTROLLERS
.push(gp.deviceAtIndex(i))
}
...
VigilantGuitar @ Client
73. start: (callback) => {
VigilantGuitar.buttonSetup(callback)
setInterval(gp.processEvents, 50)
},
VigilantGuitar @ Client
74. buttonSetup: (callback) => {
gp.on('down', (id, num) => {
if(id === VigilantGuitar.controller.deviceID){
callback(ACTIONS[num])
}
})
}
VigilantGuitar @ Client
76. const SocketServer = {
...
handlePanelConnect: (client) => {
return () => {
client.join('panels')
}
},
...
}
Socket @ Server
78. const SocketServer = {
...
handleGuitarPress: (_client) => {
return (button) => {
SocketServer.server
.to('panels')
.emit(`guitar.${button}`)
}
},
...
}
Socket @ Server
80. var colors = {
green: '#12580d',
...
}
var socket = io()
var changeColor = function(colorName) {
return () => {
document.querySelector('[data-panel]')
.style.backgroundColor =
colors[colorName]
}
}
Script @ /guitar