Skip to content

Commit d11d147

Browse files
authored
Merge pull request #73 from BiffoBear/add_typing
Added typing and made an edit to fix a typing issue (non-breaking)
2 parents 6876522 + 2d4683e commit d11d147

File tree

5 files changed

+123
-74
lines changed

5 files changed

+123
-74
lines changed

adafruit_character_lcd/character_lcd.py

Lines changed: 80 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
3030
3131
"""
32+
try:
33+
from typing import Union, Optional, List, Sequence
34+
from circuitpython_typing import pwmio
35+
except ImportError:
36+
pass
3237

3338
import time
3439
import digitalio
@@ -73,7 +78,7 @@
7378
_LCD_ROW_OFFSETS = (0x00, 0x40, 0x14, 0x54)
7479

7580

76-
def _set_bit(byte_value, position, val):
81+
def _set_bit(byte_value: int, position: int, val: bool) -> int:
7782
# Given the specified byte_value set the bit at position to the provided
7883
# boolean value val and return the modified byte.
7984
ret = None
@@ -84,7 +89,9 @@ def _set_bit(byte_value, position, val):
8489
return ret
8590

8691

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:
8895
# Affine transfer/map with constrained output.
8996
outrange = float(out_max - out_min)
9097
inrange = float(in_max - in_min)
@@ -106,16 +113,26 @@ class Character_LCD:
106113
:param ~digitalio.DigitalInOut d5: The data line 5
107114
:param ~digitalio.DigitalInOut d6: The data line 6
108115
: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
111118
112119
"""
113120

114121
LEFT_TO_RIGHT = const(0)
115122
RIGHT_TO_LEFT = const(1)
116123

117124
# 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:
119136

120137
self.columns = columns
121138
self.lines = lines
@@ -147,7 +164,6 @@ def __init__(self, rs, en, d4, d5, d6, d7, columns, lines):
147164
# Set entry mode
148165
self._write8(_LCD_ENTRYMODESET | self.displaymode)
149166
self.clear()
150-
151167
self._message = None
152168
self._enable = None
153169
self._direction = None
@@ -159,12 +175,12 @@ def __init__(self, rs, en, d4, d5, d6, d7, columns, lines):
159175

160176
# pylint: enable-msg=too-many-arguments
161177

162-
def home(self):
178+
def home(self) -> None:
163179
"""Moves the cursor "home" to position (0, 0)."""
164180
self._write8(_LCD_RETURNHOME)
165181
time.sleep(0.003)
166182

167-
def clear(self):
183+
def clear(self) -> None:
168184
"""Clears everything displayed on the LCD.
169185
170186
The following example displays, "Hello, world!", then clears the LCD.
@@ -186,21 +202,21 @@ def clear(self):
186202
time.sleep(0.003)
187203

188204
@property
189-
def column_align(self):
205+
def column_align(self) -> bool:
190206
"""If True, message text after '\\n' starts directly below start of first
191207
character in message. If False, text after '\\n' starts at column zero.
192208
"""
193209
return self._column_align
194210

195211
@column_align.setter
196-
def column_align(self, enable):
212+
def column_align(self, enable: bool):
197213
if isinstance(enable, bool):
198214
self._column_align = enable
199215
else:
200216
raise ValueError("The column_align value must be either True or False")
201217

202218
@property
203-
def cursor(self):
219+
def cursor(self) -> bool:
204220
"""True if cursor is visible. False to stop displaying the cursor.
205221
206222
The following example shows the cursor after a displayed message:
@@ -222,19 +238,19 @@ def cursor(self):
222238
return self.displaycontrol & _LCD_CURSORON == _LCD_CURSORON
223239

224240
@cursor.setter
225-
def cursor(self, show):
241+
def cursor(self, show: bool) -> None:
226242
if show:
227243
self.displaycontrol |= _LCD_CURSORON
228244
else:
229245
self.displaycontrol &= ~_LCD_CURSORON
230246
self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol)
231247

232-
def cursor_position(self, column, row):
248+
def cursor_position(self, column: int, row: int) -> None:
233249
"""Move the cursor to position ``column``, ``row`` for the next
234250
message only. Displaying a message resets the cursor position to (0, 0).
235251
236-
:param column: column location
237-
:param row: row location
252+
:param int column: column location
253+
:param int row: row location
238254
"""
239255
# Clamp row to the last row of the display
240256
if row >= self.lines:
@@ -249,7 +265,7 @@ def cursor_position(self, column, row):
249265
self.column = column
250266

251267
@property
252-
def blink(self):
268+
def blink(self) -> bool:
253269
"""
254270
Blink the cursor. True to blink the cursor. False to stop blinking.
255271
@@ -272,15 +288,15 @@ def blink(self):
272288
return self.displaycontrol & _LCD_BLINKON == _LCD_BLINKON
273289

274290
@blink.setter
275-
def blink(self, blink):
291+
def blink(self, blink: bool) -> None:
276292
if blink:
277293
self.displaycontrol |= _LCD_BLINKON
278294
else:
279295
self.displaycontrol &= ~_LCD_BLINKON
280296
self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol)
281297

282298
@property
283-
def display(self):
299+
def display(self) -> bool:
284300
"""
285301
Enable or disable the display. True to enable the display. False to disable the display.
286302
@@ -302,15 +318,15 @@ def display(self):
302318
return self.displaycontrol & _LCD_DISPLAYON == _LCD_DISPLAYON
303319

304320
@display.setter
305-
def display(self, enable):
321+
def display(self, enable: bool) -> None:
306322
if enable:
307323
self.displaycontrol |= _LCD_DISPLAYON
308324
else:
309325
self.displaycontrol &= ~_LCD_DISPLAYON
310326
self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol)
311327

312328
@property
313-
def message(self):
329+
def message(self) -> Optional[str]:
314330
"""Display a string of text on the character LCD.
315331
Start position is (0,0) if cursor_position is not set.
316332
If cursor_position is set, message starts at the set
@@ -335,7 +351,7 @@ def message(self):
335351
return self._message
336352

337353
@message.setter
338-
def message(self, message):
354+
def message(self, message: str):
339355
self._message = message
340356
# Set line to match self.row from cursor_position()
341357
line = self.row
@@ -377,7 +393,7 @@ def message(self, message):
377393
# reset column and row to (0,0) after message is displayed
378394
self.column, self.row = 0, 0
379395

380-
def move_left(self):
396+
def move_left(self) -> None:
381397
"""Moves displayed text left one column.
382398
383399
The following example scrolls a message to the left off the screen.
@@ -400,7 +416,7 @@ def move_left(self):
400416
"""
401417
self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVELEFT)
402418

403-
def move_right(self):
419+
def move_right(self) -> None:
404420
"""Moves displayed text right one column.
405421
406422
The following example scrolls a message to the right off the screen.
@@ -424,7 +440,7 @@ def move_right(self):
424440
self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVERIGHT)
425441

426442
@property
427-
def text_direction(self):
443+
def text_direction(self) -> Optional[int]:
428444
"""The direction the text is displayed. To display the text left to right beginning on the
429445
left side of the LCD, set ``text_direction = LEFT_TO_RIGHT``. To display the text right
430446
to left beginning on the right size of the LCD, set ``text_direction = RIGHT_TO_LEFT``.
@@ -448,33 +464,33 @@ def text_direction(self):
448464
return self._direction
449465

450466
@text_direction.setter
451-
def text_direction(self, direction):
467+
def text_direction(self, direction: int) -> None:
452468
self._direction = direction
453469
if direction == self.LEFT_TO_RIGHT:
454470
self._left_to_right()
455471
elif direction == self.RIGHT_TO_LEFT:
456472
self._right_to_left()
457473

458-
def _left_to_right(self):
474+
def _left_to_right(self) -> None:
459475
# Displays text from left to right on the LCD.
460476
self.displaymode |= _LCD_ENTRYLEFT
461477
self._write8(_LCD_ENTRYMODESET | self.displaymode)
462478

463-
def _right_to_left(self):
479+
def _right_to_left(self) -> None:
464480
# Displays text from right to left on the LCD.
465481
self.displaymode &= ~_LCD_ENTRYLEFT
466482
self._write8(_LCD_ENTRYMODESET | self.displaymode)
467483

468-
def create_char(self, location, pattern):
484+
def create_char(self, location: int, pattern: Sequence[int]) -> None:
469485
"""
470486
Fill one of the first 8 CGRAM locations with custom characters.
471487
The location parameter should be between 0 and 7 and pattern should
472488
provide an array of 8 bytes containing the pattern. E.g. you can easily
473489
design your custom character at http://www.quinapalus.com/hd44780udg.html
474490
To show your custom character use, for example, ``lcd.message = "\x01"``
475491
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.
478494
479495
"""
480496
# only position 0..7 are allowed
@@ -483,9 +499,9 @@ def create_char(self, location, pattern):
483499
for i in range(8):
484500
self._write8(pattern[i], char_mode=True)
485501

486-
def _write8(self, value, char_mode=False):
502+
def _write8(self, value: int, char_mode: bool = False) -> None:
487503
# Sends 8b ``value`` in ``char_mode``.
488-
# :param value: bytes
504+
# :param value: int
489505
# :param char_mode: character/data mode selector. False (default) for
490506
# data only, True for character bits.
491507
# one ms delay to prevent writing too quickly.
@@ -506,7 +522,7 @@ def _write8(self, value, char_mode=False):
506522
self.dl7.value = ((value >> 3) & 1) > 0
507523
self._pulse_enable()
508524

509-
def _pulse_enable(self):
525+
def _pulse_enable(self) -> None:
510526
# Pulses (lo->hi->lo) to send commands.
511527
self.enable.value = False
512528
# 1microsec pause
@@ -530,8 +546,8 @@ class Character_LCD_Mono(Character_LCD):
530546
:param ~digitalio.DigitalInOut d5: The data line 5
531547
:param ~digitalio.DigitalInOut d6: The data line 6
532548
: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
535551
:param ~digitalio.DigitalInOut backlight_pin: The backlight pin
536552
:param bool backlight_inverted: ``False`` if LCD is not inverted, i.e. backlight pin is
537553
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):
542558
# pylint: disable-msg=too-many-arguments
543559
def __init__(
544560
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,
555571
):
556572

557573
# Backlight pin and inversion
@@ -567,7 +583,7 @@ def __init__(
567583
# pylint: enable-msg=too-many-arguments
568584

569585
@property
570-
def backlight(self):
586+
def backlight(self) -> Optional[bool]:
571587
"""Enable or disable backlight. True if backlight is on. False if backlight is off.
572588
573589
The following example turns the backlight off, then displays, "Hello, world?", then turns
@@ -594,7 +610,7 @@ def backlight(self):
594610
return self._enable
595611

596612
@backlight.setter
597-
def backlight(self, enable):
613+
def backlight(self, enable: bool) -> None:
598614
self._enable = enable
599615
if enable:
600616
self.backlight_pin.value = not self.backlight_inverted
@@ -611,8 +627,8 @@ class Character_LCD_RGB(Character_LCD):
611627
:param ~digitalio.DigitalInOut db5: The data line 5
612628
:param ~digitalio.DigitalInOut db6: The data line 6
613629
: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
616632
:param ~pwmio.PWMOut,~digitalio.DigitalInOut red: Red RGB Anode
617633
:param ~pwmio.PWMOut,~digitalio.DigitalInOut green: Green RGB Anode
618634
:param ~pwmio.PWMOut,~digitalio.DigitalInOut blue: Blue RGB Anode
@@ -624,19 +640,19 @@ class Character_LCD_RGB(Character_LCD):
624640
# pylint: disable-msg=too-many-arguments
625641
def __init__(
626642
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:
640656

641657
# Define read_write (rw) pin
642658
self.read_write = read_write
@@ -662,7 +678,7 @@ def __init__(
662678
super().__init__(rs, en, db4, db5, db6, db7, columns, lines)
663679

664680
@property
665-
def color(self):
681+
def color(self) -> List[int]:
666682
"""
667683
The color of the display. Provide a list of three integers ranging 0 - 100, ``[R, G, B]``.
668684
``0`` is no color, or "off". ``100`` is maximum color. For example, the brightest red would
@@ -693,7 +709,7 @@ def color(self):
693709
return self._color
694710

695711
@color.setter
696-
def color(self, color):
712+
def color(self, color: Union[List[float], int]) -> None:
697713
if isinstance(color, int):
698714
if color >> 24:
699715
raise ValueError("Integer color value must be positive and 24 bits max")

0 commit comments

Comments
 (0)