|
| 1 | +''' |
| 2 | +@license MIT License |
| 3 | +
|
| 4 | +Copyright (c) 2022 lewis he |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +
|
| 24 | +@file I2CInterface.py |
| 25 | +@author Lewis He ([email protected]) |
| 26 | +@date 2022-10-20 |
| 27 | +
|
| 28 | +''' |
| 29 | + |
| 30 | +from sys import implementation |
| 31 | + |
| 32 | +if implementation.name == 'micropython': |
| 33 | + from machine import Pin, I2C |
| 34 | +if implementation.name == 'circuitpython': |
| 35 | + from adafruit_bus_device import i2c_device |
| 36 | + |
| 37 | + |
| 38 | +class I2CInterface: |
| 39 | + def __init__(self, i2c_bus: I2C, addr: int) -> None: |
| 40 | + if implementation.name == 'micropython': |
| 41 | + print('micropython') |
| 42 | + self._bus = i2c_bus |
| 43 | + if implementation.name == 'circuitpython': |
| 44 | + print('circuitpython') |
| 45 | + self._bus = i2c_device.I2CDevice(i2c_bus, addr) |
| 46 | + self._address = addr |
| 47 | + |
| 48 | + def _BV(self, bit) -> int: |
| 49 | + return (1 << bit) |
| 50 | + |
| 51 | + def _IS_BIT_SET(self, val, mask) -> bool: |
| 52 | + return bool((((val) & (mask)) == (mask))) |
| 53 | + |
| 54 | + def writeRegister(self, reg: int, val: int) -> None: |
| 55 | + if implementation.name == 'micropython': |
| 56 | + buf = bytearray(1) |
| 57 | + buf[0] = val |
| 58 | + self._bus.writeto_mem( |
| 59 | + self._address, (reg & 0xFF), buf) |
| 60 | + elif implementation.name == 'circuitpython': |
| 61 | + buf = bytearray(2) |
| 62 | + buf[0] = reg & 0xFF |
| 63 | + buf[1] = val & 0xFF |
| 64 | + with self._bus as i2c: |
| 65 | + i2c.write(buf, start=0, end=2) |
| 66 | + |
| 67 | + def readRegister(self, reg: int, length: int = 1) -> list: |
| 68 | + if implementation.name == 'micropython': |
| 69 | + buf = bytearray(length) |
| 70 | + self._bus.readfrom_mem_into( |
| 71 | + self._address, (reg & 0xFF), buf) |
| 72 | + return list(buf) |
| 73 | + elif implementation.name == 'circuitpython': |
| 74 | + with self._bus as i2c: |
| 75 | + i2c.write(bytes([reg & 0xFF])) |
| 76 | + result = bytearray(length) |
| 77 | + i2c.readinto(result) |
| 78 | + return result |
| 79 | + |
| 80 | + def getRegisterBit(self, reg, bit) -> bool: |
| 81 | + val = self.readRegister(reg)[0] |
| 82 | + return val & self._BV(bit) |
| 83 | + |
| 84 | + def setRegisterBit(self, reg: int, bit: int): |
| 85 | + val = self.readRegister(reg)[0] |
| 86 | + self.writeRegister(reg, (val | (self._BV(bit)))) |
| 87 | + |
| 88 | + def clrRegisterBit(self, reg: int, bit: int): |
| 89 | + val = self.readRegister(reg)[0] |
| 90 | + self.writeRegister(reg, (val & (~self._BV(bit)))) |
| 91 | + |
| 92 | + def readRegisterH8L4(self, highReg, lowReg) -> int: |
| 93 | + h8 = self.readRegister(highReg)[0] |
| 94 | + l4 = self.readRegister(lowReg)[0] |
| 95 | + return (h8 << 4) | (l4 & 0x0F) |
| 96 | + |
| 97 | + def readRegisterH8L5(self, highReg, lowReg) -> int: |
| 98 | + h8 = self.readRegister(highReg)[0] |
| 99 | + l5 = self.readRegister(lowReg)[0] |
| 100 | + return (h8 << 5) | (l5 & 0x1F) |
| 101 | + |
| 102 | + def readRegisterH6L8(self, highReg, lowReg) -> int: |
| 103 | + h6 = self.readRegister(highReg)[0] |
| 104 | + l8 = self.readRegister(lowReg)[0] |
| 105 | + return ((h6 & 0x3F) << 8) | l8 |
| 106 | + |
| 107 | + def readRegisterH5L8(self, highReg, lowReg) -> int: |
| 108 | + h5 = self.readRegister(highReg)[0] |
| 109 | + l8 = self.readRegister(lowReg)[0] |
| 110 | + return ((h5 & 0x1F) << 8) | l8 |
0 commit comments