#!/usr/bin/env python # encoding: utf-8 import RPi.GPIO as GPIO 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 # Si la broche 24 est mise a la masse le script va éteindre le rasbarry proprement (mais pas électriquement) # # Nécessites les libs evdev et sh : # pip install sh evdev # # Source simulation de clavier avec evedv : # https://stackoverflow.com/questions/2575528/simulating-key-press-event-using-python-for-linux#adzerk9805928 # # Tuto utilisation boutons et GPIO : # http://makezine.com/projects/tutorial-raspberry-pi-gpio-pins-and-python/ #Initalise GPIO INPUTs with pull_ups GPIO.setmode(GPIO.BCM) GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP) GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP) GPIO.setup(25, GPIO.IN, pull_up_down = GPIO.PUD_UP) 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): print("echap") simulate_key(uinput.KEY_ESC) def tab(*args,**kwargs): print("tab") simulate_key(uinput.KEY_TAB) #Add lisneners on GPIOs pins 23 & 25 GPIO.add_event_detect(23, GPIO.FALLING, callback=echap, 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 GPIO.wait_for_edge(24, GPIO.FALLING) GPIO.cleanup() shutdown('-h','now')