Created
April 4, 2023 15:21
-
-
Save jakedolan443/bb11a1c1b8ea3876abb60455f64c55e4 to your computer and use it in GitHub Desktop.
Laser module transmission script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
import time, random | |
import os, subprocess | |
from threading import Thread | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setwarnings(False) | |
import sys,tty,os,termios | |
from keypresses import getkey | |
GPIO.setup(8,GPIO.OUT) | |
GPIO.setup(40, GPIO.OUT) | |
GPIO.output(40, GPIO.HIGH) | |
GPIO.output(8, GPIO.LOW) | |
GPIO.setup(10, GPIO.IN) | |
send_speed = 0.001 | |
send_speed_mid = send_speed/2 | |
def begin_listener(expecting_bits): | |
global end_string | |
time.sleep(send_speed_mid) | |
bits = "" | |
while True: | |
read_bit = GPIO.input(10) | |
bits += str(read_bit) | |
if len(bits) == expecting_bits: | |
break | |
time.sleep(send_speed) | |
key = chr(int(bits, 2)) | |
end_string += key | |
def send_data(list_of_bits): | |
GPIO.output(8, GPIO.HIGH) | |
timer = 0 | |
print("connect laser and transmitter to begin ...") | |
while True: | |
time.sleep(0.001) | |
if not (GPIO.input(10)): | |
timer += 1 | |
else: | |
timer = 0 | |
if timer > 1000: | |
break | |
print("sending ...") | |
GPIO.output(8, GPIO.LOW) | |
oldtime = time.time() | |
for bits in list_of_bits: | |
GPIO.output(8, GPIO.LOW) | |
Thread(target=begin_listener, args=(len(bits),)).start() | |
for bit in bits: | |
GPIO.output(8, [GPIO.HIGH, GPIO.LOW][int(bit)]) | |
time.sleep(send_speed) | |
time.sleep(send_speed) | |
print("complete!") | |
print("transmission took {0:.{1}f} seconds.".format(time.time() - oldtime, 4)) | |
GPIO.output(8, GPIO.LOW) | |
end_string = "" | |
with open("new.txt", "r") as f: | |
string = f.read() | |
data = [] | |
for character in string: | |
data.append(''.join(format(x, 'b') for x in bytearray(character, 'utf-8'))) | |
print("preparing to send file 'new.txt' of size {}kB...".format(os.path.getsize("new.txt")/1000)) | |
send_data(data) | |
if not(string == end_string): | |
print("errors detected.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment