/ adafruit_register / i2c_struct.py
i2c_struct.py
1 # The MIT License (MIT) 2 # 3 # Copyright (c) 2016 Adafruit Industries 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining a copy 6 # of this software and associated documentation files (the "Software"), to deal 7 # in the Software without restriction, including without limitation the rights 8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 # copies of the Software, and to permit persons to whom the Software is 10 # furnished to do so, subject to the following conditions: 11 # 12 # The above copyright notice and this permission notice shall be included in 13 # all copies or substantial portions of the Software. 14 # 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 # THE SOFTWARE. 22 # pylint: disable=too-few-public-methods 23 """ 24 `adafruit_register.i2c_struct` 25 ==================================================== 26 27 Generic structured registers based on `struct` 28 29 * Author(s): Scott Shawcroft 30 """ 31 32 try: 33 import struct 34 except ImportError: 35 import ustruct as struct 36 37 __version__ = "0.0.0-auto.0" 38 __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Register.git" 39 40 41 class Struct: 42 """ 43 Arbitrary structure register that is readable and writeable. 44 45 Values are tuples that map to the values in the defined struct. See struct 46 module documentation for struct format string and its possible value types. 47 48 :param int register_address: The register address to read the bit from 49 :param type struct_format: The struct format string for this register. 50 """ 51 52 def __init__(self, register_address, struct_format): 53 self.format = struct_format 54 self.buffer = bytearray(1 + struct.calcsize(self.format)) 55 self.buffer[0] = register_address 56 57 def __get__(self, obj, objtype=None): 58 with obj.i2c_device as i2c: 59 i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1) 60 return struct.unpack_from(self.format, memoryview(self.buffer)[1:]) 61 62 def __set__(self, obj, value): 63 struct.pack_into(self.format, self.buffer, 1, *value) 64 with obj.i2c_device as i2c: 65 i2c.write(self.buffer) 66 67 68 class UnaryStruct: 69 """ 70 Arbitrary single value structure register that is readable and writeable. 71 72 Values map to the first value in the defined struct. See struct 73 module documentation for struct format string and its possible value types. 74 75 :param int register_address: The register address to read the bit from 76 :param type struct_format: The struct format string for this register. 77 """ 78 79 def __init__(self, register_address, struct_format): 80 self.format = struct_format 81 self.address = register_address 82 83 def __get__(self, obj, objtype=None): 84 buf = bytearray(1 + struct.calcsize(self.format)) 85 buf[0] = self.address 86 with obj.i2c_device as i2c: 87 i2c.write_then_readinto(buf, buf, out_end=1, in_start=1) 88 return struct.unpack_from(self.format, buf, 1)[0] 89 90 def __set__(self, obj, value): 91 buf = bytearray(1 + struct.calcsize(self.format)) 92 buf[0] = self.address 93 struct.pack_into(self.format, buf, 1, value) 94 with obj.i2c_device as i2c: 95 i2c.write(buf) 96 97 98 class ROUnaryStruct(UnaryStruct): 99 """ 100 Arbitrary single value structure register that is read-only. 101 102 Values map to the first value in the defined struct. See struct 103 module documentation for struct format string and its possible value types. 104 105 :param int register_address: The register address to read the bit from 106 :param type struct_format: The struct format string for this register. 107 """ 108 109 def __set__(self, obj, value): 110 raise AttributeError()