Skip to content

Instantly share code, notes, and snippets.

@dkisselev
Created July 28, 2013 18:43
Show Gist options
  • Save dkisselev/6099601 to your computer and use it in GitHub Desktop.
Save dkisselev/6099601 to your computer and use it in GitHub Desktop.
Python non-blocking execute every x seconds.Used on my raspberry pi to update the display on my (Adafruit LCD+Keypad)[http://www.adafruit.com/products/1115] board while still being able to constantly poll for button presses.Also updates the display with current date+time and wifi IP address.
#!/usr/bin/python
from sys import exit
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
from subprocess import *
from time import sleep, strftime
from datetime import datetime
import time
# Define global vars
lcd = Adafruit_CharLCDPlate()
cmd = "ip addr show wlan0 | grep inet | awk '{print $2}' | cut -d/ -f1"
# Define fn's
def update_display():
lcd.home()
ipaddr = run_cmd(cmd)
lcd.message(datetime.now().strftime('%b %d %H:%M:%S\n'))
lcd.message('IP %s' % ( ipaddr ) )
def run_cmd(cmd):
p = Popen(cmd, shell=True, stdout=PIPE)
output = p.communicate()[0]
return output
# Startup
lcd.clear()
millis = int(round(time.time() * 1000))
lastMillis = millis
# Loop
while 1:
millis = int(round(time.time() * 1000))
if millis - lastMillis >= 1000:
update_display()
lastMillis = millis
if lcd.buttonPressed(lcd.SELECT):
lcd.clear()
lcd.backlight(lcd.OFF)
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment