kuukar-rpi/kuukar/kuukar_lcd.py

65 lines
2.8 KiB
Python
Raw Normal View History

2022-11-03 08:53:27 +00:00
from time import sleep
2022-11-03 16:49:34 +00:00
from socket import gethostname, gethostbyname
2022-10-30 08:57:00 +00:00
import kuukar.kuukar_nextion as kuukar_nextion
2022-11-04 04:58:20 +00:00
import kuukar.kuukar_motion as kuukar_motion
import kuukar.kuukar_sensors as kuukar_sensors
2022-11-04 15:34:50 +00:00
from threading import Thread
2022-11-03 16:49:34 +00:00
2022-10-23 06:49:59 +00:00
class lcd:
2022-11-04 04:58:20 +00:00
def __init__(self, motion: kuukar_motion.motion, sensors: kuukar_sensors.sensors) -> None:
self.nextion = kuukar_nextion.nextion()
2022-11-04 15:34:50 +00:00
self.motion = motion
self.sensors = sensors
2022-11-03 08:53:27 +00:00
sleep(1)
2022-10-26 05:44:58 +00:00
self.nextion.send_command("page home")
2022-11-04 15:34:50 +00:00
self.nextion.send_command(
"ipTXT.txt=\"" + str(gethostbyname(gethostname())) + "\"")
2022-11-03 16:49:34 +00:00
self.nextion.external_touch_handler = self.touch_handler
2022-11-04 15:34:50 +00:00
Thread(target=self._sensor_screen_updator).start()
2022-10-23 06:49:59 +00:00
2022-11-03 16:49:34 +00:00
def play_video(self, filename: str) -> None:
2022-10-26 05:44:58 +00:00
self.nextion.send_command("page video_player")
2022-11-03 16:49:34 +00:00
self.nextion.send_command("video.path=\"sd0/" + filename + ".video\"")
2022-10-26 05:44:58 +00:00
self.nextion.send_command("video.en=1")
2022-10-23 06:49:59 +00:00
def pause_video(self) -> None:
2022-10-26 05:44:58 +00:00
self.nextion.send_command("video.en=2")
2022-10-23 06:49:59 +00:00
def resume_video(self) -> None:
2022-10-26 05:44:58 +00:00
self.nextion.send_command("video.en=1")
2022-10-23 06:49:59 +00:00
def stop_video(self) -> None:
2022-10-26 05:44:58 +00:00
self.nextion.send_command("video.en=0")
self.nextion.send_command("page home")
2022-11-03 16:49:34 +00:00
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")
2022-11-09 15:59:21 +00:00
self.motion.roam_start()
2022-11-03 16:49:34 +00:00
else:
print("AI Drive Deactivated")
2022-11-09 15:59:21 +00:00
self.motion.roam_stop()
2022-11-03 16:49:34 +00:00
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")
2022-11-04 04:58:20 +00:00
speed = 100 - (y - 60) / (205 - 60) * 200
turn = (x - 5) / (150 - 5) * 200 - 100
2022-11-03 16:49:34 +00:00
print(f"Setting speed to {speed} and turn angle to {turn}")
2022-11-09 15:59:21 +00:00
if not self.motion.is_roaming():
self.motion.drive_skew(speed,turn)
2022-11-04 04:58:20 +00:00
def _sensor_screen_updator(self):
2022-11-04 15:34:50 +00:00
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(
2022-11-09 15:59:21 +00:00
f"sonarTXT.txt=\"{int(self.sensors.sonar_get_distance(2))} / {int(self.sensors.sonar_get_distance(1))} / {int(self.sensors.sonar_get_distance(0))}\"")
2022-11-04 15:34:50 +00:00
sleep(1)