|
1 |
| -#!/usr/bin/env python |
2 |
| -""" |
| 1 | +"""pypozyx.pozyx_i2c - contains the I2C interface with Pozyx through PozyxI2C.""" |
| 2 | +from pypozyx.core import PozyxConnectionError |
3 | 3 |
|
4 |
| -""" |
| 4 | +from pypozyx.definitions.constants import (POZYX_SUCCESS, POZYX_FAILURE, |
| 5 | + PozyxConstants) |
5 | 6 |
|
6 | 7 | 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 |
9 | 12 |
|
10 | 13 |
|
11 | 14 | 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 |
12 | 37 |
|
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) |
15 | 49 |
|
16 | 50 | 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 |
18 | 71 |
|
19 | 72 | 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 |
21 | 94 |
|
22 | 95 | 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. |
24 | 126 |
|
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. |
28 | 131 |
|
29 |
| - # |
30 |
| - def waitForFlagSafe(self, interrupt_flag, timeout_ms, interrupt): |
31 |
| - pass |
| 132 | + Kwargs: |
| 133 | + interrupt: Container for the POZYX_INT_STATUS data |
32 | 134 |
|
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) |
0 commit comments