1+ """
2+ SpiritBoard helper class
3+ """
14import math
25import os
36import random
47import time
5-
68import displayio
9+
10+ # pylint: disable=import-error
711from anchored_tilegrid import AnchoredTileGrid
812
13+
914class SpiritBoard (displayio .Group ):
1015 """
1116 DisplayIO Based SpiritBoard
1217
1318 Holds and manages everything needed to draw the spirit board and planchette, as well
1419 as move the planchette around to output messages from the spirits.
1520 """
16-
1721 # Mapping of letters and words on the board to their pixel coordinates.
1822 # Points are centered on the target letter.
1923 # Words can contain a list of points, the planchette will move between them.
@@ -44,13 +48,16 @@ def __init__(self, display):
4448
4549 # board image file
4650 self .spirit_board_odb = displayio .OnDiskBitmap ("spirit_board_480x320.bmp" )
47- self .spirit_board_tilegrid = displayio .TileGrid (bitmap = self .spirit_board_odb , pixel_shader = self .spirit_board_odb .pixel_shader )
51+ self .spirit_board_tilegrid = displayio .TileGrid (
52+ bitmap = self .spirit_board_odb , pixel_shader = self .spirit_board_odb .pixel_shader )
53+
4854 self .append (self .spirit_board_tilegrid )
4955
5056 # planchette image file
5157 self .planchette_odb = displayio .OnDiskBitmap ("planchette_v1.bmp" )
5258 self .planchette_odb .pixel_shader .make_transparent (0 )
53- self .planchette_tilegrid = AnchoredTileGrid (bitmap = self .planchette_odb , pixel_shader = self .planchette_odb .pixel_shader )
59+ self .planchette_tilegrid = AnchoredTileGrid (
60+ bitmap = self .planchette_odb , pixel_shader = self .planchette_odb .pixel_shader )
5461
5562 # AnchoredTileGrid is used so that we can move the planchette
5663 # relative to the cetner of the window.
@@ -66,15 +73,15 @@ def __init__(self, display):
6673 display .root_group = self
6774
6875 @staticmethod
69- def dist (pointA , pointB ):
76+ def dist (point_a , point_b ):
7077 """
7178 Calculate the distance between two points.
7279
73- :param tuple pointA : x,y pair of the first point
74- :param pointB : x,y pair of the second point
80+ :param tuple point_a : x,y pair of the first point
81+ :param point_b : x,y pair of the second point
7582 :return: the distance between the two points
7683 """
77- return math .sqrt ((pointB [0 ] - pointA [0 ]) ** 2 + (pointB [1 ] - pointA [1 ]) ** 2 )
84+ return math .sqrt ((point_b [0 ] - point_a [0 ]) ** 2 + (point_b [1 ] - point_a [1 ]) ** 2 )
7885
7986 def slide_planchette (self , target_location , delay = 0.1 , step_size = 4 ):
8087 """
@@ -114,12 +121,14 @@ def slide_planchette(self, target_location, delay=0.1, step_size=4):
114121 # variables used to calculate where the next point
115122 # between where we are at and where we are going is.
116123 distance_ratio = step_size / distance
117- one_minus_distance_ratio = ( 1 - distance_ratio )
124+ one_minus_distance_ratio = 1 - distance_ratio
118125
119126 # calculate the next point
120127 next_point = (
121- round (one_minus_distance_ratio * current_location [0 ] + distance_ratio * target_location [0 ]),
122- round (one_minus_distance_ratio * current_location [1 ] + distance_ratio * target_location [1 ])
128+ round (one_minus_distance_ratio * current_location [0 ]
129+ + distance_ratio * target_location [0 ]),
130+ round (one_minus_distance_ratio * current_location [1 ]
131+ + distance_ratio * target_location [1 ])
123132 )
124133 # print(current_location)
125134 # print(next_point)
@@ -149,12 +158,14 @@ def slide_planchette(self, target_location, delay=0.1, step_size=4):
149158
150159 # distance ratio variables used to calculate next point
151160 distance_ratio = step_size / distance
152- one_minus_distance_ratio = ( 1 - distance_ratio )
161+ one_minus_distance_ratio = 1 - distance_ratio
153162
154163 # calculate the next point
155164 next_point = (
156- round (one_minus_distance_ratio * current_location [0 ] + distance_ratio * target_location [0 ]),
157- round (one_minus_distance_ratio * current_location [1 ] + distance_ratio * target_location [1 ])
165+ round (one_minus_distance_ratio * current_location [0 ]
166+ + distance_ratio * target_location [0 ]),
167+ round (one_minus_distance_ratio * current_location [1 ]
168+ + distance_ratio * target_location [1 ])
158169 )
159170
160171 # if we have not arrived at the target location yet
@@ -219,7 +230,8 @@ def write_message(self, message, skip_spaces=True, step_size=6):
219230 # if the word has only a single point
220231 elif isinstance (SpiritBoard .LOCATIONS [word ], tuple ):
221232 # slide the planchette to the point
222- self .slide_planchette (SpiritBoard .LOCATIONS [word ], delay = 0.02 , step_size = step_size )
233+ self .slide_planchette (SpiritBoard .LOCATIONS [word ],
234+ delay = 0.02 , step_size = step_size )
223235
224236 # pause at the point
225237 time .sleep (0.5 )
@@ -230,7 +242,8 @@ def write_message(self, message, skip_spaces=True, step_size=6):
230242 # loop over each character in the word
231243 for character in word :
232244 # slide the planchette to the current characters location
233- self .slide_planchette (SpiritBoard .LOCATIONS [character ], delay = 0.02 , step_size = step_size )
245+ self .slide_planchette (SpiritBoard .LOCATIONS [character ],
246+ delay = 0.02 , step_size = step_size )
234247
235248 # pause after we arrive
236249 time .sleep (0.5 )
@@ -239,7 +252,8 @@ def write_message(self, message, skip_spaces=True, step_size=6):
239252 if not skip_spaces and index < len (message_words ) - 1 :
240253 # handle the space
241254 # slide the planchette to the empty space location.
242- self .slide_planchette (SpiritBoard .LOCATIONS [" " ], delay = 0.02 , step_size = step_size )
255+ self .slide_planchette (SpiritBoard .LOCATIONS [" " ],
256+ delay = 0.02 , step_size = step_size )
243257
244258 # pause after we arrive
245259 time .sleep (0.5 )
@@ -298,7 +312,7 @@ def read_local_messages_file(shuffle=False) -> list:
298312 # if the spirit_messages.txt file exists
299313 if "spirit_messages.txt" in os .listdir ("/" ):
300314 # open the file
301- with open ("/spirit_messages.txt" , "r" ) as f :
315+ with open ("/spirit_messages.txt" , "r" , encoding = "utf-8" ) as f :
302316 # split on newline and set the messages found into the context
303317 messages = f .read ().split ("\n " )
304318
@@ -311,7 +325,7 @@ def read_local_messages_file(shuffle=False) -> list:
311325 "messages to spirit_messages.txt." )
312326
313327 # if there are messages and we need to shuffle them
314- elif shuffle :
328+ if shuffle :
315329 # temporary list to hold them
316330 shuffled_list = []
317331
0 commit comments