Skip to content

Instantly share code, notes, and snippets.

@anotherjesse
Created January 25, 2020 18:14
Show Gist options
  • Select an option

  • Save anotherjesse/6f19f266f38cc760e1b08b6a5bcbad93 to your computer and use it in GitHub Desktop.

Select an option

Save anotherjesse/6f19f266f38cc760e1b08b6a5bcbad93 to your computer and use it in GitHub Desktop.
miffy
# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
# esp.osdebug(None)
import web
import bunny
import uos
import machine
# uos.dupterm(None, 1) # disable REPL on UART(0)
import gc
import webrepl
webrepl.start()
gc.collect()
bunny.looper()
web.serve()
from machine import Pin
from neopixel import NeoPixel
import time
import rand as random
pixels = 24
pin = 13
np = NeoPixel(Pin(pin, Pin.OUT), pixels)
def looper():
for x in range(255, 0, -10):
setColor((0, 0, x))
time.sleep_ms(10)
def setColor(color):
for p in range(0, pixels):
np[p] = color
np.write()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Miffy</title>
<style type="text/css">
html,
body {
margin: 0;
height: 100%;
}
body {
display: flex;
flex-direction: column;
justify-content: center;
background-color: #a9cbee;
}
body div {
min-height: 100px;
width: 100%;
}
input[type="range"] {
-webkit-appearance: none;
background: transparent;
width: 90%;
max-width: 500px;
outline: none;
}
input[type="range"]:focus,
input[type="range"]:active,
input[type="range"]::-moz-focus-inner,
input[type="range"]::-moz-focus-outer {
border: 0;
outline: none;
}
input[type="range"]::-moz-range-thumb {
border: none;
height: 50px;
width: 50px;
background-color: transparent;
background-image: url("https://storage.googleapis.com/pw-stuff/thumbs-sprite.png");
background-position: 0 0;
background-size: cover;
transform: scale(1.9) rotateZ(var(--thumb-rotate, 10deg));
cursor: pointer;
}
input[type="range"]::-moz-range-thumb:active {
background-position: 100% 0px;
transform: scale(2) rotateZ(var(--thumb-rotate, 10deg));
}
input[type="range"]::-moz-range-track {
width: 100%;
height: 20px;
background: #eee;
border-radius: 10px;
box-shadow: 2px 2px 4px rgba(0,0,0,0.4);
cursor: pointer;
}
input[type="range"]::-moz-range-progress {
height: 20px;
background: #4685d7;
border-radius: 10px;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
border: none;
height: 50px;
width: 50px;
background-color: transparent;
background-image: url("https://storage.googleapis.com/pw-stuff/thumbs-sprite.png");
background-position: 0 0;
background-size: cover;
transform: scale(1.9) rotateZ(var(--thumb-rotate, 10deg));
cursor: pointer;
margin-top: -15px;
-webkit-appearance: none;
}
input[type="range"]::-webkit-slider-thumb:active {
background-position: 100% 0px;
transform: scale(2) rotateZ(var(--thumb-rotate, 10deg));
}
input[type="range"]::-webkit-slider-runnable-track {
width: 100%;
height: 20px;
background: #eee;
border-radius: 10px;
box-shadow: 2px 2px 4px rgba(0,0,0,0.4);
cursor: pointer;
-webkit-appearance: none;
}
label {
background: #eee;
border-radius: 50%;
box-shadow: 2px 2px 4px rgba(0,0,0,0.4);
padding: 14px;
margin-left: 10px;
font-family: Roboto, 'Helvetica Neue', Arial;
font-size: 20px;
width: 25px;
text-align: center;
font-weight: bold;
content: '';
}
</style>
</head>
<body>
<div><input id="red" type="range" value="0" />
<label for="red" style="color: #bb2929">0</label></div>
<div><input id="green" type="range" value="0" />
<label for="green" style="color: #30bf23">0</label></div>
<div><input id="blue" type="range" value="0" />
<label for="blue" style="color: #2968bb">0</label></div>
<script>
function update({target}) {
target.style.setProperty('--thumb-rotate', `${target.value * 7.20}deg`);
target.labels[0].innerHTML = Math.round(target.value/100*255);
fetch(`/${red.value}/${green.value}/${blue.value}/`, {method: 'POST'})
}
red.addEventListener('input', update);
green.addEventListener('input', update);
blue.addEventListener('input', update);
// // fetch('/my ', method: 'POST')
</script>
</head>
</html>
from urandom import *
def randrange(start, stop=None):
if stop is None:
stop = start
start = 0
upper = stop - start
bits = 0
pwr2 = 1
while upper > pwr2:
pwr2 <<= 1
bits += 1
while True:
r = getrandbits(bits)
if r < upper:
break
return r + start
def randint(start, stop):
return randrange(start, stop + 1)
def shuffle(seq):
l = len(seq)
for i in range(l):
j = randrange(l)
seq[i], seq[j] = seq[j], seq[i]
import bunny
import socket
def serve():
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)
while True:
cl, addr = s.accept()
print('client connected from', addr)
cl_file = cl.makefile('rwb', 0)
get = False
post = False
while True:
line = cl_file.readline()
if line.startswith('GET / '):
get = True
elif line.startswith('POST /'):
line = line.decode('utf-8')
r, g, b = line.split('/')[1:4]
bunny.setColor((int(r), int(g), int(b)))
post = True
if not line or line == b'\r\n':
break
if get:
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.sendall(open('index.html').read())
if post:
cl.send('HTTP/1.0 204 No Content')
cl.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment