-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathusbcamctl.py
More file actions
76 lines (66 loc) · 1.95 KB
/
usbcamctl.py
File metadata and controls
76 lines (66 loc) · 1.95 KB
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
#!/usr/bin/python
"""
usbcamctl - This python script makes use of OS calls to uhubctl to control the camera on
the end of the arm. (Power On/Off) (Record On/Off)
Contributors:
Konstantin Zaremski
Andrew Bruckbauer
Dependencies:
uhubctl (installed binary) <https://github.com/mvp/uhubctl.git>
"""
# Import dependencies
import RPi.GPIO as GPIO
from time import sleep
import configparser
import os
# Load configuration from config.ini
config = configparser.ConfigParser()
config.read('./config.ini')
pin = int(config['usbcamctl']['pin'])
poweron_delay = float(config['usbcamctl']['poweron_delay'])
setmode_delay = float(config['usbcamctl']['setmode_delay'])
recordon_delay = float(config['usbcamctl']['recordon_delay'])
poweroff_delay = float(config['usbcamctl']['poweroff_delay'])
recordoff_delay = float(config['usbcamctl']['recordoff_delay'])
# Setup GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(pin, GPIO.OUT)
# Control the camera's powered state from the USB port (uhubctl, then GPIO)
def power(mode):
try:
GPIO.output(pin, GPIO.HIGH)
sleep(poweron_delay if mode else poweroff_delay)
GPIO.output(pin, GPIO.LOW)
sleep(5)
return True
except:
return False
# Control the USB ports on the pi
def usb(mode):
try:
output = os.system('sudo uhubctl -a ' + ('on' if mode else 'off') + ' -l 1-1 > /dev/null')
sleep(3)
return True
except:
return False
# Control the camera's recording state (GPIO)
def toggleRecord():
try:
GPIO.output(pin, GPIO.HIGH)
sleep(recordon_delay)
GPIO.output(pin, GPIO.LOW)
sleep(3)
return True
except:
return False
# Toggle the camera's mode (GPIO)
def toggleMode():
try:
GPIO.output(pin, GPIO.HIGH)
sleep(setmode_delay)
GPIO.output(pin, GPIO.LOW)
sleep(3)
return True
except:
return False