Luego de muchos testeos fallidos acudo a la inteligencia de ustedes, resulta que tengo un script python que quiero llamar desde el inicio de la raspberry pi 3/raspbian jessie.
Para cargar el crontab uso GNOME 2.1.1 y el método utilizado es:
Código: Seleccionar todo
@reboot sleep 20 && sudo python /home/pi/Documents/test/button_event.py
Código: Seleccionar todo
import RPi.GPIO as GPIO
import time
import sys
import os
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
offBt = 21
initBt = 20
def boton_press(channel):
if(channel==offBt):
print("off:"+str(channel))
os.system("/usr/bin/python /home/pi/Documents/Programacion_final/test/imagen.py")
#os.system("sudo poweroff")
elif(channel==initBt):
os.system("/usr/bin/python /home/pi/Documents/Programacion_final/proyecto_5.py")
print("init:"+str(channel))
GPIO.setup(offBt,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(offBt,GPIO.FALLING,callback=boton_press, bouncetime=800)
GPIO.setup(initBt,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(initBt,GPIO.FALLING,callback=boton_press, bouncetime=800)
i=0
while True:
i=i+1
print(i)
time.sleep(1)
Lo extraño es lo siguiente, si yo cargo el script desde linea de comando:
Código: Seleccionar todo
sudo python /home/pi/Documents/test/button_event.py
Que puede estar pasando? algún permiso?
Por las dudas dejo el script proyecto_final.py:
Código: Seleccionar todo
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import RPi.GPIO as GPIO
import sys
import capt
from PyQt4.QtCore import QEvent
from PyQt4.QtGui import (QApplication, QHBoxLayout, QMainWindow, QWidget, QPushButton, QVBoxLayout)
from PyQt4.phonon import Phonon
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import phonon
from PyQt4 import QtGui
from PyQt4 import QtCore
import threading
import glob
import subprocess
from subprocess import Popen
import os
#-----------Ruta de archivos.
PATH_GENERAL = "/home/pi/Documents/Programacion_final/"
PATH_VIDEO = PATH_GENERAL+"videos/"
PATH_BACKGROUND = PATH_GENERAL+"imagenes/"
PATH_IMAGEN = "1.jpg"
movie = ("intro.mp4") #Duracion total 14 seg
#-----------SWITCHS VARS----------
flag1 = 0
flag2 = 0
flagPlay = 0 #Este flag vuelve al estado 0 cuando el video se reprodujo.
duracionVideo = 20 #Duracion total del video
#ESTADOS flagPlay: 0=Listo reproducir | 1=Reproduciendo | 2=estado de espera
tiempoDeCaptura = 4.5 #Tiempo que tarda en tomar la captura despues de que el video esta en play
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
global flag1
global flag2
global flagPlay
# Controles principales para organizar la ventana.
self.widget = QWidget(self)
self.layout = QVBoxLayout()
self.bottom_layout = QHBoxLayout()
# Control de reproducción de video de Qt.
self.video_player = Phonon.VideoPlayer(Phonon.VideoCategory, self)
# Acomodar controles en la pantalla.
self.layout.addWidget(self.video_player)
# Personalizar la ventana.
self.setWindowTitle("::Reproductor de video::")
self.resize(400, 200)
self.layout.setMargin(0)#borde del video
self.bottom_layout.setMargin(0)
self.widget.setLayout(self.layout)
self.setCentralWidget(self.widget)
# Reproducir el archivo.
self.video_player.play(Phonon.MediaSource(PATH_BACKGROUND+PATH_IMAGEN))
#self.video_player.mediaObject().setTickInterval(3000)
#self.video_player.mediaObject().tick.connect(self.update_time)
#self.video_player.mediaObject().finished.connect(self.finished)
setInterval(estadosSwitchs,.5)#Escucho los swtich
def update_time(self):
print(from_milliseconds(self.video_player.currentTime()))
def finished(self):
print("terminado.")
def captureImg():
print("capturo imagen")
capt.capt("imagen_capturada.png")#la imagen debe tener nombre distinto siempre
def from_milliseconds(ms):
milliseconds = ms
x = ms / 1000
seconds = x % 60
x /= 60
minutes = x % 60
x /= 60
hours = x % 24
return "{:02}:{:02}:{:02}:{:02}".format(hours, minutes, seconds, milliseconds)
def estadosSwitchs():#-----------SWITCHS----------
global flag1
global flag2
global flagPlay
global duracionVideo
global tiempoDeCaptura
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
swith1 = 3
swith2 = 26
GPIO.setup(swith1, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(swith2, GPIO.IN, GPIO.PUD_UP)
if (GPIO.input(swith1) == False and flag1 == 0 and flagPlay == 0):
flag1 = 1
flagPlay = 1
print("pulsador presionado")
elif (GPIO.input(swith1) == True and flag1 == 1):
print("pulsador soltado")
flag1 = 0
if (GPIO.input(swith2) == False and flag2 == 0 and flagPlay == 0):
flag2 = 1
flagPlay = 1
print("pulsador presionado2")
elif (GPIO.input(swith2) == True and flag2 == 1):
print("pulsador soltado2")
flag2 = 0
if(flagPlay==1):
print("muestro video")
flagPlay=2
#---------------------PLAY VIDEO---------------
player = subprocess.Popen(["omxplayer",PATH_VIDEO+movie],stdin=subprocess.PIPE)
fi = player.poll()
setTimeOut2(captureImg,tiempoDeCaptura)
setTimeOut2(cleanFlagPlay,duracionVideo)
GPIO.cleanup()
def cleanFlagPlay():
global flagPlay
print("limpio flagPlay")
flagPlay = 0;
class setTimeOut():
def __init__(self, func, sec):
def func_wrapper():
func()
self.t = threading.Timer(sec, func_wrapper)
self.t.start()
class setTimeOut2():
def __init__(self, func, sec):
def func_wrapper():
func()
self.t = threading.Timer(sec, func_wrapper)
self.t.start()
def setInterval(func, sec):
def func_wrapper():
setInterval(func, sec)
func()
t = threading.Timer(sec, func_wrapper)
t.start()
return t
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.showFullScreen()
window.show()
sys.exit(app.exec_())
Gracias de antemano!
Saludos.