-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver.py
67 lines (50 loc) · 1.59 KB
/
receiver.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
63
64
65
66
67
import config
from gpiozero import LightSensor
from time import sleep
import binascii
def text_from_bits(bits, encoding='utf-8', errors='surrogatepass'):
''' returns a readable string from a bit'''
n = int(bits, 2)
return int_to_bytes(n).decode(encoding, errors)
def int_to_bytes(i):
''' convert series of bits to a byte '''
hex_string = '%x' % i
n = len(hex_string)
return binascii.unhexlify(hex_string.zfill(n + (n & 1)))
def start():
''' initiate the receiver '''
sensor = LightSensor(config.RECEIVER_PIN)
chars = []
received = ""
running = True
''' Loop until detected laser (syncing) '''
while True:
if int(sensor.light_detected):
break
while running:
# lets get the message
bit = int(sensor.light_detected)
received += str(bit)
if len(received) >= 8:
# end
if received == config.START_END and len(chars) != 0:
print('[Receiver] stopped recording')
running = False
# start
elif received == config.START_END:
print('[Receiver] recording...')
received = ""
# append
else:
chars.append(received)
received = ""
sleep(config.SEND_RATE)
# message received, process it
msg = ""
for c in chars:
try:
msg += text_from_bits(c)
except Exception:
msg += ' unknown '
print(msg)
print(chars)