From 8fe5ac33d09070fb55dabf71a70fb29c6dcd0ba3 Mon Sep 17 00:00:00 2001 From: Asaril Date: Sat, 20 Mar 2021 00:50:11 +0100 Subject: [PATCH] initial --- wetter_gui.py | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100755 wetter_gui.py diff --git a/wetter_gui.py b/wetter_gui.py new file mode 100755 index 0000000..f24b9f6 --- /dev/null +++ b/wetter_gui.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 + +import random +import time +import busio +import digitalio +import pulseio +from board import SCK, MOSI, MISO, D8, D24, D23, D18 + +from adafruit_rgb_display import color565 +import adafruit_rgb_display.ili9341 as ili9341 + +from raspyrfm import * +import sensors +from sensors import rawsensor +import sys +import threading + +from PIL import Image, ImageDraw, ImageFont, ImageColor + +class TextBox: + def __init__(self, pos, size, max_jitter, color, background, fitting_text): + self.color = color + self.background = background + self.pos = pos + self.bounds = (size[0]+1, size[1]+1) + font_size = 100 + while (self.bounds[0] > size[0]-max_jitter) or (self.bounds[1] > size[1]-max_jitter): + font_size-=1 + self.font = ImageFont.truetype("/usr/share/fonts/truetype/noto/NotoMono-Regular.ttf", font_size) + self.bounds = self.font.getsize(fitting_text) + self.size = size + self.img = Image.new("RGB", self.bounds) + self.draw = ImageDraw.Draw(self.img) + + def draw_text(self, display, text, jitter_pos): + self.draw.rectangle((0, 0, self.img.width-1, self.img.height-1), self.background, self.color, 1) + self.draw.text(jitter_pos, text, font=self.font, fill=self.color) + print(f"self:{str(self.__dict__)}") + display.image(self.img, y=self.pos[0], x=display.width-self.pos[1]) + + + +BAUDRATE = 24000000 + +DISPLAY_SIZE=(320,240) +DAY_BRIGHT = 65535 +NIGHT_BRIGHT = 4096 + +CLOCK_FORMAT = "%H:%M" +TEMP_FORMAT = "% 2.1f°C" + +BKGND_COLOR=(0,0,0) +LINE_COLOR=(255,255,255) + +JITTER=5 +DELAY=1 + +now_parts=time.localtime() +time_str = time.strftime(CLOCK_FORMAT, now_parts) +temp_str = TEMP_FORMAT % -10.0 + +clock_box=TextBox( (0,0), (300,90), JITTER, (255,255,255), BKGND_COLOR, time_str) +temp_box =TextBox( (160,200), (160,40), JITTER, (255,0,0), BKGND_COLOR, temp_str) + +# Configuration for CS and DC pins: +CS_PIN = D8 +DC_PIN = D24 +RES_PIN = D23 +BL_PIN = D18 + +# Setup SPI bus using hardware SPI: +spi = busio.SPI(clock=SCK, MOSI=MOSI, MISO=MISO) + +# Create the ILI9341 display: +display = ili9341.ILI9341(spi, + cs=digitalio.DigitalInOut(CS_PIN), + dc=digitalio.DigitalInOut(DC_PIN), + rst=digitalio.DigitalInOut(RES_PIN), + baudrate=BAUDRATE, + width=DISPLAY_SIZE[1], + height=DISPLAY_SIZE[0], + rotation=270) + + +backlight = pulseio.PWMOut(BL_PIN, duty_cycle=NIGHT_BRIGHT) +#backlight = digitalio.DigitalInOut(BL_PIN) +#backlight.direction = digitalio.Direction.OUTPUT +#backlight.value = True + + +if raspyrfm_test(5, RFM69): + print("Found RaspyRFM mod") + rfm = RaspyRFM(5, RFM69) #when using the RaspyRFM twin +else: + print("No RFM69 module found!") + exit() + +rfm.set_params( + Freq = 868.30, #MHz center frequency + Datarate = 17.241, #kbit/s baudrate + ModulationType = rfm69.FSK, #modulation + Deviation = 30, #kHz frequency deviation + SyncPattern = [0x2d, 0xd4], #syncword + Bandwidth = 150, #kHz bandwidth + RssiThresh = -105, #dBm RSSI threshold +) + + +mean_temp = 0 +shutdown = False + +def update_temp(): + global mean_temp + temps = [] + + while not shutdown: + data = rfm.receive(12) + if data: + obj = rawsensor.CreateSensor(data).GetData() + if 'ID' in obj and 'T' in obj: + temps.append(float(obj['T'][0])) + if len(temps)>20: + temps = temps[1:] + if temps: + mean_temp = sum(temps) / len(temps) + + +temp_thread = threading.Thread(target=update_temp, name="Temp Thread") + +#temp_thread.start() + + +display.fill(color565(BKGND_COLOR)) +display.vline(0,clock_box.size[1],1,color565(LINE_COLOR)) + +def jitter(): + return (random.randint(0,JITTER), random.randint(0,JITTER)) + +# Main loop: +jitter_pos = jitter() + +try: + while True: + now = time.time() + now_parts = time.localtime(now) + + time_str = time.strftime(CLOCK_FORMAT, now_parts) + temp_str = TEMP_FORMAT % mean_temp + + clock_box.draw_text(display, time_str, jitter_pos) + + if now_parts.tm_sec:# == 0: + jitter_pos = jitter() + + if (now_parts.tm_min == 0) and (now_parts.tm_hour > 7 and now_parts.tm_hour < 19): + backlight.duty_cycle = DAY_BRIGHT + else: + backlight.duty_cycle = NIGHT_BRIGHT + + while time.time() - now < DELAY: + time.sleep(0.1*DELAY) + +except KeyboardInterrupt: + clock_box.draw_text(display, "off" ,(0,0)) + shutdown=True +# temp_thread.join() +