stabel version

This commit is contained in:
Mobkom21 2026-05-11 23:12:11 +02:00
parent 1c9a743ec3
commit e0a1cdb693
5 changed files with 120 additions and 10 deletions

View File

@ -1,5 +1,11 @@
#!/usr/bin/env python3
import time
import json
import sys
import threading
import subprocess
import numpy as np
import paho.mqtt.client as mqtt
from gps_hat import GPS_DATA
@ -18,12 +24,20 @@ def update_hat_data():
client.publish("MOBKOM/SENS/humidity", sens.humidity)
client.publish("MOBKOM/SENS/pressure", sens.pressure)
#client.publish("MOBKOM/IMU/pitch", sens.pitch, qos=0, retain=False)
#client.publish("MOBKOM/IMU/roll", sens.roll, qos=0, retain=False)
#client.publish("MOBKOM/IMU/yaw", sens.yaw, qos=0, retain=False)
client.publish("MOBKOM/IMU/pitch", sens.pitch)
client.publish("MOBKOM/IMU/roll", sens.roll)
client.publish("MOBKOM/IMU/yaw", sens.yaw)
client.publish("MOBKOM/IMU/x", sens.x)
client.publish("MOBKOM/IMU/y", sens.y)
client.publish("MOBKOM/IMU/z", sens.z)
#client.publish("MOBKOM/IMU/x", sens.x)
#client.publish("MOBKOM/IMU/y", sens.y)
#client.publish("MOBKOM/IMU/z", sens.z)
client.publish("MOBKOM/IMU/x", str(sens.x), qos=0, retain=False)
client.publish("MOBKOM/IMU/y", str(sens.y), qos=0, retain=False)
client.publish("MOBKOM/IMU/z", str(sens.z), qos=0, retain=False)
# print(sens.temp)
def encode_led_set_payload(payload):
@ -114,29 +128,67 @@ def on_connect(client, userdata, flags, rc):
client.subscribe("MOBKOM/LED/set_matrix")
client.subscribe("MOBKOM/LED/get_matrix")
def mqtt_loop_thread(run):
while not run.is_set():
client.loop()
time.sleep(0.01)
if __name__ == "__main__":
# gps = GPS_DATA("/dev/ttyUSB0")
update_timer = 0 # for mqtt update send
run = True # lauf variable für die while schleifen
gps = GPS_DATA("/dev/serial0")
sens = SENS_HAT()
sens.led_clear()
for _ in range(5):
sens.led_set_pixel(0, 0, 0, 0, 0)
time.sleep(0.5)
sens.led_set_pixel(0, 0, 255, 0, 0)
time.sleep(0.5)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 1883)
sens.led_set_pixel(1, 0, 255, 0, 0)
stop_event = threading.Event()
t = threading.Thread(target=mqtt_loop_thread, args=(stop_event,))
t.start()
sens.led_matrix = np.array([[[255,0,0],[255,109,0],[255,219,0],[182,255,0],[73,255,0],[0,255,36],[0,255,146],[0,255,255]],
[[255,109,0],[255,219,0],[182,255,0],[73,255,0],[0,255,36],[0,255,146],[0,255,255],[0,146,255]],
[[255,219,0],[182,255,0],[73,255,0],[0,255,36],[0,255,146],[0,255,255],[0,146,255],[0,36,255]],
[[182,255,0],[73,255,0],[0,255,36],[0,255,146],[0,255,255],[0,146,255],[0,36,255],[73,0,255]],
[[73,255,0],[0,255,36],[0,255,146],[0,255,255],[0,146,255],[0,36,255],[73,0,255],[182,0,255]],
[[0,255,36],[0,255,146],[0,255,255],[0,146,255],[0,36,255],[73,0,255],[182,0,255],[255,0,219]],
[[0,255,146],[0,255,255],[0,146,255],[0,36,255],[73,0,255],[182,0,255],[255,0,219],[255,0,109]],
[[0,255,255],[0,146,255],[0,36,255],[73,0,255],[182,0,255],[255,0,219],[255,0,109],[255,0,0]]])
for y, row in enumerate(sens.led_matrix):
for x, col in enumerate(row):
sens.led_set_pixel(x, y, col[0], col[1], col[2])
update_timer = 0 # for mqtt update send
try:
while True:
if update_timer + 2 < time.time():
while run:
if update_timer + 1 < time.time():
update_hat_data()
update_timer = time.time()
client.loop()
run = not sens.detect_long_press()
time.sleep(0.01)
except KeyboardInterrupt:
print("exit by user")
finally:
stop_event.set()
t.join() # warten bis der thread fertig ist
gps.ser.close()
client.disconnect()
subprocess.run(["sudo", "shutdown", "-h", "0"])
sys.exit(0)

View File

@ -1,4 +1,5 @@
import numpy as np
import time
from sense_hat import SenseHat
class SENS_HAT:
@ -14,6 +15,8 @@ class SENS_HAT:
y = 0
z = 0
time_down = 0
led_matrix = np.zeros((8, 8, 3), dtype=np.uint8)
def __init__(self):
@ -37,6 +40,8 @@ class SENS_HAT:
def led_clear(self):
self.sense.clear()
self.led_matrix = np.zeros((8, 8, 3), dtype=np.uint8)
def led_set_pixel(self, x, y, r, g, b):
if not (0 <= x <= 7) or not (0 <= y <= 7):
@ -61,8 +66,61 @@ class SENS_HAT:
return pixel_data
def detect_long_press(self):
"""
Returns True wenn der Taster für 4 Sekunden gedrückt werde.
"""
for event in self.sense.stick.get_events():
if event.direction == "middle":
if event.action == "pressed":
self.time_down = time.time()
if event.action == "released":
self.time_down = time.time()
for y, row in enumerate(self.led_matrix):
for x, col in enumerate(row):
self.sense.set_pixel(x, y, col[0], col[1], col[2])
if self.time_down + 0.5 < time.time():
self.sense.clear()
button_down_time = time.time() - self.time_down
if button_down_time >= 4:
print("shutdown")
return True
if button_down_time <= 7:
button_down_time_int = int(button_down_time * 2)
else:
button_down_time_int = 7
for y in range(8 - button_down_time_int):
self.sense.set_pixel(7, 7 - y, 255, 0, 0)
return False
if __name__ == "__main__":
sens = SENS_HAT()
print(sens.led_matrix)
print(sens.led_matrix.tolist())
sens.led_matrix = np.array([[[255,0,0],[255,109,0],[255,219,0],[182,255,0],[73,255,0],[0,255,36],[0,255,146],[0,255,255]],
[[255,109,0],[255,219,0],[182,255,0],[73,255,0],[0,255,36],[0,255,146],[0,255,255],[0,146,255]],
[[255,219,0],[182,255,0],[73,255,0],[0,255,36],[0,255,146],[0,255,255],[0,146,255],[0,36,255]],
[[182,255,0],[73,255,0],[0,255,36],[0,255,146],[0,255,255],[0,146,255],[0,36,255],[73,0,255]],
[[73,255,0],[0,255,36],[0,255,146],[0,255,255],[0,146,255],[0,36,255],[73,0,255],[182,0,255]],
[[0,255,36],[0,255,146],[0,255,255],[0,146,255],[0,36,255],[73,0,255],[182,0,255],[255,0,219]],
[[0,255,146],[0,255,255],[0,146,255],[0,36,255],[73,0,255],[182,0,255],[255,0,219],[255,0,109]],
[[0,255,255],[0,146,255],[0,36,255],[73,0,255],[182,0,255],[255,0,219],[255,0,109],[255,0,0]]])
for y, row in enumerate(sens.led_matrix):
for x, col in enumerate(row):
sens.led_set_pixel(x, y, col[0], col[1], col[2])
# print(sens.led_matrix)
# print(sens.led_matrix.tolist())
while True:
sens.detect_long_press()
time.sleep(0.1)