13
13
14
14
"""
15
15
16
+ from __future__ import annotations
17
+
16
18
import struct
17
19
18
20
from .packet import Packet
19
21
22
+ try :
23
+ from typing import Optional # adjust these as needed
24
+ except ImportError :
25
+ pass
20
26
21
27
class ButtonPacket (Packet ):
22
28
"""A packet containing a button name and its state."""
23
29
24
- BUTTON_1 = "1"
30
+ BUTTON_1 : str = "1"
25
31
"""Code for Button 1 on the Bluefruit LE Connect app Control Pad screen."""
26
- BUTTON_2 = "2"
32
+ BUTTON_2 : str = "2"
27
33
"""Button 2."""
28
- BUTTON_3 = "3"
34
+ BUTTON_3 : str = "3"
29
35
"""Button 3."""
30
- BUTTON_4 = "4"
36
+ BUTTON_4 : str = "4"
31
37
"""Button 4."""
32
38
# pylint: disable= invalid-name
33
- UP = "5"
39
+ UP : str = "5"
34
40
"""Up Button."""
35
- DOWN = "6"
41
+ DOWN : str = "6"
36
42
"""Down Button."""
37
- LEFT = "7"
43
+ LEFT : str = "7"
38
44
"""Left Button."""
39
- RIGHT = "8"
45
+ RIGHT : str = "8"
40
46
"""Right Button."""
41
47
42
- _FMT_PARSE = "<xxssx"
43
- PACKET_LENGTH = struct .calcsize (_FMT_PARSE )
48
+ _FMT_PARSE : str = "<xxssx"
49
+ PACKET_LENGTH : int = struct .calcsize (_FMT_PARSE )
44
50
# _FMT_CONSTRUCT doesn't include the trailing checksum byte.
45
- _FMT_CONSTRUCT = "<2sss"
46
- _TYPE_HEADER = b"!B"
51
+ _FMT_CONSTRUCT : str = "<2sss"
52
+ _TYPE_HEADER : bytes = b"!B"
47
53
48
- def __init__ (self , button , pressed ) :
54
+ def __init__ (self , button : str , pressed : bool ) -> None :
49
55
"""Construct a ButtonPacket from a button name and the button's state.
50
56
51
57
:param str button: a single character denoting the button
@@ -59,11 +65,11 @@ def __init__(self, button, pressed):
59
65
except Exception as err :
60
66
raise ValueError ("Button must be a single char." ) from err
61
67
62
- self ._button = button
63
- self ._pressed = pressed
68
+ self ._button : str = button
69
+ self ._pressed : bool = pressed
64
70
65
71
@classmethod
66
- def parse_private (cls , packet ) :
72
+ def parse_private (cls , packet : bytes ) -> Optional [ Packet ] :
67
73
"""Construct a ButtonPacket from an incoming packet.
68
74
Do not call this directly; call Packet.from_bytes() instead.
69
75
pylint makes it difficult to call this method _parse(), hence the name.
@@ -73,9 +79,9 @@ def parse_private(cls, packet):
73
79
raise ValueError ("Bad button press/release value" )
74
80
return cls (chr (button [0 ]), pressed == b"1" )
75
81
76
- def to_bytes (self ):
82
+ def to_bytes (self ) -> bytes :
77
83
"""Return the bytes needed to send this packet."""
78
- partial_packet = struct .pack (
84
+ partial_packet : bytes = struct .pack (
79
85
self ._FMT_CONSTRUCT ,
80
86
self ._TYPE_HEADER ,
81
87
bytes (self ._button , "utf-8" ),
@@ -84,13 +90,13 @@ def to_bytes(self):
84
90
return self .add_checksum (partial_packet )
85
91
86
92
@property
87
- def button (self ):
93
+ def button (self ) -> str :
88
94
"""A single character string (not bytes) specifying the button that
89
95
the user pressed or released."""
90
96
return self ._button
91
97
92
98
@property
93
- def pressed (self ):
99
+ def pressed (self ) -> bool :
94
100
"""``True`` if button is pressed, or ``False`` if it is released."""
95
101
return self ._pressed
96
102
0 commit comments