@@ -53,7 +53,7 @@ async def set_contrast(
5353
5454 async def set_backlight (
5555 self : Self ,
56- lcd_brightness : int ,
56+ lcd_brightness : float ,
5757 keypad_brightness : Optional [int ] = None ,
5858 timeout : Optional [float ] = None ,
5959 retry_times : Optional [int ] = None ,
@@ -182,7 +182,7 @@ async def render(self: Self) -> None:
182182 self .row , 0 , buffer , timeout = self .timeout , retry_times = self .retry_times
183183 )
184184 self .shift += 1
185- if self .shift >= device .columns :
185+ if self .shift > device .columns :
186186 self .shift = 0
187187
188188 def _line (self : Self ) -> bytes :
@@ -195,6 +195,11 @@ def _line(self: Self) -> bytes:
195195
196196
197197class Screensaver (Effect ):
198+ """
199+ A screensaver effect. Prints text at a random position, and moves it around the
200+ screen on an interval.
201+ """
202+
198203 def __init__ (
199204 self : Self ,
200205 client : EffectClient ,
@@ -207,7 +212,7 @@ def __init__(
207212 device = client .device
208213 buffer = device .character_rom .encode (text )
209214
210- if len (buffer ) >= device .columns :
215+ if len (buffer ) > device .columns :
211216 raise ValueError (
212217 f"Text length { len (buffer )} is too long to fit onto the device's "
213218 f"{ device .columns } columns"
@@ -236,3 +241,53 @@ async def render(self: Self) -> None:
236241 await self .client .send_data (
237242 row , column , self .text , timeout = self .timeout , retry_times = self .retry_times
238243 )
244+
245+
246+ class DanceParty (Effect ):
247+ """
248+ A dance party effect. Randomly changes the backlight and contrast settings on
249+ an interval.
250+ """
251+
252+ def __init__ (
253+ self : Self ,
254+ client : EffectClient ,
255+ text : str ,
256+ tick : Optional [float ] = None ,
257+ timeout : Optional [float ] = None ,
258+ retry_times : Optional [int ] = None ,
259+ loop : Optional [asyncio .AbstractEventLoop ] = None ,
260+ ) -> None :
261+ device = client .device
262+ buffer = device .character_rom .encode (text .center (device .columns ))
263+
264+ if len (buffer ) > device .columns :
265+ raise ValueError (
266+ f"Text length { len (buffer )} is too long to fit onto the device's "
267+ f"{ device .columns } columns"
268+ )
269+
270+ super ().__init__ (
271+ client = client ,
272+ tick = tick if tick is not None else 0.5 ,
273+ timeout = timeout ,
274+ retry_times = retry_times ,
275+ loop = loop ,
276+ )
277+
278+ self .text : bytes = buffer
279+
280+ def _random_contrast (self : Self ) -> float :
281+ return random .uniform (0.4 , 0.6 )
282+
283+ def _random_brightness (self : Self ) -> float :
284+ return random .uniform (0.2 , 0.8 )
285+
286+ async def start (self : Self ) -> None :
287+ await self .client .send_data (0 , 0 , self .text )
288+
289+ async def render (self : Self ) -> None :
290+ await asyncio .gather (
291+ self .client .set_contrast (self ._random_contrast ()),
292+ self .client .set_backlight (self ._random_brightness ()),
293+ )
0 commit comments