forked from inspireFly-VT/circuitpy_flight_software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpico_comms_receive_circuit_v2.py
80 lines (70 loc) · 2.99 KB
/
pico_comms_receive_circuit_v2.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
68
69
70
71
72
73
74
75
76
77
78
79
80
# This code is for the "a" side of the communications between the boards
# Start this code first and then start b's code afterwards
# Once b has started you may now input commands
from easy_comms_circuit import EasyComms
import time
import board
import busio
import storage
# Initialize communication
com1 = EasyComms(board.TX, board.RX, baud_rate=115200)
com1.start()
received_bytes = b""
# Remount the filesystem to be writable
# storage.remount("/", readonly=False)
# Start interaction loop with b:
while True:
# Enter Inputs
command = 'chunk'#input('\n~> Input "photodata" to see all the photos data, "chunk" to gather chunks, or say "finished" to end communication: ')
time.sleep(2)
# # End communications
# if command.lower() == 'finished':
# request = "finished"
# com1.overhead_send(request)
# print('Ended communications.')
# break
# # Read default overhead message for photos data
# if command.lower() == 'photodata':
# request = "photodata"
# com1.overhead_send(request)
# message = ""
# message = com1.overhead_read()
# print(f"Look for: {message.strip('\n')}")
# # Send over chunks
if command.lower() == 'chunk':
request = "chunk"
com1.overhead_send(request) # tell b you are requesting for chunks
lowerchunk = '0'#input('Chunk lower bound: ') # input lower bound
upperchunk = '10'#input('Chunk upper bound: ') # input upper bound
time.sleep(2)
if lowerchunk.isdigit() and upperchunk.isdigit() and int(lowerchunk) <= int(upperchunk):
message = lowerchunk + ' ' + upperchunk # compile lower and upper bounds in message to send
com1.overhead_send(message)
print(message)
##
jpg_bytes, count = com1.read_bytes(lowerchunk, upperchunk) # call read_bytes to read chunks from b
print('Number of chunks received = ', count)
print('Number of bytes received = ', len(jpg_bytes))
print(jpg_bytes)
# Assemble chunks into photo file
if jpg_bytes is not None:
try:
with open("received2.jpg", "wb") as file:
file.write(jpg_bytes)
print("JPG file successfully created!")
except OSError as e:
print(f"Failed to write file: {e}")
else:
# If lower and upper bound inputs are incorrect
print('Incorrect input, only accepted whole numbers.')
message = "Wrong"
com1.overhead_send(message)
command = 'finished'#input('\n~> Input "photodata" to see all the photos data, "chunk" to gather chunks, or say "finished" to end communication: ')
time.sleep(2)
if command.lower() == 'finished':
request = "finished"
com1.overhead_send(request)
print('Ended communications.')
break
else:
print('Wrong command entered, try again.')