improve serial

This commit is contained in:
Siwat Sirichai 2022-11-03 23:49:34 +07:00
parent ee8cf1037d
commit 0ea7ede2be
4 changed files with 36 additions and 13 deletions

View File

@ -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:
@ -21,3 +26,17 @@ class lcd:
self.nextion.send_command("video.en=0")
self.nextion.send_command("page home")
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}")

View File

@ -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)

10
lcd_cli.py Normal file
View File

@ -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)

View File

@ -1,4 +0,0 @@
import kuukar.kuukar_lcd as kuukar_lcd
from time import sleep
lcd = kuukar_lcd.lcd()
lcd.play_video("keke_hurt")