29
29
https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
30
30
31
31
"""
32
+ try :
33
+ from typing import Union , Optional , List , Sequence
34
+ from circuitpython_typing import pwmio
35
+ except ImportError :
36
+ pass
32
37
33
38
import time
34
39
import digitalio
73
78
_LCD_ROW_OFFSETS = (0x00 , 0x40 , 0x14 , 0x54 )
74
79
75
80
76
- def _set_bit (byte_value , position , val ) :
81
+ def _set_bit (byte_value : int , position : int , val : bool ) -> int :
77
82
# Given the specified byte_value set the bit at position to the provided
78
83
# boolean value val and return the modified byte.
79
84
ret = None
@@ -84,7 +89,9 @@ def _set_bit(byte_value, position, val):
84
89
return ret
85
90
86
91
87
- def _map (xval , in_min , in_max , out_min , out_max ):
92
+ def _map (
93
+ xval : float , in_min : float , in_max : float , out_min : float , out_max : float
94
+ ) -> float :
88
95
# Affine transfer/map with constrained output.
89
96
outrange = float (out_max - out_min )
90
97
inrange = float (in_max - in_min )
@@ -106,16 +113,26 @@ class Character_LCD:
106
113
:param ~digitalio.DigitalInOut d5: The data line 5
107
114
:param ~digitalio.DigitalInOut d6: The data line 6
108
115
:param ~digitalio.DigitalInOut d7: The data line 7
109
- :param columns: The columns on the charLCD
110
- :param lines: The lines on the charLCD
116
+ :param int columns: The columns on the charLCD
117
+ :param int lines: The lines on the charLCD
111
118
112
119
"""
113
120
114
121
LEFT_TO_RIGHT = const (0 )
115
122
RIGHT_TO_LEFT = const (1 )
116
123
117
124
# pylint: disable-msg=too-many-arguments
118
- def __init__ (self , rs , en , d4 , d5 , d6 , d7 , columns , lines ):
125
+ def __init__ (
126
+ self ,
127
+ rs : digitalio .DigitalInOut ,
128
+ en : digitalio .DigitalInOut ,
129
+ d4 : digitalio .DigitalInOut ,
130
+ d5 : digitalio .DigitalInOut ,
131
+ d6 : digitalio .DigitalInOut ,
132
+ d7 : digitalio .DigitalInOut ,
133
+ columns : int ,
134
+ lines : int ,
135
+ ) -> None :
119
136
120
137
self .columns = columns
121
138
self .lines = lines
@@ -147,7 +164,6 @@ def __init__(self, rs, en, d4, d5, d6, d7, columns, lines):
147
164
# Set entry mode
148
165
self ._write8 (_LCD_ENTRYMODESET | self .displaymode )
149
166
self .clear ()
150
-
151
167
self ._message = None
152
168
self ._enable = None
153
169
self ._direction = None
@@ -159,12 +175,12 @@ def __init__(self, rs, en, d4, d5, d6, d7, columns, lines):
159
175
160
176
# pylint: enable-msg=too-many-arguments
161
177
162
- def home (self ):
178
+ def home (self ) -> None :
163
179
"""Moves the cursor "home" to position (0, 0)."""
164
180
self ._write8 (_LCD_RETURNHOME )
165
181
time .sleep (0.003 )
166
182
167
- def clear (self ):
183
+ def clear (self ) -> None :
168
184
"""Clears everything displayed on the LCD.
169
185
170
186
The following example displays, "Hello, world!", then clears the LCD.
@@ -186,21 +202,21 @@ def clear(self):
186
202
time .sleep (0.003 )
187
203
188
204
@property
189
- def column_align (self ):
205
+ def column_align (self ) -> bool :
190
206
"""If True, message text after '\\ n' starts directly below start of first
191
207
character in message. If False, text after '\\ n' starts at column zero.
192
208
"""
193
209
return self ._column_align
194
210
195
211
@column_align .setter
196
- def column_align (self , enable ):
212
+ def column_align (self , enable : bool ):
197
213
if isinstance (enable , bool ):
198
214
self ._column_align = enable
199
215
else :
200
216
raise ValueError ("The column_align value must be either True or False" )
201
217
202
218
@property
203
- def cursor (self ):
219
+ def cursor (self ) -> bool :
204
220
"""True if cursor is visible. False to stop displaying the cursor.
205
221
206
222
The following example shows the cursor after a displayed message:
@@ -222,19 +238,19 @@ def cursor(self):
222
238
return self .displaycontrol & _LCD_CURSORON == _LCD_CURSORON
223
239
224
240
@cursor .setter
225
- def cursor (self , show ) :
241
+ def cursor (self , show : bool ) -> None :
226
242
if show :
227
243
self .displaycontrol |= _LCD_CURSORON
228
244
else :
229
245
self .displaycontrol &= ~ _LCD_CURSORON
230
246
self ._write8 (_LCD_DISPLAYCONTROL | self .displaycontrol )
231
247
232
- def cursor_position (self , column , row ) :
248
+ def cursor_position (self , column : int , row : int ) -> None :
233
249
"""Move the cursor to position ``column``, ``row`` for the next
234
250
message only. Displaying a message resets the cursor position to (0, 0).
235
251
236
- :param column: column location
237
- :param row: row location
252
+ :param int column: column location
253
+ :param int row: row location
238
254
"""
239
255
# Clamp row to the last row of the display
240
256
if row >= self .lines :
@@ -249,7 +265,7 @@ def cursor_position(self, column, row):
249
265
self .column = column
250
266
251
267
@property
252
- def blink (self ):
268
+ def blink (self ) -> bool :
253
269
"""
254
270
Blink the cursor. True to blink the cursor. False to stop blinking.
255
271
@@ -272,15 +288,15 @@ def blink(self):
272
288
return self .displaycontrol & _LCD_BLINKON == _LCD_BLINKON
273
289
274
290
@blink .setter
275
- def blink (self , blink ) :
291
+ def blink (self , blink : bool ) -> None :
276
292
if blink :
277
293
self .displaycontrol |= _LCD_BLINKON
278
294
else :
279
295
self .displaycontrol &= ~ _LCD_BLINKON
280
296
self ._write8 (_LCD_DISPLAYCONTROL | self .displaycontrol )
281
297
282
298
@property
283
- def display (self ):
299
+ def display (self ) -> bool :
284
300
"""
285
301
Enable or disable the display. True to enable the display. False to disable the display.
286
302
@@ -302,15 +318,15 @@ def display(self):
302
318
return self .displaycontrol & _LCD_DISPLAYON == _LCD_DISPLAYON
303
319
304
320
@display .setter
305
- def display (self , enable ) :
321
+ def display (self , enable : bool ) -> None :
306
322
if enable :
307
323
self .displaycontrol |= _LCD_DISPLAYON
308
324
else :
309
325
self .displaycontrol &= ~ _LCD_DISPLAYON
310
326
self ._write8 (_LCD_DISPLAYCONTROL | self .displaycontrol )
311
327
312
328
@property
313
- def message (self ):
329
+ def message (self ) -> Optional [ str ] :
314
330
"""Display a string of text on the character LCD.
315
331
Start position is (0,0) if cursor_position is not set.
316
332
If cursor_position is set, message starts at the set
@@ -335,7 +351,7 @@ def message(self):
335
351
return self ._message
336
352
337
353
@message .setter
338
- def message (self , message ):
354
+ def message (self , message : str ):
339
355
self ._message = message
340
356
# Set line to match self.row from cursor_position()
341
357
line = self .row
@@ -377,7 +393,7 @@ def message(self, message):
377
393
# reset column and row to (0,0) after message is displayed
378
394
self .column , self .row = 0 , 0
379
395
380
- def move_left (self ):
396
+ def move_left (self ) -> None :
381
397
"""Moves displayed text left one column.
382
398
383
399
The following example scrolls a message to the left off the screen.
@@ -400,7 +416,7 @@ def move_left(self):
400
416
"""
401
417
self ._write8 (_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVELEFT )
402
418
403
- def move_right (self ):
419
+ def move_right (self ) -> None :
404
420
"""Moves displayed text right one column.
405
421
406
422
The following example scrolls a message to the right off the screen.
@@ -424,7 +440,7 @@ def move_right(self):
424
440
self ._write8 (_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVERIGHT )
425
441
426
442
@property
427
- def text_direction (self ):
443
+ def text_direction (self ) -> Optional [ int ] :
428
444
"""The direction the text is displayed. To display the text left to right beginning on the
429
445
left side of the LCD, set ``text_direction = LEFT_TO_RIGHT``. To display the text right
430
446
to left beginning on the right size of the LCD, set ``text_direction = RIGHT_TO_LEFT``.
@@ -448,33 +464,33 @@ def text_direction(self):
448
464
return self ._direction
449
465
450
466
@text_direction .setter
451
- def text_direction (self , direction ) :
467
+ def text_direction (self , direction : int ) -> None :
452
468
self ._direction = direction
453
469
if direction == self .LEFT_TO_RIGHT :
454
470
self ._left_to_right ()
455
471
elif direction == self .RIGHT_TO_LEFT :
456
472
self ._right_to_left ()
457
473
458
- def _left_to_right (self ):
474
+ def _left_to_right (self ) -> None :
459
475
# Displays text from left to right on the LCD.
460
476
self .displaymode |= _LCD_ENTRYLEFT
461
477
self ._write8 (_LCD_ENTRYMODESET | self .displaymode )
462
478
463
- def _right_to_left (self ):
479
+ def _right_to_left (self ) -> None :
464
480
# Displays text from right to left on the LCD.
465
481
self .displaymode &= ~ _LCD_ENTRYLEFT
466
482
self ._write8 (_LCD_ENTRYMODESET | self .displaymode )
467
483
468
- def create_char (self , location , pattern ) :
484
+ def create_char (self , location : int , pattern : Sequence [ int ]) -> None :
469
485
"""
470
486
Fill one of the first 8 CGRAM locations with custom characters.
471
487
The location parameter should be between 0 and 7 and pattern should
472
488
provide an array of 8 bytes containing the pattern. E.g. you can easily
473
489
design your custom character at http://www.quinapalus.com/hd44780udg.html
474
490
To show your custom character use, for example, ``lcd.message = "\x01 "``
475
491
476
- :param location: integer in range(8) to store the created character
477
- :param ~bytes pattern: len(8) describes created character
492
+ :param int location: Integer in range(8) to store the created character.
493
+ :param Sequence[int] pattern: len(8) describes created character.
478
494
479
495
"""
480
496
# only position 0..7 are allowed
@@ -483,9 +499,9 @@ def create_char(self, location, pattern):
483
499
for i in range (8 ):
484
500
self ._write8 (pattern [i ], char_mode = True )
485
501
486
- def _write8 (self , value , char_mode = False ):
502
+ def _write8 (self , value : int , char_mode : bool = False ) -> None :
487
503
# Sends 8b ``value`` in ``char_mode``.
488
- # :param value: bytes
504
+ # :param value: int
489
505
# :param char_mode: character/data mode selector. False (default) for
490
506
# data only, True for character bits.
491
507
# one ms delay to prevent writing too quickly.
@@ -506,7 +522,7 @@ def _write8(self, value, char_mode=False):
506
522
self .dl7 .value = ((value >> 3 ) & 1 ) > 0
507
523
self ._pulse_enable ()
508
524
509
- def _pulse_enable (self ):
525
+ def _pulse_enable (self ) -> None :
510
526
# Pulses (lo->hi->lo) to send commands.
511
527
self .enable .value = False
512
528
# 1microsec pause
@@ -530,8 +546,8 @@ class Character_LCD_Mono(Character_LCD):
530
546
:param ~digitalio.DigitalInOut d5: The data line 5
531
547
:param ~digitalio.DigitalInOut d6: The data line 6
532
548
:param ~digitalio.DigitalInOut d7: The data line 7
533
- :param columns: The columns on the charLCD
534
- :param lines: The lines on the charLCD
549
+ :param int columns: The columns on the charLCD
550
+ :param int lines: The lines on the charLCD
535
551
:param ~digitalio.DigitalInOut backlight_pin: The backlight pin
536
552
:param bool backlight_inverted: ``False`` if LCD is not inverted, i.e. backlight pin is
537
553
connected to common anode. ``True`` if LCD is inverted i.e. backlight pin is connected
@@ -542,16 +558,16 @@ class Character_LCD_Mono(Character_LCD):
542
558
# pylint: disable-msg=too-many-arguments
543
559
def __init__ (
544
560
self ,
545
- rs ,
546
- en ,
547
- db4 ,
548
- db5 ,
549
- db6 ,
550
- db7 ,
551
- columns ,
552
- lines ,
553
- backlight_pin = None ,
554
- backlight_inverted = False ,
561
+ rs : digitalio . DigitalInOut ,
562
+ en : digitalio . DigitalInOut ,
563
+ db4 : digitalio . DigitalInOut ,
564
+ db5 : digitalio . DigitalInOut ,
565
+ db6 : digitalio . DigitalInOut ,
566
+ db7 : digitalio . DigitalInOut ,
567
+ columns : int ,
568
+ lines : int ,
569
+ backlight_pin : Optional [ digitalio . DigitalInOut ] = None ,
570
+ backlight_inverted : bool = False ,
555
571
):
556
572
557
573
# Backlight pin and inversion
@@ -567,7 +583,7 @@ def __init__(
567
583
# pylint: enable-msg=too-many-arguments
568
584
569
585
@property
570
- def backlight (self ):
586
+ def backlight (self ) -> Optional [ bool ] :
571
587
"""Enable or disable backlight. True if backlight is on. False if backlight is off.
572
588
573
589
The following example turns the backlight off, then displays, "Hello, world?", then turns
@@ -594,7 +610,7 @@ def backlight(self):
594
610
return self ._enable
595
611
596
612
@backlight .setter
597
- def backlight (self , enable ) :
613
+ def backlight (self , enable : bool ) -> None :
598
614
self ._enable = enable
599
615
if enable :
600
616
self .backlight_pin .value = not self .backlight_inverted
@@ -611,8 +627,8 @@ class Character_LCD_RGB(Character_LCD):
611
627
:param ~digitalio.DigitalInOut db5: The data line 5
612
628
:param ~digitalio.DigitalInOut db6: The data line 6
613
629
:param ~digitalio.DigitalInOut db7: The data line 7
614
- :param columns: The columns on the charLCD
615
- :param lines: The lines on the charLCD
630
+ :param int columns: The columns on the charLCD
631
+ :param int lines: The lines on the charLCD
616
632
:param ~pwmio.PWMOut,~digitalio.DigitalInOut red: Red RGB Anode
617
633
:param ~pwmio.PWMOut,~digitalio.DigitalInOut green: Green RGB Anode
618
634
:param ~pwmio.PWMOut,~digitalio.DigitalInOut blue: Blue RGB Anode
@@ -624,19 +640,19 @@ class Character_LCD_RGB(Character_LCD):
624
640
# pylint: disable-msg=too-many-arguments
625
641
def __init__ (
626
642
self ,
627
- rs ,
628
- en ,
629
- db4 ,
630
- db5 ,
631
- db6 ,
632
- db7 ,
633
- columns ,
634
- lines ,
635
- red ,
636
- green ,
637
- blue ,
638
- read_write = None ,
639
- ):
643
+ rs : digitalio . DigitalInOut ,
644
+ en : digitalio . DigitalInOut ,
645
+ db4 : digitalio . DigitalInOut ,
646
+ db5 : digitalio . DigitalInOut ,
647
+ db6 : digitalio . DigitalInOut ,
648
+ db7 : digitalio . DigitalInOut ,
649
+ columns : int ,
650
+ lines : int ,
651
+ red : Union [ pwmio . PWMOut , digitalio . DigitalInOut ] ,
652
+ green : Union [ pwmio . PWMOut , digitalio . DigitalInOut ] ,
653
+ blue : Union [ pwmio . PWMOut , digitalio . DigitalInOut ] ,
654
+ read_write : Optional [ digitalio . DigitalInOut ] = None ,
655
+ ) -> None :
640
656
641
657
# Define read_write (rw) pin
642
658
self .read_write = read_write
@@ -662,7 +678,7 @@ def __init__(
662
678
super ().__init__ (rs , en , db4 , db5 , db6 , db7 , columns , lines )
663
679
664
680
@property
665
- def color (self ):
681
+ def color (self ) -> List [ int ] :
666
682
"""
667
683
The color of the display. Provide a list of three integers ranging 0 - 100, ``[R, G, B]``.
668
684
``0`` is no color, or "off". ``100`` is maximum color. For example, the brightest red would
@@ -693,7 +709,7 @@ def color(self):
693
709
return self ._color
694
710
695
711
@color .setter
696
- def color (self , color ) :
712
+ def color (self , color : Union [ List [ float ], int ]) -> None :
697
713
if isinstance (color , int ):
698
714
if color >> 24 :
699
715
raise ValueError ("Integer color value must be positive and 24 bits max" )
0 commit comments