46
46
* Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
47
47
"""
48
48
49
-
49
+ import struct
50
50
import adafruit_bus_device .i2c_device as i2cdevice
51
51
__version__ = "0.0.0-auto.0"
52
52
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TLV493D.git"
@@ -79,8 +79,8 @@ class TLV493D:
79
79
"INT" :(1 , 0x04 , 2 ),
80
80
"FAST" :(1 , 0x02 , 1 ),
81
81
"LOWPOWER" :(1 , 0x01 , 0 ),
82
- "TEMP_EN " :(3 , 0x80 , 7 ),
83
- "LOWPOWER_PERIOD " :(3 , 0x40 , 6 ),
82
+ "TEMP_DISABLE " :(3 , 0x80 , 7 ),
83
+ "LP_PERIOD " :(3 , 0x40 , 6 ),
84
84
"POWERDOWN" :(3 , 0x20 , 5 ),
85
85
"RES1" :(1 , 0x18 , 3 ),
86
86
"RES2" :(2 , 0xFF , 0 ),
@@ -98,27 +98,37 @@ def __init__(self, i2c_interface, i2c_address=DEFAULT_TLV_ADDRESS):
98
98
# // copy factory settings to write registers
99
99
# setRegBits(tlv493d::W_RES1, getRegBits(tlv493d::R_RES1)); THREE TIMES?
100
100
# // enable parity detection
101
- # setRegBits(tlv493d::W_PARITY_EN, 1);
101
+
102
+ print ("\n Write buffer:" )
103
+ self .print_bytes (self .write_buffer )
104
+ self ._set_write_key ('PARITY' , 1 );
105
+ print ("\n Write buffer:" )
106
+ self .print_bytes (self .write_buffer )
102
107
# // config sensor to lowpower mode
103
108
# // also contains parity calculation and writeout to sensor
104
- # setAccessMode(TLV493D_DEFAULTMODE);
109
+ # setAccessMode(TLV493D_DEFAULTMODE);
110
+ self ._set_write_key ('PARITY' , 1 );
111
+ self ._set_write_key ('LOWPOWER' , 1 );
112
+ self ._set_write_key ('LP_PERIOD' , 1 );
113
+ print ("\n Write buffer:" )
114
+ self .print_bytes (self .write_buffer )
115
+ self ._write_i2c ()
116
+ print ("wrote output buffer out" )
105
117
106
118
def _read_i2c (self ):
107
119
with self .i2c_device as i2c :
108
120
i2c .readinto (self .read_buffer )
121
+ # self.print_bytes(self.read_buffer)
109
122
110
123
@staticmethod
111
124
def print_bytes (bites ):
112
125
"""DOC STRING"""
113
126
for byte in bites :
114
127
print ("%s (%s)" % (bin (byte ), hex (byte )))
115
128
116
- # def _write_i2c(self, obj, value):
117
- # buf = bytearray(1+struct.calcsize(self.format))
118
- # buf[0] = self.address
119
- # struct.pack_into(self.format, buf, 1, value)
120
- # with obj.i2c_device as i2c:
121
- # i2c.write(buf)
129
+ def _write_i2c (self ):
130
+ with self .i2c_device as i2c :
131
+ i2c .write (self .write_buffer )
122
132
123
133
def _setup_write_buffer (self ):
124
134
self ._read_i2c ()
@@ -136,12 +146,9 @@ def _setup_write_buffer(self):
136
146
self .print_bytes (self .write_buffer )
137
147
138
148
def _get_read_key (self , key ):
139
- print ("%s -> read: %s write: %s" % (key , self .read_masks [key ], self .write_masks [key ]))
140
149
read_byte_num , read_mask , read_shift = self .read_masks [key ]
141
150
raw_read_value = self .read_buffer [read_byte_num ]
142
- print ("value: %s" % bin (raw_read_value ))
143
151
write_value = (raw_read_value & read_mask )>> read_shift
144
- print ("write val: %s" % bin (write_value ))
145
152
return write_value
146
153
147
154
def _set_write_key (self , key , value ):
@@ -150,3 +157,36 @@ def _set_write_key(self, key, value):
150
157
current_write_byte &= ~ write_mask
151
158
current_write_byte |= value << write_shift
152
159
self .write_buffer [write_byte_num ] = current_write_byte
160
+
161
+ @property
162
+ def x (self ):
163
+ self ._read_i2c ()
164
+ x_top = self ._get_read_key ("BX1" )
165
+ x_bot = self ._get_read_key ("BX2" )
166
+ x_val = x_top << 4 | x_bot
167
+ xtop = (x_val & 0xFF00 )>> 8
168
+ xbot = (x_val & 0x00FF )
169
+ binval = struct .unpack_from (">h" , bytearray ([xtop , xbot ]))[0 ]
170
+ return binval
171
+
172
+ @property
173
+ def y (self ):
174
+ self ._read_i2c ()
175
+ y_top = self ._get_read_key ("BY1" )
176
+ y_bot = self ._get_read_key ("BY2" )
177
+ y_val = y_top << 4 | y_bot
178
+ ytop = (y_val & 0xFF00 )>> 8
179
+ ybot = (y_val & 0x00FF )
180
+ binval = struct .unpack_from (">h" , bytearray ([ytop , ybot ]))[0 ]
181
+ return binval
182
+
183
+ @property
184
+ def z (self ):
185
+ self ._read_i2c ()
186
+ z_top = self ._get_read_key ("BZ1" )
187
+ z_bot = self ._get_read_key ("BZ2" )
188
+ z_val = z_top << 4 | z_bot
189
+ ztop = (z_val & 0xFF00 )>> 8
190
+ zbot = (z_val & 0x00FF )
191
+ binval = struct .unpack_from (">h" , bytearray ([ztop , zbot ]))[0 ]
192
+ return binval
0 commit comments