Created
May 29, 2019 10:36
-
-
Save blippy/ea7709597cae84c3bcde02d2bc18b1e5 to your computer and use it in GitHub Desktop.
LCD DOG clock with ESP8266 with fancy LCD commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from machine import Pin, SPI | |
import ntptime | |
from utime import sleep_ms | |
import utime | |
import mel | |
rs_pin = Pin(2, Pin.OUT) # Pin D4. Do NOT use the regular MISO pin (D6) | |
cs_pin = Pin(15, Pin.OUT) # aka SS slave select | |
cs_pin.on() | |
#spi = SPI(sck=Pin(14), mosi=Pin(13), miso=Pin(12)) | |
spi = SPI(1) | |
spi.init(phase = 0) | |
spi.init(baudrate=400000) | |
def write_val(csv, c): | |
global cs_pin, rs_pin, spi | |
rs_pin.value(csv) | |
cs_pin.off() | |
spi.write(bytearray([c])) | |
cs_pin.on() | |
sleep_ms(60) | |
def write_arr(csv, arr): | |
global cs_pin, rs_pin, spi | |
rs_pin.value(csv) | |
cs_pin.off() | |
spi.write(bytearray(arr)) | |
cs_pin.on() | |
sleep_ms(60) | |
contrast = 0x70 + 0b1000 # from 0x7C | |
contrast = 0x7C # original value (and better) | |
display = 0b1111 # ori 0x0F | |
init = [0x39, 0x1D, 0x50, 0x6C, contrast , 0x38, display, 0x01, 0x06] | |
for c in init: write_val(0, c) | |
#from machine import RTC | |
rtc = machine.RTC() | |
# execute table instruction N with value VAL | |
def tinst(n, val): | |
write_arr(0, [0b001110000 + n, val]) | |
# turn off cursor and blinking | |
tinst(0, 0b1100) | |
def home(): tinst(0, 0b10) | |
def cls(): tinst(0, 0b1) | |
cls() | |
# https://www.ccsinfo.com/forum/viewtopic.php?t=39552 | |
write_arr(0, [0b00101010, 0b00011000, 0b00101100]) # double size | |
def write_home(text): | |
home() | |
write_arr(1, text) | |
def show_date(): | |
y, mon, d, _, h, mins, s, _ = rtc.datetime() | |
mon, d, h = mel.adjustBST(y, mon, d, h) | |
#text = '{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format(t[0], t[1], t[2], t[4], t[5], t[6]) | |
text = '{:02d}:{:02d}:{:02d}'.format(h, mins, s) | |
write_home(text) | |
show_date() | |
mel.do_connect() | |
def loop(): | |
while True: | |
cls() | |
write_home("NTP") | |
ntptime.settime() | |
for i in range(60*30): # every 30 minutes, approximately | |
show_date() | |
utime.sleep(1) | |
loop() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment