-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.py
63 lines (50 loc) · 1.41 KB
/
main2.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
60
61
62
# high chip select means writing, low means reading
# clock is negative edged
import RPi.GPIO as GPIO
from time import time
GPIO.setmode(GPIO.BCM)
data_pins = [24, 4, 17, 22, 13, 25, 18, 23]
clock_pin = 16
chip_select = 26
GPIO.setup(clock_pin, GPIO.OUT)
GPIO.setup(chip_select, GPIO.OUT)
def send_byte(byte_out):
"""
Send a single byte.
"""
GPIO.output(clock_pin, 0)
# set the chip select to write
GPIO.output(chip_select, 1)
# send the byte
values = [(ord(byte_out) >> i) % 2 for i in range(0, 8)]
GPIO.setup(data_pins, GPIO.OUT)
GPIO.output(data_pins, values)
# flash the clock pin
GPIO.output(clock_pin, 1)
GPIO.output(clock_pin, 0)
def get_byte():
"""
Get a single byte.
"""
GPIO.setup(data_pins, GPIO.IN)
# read the data pins
GPIO.output(chip_select, 0)
GPIO.output(clock_pin, 1)
GPIO.output(clock_pin, 0)
value = 0
for i in range(0, 8):
value += GPIO.input(data_pins[i]) << i
return value
def read_dimension(dimension):
# first, set the dimension (x, y, z)
send_byte(dimension)
first_byte = get_byte()
second_byte = get_byte()
return first_byte + (second_byte << 8)
if __name__ == "__main__":
while True:
start = time()
x_val = read_dimension(b'x')
y_val = read_dimension(b'y')
z_val = read_dimension(b'z')
print(x_val, y_val, z_val, time()-start)