-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecode_message_3.py
44 lines (35 loc) · 1.24 KB
/
decode_message_3.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
import time
import RPi.GPIO as GPIO
CLOCK_CHANNEL = 16
DATA_CHANNEL = 23
# This is a callback function that will be called whenever we have a high/low
# or low/high change in the signal.
def my_callback(_channel):
result = GPIO.input(DATA_CHANNEL)
if result:
print("1", end="")
my_callback.result_byte += 1 << (7 - my_callback.counter)
else:
print("0", end="")
my_callback.counter += 1
if my_callback.counter > 7:
print(f" = {my_callback.result_byte} = {chr(my_callback.result_byte)}")
my_callback.counter = 0
my_callback.result_byte = 0
my_callback.counter = 0
my_callback.result_byte = 0
# Set pin 12 up for input
GPIO.setmode(GPIO.BCM)
GPIO.setup(CLOCK_CHANNEL, GPIO.IN)
GPIO.setup(DATA_CHANNEL, GPIO.IN)
# Now we have to 'register' the callback function so that the GPIO library
# will call the function when the change occurs on pin 12.
# GPIO.BOTH means it is called on both rising and falling changes.
# Use GPIO.FALLING if you want it called for high to low
# Use GPIO.RISING if you want it called low to high
GPIO.add_event_detect(CLOCK_CHANNEL, GPIO.FALLING, callback=my_callback)
# Now just wait forever.
print("Running")
while True:
time.sleep(60)
print("Still running")