Moved Blinking logic to the ProotState class.
Made ProotState class a singleton.
This commit is contained in:
parent
8dab8805e6
commit
25c9b953bf
1 changed files with 68 additions and 60 deletions
120
rpi/antRender.py
120
rpi/antRender.py
|
|
@ -5,17 +5,42 @@ from PIL import Image
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import math
|
import math
|
||||||
from scipy.optimize import linear_sum_assignment
|
from scipy.optimize import linear_sum_assignment
|
||||||
|
import threading
|
||||||
|
|
||||||
|
|
||||||
|
print("start configuring matrix")
|
||||||
|
startT = curr_time = round(time.time()*1000)
|
||||||
|
|
||||||
|
# Configuration for the matrix screens
|
||||||
|
options = RGBMatrixOptions()
|
||||||
|
options.rows = 32
|
||||||
|
options.cols = 64
|
||||||
|
options.chain_length = 2
|
||||||
|
options.parallel = 1
|
||||||
|
options.hardware_mapping = 'regular' # If you have an Adafruit HAT: 'adafruit-hat'
|
||||||
|
matrix = RGBMatrix(options=options)
|
||||||
|
|
||||||
|
endT = curr_time = round(time.time()*1000)
|
||||||
|
print("configuring matrix took: " + str(endT - startT) + " ms")
|
||||||
|
|
||||||
|
|
||||||
|
# array to hold the blink animation frames
|
||||||
|
blinkFrameCanvases = []
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ProotState:
|
class ProotState:
|
||||||
def __init__(self):
|
_instance = None
|
||||||
self.current_blink_state = 0
|
|
||||||
self.desired_blink_state = 0
|
|
||||||
|
|
||||||
def startBlink(self):
|
def __new__(cls):
|
||||||
self.desired_blink_state = 10
|
if cls._instance is None:
|
||||||
|
cls._instance = super().__new__(cls)
|
||||||
|
cls._instance.current_blink_state = 0
|
||||||
|
cls._instance.desired_blink_state = 0
|
||||||
|
cls._instance.blinks_frames_ready = False
|
||||||
|
return cls._instance
|
||||||
|
|
||||||
def next_blink_state(self) -> int:
|
def next_blink_frame_number(self) -> int:
|
||||||
if self.current_blink_state == self.desired_blink_state and self.current_blink_state == 10:
|
if self.current_blink_state == self.desired_blink_state and self.current_blink_state == 10:
|
||||||
self.desired_blink_state = 0
|
self.desired_blink_state = 0
|
||||||
return 10
|
return 10
|
||||||
|
|
@ -27,6 +52,19 @@ class ProotState:
|
||||||
|
|
||||||
return self.current_blink_state
|
return self.current_blink_state
|
||||||
|
|
||||||
|
def set_desired_blink_state(self, state: int):
|
||||||
|
self.desired_blink_state = state
|
||||||
|
|
||||||
|
def set_blinks_frames_ready(self, ready: bool):
|
||||||
|
self.blinks_frames_ready = ready
|
||||||
|
|
||||||
|
def get_desired_blink_state(self) -> int:
|
||||||
|
return self.desired_blink_state
|
||||||
|
|
||||||
|
def get_blinks_frames_ready(self) -> bool:
|
||||||
|
return self.blinks_frames_ready
|
||||||
|
|
||||||
|
|
||||||
class Point2D:
|
class Point2D:
|
||||||
x = 0
|
x = 0
|
||||||
y = 0
|
y = 0
|
||||||
|
|
@ -156,24 +194,24 @@ def interpolate_point_pairs(pairs: list[tuple[Point2D, Point2D]], percentage: fl
|
||||||
return interpolated_points
|
return interpolated_points
|
||||||
|
|
||||||
|
|
||||||
|
def update_screen():
|
||||||
|
# TODO move blinking animation logic to the ProotState class
|
||||||
|
global blinkFrameCanvases, matrix
|
||||||
|
|
||||||
|
proot_state = ProotState()
|
||||||
|
|
||||||
|
if proot_state.get_blinks_frames_ready():
|
||||||
|
matrix.SwapOnVSync(blinkFrameCanvases[proot_state.next_blink_frame_number()])
|
||||||
|
|
||||||
|
|
||||||
|
def interrupt_timer():
|
||||||
|
while True:
|
||||||
|
update_screen()
|
||||||
|
time.sleep(0.01)
|
||||||
|
|
||||||
|
# Create and start the interrupt thread
|
||||||
print("start configuring matrix")
|
screen_update_thread = threading.Thread(target=interrupt_timer)
|
||||||
startT = curr_time = round(time.time()*1000)
|
screen_update_thread.start()
|
||||||
|
|
||||||
# Configuration for the matrix
|
|
||||||
options = RGBMatrixOptions()
|
|
||||||
options.rows = 32
|
|
||||||
options.cols = 64
|
|
||||||
options.chain_length = 2
|
|
||||||
options.parallel = 1
|
|
||||||
options.hardware_mapping = 'regular' # If you have an Adafruit HAT: 'adafruit-hat'
|
|
||||||
matrix = RGBMatrix(options=options)
|
|
||||||
|
|
||||||
endT = curr_time = round(time.time()*1000)
|
|
||||||
print("configuring matrix took: " + str(endT - startT) + " ms")
|
|
||||||
|
|
||||||
|
|
||||||
print("start setting ProotScreen")
|
print("start setting ProotScreen")
|
||||||
|
|
@ -215,8 +253,6 @@ print("generating pixel array took: " + str(endT - startT) + " ms")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
print("start pairing points for one eye")
|
print("start pairing points for one eye")
|
||||||
startT = curr_time = round(time.time()*1000)
|
startT = curr_time = round(time.time()*1000)
|
||||||
|
|
||||||
|
|
@ -228,11 +264,6 @@ endT = curr_time = round(time.time()*1000)
|
||||||
print("pairing points for one eye took: " + str(endT - startT) + " ms")
|
print("pairing points for one eye took: " + str(endT - startT) + " ms")
|
||||||
|
|
||||||
|
|
||||||
DesiredBlinkState = 10
|
|
||||||
currentBlinkState = 0
|
|
||||||
|
|
||||||
blinkFrameCanvases = []
|
|
||||||
|
|
||||||
|
|
||||||
print("start populating matrices for each blink frame")
|
print("start populating matrices for each blink frame")
|
||||||
startT = curr_time = round(time.time()*1000)
|
startT = curr_time = round(time.time()*1000)
|
||||||
|
|
@ -254,30 +285,9 @@ for alpha in range(0,11):
|
||||||
endT = curr_time = round(time.time()*1000)
|
endT = curr_time = round(time.time()*1000)
|
||||||
print("populating matrices for each blink frame took: " + str(endT - startT) + " ms")
|
print("populating matrices for each blink frame took: " + str(endT - startT) + " ms")
|
||||||
|
|
||||||
|
proot_state = ProotState()
|
||||||
|
|
||||||
def update_screen():
|
proot_state.set_blinks_frames_ready(True)
|
||||||
# TODO move blinking animation logic to the ProotState class
|
|
||||||
global DesiredBlinkState, currentBlinkState, blinkFrameCanvases, matrix
|
|
||||||
|
|
||||||
# open eye again after blink
|
|
||||||
if currentBlinkState == 10:
|
|
||||||
DesiredBlinkState = 0
|
|
||||||
|
|
||||||
if currentBlinkState == DesiredBlinkState:
|
|
||||||
next_canvas = blinkFrameCanvases[currentBlinkState]
|
|
||||||
next_canvas = matrix.SwapOnVSync(next_canvas)
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
next_canvas = blinkFrameCanvases[currentBlinkState]
|
|
||||||
|
|
||||||
if currentBlinkState < DesiredBlinkState:
|
|
||||||
currentBlinkState += 1
|
|
||||||
else:
|
|
||||||
currentBlinkState -= 1
|
|
||||||
|
|
||||||
next_canvas = matrix.SwapOnVSync(next_canvas)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -290,8 +300,8 @@ def on_connect(client, userdata, flags, response_code):
|
||||||
def on_message(client, userdata, message):
|
def on_message(client, userdata, message):
|
||||||
print("Received message '" + str(message.payload) + "' on topic '"
|
print("Received message '" + str(message.payload) + "' on topic '"
|
||||||
+ message.topic + "' with QoS " + str(message.qos))
|
+ message.topic + "' with QoS " + str(message.qos))
|
||||||
global DesiredBlinkState
|
proot_state = ProotState()
|
||||||
DesiredBlinkState = 10
|
proot_state.set_desired_blink_state(10)
|
||||||
|
|
||||||
# MQTT broker configuration
|
# MQTT broker configuration
|
||||||
broker_address = "10.1.13.173" # Replace with your MQTT broker's address
|
broker_address = "10.1.13.173" # Replace with your MQTT broker's address
|
||||||
|
|
@ -309,9 +319,7 @@ client.loop_start()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
# this sleep sets the time between finishing one screen update and the next starting
|
# this sleep sets the time between finishing one screen update and the next starting
|
||||||
# TODO replace this mechanism with an interupt to use the cpu time between frame updates.
|
pass
|
||||||
time.sleep(0.01)
|
|
||||||
update_screen()
|
|
||||||
|
|
||||||
|
|
||||||
# TODO create a splash screen to display super quick before the rest of the assets are loading
|
# TODO create a splash screen to display super quick before the rest of the assets are loading
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue