forked from Traumflug/Teacup_Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender-mac.sh
executable file
·126 lines (114 loc) · 3.35 KB
/
sender-mac.sh
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
#!/bin/bash
# This is a script to send G-code from Mac OS X. The problem with Mac OS X is,
# stty settings on serial ports don't "stick", so it's impossible to set
# e.g. the baud rate before connecting to that port. The solution is to use
# a tool which can do both, setting the baud rate and sending characters.
#
# The tool of choice is the somewhat confusing "screen", part of the
# standard Mac OS X distribution.
#
# --- Usage ---
#
# A nice feature of screen is, you can "detach" a connection which means,
# you close the interactive part of screen, but the connection on the serial
# is kept open. This allows so send multiple files to the controller without
# triggering an auto-reset in between. You can do a full close as well, of
# course.
#
# Start sending commands manually or re-attach to an earlier session:
#
# ./sender-mac.sh
#
# Send a file:
#
# ./sender-mac.sh <G-code file>
#
# Send multiple files:
#
# ./sender-mac.sh <G-code file 1>
# ctrl-a d
# ./sender-mac.sh <G-code file 2>
#
# The tricky part with screen is to leave such an interactive session. Screen
# doesn't react to the usual ctrl-c, but asks for ctrl-a. Following that ctrl-a
# you can type single-character commands, like d (detach) or k (kill). There
# is no visual feedback on hitting ctrl-a, type the command character blindly.
#
# Leave the session, keep the connection alive:
#
# ctrl-a d
#
# Leave the session, drop the connection:
#
# ctrl-a k y
#
# Stop sending the current file immediately:
#
# ctrl-a k y
#
# After the file is sent, you're always dropped into interactive mode, so you
# can send additional commands and/or leave the session.
DEV=$(echo /dev/tty.usbserial*)
BAUD=115200
function strip_text {
STRIP_TEXT=$(echo $1 | tr -d '\r\n')
STRIP_TEXT="${STRIP_TEXT## }"
STRIP_TEXT="${STRIP_TEXT%% }"
STRIP_TEXT="${STRIP_TEXT##\t}"
STRIP_TEXT="${STRIP_TEXT%%\t}"
}
if [ "${STY}" = "" ]; then
# we're not inside a screen session, so
# create one and restart our selfs
# make sure we have a session
screen -r teacup -X detach >/dev/null
if [ $? -ne 0 ]; then
# creating in detached mode prevents -X exec later, unfortunately
(sleep 3 && screen -S teacup -d) & ## hack alarm!
screen -S teacup $DEV $BAUD
fi
screen -S teacup -X clear
screen -S teacup -X exec '!!.' "$PWD"/$(basename $0) "$1"
screen -r teacup
else
# we're called from inside screen,
# with the RepRap controller on stdin/stdout
# get an initial prompt
OK=""
echo
while true; do
read -t 10 OK MESSAGE
if [ "$OK" = "ok" ]; then break; fi
echo # trigger a new "ok"
done
if [ -r "$1" ]; then
# send line by line and wait for "ok" each time
exec 3<> "$1"
while read <&3 LINE; do
strip_text "$LINE" && LINE="$STRIP_TEXT"
echo -n "$LINE" >&2
echo $LINE
OK=""
while true; do
read OK MESSAGE
if [ $? -ne 0 ]; then
# probably a disconnection from screen
exit
fi
strip_text "$OK" && OK="$STRIP_TEXT"
strip_text "$MESSAGE" && MESSAGE="$STRIP_TEXT"
echo "<$OK>" >&2
if [ "$OK" = "ok" ]; then
break
fi
done
if [ "$MESSAGE" != "" ]; then
echo "$MESSAGE" >&2
sleep 1
fi
done
echo "File done, dropping to manual operation." >&2
else
echo "No readable file, proceed manually." >&2
fi
fi