Mercurial > code > home > repos > 7key
changeset 1:5a93179ccae9
7key code
author | drewp@bigasterisk.com |
---|---|
date | Thu, 11 May 2023 15:07:10 -0700 |
parents | b0c0a808af2b |
children | 3fe178258a80 |
files | display.py keys.py main.py net.py |
diffstat | 4 files changed, 199 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/display.py Thu May 11 15:07:10 2023 -0700 @@ -0,0 +1,45 @@ +import time +import random +from machine import Pin, PWM, SPI +from ST7735 import TFT, TFTColor +from sysfont import sysfont + + +class Display: + def __init__(self): + self.lcd_backlight = PWM(Pin(17), freq=20000, duty=1023) + self.backlight_level = 0 # to 100 + self.spi = SPI(1, + baudrate=20000000, + polarity=0, + phase=0, + sck=Pin(5), + mosi=Pin(18)) + self.tft = TFT( + self.spi, + aDC=19, + aReset=16, # not connected + aCS=21) + self.tft.rotation(2) + self.tft.initr() + self.tft.rgb(True) + + self.tft.fill(TFT.BLACK) + # for x in range(0, self.tft.size()[0], 6): + # self.tft.line((0, 0), (x, self.tft.size()[1] - 1), + # TFTColor(random.randrange(0, 256), + # random.randrange(0, 256), + # random.randrange(0, 256))) + def startupText(self, row, msg): + y1 = (row + 1) * 10 + self.tft.fillrect((0, y1), (128, 10), TFT.BLACK) + self.tft.text((3, y1), msg, TFT.WHITE, sysfont) + + def backlight(self, new, step_sec=0.005): + v = self.backlight_level + while v != new: + v = v + (-1 if v > new else (1 if v < new else 0)) + self.lcd_backlight.duty(v * 10) + self.backlight_level = v + time.sleep(step_sec) +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/keys.py Thu May 11 15:07:10 2023 -0700 @@ -0,0 +1,26 @@ +from machine import Pin +class Keys: + def __init__(self): + + self.rows = [ + Pin(23, Pin.OUT), + Pin(22, Pin.OUT), + ] + self.cols = [ + Pin(36, Pin.IN, pull=-1), # these don't have internal pullups + Pin(39, Pin.IN, pull=-1), + Pin(34, Pin.IN, pull=-1), + Pin(35, Pin.IN, pull=-1) + ] + self.prev = '' + + def newKeysDown(self): + res = '' + for r, rp in enumerate(self.rows): + rp.value(0) + for c, cp in enumerate(self.cols): + res += str(1 - cp()) + rp.value(1) + if res != self.prev: + self.prev = res + return res \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.py Thu May 11 15:07:10 2023 -0700 @@ -0,0 +1,85 @@ +''' +see https://github.com/ustropo/Python_ILI9486/blob/master/Python_ILI9486/ILI9486.py + +keyboard: +key esp +--------- +1 35 & pullup +b 22 +3 34 & pullup +a 23 +0 39 & pullup +2 36 & pullup + +ST7785: +esp lcd +----------- +3.3 vcc +gnd gnd +21 cs +3.3 reset +19 a0 +18 sda +5 sdk +17/tx2 led + +====================================================== + +prev lcd: +esp - LCD +32 blk lcd_d1 +33 brn lcd_d0 +25 red lcd_d7 +26 orn lcd_d6 +27 yel lcd_d5 +14 grn lcd_d4 +12 blu lcd_d3 +13 vio lcd_d2 + +NC reset +NC 3.3v +5v/VIN 5v +gnd gnd +18 lcd_rd +5 lcd_wr +17/TX2 lcd_rs/dc +16/RX2 lcd_cs +3.3v lcd_rst +NC f_cs +''' +import time + +import esp +from machine import Pin + +from net import Net +from keys import Keys +from display import Display + +# esp.osdebug(None) +# import gc +# gc.collect() + +display = Display() +display.startupText(0, "Startup:") +display.backlight(100) + +net = Net() +net.connect_wifi(display.startupText) +net.connect_mqtt(display.startupText) +display.backlight(50) +keys = Keys() + +while True: + net.mqtt_poll() + r = keys.newKeysDown() + if r is not None: + net.send('7key/sensor', r) + last_message = time.time() + +print("go2") +blue_led = Pin(2, Pin.OUT) +print(res) +blue_led(1) +time.sleep(.05) +blue_led(0) \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/net.py Thu May 11 15:07:10 2023 -0700 @@ -0,0 +1,43 @@ +import network +import secrets +from umqttsimple import MQTTClient + + +class Net: + def connect_wifi(self, status): + status(1, "wifi: ") + station = network.WLAN(network.STA_IF) + station.active(True) # activate the interface + status(1, "wifi: on") + + station.connect(secrets.ssid, secrets.wifi_pass) + status(1, "wifi: on/connect") + + while station.isconnected() == False: + pass + + status(1, "wifi: on/connect/ok") + + def connect_mqtt(self, status): + client_id = '7key' + status(2, "mqtt: ") + self.client = MQTTClient(client_id, secrets.mqtt_broker) + + self.client.set_callback(self.on_subscribed_msg) + status(2, "mqtt: connect") + self.client.connect() + status(2, "mqtt: connect/ok") + + def subscribe(self, topic, cb): + self.client.subscribe(topic) + + def on_subscribed_msg(self, topic, msg): + print('mqtt ->', (topic, msg)) + if topic == b'notification' and msg == b'received': + print('ESP received hello message') + + def mqtt_poll(self): + self.client.check_msg() + + def send(self, topic, msg): + self.client.publish(topic.encode('ascii'), msg.encode('ascii'))