Repository navigation
Expand file tree
/
Copy pathscript.py
More file actions
executable file
·123 lines (99 loc) · 3.26 KB
/
Copy pathscript.py
File metadata and controls
executable file
·123 lines (99 loc) · 3.26 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
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
#!/usr/bin/python
import os
import sys
import time
import glob
import signal
import serial
MAX_VAL = 750.0 # ~25V
OFFSET = 0.2
DEBUG_PRINT = 1
FILMS_PATH = "cyclo_films" # "/media/cyclo/*/" # TODO: test USB drive path
FILMS = FILMS_PATH + "/*"
# stackoverflow.com/questions/2632205/count-the-number-of-files-in-a-directory-using-python
FIFO_FILE = "/tmp/mplayer.fifo"
################################# funktions #######################################
def welcome():
# TODO: test number of files
print
print " >>> Movies folder: " + FILMS_PATH + "\n"
print " >>> Press ctrl-c to stop.\n"
signal.signal(signal.SIGINT, signal_handler)
# allow clean exit with ctrl-c
def signal_handler(signal, frame):
print "\n\nCiao!"
# TODO: use os.kill(pid, sig)
cmd = "killall -9 mplayer &> /dev/null && true"
execute(cmd)
os.remove(FIFO_FILE)
sys.exit(0)
def serial_init():
devices = glob.glob("/dev/ttyACM*")
if DEBUG_PRINT:
print devices
ser = serial.Serial(devices[0], 115200)
success = ser.isOpen()
if not success:
print "\n!!! Error: serial device not found !!!"
sys.exit(-1)
return ser
# execute a command (if in debug mode, print it before)
def execute(cmd):
err = os.system(cmd)
if DEBUG_PRINT:
print "command: " + cmd
if err:
print "!!! ERROR !!! (code = " + str(err) + ") \n\n"
# return True if vlc is running by checking in process list
def isRunning():
proc_list = os.popen("ps -Af").read() # look for an mplayer process...
mplayer_found = ("mplayer" and "fifo" in proc_list) # ...with the right args.
if DEBUG_PRINT:
print "isRunning(): " + str(mplayer_found)
return mplayer_found
# playback speed modulation request
def set_speed(speed):
# TODO: use os.write() ? www.tutorialspoint.com/python/os_write.htm
cmd = "echo speed_set " + str(speed) + " > " + FIFO_FILE
execute(cmd)
# start playing at a specified speed (list loop, full screen, remote mode)
def play():
if not os.path.exists(FIFO_FILE):
ret = os.mkfifo(FIFO_FILE)
if DEBUG_PRINT:
print "play(): " + str(ret)
cmd = "mplayer -msglevel all=-1 -fs -slave -input file=" + FIFO_FILE + " " + FILMS + " 2> /dev/null &"
execute(cmd)
def constrain(speed, min_s, max_s):
if speed < min_s:
speed = min_s
elif speed > max_s:
speed = max_s
return speed
def get_speed(ser):
vin = ser.readline()
ser.flush() # TODO to avoid potential overflow ?
if DEBUG_PRINT:
print "\nvin = ", int(vin)
speed = int(vin) / MAX_VAL + OFFSET
return int(speed*20) / 20.0 # round to 5%
#################################### main #########################################
def main():
MIN_DIF = 0.1
oldspeed = 0
welcome()
ser = serial_init()
if not isRunning():
play()
set_speed(OFFSET)
while True: # wait for a ctrl-c interrupt
speed = get_speed(ser)
speed = constrain(speed, OFFSET, 2)
if DEBUG_PRINT:
print "(speed:", speed, ") (old:", oldspeed, ") => diff: ", (speed-oldspeed)
if (abs(speed-oldspeed) > MIN_DIF):
set_speed(speed)
oldspeed = speed
time.sleep(0.3)
if __name__ == "__main__":
main()