Add MicroPython snippets

This commit is contained in:
Lauri Võsandi 2017-07-13 22:49:53 +03:00
parent 11fc4cf30e
commit a47694399e
2 changed files with 126 additions and 0 deletions

33
boot.py Normal file
View File

@ -0,0 +1,33 @@
# Just in case prevent boot loops
from time import sleep
print("Press Ctrl-C to stop boot script...")
sleep(1)
# Connect to wireless network as client
import network
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect("Robootikaklubi", "u4HNj3sgYK")
print("Copy paste following to WebREPL client:")
print("ws://%s:8266/" % sta_if.ifconfig()[0])
# Synchronize clock
import ntptime
ntptime.settime()
# Start WebREPL server
import webrepl
webrepl.start(password="salakala")
# Derive hostname
import ubinascii
MAC_SUFFIX=ubinascii.hexlify(sta_if.config("mac")[-3:])
HOSTNAME="ESP_%s" % MAC_SUFFIX.decode("ascii").upper()
# Perform HTTP POST request
#import urequests
#urequests.post("http://sumo.koodur.com/pub/", data=HOSTNAME)
# Clean up
import gc
gc.collect()

93
main.py Normal file
View File

@ -0,0 +1,93 @@
from time import sleep, localtime
from machine import Pin, Timer
clock = Pin(3, mode=Pin.OUT)
latch = Pin(0, mode=Pin.OUT)
data = Pin(2, mode=Pin.OUT)
lut = [
0b1011, # 0
0b1001, # 1
0b1100, # 2
0b1000, # 3
0b0000, # 4
0b0100, # 5
0b0001, # 6
0b0011, # 7
0b0010, # 8
0b1010, # 9
]
def bitbang_bit(value):
if value & 1:
data.on()
else:
data.off()
clock.on()
clock.off()
def bitbang_digit(digit, width=4):
for i in range(0,width):
bitbang_bit(lut[digit] << i << 1 >> width)
def bitbang_latch():
latch.on()
latch.off()
state = 0
timer = Timer(-1)
def schedule(delay=0):
if delay:
timer.init(period=1000, mode=Timer.ONE_SHOT, callback=dump)
else:
dump()
def dump_time(hour, minute, second):
bitbang_bit(state)
bitbang_digit(hour // 10)
bitbang_bit(state)
bitbang_bit(state)
bitbang_bit(state)
bitbang_bit(state)
bitbang_digit(hour % 10)
bitbang_bit(state)
bitbang_bit(state)
bitbang_bit(state)
bitbang_bit(state)
bitbang_digit(minute // 10)
bitbang_bit(state)
bitbang_bit(state)
bitbang_bit(state)
bitbang_bit(state)
bitbang_digit(minute % 10)
bitbang_bit(state)
bitbang_bit(state)
bitbang_bit(state)
bitbang_bit(state)
bitbang_digit(second // 10)
bitbang_bit(state)
bitbang_bit(state)
bitbang_bit(state)
bitbang_bit(state)
bitbang_digit(second % 10)
bitbang_bit(state)
bitbang_bit(state)
bitbang_bit(state)
def dump(t=None):
global state
year, month, day, hour, minute, second, _, millis = localtime()
dump_time((hour + 3) % 24, minute, second)
bitbang_latch()
state = 1-state
schedule(1000-millis)
schedule()