From 0ea7ede2be2155f51d96e3cc4df62a194948fbf0 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Thu, 3 Nov 2022 23:49:34 +0700 Subject: [PATCH] improve serial --- kuukar/kuukar_lcd.py | 25 ++++++++++++++++++++++--- kuukar/kuukar_nextion.py | 10 ++++------ lcd_cli.py | 10 ++++++++++ lcd_test.py | 4 ---- 4 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 lcd_cli.py delete mode 100644 lcd_test.py diff --git a/kuukar/kuukar_lcd.py b/kuukar/kuukar_lcd.py index 1c4dae2..8e1ebe8 100644 --- a/kuukar/kuukar_lcd.py +++ b/kuukar/kuukar_lcd.py @@ -1,14 +1,19 @@ from time import sleep +from socket import gethostname, gethostbyname import kuukar.kuukar_nextion as kuukar_nextion + + class lcd: def __init__(self) -> None: self.nextion = kuukar_nextion.nextion() sleep(1) self.nextion.send_command("page home") + self.nextion.send_command("ipTXT.txt=\""+str(gethostbyname(gethostname()))+"\"") + self.nextion.external_touch_handler = self.touch_handler - def play_video(self,filename: str) -> None: + def play_video(self, filename: str) -> None: self.nextion.send_command("page video_player") - self.nextion.send_command("video.path=\"sd0/"+filename+".video\"") + self.nextion.send_command("video.path=\"sd0/" + filename + ".video\"") self.nextion.send_command("video.en=1") def pause_video(self) -> None: @@ -20,4 +25,18 @@ class lcd: def stop_video(self) -> None: self.nextion.send_command("video.en=0") self.nextion.send_command("page home") - \ No newline at end of file + + def touch_handler(self, page, component_id, press_type): + print(f'page:{page} cid:{component_id} pt:{press_type}') + if page == 1 and component_id == 2 and press_type == '01': + if self.nextion.get_attribute("aiTG.val"): + print("AI Drive Activated") + else: + print("AI Drive Deactivated") + elif page == 3: + if component_id == 2 and press_type == '00': + x = self.nextion.get_attribute("joystick.x") + y = self.nextion.get_attribute("joystick.y") + speed = 100-(y-60)/(205-60)*200 + turn = (x-5)/(150-5)*200-100 + print(f"Setting speed to {speed} and turn angle to {turn}") diff --git a/kuukar/kuukar_nextion.py b/kuukar/kuukar_nextion.py index 8b97379..02f6155 100644 --- a/kuukar/kuukar_nextion.py +++ b/kuukar/kuukar_nextion.py @@ -18,8 +18,7 @@ class nextion: allow_serial_read = True def __init__(self) -> None: - self.device = serial.Serial(SERIAL_LCD, baudrate=9600, timeout=15) - print(self.device.is_open) + self.device = serial.Serial(SERIAL_LCD, baudrate=115200, timeout=15) self.__send_stop_bit() self.reset_device() threading.Thread(target=self.__serial_reader).start() @@ -62,7 +61,6 @@ class nextion: data = ser.read_until(self.__get_stop_bit()) data = data[0:len(data)-3] data: list = data.hex('/').split('/') - print(data) return data def handle_serial(self, data: list): @@ -75,8 +73,8 @@ class nextion: component_id = int(data.pop(0), 16) press_type = data.pop(0) self.external_touch_handler(page,component_id,press_type) - print("Got a touch event at page "+str(page)+" in component "+str(component_id) - + " with type "+str(press_type)) + #print("Got a touch event at page "+str(page)+" in component "+str(component_id) + # + " with type "+str(press_type)) def external_touch_handler(self, page, component_id, press_type): # To be overridden @@ -84,7 +82,7 @@ class nextion: def get_attribute(self, attribute: str): self.allow_serial_read = False self.send_command("get "+str(attribute)) - sleep(0.1) + sleep(0.01) data = self.read_serial() self.allow_serial_read = True data_type = data.pop(0) diff --git a/lcd_cli.py b/lcd_cli.py new file mode 100644 index 0000000..93c934f --- /dev/null +++ b/lcd_cli.py @@ -0,0 +1,10 @@ +import sys + +import kuukar.kuukar_lcd as kuukar_lcd +from time import sleep + +lcd = kuukar_lcd.lcd() +print("Project KuuKar LCD Device Command Line Interface") +while True: + cmd = input("kuukar_lcd$ ") + lcd.nextion.send_command(cmd) \ No newline at end of file diff --git a/lcd_test.py b/lcd_test.py deleted file mode 100644 index 5bd6177..0000000 --- a/lcd_test.py +++ /dev/null @@ -1,4 +0,0 @@ -import kuukar.kuukar_lcd as kuukar_lcd -from time import sleep -lcd = kuukar_lcd.lcd() -lcd.play_video("keke_hurt") \ No newline at end of file