Skip to content

Commit 9b29b1a

Browse files
Add type annotations to adafruit_amg88xx.py
1 parent 2e7705b commit 9b29b1a

File tree

1 file changed

+52
-45
lines changed

1 file changed

+52
-45
lines changed

adafruit_amg88xx.py

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,56 +23,63 @@
2323
**Notes:**
2424
"""
2525

26-
__version__ = "0.0.0-auto.0"
27-
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_AMG88xx"
26+
__version__: str = "0.0.0-auto.0"
27+
__repo__: str = "https://github.com/adafruit/Adafruit_CircuitPython_AMG88xx"
2828

2929
from adafruit_bus_device.i2c_device import I2CDevice
3030
from adafruit_register import i2c_bit, i2c_bits
31+
from adafruit_register.i2c_bits import RWBits, ROBits
32+
from adafruit_register.i2c_bit import RWBit, ROBit
3133
from micropython import const
34+
import busio
3235

36+
try:
37+
from typing import List
38+
except ImportError:
39+
pass
3340

3441
# Registers are defined below in the class. These are possible register values.
3542

3643
# Operating Modes
37-
_NORMAL_MODE = const(0x00)
38-
_SLEEP_MODE = const(0x10)
39-
_STAND_BY_60 = const(0x20)
40-
_STAND_BY_10 = const(0x21)
44+
_NORMAL_MODE: int = const(0x00)
45+
_SLEEP_MODE: int = const(0x10)
46+
_STAND_BY_60: int = const(0x20)
47+
_STAND_BY_10: int = const(0x21)
4148

4249
# sw resets
43-
_FLAG_RESET = const(0x30)
44-
_INITIAL_RESET = const(0x3F)
50+
_FLAG_RESET: int = const(0x30)
51+
_INITIAL_RESET: int = const(0x3F)
4552

4653
# frame rates
47-
_FPS_10 = const(0x00)
48-
_FPS_1 = const(0x01)
54+
_FPS_10: int = const(0x00)
55+
_FPS_1: int = const(0x01)
4956

5057
# int enables
51-
_INT_DISABLED = const(0x00)
52-
_INT_ENABLED = const(0x01)
58+
_INT_DISABLED: int = const(0x00)
59+
_INT_ENABLED: int = const(0x01)
5360

5461
# int modes
55-
_DIFFERENCE = const(0x00)
56-
_ABSOLUTE_VALUE = const(0x01)
62+
_DIFFERENCE: int = const(0x00)
63+
_ABSOLUTE_VALUE: int = const(0x01)
5764

58-
_INT_OFFSET = const(0x010)
59-
_PIXEL_OFFSET = const(0x80)
65+
_INT_OFFSET: int = const(0x010)
66+
_PIXEL_OFFSET: int = const(0x80)
6067

61-
_PIXEL_ARRAY_WIDTH = const(8)
62-
_PIXEL_ARRAY_HEIGHT = const(8)
63-
_PIXEL_TEMP_CONVERSION = 0.25
64-
_THERMISTOR_CONVERSION = 0.0625
68+
_PIXEL_ARRAY_WIDTH: int = const(8)
69+
_PIXEL_ARRAY_HEIGHT: int = const(8)
70+
_PIXEL_TEMP_CONVERSION: int = 0.25
71+
_THERMISTOR_CONVERSION: int = 0.0625
6572

6673

67-
def _signed_12bit_to_float(val):
74+
def _signed_12bit_to_float(val: int) -> float:
6875
# take first 11 bits as absolute val
6976
abs_val = val & 0x7FF
7077
if val & 0x800:
7178
return 0 - float(abs_val)
7279
return float(abs_val)
7380

7481

75-
def _twos_comp_to_float(val):
82+
def _twos_comp_to_float(val: int) -> float:
7683
val &= 0xFFF
7784
if val & 0x800:
7885
val -= 0x1000
@@ -83,34 +90,34 @@ class AMG88XX:
8390
"""Driver for the AMG88xx GRID-Eye IR 8x8 thermal camera."""
8491

8592
# Set up the registers
86-
_pctl = i2c_bits.RWBits(8, 0x00, 0)
87-
_rst = i2c_bits.RWBits(8, 0x01, 0)
88-
_fps = i2c_bit.RWBit(0x02, 0)
89-
_inten = i2c_bit.RWBit(0x03, 0)
90-
_intmod = i2c_bit.RWBit(0x03, 1)
93+
_pctl: RWBits = i2c_bits.RWBits(8, 0x00, 0)
94+
_rst: RWBits = i2c_bits.RWBits(8, 0x01, 0)
95+
_fps: RWBit = i2c_bit.RWBit(0x02, 0)
96+
_inten: RWBit = i2c_bit.RWBit(0x03, 0)
97+
_intmod: RWBit = i2c_bit.RWBit(0x03, 1)
9198

92-
_intf = i2c_bit.RWBit(0x04, 1)
93-
_ovf_irs = i2c_bit.RWBit(0x04, 2)
94-
_ovf_ths = i2c_bit.RWBit(0x04, 3)
99+
_intf: RWBit = i2c_bit.RWBit(0x04, 1)
100+
_ovf_irs: RWBit = i2c_bit.RWBit(0x04, 2)
101+
_ovf_ths: RWBit = i2c_bit.RWBit(0x04, 3)
95102

96-
_intclr = i2c_bit.RWBit(0x05, 1)
97-
_ovs_clr = i2c_bit.RWBit(0x05, 2)
98-
_ovt_clr = i2c_bit.RWBit(0x05, 3)
103+
_intclr: RWBit = i2c_bit.RWBit(0x05, 1)
104+
_ovs_clr: RWBit = i2c_bit.RWBit(0x05, 2)
105+
_ovt_clr: RWBit = i2c_bit.RWBit(0x05, 3)
99106

100-
_mamod = i2c_bit.RWBit(0x07, 5)
107+
_mamod: RWBit = i2c_bit.RWBit(0x07, 5)
101108

102-
_inthl = i2c_bits.RWBits(8, 0x08, 0)
103-
_inthh = i2c_bits.RWBits(4, 0x09, 0)
104-
_intll = i2c_bits.RWBits(8, 0x0A, 0)
105-
_intlh = i2c_bits.RWBits(4, 0x0B, 0)
106-
_ihysl = i2c_bits.RWBits(8, 0x0C, 0)
107-
_ihysh = i2c_bits.RWBits(4, 0x0D, 0)
109+
_inthl: RWBits = i2c_bits.RWBits(8, 0x08, 0)
110+
_inthh: RWBits = i2c_bits.RWBits(4, 0x09, 0)
111+
_intll: RWBits = i2c_bits.RWBits(8, 0x0A, 0)
112+
_intlh: RWBits = i2c_bits.RWBits(4, 0x0B, 0)
113+
_ihysl: RWBits = i2c_bits.RWBits(8, 0x0C, 0)
114+
_ihysh: RWBits = i2c_bits.RWBits(4, 0x0D, 0)
108115

109-
_tthl = i2c_bits.RWBits(8, 0x0E, 0)
116+
_tthl: RWBits = i2c_bits.RWBits(8, 0x0E, 0)
110117

111-
_tthh = i2c_bits.RWBits(4, 0x0F, 0)
118+
_tthh: RWBits = i2c_bits.RWBits(4, 0x0F, 0)
112119

113-
def __init__(self, i2c, addr=0x69):
120+
def __init__(self, i2c: busio.I2C, addr: int = 0x69):
114121
self.i2c_device = I2CDevice(i2c, addr)
115122

116123
# enter normal mode
@@ -126,13 +133,13 @@ def __init__(self, i2c, addr=0x69):
126133
self._fps = _FPS_10
127134

128135
@property
129-
def temperature(self):
136+
def temperature(self) -> float:
130137
"""Temperature of the sensor in Celsius"""
131138
raw = (self._tthh << 8) | self._tthl
132139
return _signed_12bit_to_float(raw) * _THERMISTOR_CONVERSION
133140

134141
@property
135-
def pixels(self):
142+
def pixels(self) -> List[List[float]]:
136143
"""Temperature of each pixel across the sensor in Celsius.
137144
138145
Temperatures are stored in a two dimensional list where the first index is the row and

0 commit comments

Comments
 (0)