Python Class to write chars to 7 segment LED

Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Locked
User avatar
mrkahuna
 
Posts: 19
Joined: Mon Feb 04, 2013 5:16 pm

Python Class to write chars to 7 segment LED

Post by mrkahuna »

I'm putting together a small "weather station" with Temp/Humidity/Pressure sensors and some LED 7 segment displays. I'm going to write out various things to the displays, including characters, and I didn't want to have to hard-code bitmask values for the 'writeDigitRaw' functions. I wrote a new class that allows you to pass a string which the class will then parse and determine the correct bitmask for. You still have to be aware of only passing valid characters and only 4 of them at a time but hopefully it'll be easier. Oh, and you can pass it the I2C address for the LED but I haven't tested it with 2 yet.

This is my first pass so I'd appreciate any suggestions for cleaning up the code or places where I might not have found the inevitable bugs.

These should be put in the Adafruit_LEDBackpack directory

I called this 'DigitWriteRaw.py'

Code: Select all

#!/usr/bin/python

from Adafruit_7Segment import SevenSegment

# Class to allow writing of Raw characters to 7 Segment backpack without having to hardcode values

# List of allowable characters. Case matters
# A b C d E F G H h I i J L n O o P q r S t u U y _ - : . 0 1 2 3 4 5 6 7 8 9
# Invalid chars should cause the display to show 'Err'
# To this function pass - I2C Address , string , colon flag (1=on , 0=off)
# If you want the "dot" on after a char pass it in the string after the number e.g  'hEL.o' will fill in the dot next to L
# There is no checking for too long strings yet, but sure to pass only 4 chars (not including the dots)

class DigitRawWrite:

  sevseg_rawdict = {'A': 119, 'b': 124, 'C': 57, 'd': 94, 'E': 121, 'F': 113, 'G': 125, 'H': 118, 'h': 116, 'I': 6, 'i': 4, 'J': 30, 'L': 56, 'n': 84, 'O': 63, 'o': 92, 'P': 115, 'q': 103, 'r':  80, 'S': 109, 't': 70, 'u': 28, 'U': 62, 'y': 110, '-': 64, '_': 8, '-': 1, ':': 255, '.': 128, ' ': 0, 0: 63, 1: 6, 2: 91, 3: 79, 4: 102, 5: 109, 6: 125, 7: 7, 8: 127, 9: 103}
  
  def Output_7seg(self, addr, d_str, colon):
    segment = SevenSegment(address=addr)  
    ctr = 0
    dict_error = 0
    seglist = list()
    for c in d_str:
        if c == '.':
          seglist[ctr - 1] = seglist[ctr - 1] + 128
        else:
            try:
                index_c = int(c)
            except ValueError:
                index_c = c
            if index_c not in self.sevseg_rawdict:
                dict_error = 1
            else:
                seglist.append(self.sevseg_rawdict[index_c])
            ctr = ctr + 1    
    
    if dict_error:
        seglist = (121,80,80,0)
        
    ctr = 4
    offset = 5 - len(seglist)
    while ctr >= 0:
        if ctr == 2:
            ctr = ctr - 1
            offset = offset - 1
        else:
            segment.writeDigitRaw(ctr, seglist[ctr - offset])
            ctr = ctr - 1
        
    segment.setColon(colon)
Here is an example that cycles between 'hello' and the time (based on the Adafruit examples):

Code: Select all

#!/usr/bin/python

import time
import datetime
from DigitRawWrite import DigitRawWrite

drw = DigitRawWrite()

print "Press CTRL+Z to exit"

# Continually update the display with "hello" and time
while(True):
  o_str = 'hEL.o'
  drw.Output_7seg(0x72, o_str, 1)
  time.sleep(2)
  now = datetime.datetime.now()
  hour = now.hour
  minute = now.minute
  timeseg = str(int(hour/10)) + str(hour % 10) + str(int(minute / 10)) + str(minute % 10)
  drw.Output_7seg(0x72, timeseg, 1)
  time.sleep(2) 
  

User avatar
mrkahuna
 
Posts: 19
Joined: Mon Feb 04, 2013 5:16 pm

Re: Python Class to write chars to 7 segment LED

Post by mrkahuna »

Some updates:
- Fixed bug where displays less than the full 4 chars were wrapping, now right justified
- added a Brightness variable (be sure to have the newest version of Adafruit_7segment.py
- cleaned up the example code

DigitRawWrite.py

Code: Select all

#!/usr/bin/python

from Adafruit_7Segment import SevenSegment

# Class to allow writing of Raw characters to 7 Segment backpack without having to hardcode values

# List of allowable characters. Case matters
# A b C d E F G H h I i J L n O o P q r S t u U y _ - : . 0 1 2 3 4 5 6 7 8 9
# Invalid chars should cause the display to show 'Err'
# To this function pass - I2C Address , string , colon flag (1=on , 0=off), brightness value ( 1 - 16 )
# If you want the "dot" on after a char pass it in the string after the number e.g  'hEL.o' will fill in the dot next to L
# There is no checking for too long strings yet, but sure to pass only 4 chars (not including the dots)

class DigitRawWrite:

  sevseg_rawdict = {'A': 119, 'b': 124, 'C': 57, 'd': 94, 'E': 121, 'F': 113, 'G': 125, 'H': 118, 'h': 116, 'I': 6, 'i': 4, 'J': 30, 'L': 56, 'n': 84, 'O': 63, 'o': 92, 'P': 115, 'q': 103, 'r':  80, 'S': 109, 't': 70, 'u': 28, 'U': 62, 'y': 110, '-': 64, '_': 8, '-': 1, ':': 255, '.': 128, ' ': 0, 0: 63, 1: 6, 2: 91, 3: 79, 4: 102, 5: 109, 6: 125, 7: 7, 8: 127, 9: 103}
  
  def Output_7seg(self, addr, d_str, colon, brtness):  
    segment = SevenSegment(address=addr)
    segment.setBrightness(brtness)
    ctr = 0
    dict_error = 0
    seglist = list()
    for c in d_str:
        if c == '.':
          seglist[ctr - 1] = seglist[ctr - 1] + 128
        else:
            try:
                index_c = int(c)
            except ValueError:
                index_c = c
            if index_c not in self.sevseg_rawdict:
                dict_error = 1
            else:
                seglist.append(self.sevseg_rawdict[index_c])
            ctr += 1    
    
    if dict_error:
        seglist = (121,80,80,0)
        
    ctr = 4
    blanks = 4 - len(seglist)
    offset = 5 - len(seglist)
    while ctr >= 0:
        if ctr == 2:
            ctr -= 1
            blanks -= 1
            offset -= 1
        else:
            if ctr > blanks:
                segment.writeDigitRaw(ctr, seglist[ctr - offset])
            else:
                segment.writeDigitRaw(ctr, 0)
            ctr -= 1
        
    segment.setColon(colon)
new_example.py

Code: Select all

#!/usr/bin/python

import time
import datetime
from DigitRawWrite import DigitRawWrite

drw = DigitRawWrite()

print "Press CTRL+Z to exit"

# Continually update the display with "hello" and time
while(True):
  o_str = 'hEL.o'
  drw.Output_7seg(0x72, o_str, 0, 16)
  time.sleep(2)
  now = datetime.datetime.now()
  timeseg = str(now.hour) + str(now.minute)
  drw.Output_7seg(0x72, timeseg, 1, 8)
  time.sleep(2) 
  

User avatar
mrkahuna
 
Posts: 19
Joined: Mon Feb 04, 2013 5:16 pm

Re: Python Class to write chars to 7 segment LED

Post by mrkahuna »

I almost forgot. You need to add a few lines to Adafruit_7Segment.py to be able to control the brightness.

Add to the bottom of Adafruit_7Segment.py

Code: Select all

  def setBrightness(self, brightness):
    "Sets the brightness level from 0..15"
    self.disp.setBrightness(brightness)
Shamelessly borrowed from this post: http://forums.adafruit.com/viewtopic.ph ... ss#p190135

User avatar
cameraready
 
Posts: 55
Joined: Wed Jan 30, 2013 11:10 pm

Re: Python Class to write chars to 7 segment LED

Post by cameraready »

Looks good. I'll have to try it out on my 7segment when I get a chance.

Locked
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Return to “Adafruit Raspberry Pi® accessories”