-
Notifications
You must be signed in to change notification settings - Fork 0
/
foo-adxl-accel.py
executable file
·59 lines (37 loc) · 1.1 KB
/
foo-adxl-accel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
# i2c_ADXL345.py
# 2015-04-01
# Public Domain
import time
import struct
import sys
#import pigpio # http://abyz.co.uk/rpi/pigpio/python.html
import lgpio as sbc
if sys.version > '3':
buffer = memoryview
BUS=1
ADXL345_I2C_ADDR=0x53
RUNTIME=60.0
#pi=pigpio.pi() # open local Pi
h = sbc.i2c_open(BUS, ADXL345_I2C_ADDR)
if h >= 0: # Connected OK?
# Initialise ADXL345.
sbc.i2c_write_byte_data(h, 0x2d, 0) # POWER_CTL reset.
sbc.i2c_write_byte_data(h, 0x2d, 8) # POWER_CTL measure.
sbc.i2c_write_byte_data(h, 0x31, 0) # DATA_FORMAT reset.
sbc.i2c_write_byte_data(h, 0x31, 11) # DATA_FORMAT full res +/- 16g.
read = 0
start_time = time.time()
while (time.time()-start_time) < RUNTIME:
# 0x32 = X LSB, 0x33 = X MSB
# 0x34 = Y LSB, 0x35 = Y MSB
# 0x36 = Z LSB, 0x37 = Z MSB
# < = little endian
(s, b) = sbc.i2c_read_i2c_block_data(h, 0x32, 6)
if s >= 0:
(x, y, z) = struct.unpack('<3h', buffer(b))
print("{} {} {}".format(x, y, z))
read += 1
sbc.i2c_close(h)
#pi.stop()
print(read, read/RUNTIME)