-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterface.jl
246 lines (178 loc) · 6.68 KB
/
Interface.jl
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
""" Interface
This contains a module used to interface with the devices over
a USB port (usually either an Arduino or the CubeSat). Several common
functions are provided, so that all that needs to be written is the specific
program to run.
Includes some simple example functions to send/receive messages from a device.
Allows for communications with two devices at the same time.
Still a work in progress
"""
module Interface
using LibSerialPort
export interface, double_interface
export str2vec, vec2str, check_open_ports
### INTERFACE FUNCTIONS ###
""" interface(f; port, rate)
Simple interface function that connects to a device at `port` using a baudrate of
`rate` and then calls function `f`. Only allows for one device; for two, use
`double_interface`.
Automatically closes the port when finished or due to a failure.
"""
function interface(f = default; port = "/dev/ttyACM0", rate = 115200, kwargs...)
LibSerialPort.open(port, rate) do dev
f(dev; kwargs...)
end
end
""" double_interface(f; port1, rate1, port2, rate2)
Simple interface function that connects to two devices, using their respective baudrates.
Once connected, this calls function `f`. Only works for two devices
"""
function double_interface(f = default; port1 = "/dev/ttyACM0", rate1 = 115200, port2 = "/dev/tty/ACM1", rate2 = 115200, kwargs...)
LibSerialPort.open(port1, rate1) do dev1
LibSerialPort.open(port2, rate2) do dev2
f(dev1, dev2; kwargs...)
end
end
end
### EXAMPLE FUNCTIONS TO RUN ON DEVICE ###
""" default(dev)
Simple function that displays a message on a device until
'Esc' is pressed.
"""
function default(dev::SerialPort)
usr_msg = ""
println("To Exit, hit `esc` (and maybe enter too)")
while true
@async usr_msg *= readline(keep = true)
println("Saying hello...")
write(dev, "Hello Device!")
sleep(1.0)
yield()
occursin("\e", usr_msg) && break # Check if 'Esc' has been pressed to close
if usr_msg != ""
@show usr_msg
end
end
end
""" default(dev1, dev2)
Simple function that displays a message on both devices until
'Esc' is pressed.
"""
function default(dev1::SerialPort, dev2::SerialPort)
usr_msg = ""
while true
@async usr_msg *= readline(keep = true)
println("Saying hello...")
write(dev1, "Hello Device 1!")
write(dev2, "Hello Device 2!")
sleep(0.5)
occursin("e", usr_msg) && break # Check if 'Esc' has been pressed to close
end
end
""" send_msg(dev::SerialPort)
Simple function that reads user input and sends it to the
provided device. Terminates when 'Esc' is input.
"""
function send_msg(dev::Interface.SerialPort)
while true
usr_msg = readline(keep = false) # Removes the \n character
occursin("\e", usr_msg) && break # Check if 'Esc' has been pressed to close
msg = usr_msg * "\r" * "\n"
msg = codeunits("$msg") # Convert to hex
write(dev, msg)
sleep(0.25)
end
end
# # # Under Construction # # #
function send_and_receive(dev::Interface.SerialPort)
@info "Starting `send_and_receive` loop. Press ESC [return] to quit"
dev_msg = String(read(dev)) # Clear the line
while true
### SEND MESSAGE ###
## Get message from user
usr_msg = readline(keep = false)
occursin("\e", usr_msg) && break # Exit if 'Esc' is pressed
## Send to device
# msg = "\r" * "\n" * usr_msg
msg = usr_msg * "\r" * "\n"
msg = codeunits("$msg")
write(dev, msg)
sleep(0.25)
### RECEIVE MESSAGE ### WARNING - Manages to catch the message you sent, too
dev_msg = String(read(dev))
dev_msg = strip(dev_msg, ['\r', '\n'])
@show dev_msg
end
end
####################
# HELPER FUNCTIONS #
####################
""" str2vec(s)
Takes in a string of form '[n₁, n₂, ..., nₙ]' and returns it as
a vector.
"""
function str2vec(s)
s = strip(s, ['[', ']', '\r']) # Remove the '[' and ']' characters, as well as the return '\r'
v = parse.(Float64, split(s, ",")) # Split by ',' and convert to Floats
return v
end
""" vec2str(v)
Takes in a vector and converts it to a string of form '[n₁, n₂, ..., nₙ]'
"""
function vec2str(v)
s = "["
for i in v
s *= "$i, "
end
s = s[1:end-2] * "]"
end
""" Checks to see which ports are available """
function check_open_ports()
println("Usage: $(basename(@__FILE__)) port baud rate")
println("Available ports: ")
list_ports()
end
##############################################
# Simple functions to run on the python side #
##############################################
""" Waits for input, and lights up when certain values are input """
"""
import time
from pycubed import cubesat
import ulab.numpy as np
while True:
cubesat.RGB = WHITE
c = input()
cubesat.RGB = BLUE
if c == "a"
cubesat.RGB = GREEN
elif c == "1":
cubesat.RGB = BLUE
else:
cubesat.RGB = RED
time.sleep(0.25)
"""
""" Takes in a vector of times to hold each color (e.g., [1.0, 2.2, 0.5] seconds)"""
"""
import time
from pycubed import cubesat
import ulab.numpy as np
while True:
cubesat.RGB = WHITE
c = input()
cubesat.RGB = BLUE
if (len(c) > 1) and (c[0] == "["):
v = c.strip("[]")
v = v.split(",")
v = [float(v[i]) for i in range(len(v))]
cubesat.RGB = GREEN
time.sleep(v[0])
cubesat.RGB = BLUE
time.sleep(v[1])
cubesat.RGB = TEAL
time.sleep(v[2])
else:
cubesat.RGB = RED
time.sleep(0.25)
"""
end