Outils d'utilisateurs

Outils du Site


projets:retropie_gpio_additions

Différences

Cette page vous donne les différences entre la révision choisie et la version actuelle de la page.

Lien vers cette vue

projets:retropie_gpio_additions [2014/11/15 13:33]
bumblebee
projets:retropie_gpio_additions [2015/04/11 12:45] (Version actuelle)
bully
Ligne 7: Ligne 7:
 logiciels: logiciels:
 sources: sources:
-liens:[[https://stackoverflow.com/questions/2575528/simulating-key-press-event-using-python-for-linux#adzerk9805928|Simulating key press event using python for linux (en)]], [[http://makezine.com/projects/tutorial-raspberry-pi-gpio-pins-and-python/|Tutorial: Raspberry Pi GPIO Pins and Python (en)]], [[http://blog.petrockblock.com/retropie/| Retropie (en)]], [[projets:boitierraspberrybplus|]]  +liens:[[http://makezine.com/projects/tutorial-raspberry-pi-gpio-pins-and-python/|Tutorial: Raspberry Pi GPIO Pins and Python (en)]], [[http://blog.petrockblock.com/retropie/| Retropie (en)]], [[projets:boitierraspberrybplus|]]  
-langages:javascript svg+langages:python
 tags: raspberry_pi gpio python retropie tags: raspberry_pi gpio python retropie
 usager:bumblebee  usager:bumblebee 
Ligne 18: Ligne 18:
 ===== Schéma de branchement ===== ===== Schéma de branchement =====
 {{  :projets:retropie_gpio_additions:esc_tab_poweroff.svg  |}} {{  :projets:retropie_gpio_additions:esc_tab_poweroff.svg  |}}
-===== Script python =====+===== Script ===== 
 +==== Dépendances ====
 Ce script utilise 3 libraires :  Ce script utilise 3 libraires : 
-  * La libraire ''[[https://pypi.python.org/pypi/RPi.GPIO|RPi.GPIO]]'' qui est normalement intégrée nativement dans les version récente de raspbian et donc je l'espère sur les images de retropie => à tester. +  * La libraire ''[[https://pypi.python.org/pypi/RPi.GPIO|RPi.GPIO]]'' qui est normalement intégrée nativement dans les version récente de raspbian 
-  * La libraire ''[[http://python-evdev.readthedocs.org/en/latest/|evdev]]'' qui permet de gérer ou simuler des input type clavier et souris sous linux+  * <del>La libraire ''[[http://python-evdev.readthedocs.org/en/latest/|evdev]]'' qui permet de gérer ou simuler des input type clavier et souris sous linux</del> 
 +  * La libraire [[http://tjjr.fi/sw/python-uinput/|python-uinput]] permettant de simuler un device (ici clavier) virtuel
   * la libraire ''[[https://github.com/amoffat/sh|sh]]'' qui permet de facilement lancer des commande shell dans python   * la libraire ''[[https://github.com/amoffat/sh|sh]]'' qui permet de facilement lancer des commande shell dans python
  
 +Pour installer les lib : 
 <code bash> <code bash>
-sudo apt-get install python-pip python-dev +sudo apt-get install python-pip python-dev libudev-dev 
-sudo pip install evdev+sudo pip install python-uinput
 sudo pip install sh sudo pip install sh
 </code> </code>
  
-<note warning>En cours de test, la simulation des touches de clavier ne marche pas</note> + 
-<code python retropie_additions.py>+==== La script python ==== 
 +<code bash>
 #!/usr/bin/env python #!/usr/bin/env python
 # encoding: utf-8 # encoding: utf-8
- + 
-from evdev import uinput, ecodes as e+
 import RPi.GPIO as GPIO import RPi.GPIO as GPIO
 from sh import shutdown from sh import shutdown
 +import uinput 
 + 
 # Script de test permettant de simuler l'appui sur la touche echap avec un bouton mettant a la masse la broche 23 de la Raspberry Pi # Script de test permettant de simuler l'appui sur la touche echap avec un bouton mettant a la masse la broche 23 de la Raspberry Pi
 # Si la broche 24 est mise a la masse le script va éteindre le rasbarry proprement (mais pas électriquement) # Si la broche 24 est mise a la masse le script va éteindre le rasbarry proprement (mais pas électriquement)
Ligne 50: Ligne 54:
 # Tuto utilisation boutons et GPIO : # Tuto utilisation boutons et GPIO :
 # http://makezine.com/projects/tutorial-raspberry-pi-gpio-pins-and-python/ # http://makezine.com/projects/tutorial-raspberry-pi-gpio-pins-and-python/
 + 
 #Initalise GPIO INPUTs with pull_ups #Initalise GPIO INPUTs with pull_ups
 GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
Ligne 57: Ligne 61:
 GPIO.setup(25, GPIO.IN, pull_up_down = GPIO.PUD_UP) GPIO.setup(25, GPIO.IN, pull_up_down = GPIO.PUD_UP)
  
-#Callback function tha simulate keyboards events 
-def keypress(code): 
-    with uinput.UInput() as ui: 
-         ui.write(e.EV_KEY, code, 1) 
-         ui.write(e.EV_KEY, code, 0) 
-         ui.syn() 
  
 +import uinput
 +import time
 +
 +#Virtual Keyboard device to simulate key events
 +events = (
 +    uinput.KEY_ESC,
 +    uinput.KEY_TAB
 +    )
 +device = uinput.Device(events)
 +
 +def simulate_key(code):
 +    device.emit(code, 1) # Press.
 +    time.sleep(0.1)
 +    device.emit(code, 0) # Release.
 + 
 def echap(*args,**kwargs): def echap(*args,**kwargs):
     print("echap")     print("echap")
-    keypress(e.KEY_ESC) +    simulate_key(uinput.KEY_ESC) 
 + 
 def tab(*args,**kwargs): def tab(*args,**kwargs):
     print("tab")     print("tab")
-    keypress(e.KEY_TAB) +    simulate_key(uinput.KEY_TAB) 
- +  
 + 
 #Add lisneners on GPIOs pins 23 & 25  #Add lisneners on GPIOs pins 23 & 25 
 GPIO.add_event_detect(23, GPIO.FALLING, callback=echap, bouncetime=300) GPIO.add_event_detect(23, GPIO.FALLING, callback=echap, bouncetime=300)
 GPIO.add_event_detect(25, GPIO.FALLING, callback=tab, bouncetime=300) GPIO.add_event_detect(25, GPIO.FALLING, callback=tab, bouncetime=300)
  
 +print("Started") 
 #Wait for a falling edge on GPIO 24 to shut down the system #Wait for a falling edge on GPIO 24 to shut down the system
 GPIO.wait_for_edge(24, GPIO.FALLING) GPIO.wait_for_edge(24, GPIO.FALLING)
Ligne 89: Ligne 103:
 </code> </code>
 Mes deux sources d'info pour ce script :  Mes deux sources d'info pour ce script : 
-  * Simulation de clavier en python : https://stackoverflow.com/questions/2575528/simulating-key-press-event-using-python-for-linux#adzerk9805928+  * <del>Simulation de clavier en python : https://stackoverflow.com/questions/2575528/simulating-key-press-event-using-python-for-linux#adzerk9805928</del> 
 +  * Simulation de clavier en python : http://www.raspberrypi.org/forums/viewtopic.php?f=32&t=23548&p=219560&hilit=uinput#p219560
   * Utilisation des GPIO du raspberry en python : http://makezine.com/projects/tutorial-raspberry-pi-gpio-pins-and-python/   * Utilisation des GPIO du raspberry en python : http://makezine.com/projects/tutorial-raspberry-pi-gpio-pins-and-python/
 +
 +==== Lancement automatique ====
 +En supposant que le script est enregistré dans ''/home/pi/'', il suffit de rajouter la ligne suivante dans le fichier ''/etc/rc.local'' //(''sudo nano /etc/rc.local'')// avant la ligne ''exit 0'' : 
 +<code bash>
 +/usr/bin/python /home/pi/retropie_additions.py &
 +</code>
 +
 +==== Aide pour la réalisation ====
 +
 +
 +Pour les personnes qui ne savent pas trop comment bidouiller dans ce domaine, voila la marche à suivre pas à pas:
 +
 +
 +  * Lancez votre retropie, une fois prête, appuyez sur F4 et installez les librairies en faisant comme indiqué plus haut (sudo apt-get install .......)
 +  * Créez un fichier texte nommé " retropie_additions " sur un ordinateur autre
 +  * Copiez le script plus haut dans ce fichier. Remplacez le *.txt/odt/... par le .py
 +  * Éteignez votre retropie
 +  * Placez le fichier fraichement créé dans le répertoire /home/pi de votre retropie (à partir d'un linux faites un drag'n drop... pour les autres OS, aucune idée)
 +  * Pour l'automatisation, lancez votre retropie (pas trop loin)
 +  * Une fois en marche, appuyez encore une fois sur F4
 +  * Tapez la commande plus haut dans la rubrique automatisation (sudo nano ...) pour lire le contenu du fichier
 +  * Faite la commande pour éditer le texte (Ctrl + X de mémoire)
 +  * Tapez la ligne indiquée à la fin du texte
 +  * enregistrez vos modification 
 +  * Redémarrez et ça marche
 +
  
 ===== Tests ==== ===== Tests ====
Ligne 96: Ligne 137:
   * GPIO : :OK:   * GPIO : :OK:
   * Shutdown: :OK:    * Shutdown: :OK: 
-  * Simulation du clavier : :KO:+  * Simulation du clavier : :OK: //(pas réussi avec la lib evdev mais cela fonctionne avec la lib uinput)// 
 +  * Lancement automatique : :OK
  
 ===== Ce qu'il reste à faire ===== ===== Ce qu'il reste à faire =====
-  * Trouver pourquoi la simulation du clavier ne marche pas, ou trouver une autre solution+  * <del>Trouver pourquoi la simulation du clavier ne marche pas, ou trouver une autre solution</del>
   * Ajouter d'autres boutons/touches/fonctionnalités ?   * Ajouter d'autres boutons/touches/fonctionnalités ?
-  * Automatiser le démarrage du script (service ?) +  * <del>Automatiser le démarrage du script (service ?)</del> 
-  * Faire un boitier qui intégré les boutons+  * Faire un boitier qui intégre les boutons et le raspberry ?
   * ...   * ...
  
  
projets/retropie_gpio_additions.1416054814.txt.gz · Dernière modification: 2014/11/15 13:33 par bumblebee