-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbosp.py
executable file
·146 lines (117 loc) · 3.69 KB
/
bosp.py
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
#!/usr/bin/env python3
# Project: Bolo Player
# File: bosp.py
# Version: 0.1
# Create by: Rom1 <[email protected]> - Musée Bolo - https://www.museebolo.ch
# Date: 10/02/2020
# Licence: GNU GENERAL PUBLIC LICENSE v3
# Language: Python 3
# Description: Surcouche du lecteur de vidéo "omxplayer" pour Raspberry Pi pour
# les bornes OSCAR du musée Bolo.
from omxplayer.player import OMXPlayer
from pathlib import Path
import RPi.GPIO as GPIO
import signal
import sys
import time
# Configurations
###
intro_delay = 3 # Délais pour bloquer le bouton pour qu'il évite de se répéter.
bouncetime = 300 # Anti-rebond du bouton [ms]
blink_delay = 0.5 # Délais du clignotement de la led [s].
stat_f = "/tmp/statistique.log"
# Fonctions
###
# Affiche l'aide.
def usage():
print("Usage: {name} file".format(name=sys.argv[0]))
# Interuption lorsque que le bouton est pressé.
def restart(ch):
global intro_lock
global led_blink
write_stat("{time} restart".format(time=time.time()))
if int(player.position()) > intro_delay or intro_lock == False:
if player.can_pause():
player.pause()
player.set_position(0.0)
if player.can_play():
player.play()
led_blink = False
intro_lock = True
# Signal pour quitter le programme
def signal_handler(sig, frame):
player.quit()
print('\nYou pressed Ctrl+C!\n')
sys.exit(0)
def write_stat(text):
with open(stat_f, 'a') as f:
f.write(text)
f.write('\n')
# Lecture et vérification des arguments
###
if len(sys.argv) != 2:
print("N'a pas le bon nombre d'argument")
usage()
sys.exit(1)
if sys.argv[1] == "help":
usage()
sys.exit(0)
video = Path(sys.argv[1])
if not video.exists():
print("Le fichier n'existe pas!")
usage()
sys.exit(1)
# GPIO
###
GPIO.setwarnings(False) # Désactive les messages d'avertissement.
GPIO.setmode(GPIO.BCM) # Utilise la numérotation des pattes BCM
gpio_led = 23 # Configuration de la led pour lecture
GPIO.setup(gpio_led, GPIO.OUT)
GPIO.output(gpio_led, GPIO.HIGH)
gpio_but = 24 # Configuration du bouton play/restart
GPIO.setup(gpio_but, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(gpio_but, GPIO.RISING, callback=restart, bouncetime=bouncetime)
def led_on(ch):
GPIO.output(ch, GPIO.HIGH)
def led_off(ch):
GPIO.output(ch, GPIO.LOW)
time_tmp = 0.0
led_state = False
def led_blink_handler(ch, status):
global led_state
global time_tmp
if status == True:
if time.time() > (time_tmp + float(blink_delay)):
if led_state == True:
led_on(ch)
led_state = False
else:
led_off(ch)
led_state = True
time_tmp = time.time()
else:
led_on(ch)
# Main
###
# Démarre la lecture de la vidéo
player = OMXPlayer(video,
args=['--no-osd', '-o', 'local', '--loop', '--align', 'center'],
dbus_name='org.mpris.MediaPlayer2.omxplayer',
pause=True)
# Capture les signaux pour arrêter proprement le programme
signal.signal(signal.SIGINT, signal_handler) # Singal INT (^c)
signal.signal(signal.SIGTERM, signal_handler) # Signal TERM (kill)
# Boucle principale pour voir si la lecture est terminée.
intro_lock = False
led_blink = True
while True:
led_blink_handler(gpio_led, led_blink)
if int(player.position()) == int(player.duration()):
player.pause()
player.set_position(0.0)
player.play()
time.sleep(0.5)
player.pause()
led_blink = True
intro_lock = False
time.sleep(0.1)