#!/bin/python # # # Written by Courtney Krehbiel # October 2015 # # # Purpose is to run as a background program and monitor GPIO 21 to see if it is grounded. # If high to low (ground) detected it will perform an orderly shutdown of the Raspberry Pi # # import RPi.GPIO as GPIO import os import sys import time switchpin = 21 # Using GPIO 21, physical pin 40 since it's close to a ground pin. GPIO.setmode(GPIO.BCM) # Using BCM numbers, not physical pin numbers GPIO.setup(switchpin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set as input, and pulled high by default GPIO.wait_for_edge(switchpin, GPIO.FALLING, bouncetime=200) # Program will stop at this line and wait for falling edge, if ever. # If program ever gets here, we're shutting down. # Define GPIO to LCD mapping LCD_RS = 14 LCD_E = 15 LCD_D4 = 17 LCD_D5 = 18 LCD_D6 = 27 LCD_D7 = 22 # Define some device constants LCD_WIDTH = 16 # Maximum characters per line LCD_CHR = True LCD_CMD = False LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line # Timing constants E_PULSE = 0.0005 E_DELAY = 0.0005 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers GPIO.setup(LCD_E, GPIO.OUT) # E GPIO.setup(LCD_RS, GPIO.OUT) # RS GPIO.setup(LCD_D4, GPIO.OUT) # DB4 GPIO.setup(LCD_D5, GPIO.OUT) # DB5 GPIO.setup(LCD_D6, GPIO.OUT) # DB6 GPIO.setup(LCD_D7, GPIO.OUT) # DB7 def lcd_init(): # Initialise display lcd_byte(0x33,LCD_CMD) # 110011 Initialise lcd_byte(0x32,LCD_CMD) # 110010 Initialise lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size lcd_byte(0x01,LCD_CMD) # 000001 Clear display time.sleep(E_DELAY) def lcd_byte(bits, mode): # Send byte to data pins # bits = data # mode = True for character # False for command GPIO.output(LCD_RS, mode) # RS # High bits GPIO.output(LCD_D4, False) GPIO.output(LCD_D5, False) GPIO.output(LCD_D6, False) GPIO.output(LCD_D7, False) if bits&0x10==0x10: GPIO.output(LCD_D4, True) if bits&0x20==0x20: GPIO.output(LCD_D5, True) if bits&0x40==0x40: GPIO.output(LCD_D6, True) if bits&0x80==0x80: GPIO.output(LCD_D7, True) # Toggle 'Enable' pin lcd_toggle_enable() # Low bits GPIO.output(LCD_D4, False) GPIO.output(LCD_D5, False) GPIO.output(LCD_D6, False) GPIO.output(LCD_D7, False) if bits&0x01==0x01: GPIO.output(LCD_D4, True) if bits&0x02==0x02: GPIO.output(LCD_D5, True) if bits&0x04==0x04: GPIO.output(LCD_D6, True) if bits&0x08==0x08: GPIO.output(LCD_D7, True) # Toggle 'Enable' pin lcd_toggle_enable() def lcd_toggle_enable(): # Toggle enable time.sleep(E_DELAY) GPIO.output(LCD_E, True) time.sleep(E_PULSE) GPIO.output(LCD_E, False) time.sleep(E_DELAY) def lcd_string(message,line): # Send string to display message = message.ljust(LCD_WIDTH," ") lcd_byte(line, LCD_CMD) for i in range(LCD_WIDTH): lcd_byte(ord(message[i]),LCD_CHR) def haltsystem(ev=None): lcd_string("Halting RPi... ", LCD_LINE_1) lcd_string("Pse wait 30 sec.", LCD_LINE_2) os.system("sudo shutdown -h now") # Send command to shutdown the system # print 'System being halted' sys.exit(0) # Initialise display lcd_init() GPIO.remove_event_detect(switchpin) # Send some test lcd_byte(0x01,LCD_CMD) # 000001 Clear display lcd_string("REBOOT in 15 sec",LCD_LINE_1) lcd_string("Push agn to HALT",LCD_LINE_2) time.sleep(2) # Debounce the keypress to make sure there are two distinct pushes. GPIO.add_event_detect(switchpin, GPIO.FALLING) # wait for any activity CountDown = 1500 while CountDown > 0: if GPIO.event_detected(switchpin): haltsystem() time.sleep(0.01) CountDown = CountDown - 1 # Button not pressed 2nd time, so will reboot instead lcd_string("Rebooting RPi...", LCD_LINE_1) lcd_string("Pse wait 30 sec.", LCD_LINE_2) #print 'System being rebooted' #time.sleep(5) #lcd_init() # Clean up display #GPIO.cleanup() # Tidy up before exit os.system("sudo reboot") # Send command to reboot the system # To run as a background program which takes very few resources, use the command: sudo python sw-shutdown.py &