kuukar-rpi/kuukar/kuukar_nextion.py

106 lines
3.3 KiB
Python
Raw Permalink Normal View History

import binascii
from time import sleep
import serial
import threading
2022-11-03 08:53:27 +00:00
from kuukar.kuukar_config import SERIAL_LCD
DATATYPE_NUMBER = '71'
DATATYPE_STRING = '70'
PRESSTYPE_PRESS = '01'
PRESSTYPE_RELEASE = '00'
EVENT_TOUCH = '65'
class nextion:
__commands_output_buffer__ = []
2022-11-03 08:53:27 +00:00
allow_serial_read = True
def __init__(self) -> None:
2022-11-03 16:49:34 +00:00
self.device = serial.Serial(SERIAL_LCD, baudrate=115200, timeout=15)
2022-10-28 07:14:00 +00:00
self.__send_stop_bit()
self.reset_device()
2022-10-28 07:14:00 +00:00
threading.Thread(target=self.__serial_reader).start()
threading.Thread(target=self.__serial_writer__).start()
def send_command(self, command: str):
command = bytes(command, 'ascii')
self.__commands_output_buffer__.append(command)
def __serial_writer__(self):
2022-11-03 08:53:27 +00:00
while True:
if len(self.__commands_output_buffer__) > 0:
self.device.write(self.__commands_output_buffer__.pop(0))
self.__send_stop_bit()
sleep(0.05)
def reset_device(self):
self.send_command("rest")
2022-10-28 07:14:00 +00:00
def __get_stop_bit(self) -> bytearray:
stop_bit = bytearray()
stop_bit.append(0xFF)
stop_bit.append(0xFF)
stop_bit.append(0XFF)
return stop_bit
2022-10-28 07:14:00 +00:00
def __send_stop_bit(self):
self.device.write(self.__get_stop_bit())
2022-10-28 07:14:00 +00:00
def __serial_reader(self) -> None:
ser = self.device
while True:
2022-11-11 15:32:00 +00:00
while self.allow_serial_read:
buffer_length = ser.in_waiting
if buffer_length > 0:
self.handle_serial(self.read_serial())
2022-11-11 15:32:00 +00:00
sleep(0.05)
def read_serial(self) -> list:
ser = self.device
2022-11-03 08:53:27 +00:00
data = ser.read_until(self.__get_stop_bit())
data = data[0:len(data)-3]
data: list = data.hex('/').split('/')
return data
def handle_serial(self, data: list):
message_type = data.pop(0)
if message_type == EVENT_TOUCH:
self.handle_touch_event(data)
def handle_touch_event(self, data: list):
page = int(data.pop(0), 16)
component_id = int(data.pop(0), 16)
press_type = data.pop(0)
2022-11-03 08:53:27 +00:00
self.external_touch_handler(page,component_id,press_type)
2022-11-03 16:49:34 +00:00
#print("Got a touch event at page "+str(page)+" in component "+str(component_id)
# + " with type "+str(press_type))
2022-11-03 08:53:27 +00:00
def external_touch_handler(self, page, component_id, press_type):
# To be overridden
pass
def get_attribute(self, attribute: str):
self.allow_serial_read = False
self.send_command("get "+str(attribute))
2022-11-03 16:49:34 +00:00
sleep(0.01)
data = self.read_serial()
self.allow_serial_read = True
data_type = data.pop(0)
if data_type == DATATYPE_STRING:
data = nextion.hex_string_list_to_bytes(data)
return data.decode('ascii')
elif data_type == DATATYPE_NUMBER:
return nextion.little_endian_to_int(data)
@staticmethod
def hex_string_list_to_bytes(hexstring_list: list) -> bytes:
hexstring = ''.join(hexstring_list)
return binascii.unhexlify(hexstring)
@staticmethod
def little_endian_to_int(data: list) -> int:
data.reverse()
hexstring = ''.join(data)
value = int(hexstring, 16)
return value