Skip to content

Instantly share code, notes, and snippets.

@boochow
Created September 30, 2017 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boochow/23e8bfc4eb0ec7c260f8a72c61e46883 to your computer and use it in GitHub Desktop.
Save boochow/23e8bfc4eb0ec7c260f8a72c61e46883 to your computer and use it in GitHub Desktop.
show current yen/dollar exchange rate on LCD
from micropython import const
from machine import Pin, I2C
import time
CLEAR_DISPLAY = const(0x01)
RETURN_HOME = const(0x02)
SET_ENTRY_MODE = const(0x04)
DISPLAY_ON = const(0x0C)
FUNCTION_SET = const(0x20)
OSC_FREQUENCY = const(0x10)
POW_ICON_CONTRAST = const(0x50)
FOLLOWER = const(0x60)
SET_CONTRAST = const(0x70)
SET_DDRAM_ADDR = const(0x80)
class ST7032i(object):
def __init__(self, i2c, addr=0x3e):
self.i2c = i2c
self.addr = addr
self.buf = bytearray(2)
self.init_display()
def write_cmd(self, cmd):
self.buf[0] = 0x00
self.buf[1] = cmd
self.i2c.writeto(self.addr, self.buf)
def write_data(self, char):
self.buf[0] = 0x40
self.buf[1] = char
self.i2c.writeto(self.addr, self.buf)
def init_display(self):
for cmd in (
# data is 8 bits | 2 lines mode
FUNCTION_SET | 0x10 | 0x08,
# select Instruction table 1
FUNCTION_SET | 0x10 | 0x08 | 0x01,
# BS=1 (1/4 bias), OSC frequency=183Hz
OSC_FREQUENCY | 0x08 | 0x04,
# ICON on, Booster on, Contrast(upper 2bits) = 10
POW_ICON_CONTRAST | 0x08 | 0x04 | 0x02,
# Contrast(lower 4bits) = 1000
SET_CONTRAST | 0x08,
# Follower on, voltage regulator amplified ratio = 3.33
FOLLOWER | 0x08 | 0x03
):
self.write_cmd(cmd)
time.sleep(0.2)
for cmd in (
DISPLAY_ON,
CLEAR_DISPLAY,
# Left to right, no shift
SET_ENTRY_MODE | 0x02
):
self.write_cmd(cmd)
def clear(self):
self.write_cmd(CLEAR_DISPLAY)
def set_cursor(self, row, col=0):
row = min(0x3f, max(0, row))
col = min(1, max(0, col))
self.write_cmd(SET_DDRAM_ADDR | row + (0x00, 0x40)[col])
def print_str(self, s):
for c in s:
self.write_data(ord(c))
def wlan_connect(ssid='SSID', password='PASSWD'):
import network
wlan = network.WLAN(network.STA_IF)
if not wlan.active() or not wlan.isconnected():
wlan.active(True)
print('connecting to:', ssid)
wlan.connect(ssid, password)
while not wlan.isconnected():
pass
def http_get(url):
import socket
_, _, host, path = url.split('/', 3)
addr = socket.getaddrinfo(host, 80)[0][-1]
s = socket.socket()
s.connect(addr)
s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
while True:
line=s.readline()
if len(line.rstrip()) == 0:
break
line = ""
while True:
data = s.recv(100)
if data:
line = line + str(data, 'utf8')
else:
break
s.close()
return line
def main():
import ujson
wlan_connect()
i2c = I2C(scl=Pin(21), sda=Pin(22), freq=400000)
i2c.scan() # workaround for SPLC792-I2C-M communication problem
lcd = ST7032i(i2c)
content = http_get('http://api.fixer.io/latest?base=USD&symbols=JPY')
parsed = ujson.loads(content)
lcd.set_cursor(0, 0);
lcd.print_str(parsed["date"]);
lcd.set_cursor(0, 1);
lcd.print_str("JPY/" + parsed["base"] + ": " + str(parsed["rates"]["JPY"]))
@boochow
Copy link
Author

boochow commented Sep 30, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment