My 7-Segment Display
I ordered the device from Adafruit. The main reason was to check out getting a second I2C device to work. Here's how I wired it from the Pi:
http://learn.adafruit.com/adafruit-led-backpack/0-dot-56-seven-segment-backpack
I found soldering the 24 tiny pins to connect the display to the "backpack" a trial, but surprise, surprise -- it worked with their example 24-hour clock program first try. I had previously downloaded --
Adafruit-Raspberry-Pi-Python-Code-master/Adafruit_LEDBackpack
(Do you think they could make the directory names any longer?)
I wanted my clock to display 12-hour/AM/PM time. Here's my rework of the software:
#!/usr/bin/python
import time
import datetime
import signal
import sys
from Adafruit_7Segment import SevenSegment
def signal_handler(signal, frame): # to stop the clock
print 'You pressed Ctrl+C!'
SevenSegment(address=0x70) #clear display
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
segment = SevenSegment(address=0x70)
lastHour = -1
print "Press CTRL+C to exit"
# Continually update the time on a 4 char, 7-segment display
while(True):
now = datetime.datetime.now() # get 24-hour time values
hour = now.hour
pm = False
# Set hours
if hour != lastHour: # don't change display
lastHour = hour
if hour > 11: # change to 12 hour clock
hour -= 12
pm = True
if hour == 0: # just after midnight
hour = 12
if int(hour / 10) == 0: # turn off leading zero
segment = SevenSegment(address=0x70) # turns all digits off
if int(hour / 10) != 0:
segment.writeDigit(0, int(hour / 10)) # Tens
segment.writeDigit(1, hour % 10) # Ones
# Set minutes
minute = now.minute
segment.writeDigit(3, int(minute / 10)) # Tens
segment.writeDigit(4, minute % 10, pm) # Ones + PM dot
# turn on colon
segment.setColon(1)
time.sleep(60) # update in 1 minute -- may be 1 minute slow
One of the irritations of Python is that a program like this can only be executed from the directory its local "import" file is in. There are ways around this but it's enough to make C language attractive.
Most importantly, both of my I2C-connected devices still work. And I can add more. From two GPIO pins (3=SLA, 5=SCL), many devices!
Most importantly, both of my I2C-connected devices still work. And I can add more. From two GPIO pins (3=SLA, 5=SCL), many devices!
No comments:
Post a Comment