Skip to content

Commit 36f1dd3

Browse files
committed
Implemented PozyxI2C
1 parent ff356aa commit 36f1dd3

File tree

7 files changed

+135
-23
lines changed

7 files changed

+135
-23
lines changed

pypozyx/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080

8181
from pypozyx.definitions import *
8282
from pypozyx.pozyx_serial import *
83+
from pypozyx.pozyx_i2c import *
8384
from pypozyx.structures.device import *
8485
from pypozyx.structures.generic import *
8586
from pypozyx.structures.sensor_data import *

pypozyx/definitions/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99

1010
class PozyxConstants:
11+
# I2C
12+
POZYX_I2C_ADDRESS = 0x4B
13+
1114
# Pozyx serial buffer sizes
1215
MAX_BUF_SIZE = 100
1316
MAX_SERIAL_SIZE = 28

pypozyx/lib.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,10 +1117,10 @@ def doPositioning(self, position, dimension=PozyxConstants.DIMENSION_3D, height=
11171117

11181118
def getPositioningData(self, positioning_data):
11191119
flags = Data([positioning_data.flags], 'H')
1120-
flags.load_hex_string()
1121-
s = 'F,%0.2x,%s,%i\r' % (PozyxRegisters.DO_POSITIONING_WITH_DATA, flags.byte_data, positioning_data.byte_size + 61)
1122-
# very custom solution...
1123-
r = self.serialExchange(s)
1120+
temp_data = Data([0] * (positioning_data.byte_size + 60))
1121+
ret = self.useFunction(PozyxRegisters.DO_POSITIONING_WITH_DATA, flags, temp_data)
1122+
temp_data.load_hex_string()
1123+
r = ('%0.2x' % ret) + temp_data.byte_data
11241124
if positioning_data.has_ranges():
11251125
amount_of_ranges = int(r[2 * positioning_data.byte_size:2 * positioning_data.byte_size + 2], 16)
11261126
positioning_data.set_amount_of_ranges(amount_of_ranges)

pypozyx/pozyx_i2c.py

Lines changed: 124 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,140 @@
1-
#!/usr/bin/env python
2-
"""
1+
"""pypozyx.pozyx_i2c - contains the I2C interface with Pozyx through PozyxI2C."""
2+
from pypozyx.core import PozyxConnectionError
33

4-
"""
4+
from pypozyx.definitions.constants import (POZYX_SUCCESS, POZYX_FAILURE,
5+
PozyxConstants)
56

67
from pypozyx.lib import PozyxLib
7-
from pypozyx.definitions.constants import MODE_POLLING
8-
# from pypozyx.definitions.registers import *
8+
from pypozyx.structures.generic import SingleRegister
9+
from smbus2 import SMBus, i2c_msg
10+
11+
from warnings import warn
912

1013

1114
class PozyxI2C(PozyxLib):
15+
"""This class provides the Pozyx I2C interface.
16+
All functionality from PozyxLib and PozyxCore is included.
17+
18+
Args:
19+
bus: i2c bus number (e.g. 0 or 1) or an absolute file path (e.g. `/dev/i2c-42`).
20+
print_output (optional): boolean for printing the serial exchanges, mainly for debugging purposes
21+
suppress_warnings (optional): boolean for suppressing warnings in the Pozyx use, usage not recommended
22+
debug_trace (optional): boolean for printing the trace on bad serial init (DEPRECATED)
23+
show_trace (optional): boolean for printing the trace on bad serial init (DEPRECATED)
24+
"""
25+
26+
def __init__(self, bus=1,
27+
print_output=False, debug_trace=False, show_trace=False,
28+
suppress_warnings=False):
29+
"""Initializes the PozyxI2C object. See above for details."""
30+
super(PozyxI2C, self).__init__()
31+
self.print_output = print_output
32+
if debug_trace is True or show_trace is True:
33+
if not suppress_warnings:
34+
warn("debug_trace or show_trace are on their way out, exceptions of the type PozyxException are now raised.",
35+
DeprecationWarning)
36+
self.suppress_warnings = suppress_warnings
1237

13-
def __init__(self, mode=MODE_POLLING, print_output=False):
14-
pass
38+
self.bus = SMBus(bus)
39+
40+
self.validatePozyx()
41+
42+
def validatePozyx(self):
43+
"""Validates whether the connected device is indeed a Pozyx device"""
44+
whoami = SingleRegister()
45+
if self.getWhoAmI(whoami) != POZYX_SUCCESS:
46+
raise PozyxConnectionError("Connected to device, but couldn't read I2C data. Is it a Pozyx?")
47+
if whoami.value != 0x43:
48+
raise PozyxConnectionError("POZYX_WHO_AM_I returned 0x%0.2x, something is wrong with Pozyx." % whoami.value)
1549

1650
def regWrite(self, address, data):
17-
pass
51+
"""
52+
Writes data to the Pozyx registers, starting at a register address,
53+
if registers are writable.
54+
55+
Args:
56+
address: Register address to start writing at.
57+
data: Data to write to the Pozyx registers.
58+
Has to be ByteStructure-derived object.
59+
60+
Returns:
61+
POZYX_SUCCESS, POZYX_FAILURE
62+
"""
63+
bdata = data.transform_to_bytes()
64+
try:
65+
msg_addr = i2c_msg.write(PozyxConstants.POZYX_I2C_ADDRESS, bytes([address]))
66+
msg_write = i2c_msg.write(PozyxConstants.POZYX_I2C_ADDRESS, bdata)
67+
self.bus.i2c_rdwr(msg_addr, msg_write)
68+
except OSError:
69+
return POZYX_FAILURE
70+
return POZYX_SUCCESS
1871

1972
def regRead(self, address, data):
20-
pass
73+
"""
74+
Reads data from the Pozyx registers, starting at a register address,
75+
if registers are readable.
76+
77+
Args:
78+
address: Register address to start writing at.
79+
data: Data to read from the Pozyx registers.
80+
Has to be ByteStructure-derived object.
81+
82+
Returns:
83+
POZYX_SUCCESS, POZYX_FAILURE
84+
"""
85+
try:
86+
msg_addr = i2c_msg.write(PozyxConstants.POZYX_I2C_ADDRESS, bytes([address]))
87+
msg_read = i2c_msg.read(PozyxConstants.POZYX_I2C_ADDRESS, data.byte_size)
88+
self.bus.i2c_rdwr(msg_addr, msg_read)
89+
r = bytes(msg_read)
90+
except OSError:
91+
return POZYX_FAILURE
92+
data.load_packed(r)
93+
return POZYX_SUCCESS
2194

2295
def regFunction(self, address, params, data):
23-
pass
96+
"""
97+
Performs a register function on the Pozyx, if the address is a register
98+
function.
99+
100+
Args:
101+
address: Register function address of function to perform.
102+
params: Parameters for the register function.
103+
Has to be ByteStructure-derived object.
104+
data: Container for the data the register function returns.
105+
Has to be ByteStructure-derived object.
106+
107+
Returns:
108+
POZYX_SUCCESS, POZYX_FAILURE
109+
"""
110+
bparams = params.transform_to_bytes()
111+
try:
112+
msg_write = i2c_msg.write(PozyxConstants.POZYX_I2C_ADDRESS, bytes([address]) + bytes(bparams))
113+
msg_read = i2c_msg.read(PozyxConstants.POZYX_I2C_ADDRESS, data.byte_size + 1)
114+
self.bus.i2c_rdwr(msg_write, msg_read)
115+
r = bytes(msg_read)
116+
except OSError:
117+
return POZYX_FAILURE
118+
if len(data) > 0:
119+
data.load_packed(r[1:])
120+
return r[0]
121+
122+
def waitForFlag(self, interrupt_flag, timeout_s, interrupt):
123+
"""
124+
Waits for a certain interrupt flag to be triggered, indicating that
125+
that type of interrupt occured.
24126
25-
#
26-
def waitForFlag(self, interrupt_flag, timeout_ms, interrupt):
27-
pass
127+
Args:
128+
interrupt_flag: Flag indicating interrupt type.
129+
timeout_s: time in seconds that POZYX_INT_STATUS will be checked
130+
for the flag before returning POZYX_TIMEOUT.
28131
29-
#
30-
def waitForFlagSafe(self, interrupt_flag, timeout_ms, interrupt):
31-
pass
132+
Kwargs:
133+
interrupt: Container for the POZYX_INT_STATUS data
32134
33-
def configInterruptPin(self, pin, mode, bActiveHigh, bLatch, remote_id=None):
34-
pass
135+
Returns:
136+
POZYX_SUCCESS, POZYX_FAILURE, POZYX_TIMEOUT
137+
"""
138+
if interrupt is None:
139+
interrupt = SingleRegister()
140+
return self.waitForFlagSafe(interrupt_flag, timeout_s, interrupt)

pypozyx/pozyx_serial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def regRead(self, address, data):
250250
251251
Args:
252252
address: Register address to start writing at.
253-
data: Data to write to the Pozyx registers.
253+
data: Data to read from the Pozyx registers.
254254
Has to be ByteStructure-derived object.
255255
256256
Returns:

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pyserial>=3.0
2+
smbus2>=0.3.0

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
description='Python library for Pozyx devices',
1919
install_requires=[
2020
'pyserial>=3.0'
21+
'smbus2>=0.3.0'
2122
],
2223
long_description=long_description,
2324
author='Laurent Van Acker',

0 commit comments

Comments
 (0)