-
Notifications
You must be signed in to change notification settings - Fork 2
/
controller.sh
executable file
·287 lines (237 loc) · 9.1 KB
/
controller.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/bash
# The master controller that starts the camera or test card
# and monitors the button and switch
############ Set Environment Variables ###############
CONFIGFILE=/boot/hdmisource_config.txt
############ Function to Read value with - from Config File ###############
get-config_var() {
lua - "$1" "$2" <<EOF
local key=assert(arg[1])
local fn=assert(arg[2])
local file=assert(io.open(fn))
for line in file:lines() do
local val = line:match("^#?%s*"..key.."=[+-]?(.*)$")
if (val ~= nil) then
print(val)
break
end
end
EOF
}
############ Function to check available cameras ###############
do_check_cameras()
{
# Check each in turn. Reset first
OLDC920_CONNECTED="no"
EAGLEEYE_CONNECTED="no"
EAGLEEYE2_CONNECTED="no"
USBTV007_CONNECTED="no"
MS210X_CONNECTED="no"
lsusb | grep -q "046d:082d"
if [ $? == 0 ] ; then
OLDC920_CONNECTED="yes"
fi
lsusb | grep -q "095d:3001"
if [ $? == 0 ]; then
EAGLEEYE_CONNECTED="yes"
EAGLEEYES="$(lsusb | grep -c "095d:3001")"
if [ $EAGLEEYES == 2 ]; then
EAGLEEYE2_CONNECTED="yes"
fi
fi
lsusb | grep -q "1b71:3002"
if [ $? == 0 ]; then
USBTV007_CONNECTED="yes"
fi
lsusb | grep -q "534d:0021"
if [ $? == 0 ]; then
MS210X_CONNECTED="yes"
fi
# Now determine the next in sequence
if [ "$CURRENT_CAMERA" == "none" ] || [ "$CURRENT_CAMERA" == "picam" ]; then
if [ "$OLDC920_CONNECTED" == "yes" ]; then
NEXT_CAMERA="c920"
else
if [ "$EAGLEEYE_CONNECTED" == "yes" ]; then
NEXT_CAMERA="eagleeye"
else
if [ "$USBTV007_CONNECTED" == "yes" ]; then
NEXT_CAMERA="easycap"
else
if [ "$MS210X_CONNECTED" == "yes" ]; then
NEXT_CAMERA="easycap"
else
NEXT_CAMERA="picam"
fi
fi
fi
fi
elif [ "$CURRENT_CAMERA" == "c920" ]; then
if [ "$EAGLEEYE_CONNECTED" == "yes" ]; then
NEXT_CAMERA="eagleeye"
else
if [ "$USBTV007_CONNECTED" == "yes" ]; then
NEXT_CAMERA="easycap"
else
if [ "$MS210X_CONNECTED" == "yes" ]; then
NEXT_CAMERA="easycap"
else
if [ "$PICAM_CONNECTED" == "yes" ]; then
NEXT_CAMERA="picam"
else
NEXT_CAMERA="c920"
fi
fi
fi
fi
elif [ "$CURRENT_CAMERA" == "eagleeye" ]; then
if [ "$EAGLEEYE2_CONNECTED" == "yes" ]; then
NEXT_CAMERA="eagleeye2"
else
if [ "$USBTV007_CONNECTED" == "yes" ]; then
NEXT_CAMERA="easycap"
else
if [ "$MS210X_CONNECTED" == "yes" ]; then
NEXT_CAMERA="easycap"
else
if [ "$PICAM_CONNECTED" == "yes" ]; then
NEXT_CAMERA="picam"
else
if [ "$OLDC920_CONNECTED" == "yes" ]; then
NEXT_CAMERA="c920"
else
NEXT_CAMERA="eagleeye"
fi
fi
fi
fi
fi
elif [ "$CURRENT_CAMERA" == "eagleeye2" ]; then
if [ "$USBTV007_CONNECTED" == "yes" ]; then
NEXT_CAMERA="easycap"
else
if [ "$MS210X_CONNECTED" == "yes" ]; then
NEXT_CAMERA="easycap"
else
if [ "$PICAM_CONNECTED" == "yes" ]; then
NEXT_CAMERA="picam"
else
if [ "$OLDC920_CONNECTED" == "yes" ]; then
NEXT_CAMERA="c920"
else
NEXT_CAMERA="eagleeye"
fi
fi
fi
fi
elif [ "$CURRENT_CAMERA" == "easycap" ]; then
if [ "$PICAM_CONNECTED" == "yes" ]; then
NEXT_CAMERA="picam"
else
if [ "$OLDC920_CONNECTED" == "yes" ]; then
NEXT_CAMERA="c920"
else
if [ "$EAGLEEYE_CONNECTED" == "yes" ]; then
NEXT_CAMERA="eagleeye"
else
NEXT_CAMERA="easycap"
fi
fi
fi
fi
}
################################################################
############ Read Config File ###############
CAM_SWITCH=$(get-config_var cam_switch $CONFIGFILE)
BUTTON=$(get-config_var button $CONFIGFILE)
ACTIVE=$(get-config_var active $CONFIGFILE)
CAMERA=$(get-config_var camera $CONFIGFILE)
CURRENT_CAMERA="none"
NEXT_CAMERA="none"
FIRST_CAMERA="yes"
clear # Clear the screen
sudo systemctl start pigpiod >/dev/null 2>/dev/null # Start the GPIO Deamon
sleep 1 # Wait for the deamon to start
#CAM_SWITCH=24 # Physical pin 16 3v3 pull-up. Ground for camera
#BUTTON=23 # Physical pin 18 3v3 pull-up. Ground for next or shutdown
pigs m $CAM_SWITCH r # set the Camera switch GPIO to read mode
pigs m $BUTTON r # set the Button GPIO to read mode
pigs m $ACTIVE w # set the Active LED GPIO to write mode
pigs pud $CAM_SWITCH u # Enable the pull-up on the camera switch GPIO
pigs pud $BUTTON u # Enable the pull-up on the button GPIO
pigs w $ACTIVE 1 # set the Active LED GPIO high
# Check if picam is connected at boot
libcamera-hello --list-cameras | grep -q "No cameras available"
if [ $? != 0 ] ; then
PICAM_CONNECTED="yes"
fi
while true; do # Main loop
CAM_SWITCH_POS=$(pigs r $CAM_SWITCH)
if [[ $CAM_SWITCH_POS -eq 0 ]]; then # Camera selected
if [ "$FIRST_CAMERA" == "yes" ]; then
/home/pi/hdmisource/camera.sh & # Run as normal
else
eval "/home/pi/hdmisource/camera.sh $CURRENT_CAMERA &" # Start last used camera
fi
sleep 0.1s
SHUTDOWN_COUNT=0 # Now monitor for shutdown or other change request
while [[ $CAM_SWITCH_POS -eq 0 ]] && (! test -f /home/pi/tmp/camera_change); do # Exit if camera is deselected
CAM_SWITCH_POS=$(pigs r $CAM_SWITCH) # Check camera switch
BUTTON_POS=$(pigs r $BUTTON) # Check Button
if [[ $BUTTON_POS -eq 0 ]]; then
let SHUTDOWN_COUNT=$SHUTDOWN_COUNT+1 # Count the number of 100ms loops the button has been pressed for
else
SHUTDOWN_COUNT=0
fi
if [[ $SHUTDOWN_COUNT -eq 1 ]] && [ "$CAMERA" == "auto" ]; then # only change camera once
echo shutdown | nc 127.0.0.1 1111 >/dev/null 2>/dev/null
sudo killall libcamera-hello >/dev/null 2>/dev/null
do_check_cameras
eval "/home/pi/hdmisource/camera.sh $NEXT_CAMERA &" # Start camera
CURRENT_CAMERA=$NEXT_CAMERA
FIRST_CAMERA="no"
fi
if [ $SHUTDOWN_COUNT -gt 19 ]; then # 2 second press, so
echo shutdown | nc 127.0.0.1 1111 >/dev/null 2>/dev/null
sudo killall libcamera-hello >/dev/null 2>/dev/null
# echo ShutDown # display splash
sudo fbi -T 1 -noverbose -a /home/pi/hdmisource/images/shutdown_caption.jpg >/dev/null 2>/dev/null
pigs w $ACTIVE 0 # set the Active LED GPIO low
sudo systemctl stop pigpiod & # Prevent occasional shutdown hang
sleep 2
sudo shutdown now # and shutdown
exit
fi
sleep 0.1s
done # End of camera switch "ON" monitoring loop
rm /home/pi/tmp/camera_change >/dev/null 2>/dev/null
echo shutdown | nc 127.0.0.1 1111 >/dev/null 2>/dev/null
sudo killall libcamera-hello >/dev/null 2>/dev/null
fi # End of if camera selected
if [[ $CAM_SWITCH_POS -eq 1 ]]; then # Test Card selected
/home/pi/hdmisource/test_card.sh & # Display Test Card
sleep 0.1s
SHUTDOWN_COUNT=0 # Now monitor for shutdown or other change request
while [[ $CAM_SWITCH_POS -eq 1 ]]; do # Exit if Test Card is deselected
CAM_SWITCH_POS=$(pigs r $CAM_SWITCH) # Check camera switch
BUTTON_POS=$(pigs r $BUTTON) # Check Button
if [ $BUTTON_POS = 0 ]; then
let SHUTDOWN_COUNT=$SHUTDOWN_COUNT+1 # Count the number of 100ms loops the button has been pressed for
else
SHUTDOWN_COUNT=0
fi
if [ $SHUTDOWN_COUNT -gt 19 ]; then # 2 second press, so
# echo ShutDown # display splash
sudo fbi -T 1 -noverbose -a /home/pi/hdmisource/images/shutdown_caption.jpg >/dev/null 2>/dev/null
pigs w $ACTIVE 0 # set the Active LED GPIO low
sudo systemctl stop pigpiod & # Prevent occasional shutdown hang
sleep 2
sudo shutdown now # and shutdown
exit
fi
sleep 0.1s
done # End of camera switch "OFF" monitoring loop
(sleep 2; sudo killall fbi >/dev/null 2>/dev/null) & # kill fbi after delay for camera to start
fi # End of if Test Card selected
sudo killall test_card.sh >/dev/null 2>/dev/null # make sure that we don't get multiple test cards running
done # End of Main Loop