-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaser.py
37 lines (29 loc) · 894 Bytes
/
laser.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
from gpiozero import LED
import config
from time import sleep
import binascii
laser = LED(config.LASER_PIN)
def text_to_bits(text, encoding='utf-8', errors='surrogatepass'):
''' convert text to sendable bits '''
bits = bin(int(binascii.hexlify(text.encode(encoding, errors)), 16))[2:]
return list(bits.zfill(8 * ((len(bits) + 7) // 8)))
def sendStartFinish():
''' Kill and Initiate Signal '''
laser.on()
sleep(config.SEND_RATE * 8)
def send(text):
''' sends text via laser in binary '''
sendStartFinish()
bits = text_to_bits(text)
print('Sending: [{}] {} [{}]'.format(
'1'*8, bits, '1'*8
))
for bit in bits:
if bit == '1':
laser.on()
else:
laser.off()
sleep(config.SEND_RATE)
sendStartFinish()
laser.off()
print('Message sent!')