Repository navigation
Expand file tree
/
Copy pathQwiic.py
More file actions
85 lines (76 loc) · 2.5 KB
/
Copy pathQwiic.py
File metadata and controls
85 lines (76 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# FILE: Qwiic.py
# AUTHOR: Josip Šimun Kuči @ Soldered
# BRIEF: MicroPython module for Qwiic functionalities
# LAST UPDATED: 2025-06-10
from machine import I2C, Pin
from os import uname
class Qwiic:
def __init__(self, i2c=None, address: int = 0x30, native: bool = False):
"""
Initialize the Qwiic interface.
:param i2c: Initialized machine.I2C object
:param address: I2C address of the device
:param native: Use native initialization instead of I2C
"""
if i2c != None:
self.i2c = i2c
else:
if uname().sysname in ("esp32", "esp8266", "Soldered Dasduino CONNECTPLUS"):
self.i2c = I2C(0, scl=Pin(22), sda=Pin(21))
else:
raise Exception("Board not recognized, enter Qwiic I2C pins manually")
self.address = address
self.native = native
self.begin_done = False
self.err = 0
def begin(self):
"""
Initializes the sensor using native or I2C.
"""
if self.native:
self.initialize_native()
else:
# No action needed for MicroPython, assume i2c is already initialized
pass
self.begin_done = True
def initialize_native(self):
"""
Override this method in subclasses if native initialization is required.
"""
raise NotImplementedError("Subclasses must implement initialize_native()")
def send_address(self, reg_addr: int) -> int:
"""
Sends a single byte (register address) to the device.
"""
try:
self.i2c.writeto(self.address, bytes([reg_addr]))
return 0
except Exception as e:
self.err = e
return -1
def read_data(self, n: int) -> bytes:
"""
Reads `n` bytes from the device.
"""
try:
return self.i2c.readfrom(self.address, n)
except Exception as e:
self.err = e
return bytes()
def read_register(self, reg_addr: int, n: int) -> bytes:
"""
Sends a register address, then reads `n` bytes from the device.
"""
if self.send_address(reg_addr) != 0:
return bytes()
return self.read_data(n)
def send_data(self, data: bytes) -> int:
"""
Writes data to the device.
"""
try:
self.i2c.writeto(self.address, data)
return 0
except Exception as e:
self.err = e
return -1