Modifications

Aller à : navigation, rechercher

CellarKare

2 358 octets ajoutés, 28 octobre 2015 à 22:44
Bouton OFF
Les parties utiles du code:
 
<syntaxhighlight code="python">
# La fonction qui effectuer l'arrêt propre du RPi
def btn_proper_halt(pin):
global off_state, cur_millis, off_millis
# On lit le nombre de millisecondes courant
cur_millis = time.time()*1000
# Utilise la variable globale off_state
off_state = not off_state
# DEBUG
logging.debug("Bouton OFF_PIN pressé, son état est maintenant %s" % str(off_state))
if off_state == True:
off_millis = cur_millis
elif off_state == False:
# DEBUG
logging.debug("Bouton OFF_PIN pressé pendant {0:0.0f} ms".format(cur_millis - off_millis))
if (cur_millis - off_millis) > OFF_INTERVAL:
# On a relâché le bouton avec un écart long => shutdown
logging.debug("Appui long sur le bouton OFF_PIN")
logging.info("Arrêt du Raspberry PI, attendre encore 10 sec avant de débrancher")
subprocess.call('halt', shell=False)
else:
# Appuis court => on ne fait rien
logging.debug("Appui court sur le bouton OFF_PIN")
</syntaxhighlight>
 
<syntaxhighlight code="python">
# Fonction de terminaison du programme (appelée lorsque le script est arrêté
def terminate():
logging.debug("Terminate function started")
# efface l'écran LCD
lcd_dim_backlight(0.0, 0.2)
lcd.clear()
logging.debug("Terminate function ended")
</syntaxhighlight>
 
<syntaxhighlight code="python">
# Fonction d'initialisation
def initialize():
global lcd
logging.debug("Initialize function started")
# Initialisation du LCD avec les variables ci-dessus
lcd = LCD.Adafruit_CharLCD(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7,
LCD_COLUMNS, LCD_ROWS,
LCD_BACKLIGHT, invert_polarity = False, enable_pwm = True,
initial_backlight = lcd_backlight)
# Initialisation des autres GPIO's
GPIO.setmode(GPIO.BCM) # On utilise la numérotation BCM pour les PIN
# On définit la pin OFF_PIN comme entrée, et on active la résistance pull-down interne du RPi
GPIO.setup(OFF_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Configure une interruption qui va détecter les mouvements up et down sur le bouton
GPIO.add_event_detect(OFF_PIN, GPIO.BOTH, callback=btn_proper_halt, bouncetime=20)
</syntaxhighlight>
== Say "cheese" ==

Menu de navigation