From b0b401297336ebb841874f5b0ae2b4ca09ac370a Mon Sep 17 00:00:00 2001 From: Benjamin Tayehanpour Date: Sat, 26 Sep 2026 18:42:45 +0000 Subject: [PATCH] LCD --- kladd/LCD-1602-I2C/LCD.py => LCD.py | 26 ++++++++++++++++---------- LICENSE | 3 ++- decoder.py | 1 - fancontrol.py | 4 ++++ 4 files changed, 22 insertions(+), 12 deletions(-) rename kladd/LCD-1602-I2C/LCD.py => LCD.py (81%) create mode 100644 fancontrol.py diff --git a/kladd/LCD-1602-I2C/LCD.py b/LCD.py similarity index 81% rename from kladd/LCD-1602-I2C/LCD.py rename to LCD.py index 03eea42..d697ae0 100644 --- a/kladd/LCD-1602-I2C/LCD.py +++ b/LCD.py @@ -1,12 +1,14 @@ +import asyncio import smbus -import time + +lock = asyncio.lock() class LCD: - def __init__(self, pi_rev = 2, i2c_addr = 0x3F, backlight = True): + async def __init__(self, pi_rev = 2, i2c_addr = 0x3F, backlight = True): # device constants self.I2C_ADDR = i2c_addr - self.LCD_WIDTH = 16 # Max. characters per line + self.LCD_WIDTH = 20 # Max. characters per line self.LCD_CHR = 1 # Mode - Sending data self.LCD_CMD = 0 # Mode - Sending command @@ -47,7 +49,7 @@ class LCD: self.lcd_byte(0x28, self.LCD_CMD) # 101000 Data length, number of lines, font size self.lcd_byte(0x01, self.LCD_CMD) # 000001 Clear display - def lcd_byte(self, bits, mode): + async def lcd_byte(self, bits, mode): # Send byte to data pins # bits = data # mode = 1 for data, 0 for command @@ -63,14 +65,18 @@ class LCD: self.bus.write_byte(self.I2C_ADDR, bits_low) self.toggle_enable(bits_low) - def toggle_enable(self, bits): - time.sleep(self.E_DELAY) + async def toggle_enable(self, bits): + await asyncio.sleep(self.E_DELAY) self.bus.write_byte(self.I2C_ADDR, (bits | self.ENABLE)) - time.sleep(self.E_PULSE) + await asyncio.sleep(self.E_PULSE) self.bus.write_byte(self.I2C_ADDR,(bits & ~self.ENABLE)) - time.sleep(self.E_DELAY) + await asyncio.sleep(self.E_DELAY) + + async def async_message(self, string, line = 1): + async with lock: + result = await asyncio.to_thread(_message, string = string, line = line) - def message(self, string, line = 1): + def _message(self, string, line = 1): # display message string on indicated LCD line if line == 1: lcd_line = self.LCD_LINE_1 @@ -90,6 +96,6 @@ class LCD: for i in range(self.LCD_WIDTH): self.lcd_byte(ord(string[i]), self.LCD_CHR) - def clear(self): + async def clear(self): # clear LCD display self.lcd_byte(0x01, self.LCD_CMD) diff --git a/LICENSE b/LICENSE index 89ba91d..6c3548f 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,7 @@ MIT License -Copyright (c) 2026 sa0tay +Copyright (c) 2026 Benjamin Tayehanpour +LCD.py based on code (c) 2018 Sterling Beason, also MIT Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/decoder.py b/decoder.py index 6cbf11f..ab67d72 100755 --- a/decoder.py +++ b/decoder.py @@ -1,5 +1,4 @@ import asyncio - import asyncpio class decoder: diff --git a/fancontrol.py b/fancontrol.py new file mode 100644 index 0000000..0736881 --- /dev/null +++ b/fancontrol.py @@ -0,0 +1,4 @@ +import asyncio +from decoder import decoder +from display import display +