@@ -246,43 +246,11 @@ def __init__(
246
246
layout_class : type [KeyboardLayoutBase ] = KeyboardLayoutUS ,
247
247
keycode_class : type [Keycode ] = Keycode ,
248
248
):
249
- if rotation not in (0 , 90 , 180 , 270 ):
250
- raise ValueError ("Only 90 degree rotations are supported." )
251
-
252
249
# Define LEDs:
253
250
self ._pixels = neopixel .NeoPixel (board .NEOPIXEL , 12 )
254
251
self ._led = digitalio .DigitalInOut (board .LED )
255
252
self ._led .switch_to_output ()
256
253
257
- # Define key and pixel maps based on rotation:
258
- self ._rotated_pixels = None
259
- self ._key_pins = None
260
-
261
- def _keys_and_pixels (
262
- order : Tuple [int , int , int , int , int , int , int , int , int , int , int , int ]
263
- ) -> None :
264
- """
265
- Generate key and pixel maps based on a specified order.
266
- :param order: Tuple containing the order of the keys and pixels.
267
- """
268
- self ._key_pins = [getattr (board , "KEY%d" % (num + 1 )) for num in order ]
269
- self ._rotated_pixels = _PixelMapLite (self ._pixels , order = order )
270
-
271
- if rotation == 0 :
272
- _keys_and_pixels (order = ROTATED_KEYMAP_0 )
273
-
274
- if rotation == 90 :
275
- _keys_and_pixels (order = ROTATED_KEYMAP_90 )
276
-
277
- if rotation == 180 :
278
- _keys_and_pixels (order = ROTATED_KEYMAP_180 )
279
-
280
- if rotation == 270 :
281
- _keys_and_pixels (order = ROTATED_KEYMAP_270 )
282
-
283
- # Define keys:
284
- self ._keys = keypad .Keys (self ._key_pins , value_when_pressed = False , pull = True )
285
-
286
254
# Define rotary encoder and encoder switch:
287
255
self ._encoder = rotaryio .IncrementalEncoder (board .ROTA , board .ROTB )
288
256
self ._encoder_switch = digitalio .DigitalInOut (board .BUTTON )
@@ -292,10 +260,15 @@ def _keys_and_pixels(
292
260
# Define display:
293
261
if not isinstance (board .DISPLAY , type (None )):
294
262
self .display = board .DISPLAY
295
- self .display .rotation = rotation
296
263
self .display .bus .send (_DISPLAY_WAKE_COMMAND , b"" )
297
264
self ._display_sleep = False
298
265
266
+ # Define key and pixel maps based on rotation:
267
+ self ._rotated_pixels = None
268
+ self ._key_pins = None
269
+ self ._keys = None
270
+ self .rotate (rotation )
271
+
299
272
# Define audio:
300
273
self ._speaker_enable = digitalio .DigitalInOut (board .SPEAKER_ENABLE )
301
274
self ._speaker_enable .switch_to_output (value = False )
@@ -328,6 +301,65 @@ def _keys_and_pixels(
328
301
# No MIDI ports available.
329
302
self ._midi = None
330
303
304
+ def rotate (self , rotation ):
305
+ """
306
+ Set the display rotation
307
+
308
+ :param int rotation: The rotational position of the MacroPad. Allows for rotating the
309
+ MacroPad in 90 degree increments to four different positions and
310
+ rotates the keypad layout and display orientation to match. Keypad
311
+ layout is always left to right, top to bottom, beginning with key
312
+ number 0 in the top left, and ending with key number 11 in the bottom
313
+ right. Supports ``0``, ``90``, ``180``, and ``270`` degree rotations.
314
+ ``0`` is when the USB port is at the top, ``90`` is when the USB port
315
+ is to the left, ``180`` is when the USB port is at the bottom, and
316
+ ``270`` is when the USB port is to the right. Defaults to ``0``.
317
+ """
318
+ if rotation not in (0 , 90 , 180 , 270 ):
319
+ raise ValueError ("Only 90 degree rotations are supported." )
320
+
321
+ self ._rotation = rotation
322
+
323
+ def _keys_and_pixels (
324
+ order : Tuple [int , int , int , int , int , int , int , int , int , int , int , int ]
325
+ ) -> None :
326
+ """
327
+ Generate key and pixel maps based on a specified order.
328
+ :param order: Tuple containing the order of the keys and pixels.
329
+ """
330
+ self ._key_pins = [getattr (board , "KEY%d" % (num + 1 )) for num in order ]
331
+ self ._rotated_pixels = _PixelMapLite (self ._pixels , order = order )
332
+
333
+ if rotation == 0 :
334
+ _keys_and_pixels (order = ROTATED_KEYMAP_0 )
335
+
336
+ if rotation == 90 :
337
+ _keys_and_pixels (order = ROTATED_KEYMAP_90 )
338
+
339
+ if rotation == 180 :
340
+ _keys_and_pixels (order = ROTATED_KEYMAP_180 )
341
+
342
+ if rotation == 270 :
343
+ _keys_and_pixels (order = ROTATED_KEYMAP_270 )
344
+
345
+ # Define keys:
346
+ if self ._keys is not None :
347
+ self ._keys .deinit ()
348
+ self ._keys = keypad .Keys (self ._key_pins , value_when_pressed = False , pull = True )
349
+
350
+ self .display .rotation = rotation
351
+
352
+ @property
353
+ def rotation (self ) -> int :
354
+ """
355
+ The current rotation
356
+ """
357
+ return self ._rotation
358
+
359
+ @rotation .setter
360
+ def rotation (self , new_rotation ) -> None :
361
+ self .rotate (new_rotation )
362
+
331
363
@property
332
364
def display_sleep (self ) -> bool :
333
365
"""The power saver mode of the display. Set it to put the display to
0 commit comments