kuukar-rpi/kuukar/kuukar_lcd.py

66 lines
2.8 KiB
Python

from time import sleep
from socket import gethostname, gethostbyname
import kuukar.kuukar_nextion as kuukar_nextion
import kuukar.kuukar_motion as kuukar_motion
import kuukar.kuukar_sensors as kuukar_sensors
from threading import Thread
class lcd:
def __init__(self, motion: kuukar_motion.motion, sensors: kuukar_sensors.sensors) -> None:
self.nextion = kuukar_nextion.nextion()
self.motion = motion
self.sensors = sensors
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
Thread(target=self._sensor_screen_updator).start()
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.en=1")
def pause_video(self) -> None:
self.nextion.send_command("video.en=2")
def resume_video(self) -> None:
self.nextion.send_command("video.en=1")
def stop_video(self) -> None:
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")
self.motion.roam_start()
else:
print("AI Drive Deactivated")
self.motion.roam_stop()
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")
if x is not None and y is not None:
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}")
if not self.motion.is_roaming():
self.motion.drive_skew(speed,turn)
def _sensor_screen_updator(self):
while True:
self.nextion.send_command(
f"tempTXT.txt=\"{self.sensors.get_temperature()} C\"")
self.nextion.send_command(
f"humidityTXT.txt=\"{self.sensors.get_humidity_pct()}%\"")
self.nextion.send_command(
f"brightnessTXT.txt=\"{self.sensors.get_brightness_pct()}\"")
self.nextion.send_command(
f"sonarTXT.txt=\"{int(self.sensors.sonar_get_distance(2))} / {int(self.sensors.sonar_get_distance(1))} / {int(self.sensors.sonar_get_distance(0))}\"")
sleep(1)