Navigation Menu

Skip to content

Commit

Permalink
add som play screps
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Jul 12, 2017
1 parent 3bfd12f commit c6ef6d5
Show file tree
Hide file tree
Showing 9 changed files with 597 additions and 2 deletions.
429 changes: 427 additions & 2 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions package.json
Expand Up @@ -6,5 +6,10 @@
"dependencies": {
"crypto-js": "^3.1.9-1",
"noble": "^1.8.1"
},
"optionalDependencies": {
"body-parser": "^1.17.2",
"express": "^4.15.3",
"tinygradient": "^0.3.1"
}
}
18 changes: 18 additions & 0 deletions play/go.js
@@ -0,0 +1,18 @@
const Blub = require('..')
const blubs = []
const paired = []

Blub.discover(blub => {
const {address} = blub
if (!paired.includes(address)) {
Blub.pair(blub, dispatch => {
blubs.push(dispatch)
paired.push(address)
})
}
})

const colors = [0x33, 0xcc, 0xff]

setInterval(() =>
blubs.forEach(dispatch => dispatch(Blub.setColors(...colors, 0xff))), 250)
12 changes: 12 additions & 0 deletions play/http.js
@@ -0,0 +1,12 @@
const net = require('net')
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const socket = net.connect('/tmp/blub')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.post('/colors', function (request, response) {
socket.write(request.body.color)
response.send('thanks!')
})
app.listen(8018)
5 changes: 5 additions & 0 deletions play/index.html
@@ -0,0 +1,5 @@
<!doctype html>
<form action="http://localhost:8018/colors" method="POST">
<input type="color" name="color">
<input type="submit" value="please">
</form>
47 changes: 47 additions & 0 deletions play/rainbow-gradient.js
@@ -0,0 +1,47 @@
Array.prototype.mapPairs = function (fn) {
// only works on evenly lengthed lists
const length = this.length
const out = []
for (let i = 0; i < length; i += 2) {
out.push(fn([this[i], this[i + 1]]))
}
return out
}
const net = require('net')
const socket = net.connect('/tmp/blub')
const tinygradient = require('tinygradient')

const rainbow = tinygradient([
'#ff2217',
'#ffaa11',
'#eeee22',
'#33ff66',
'#33ccff',
'#1111ee',
'purple',
'#9933ec',
'#ff22ee',
'#ff2217'
]).rgb(9 * 9 * 2)

// const rainbow = tinygradient([
// 'red',
// 'orange',
// 'yellow',
// 'green',
// 'blue',
// 'indigo',
// 'violet'
// ]).rgb(7 * 7 * 2)

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))

;(async () => {
while (true) {
for (const color of rainbow) {
console.log(`writing ${color.toHexString()}`)
socket.write(color.toHexString())
await new Promise(resolve => setTimeout(resolve, 7 * 7 * 2))
}
}
})()
33 changes: 33 additions & 0 deletions play/rainbow.js
@@ -0,0 +1,33 @@
const net = require('net')
const socket = net.connect('/tmp/blub')

// const rainbow = [
// [0xff, 0x22, 0x17],
// [0xff, 0xaa, 0x11],
// [0xee, 0xee, 0x22],
// [0x33, 0xff, 0x99],
// [0x33, 0xcc, 0xff],
// [0x11, 0x11, 0xee],
// [0x99, 0x33, 0xec],
// [0xff, 0x22, 0xee]
// ]

const rainbow = [
'ff2217',
'ffaa11',
'eeee22',
'33ff66',
'33ccff',
'1111ee',
'9933ec',
'ff22ee'
]

;(async () => {
let index = 0
while (true) {
index = index >= rainbow.length ? 0 : index + 1
socket.write(`#${rainbow[index]}`)
await new Promise(resolve => setTimeout(resolve, 500))
}
})()
38 changes: 38 additions & 0 deletions play/socket.js
@@ -0,0 +1,38 @@
const fs = require('fs')
const net = require('net')
const Blub = require('..')
const blubs = []
const paired = []

Blub.discover(blub => {
const {address} = blub
if (!paired.includes(address)) {
Blub.pair(blub, dispatch => {
blubs.push(dispatch)
paired.push(address)
})
}
})

const path = '/tmp/blub'

fs.unlink(path, () => {
const server = net.createServer(connection => {
connection.on('data', data => {
const hexmatch = data.toString().match(/^#([\da-z]{2})([\da-z]{2})([\da-z]{2})$/)
const rgbmatch = data.toString().match(/^rgb\(\s*(\d+),\s*(\d+),\s*(\d+)\)$/)
const brightness = 0xff
let red, green, blue
if (hexmatch) {
[red, green, blue] = hexmatch.slice(1).map(n => parseInt(n, 16))
}
if (rgbmatch) {
[red, green, blue] = rgbmatch.slice(1)
}
blubs.forEach(dispatch => dispatch(Blub.setColors(red, green, blue, brightness)))
})
}).listen(path, () => {
console.log('server bound on %s', path)
})
})

12 changes: 12 additions & 0 deletions play/vap.js
@@ -0,0 +1,12 @@
(async () => {
const net = require('net')
const socket = net.connect('/tmp/blub')
const pink = '#f8caff'
const blue = '#72f0ca'
let color = pink
while (true) {
color = color === pink ? blue : pink
socket.write(color)
await new Promise(resolve => setTimeout(resolve, 700))
}
})()

0 comments on commit c6ef6d5

Please sign in to comment.