4343 'F' : 0x71
4444}
4545
46+ IDIGIT_VALUES = {
47+ ' ' : 0x00 ,
48+ '-' : 0x40 ,
49+ '0' : 0x3F ,
50+ '1' : 0x30 ,
51+ '2' : 0x5B ,
52+ '3' : 0x79 ,
53+ '4' : 0x74 ,
54+ '5' : 0x6D ,
55+ '6' : 0x6F ,
56+ '7' : 0x38 ,
57+ '8' : 0x7F ,
58+ '9' : 0x7D ,
59+ 'A' : 0x7E ,
60+ 'B' : 0x67 ,
61+ 'C' : 0x0F ,
62+ 'D' : 0x73 ,
63+ 'E' : 0x4F ,
64+ 'F' : 0x4E
65+ }
66+
67+
4668
4769class SevenSegment (HT16K33 .HT16K33 ):
4870 """Seven segment LED backpack display."""
@@ -52,6 +74,12 @@ def __init__(self, **kwargs):
5274 initializer, including optional I2C address and bus number parameters.
5375 """
5476 super (SevenSegment , self ).__init__ (** kwargs )
77+ self .invert = True
78+
79+ def set_invert (self , _invert ):
80+ """Set whether the display is upside-down or not.
81+ """
82+ self .invert = _invert
5583
5684 def set_digit_raw (self , pos , bitmask ):
5785 """Set digit at position to raw bitmask value. Position should be a value
@@ -61,8 +89,15 @@ def set_digit_raw(self, pos, bitmask):
6189 return
6290 # Jump past the colon at position 2 by adding a conditional offset.
6391 offset = 0 if pos < 2 else 1
92+
93+ # Calculate the correct position depending on orientation
94+ if self .invert :
95+ pos = 4 - (pos + offset )
96+ else :
97+ pos = pos + offset
98+
6499 # Set the digit bitmask value at the appropriate position.
65- self .buffer [( pos + offset ) * 2 ] = bitmask & 0xFF
100+ self .buffer [pos * 2 ] = bitmask & 0xFF
66101
67102 def set_decimal (self , pos , decimal ):
68103 """Turn decimal point on or off at provided position. Position should be
@@ -74,18 +109,29 @@ def set_decimal(self, pos, decimal):
74109 return
75110 # Jump past the colon at position 2 by adding a conditional offset.
76111 offset = 0 if pos < 2 else 1
112+
113+ # Calculate the correct position depending on orientation
114+ if self .invert :
115+ pos = 4 - (pos + offset )
116+ else :
117+ pos = pos + offset
118+
77119 # Set bit 7 (decimal point) based on provided value.
78120 if decimal :
79- self .buffer [( pos + offset ) * 2 ] |= (1 << 7 )
121+ self .buffer [pos * 2 ] |= (1 << 7 )
80122 else :
81- self .buffer [( pos + offset ) * 2 ] &= ~ (1 << 7 )
123+ self .buffer [pos * 2 ] &= ~ (1 << 7 )
82124
83125 def set_digit (self , pos , digit , decimal = False ):
84126 """Set digit at position to provided value. Position should be a value
85127 of 0 to 3 with 0 being the left most digit on the display. Digit should
86128 be a number 0-9, character A-F, space (all LEDs off), or dash (-).
87129 """
88- self .set_digit_raw (pos , DIGIT_VALUES .get (str (digit ).upper (), 0x00 ))
130+ if self .invert :
131+ self .set_digit_raw (pos , IDIGIT_VALUES .get (str (digit ).upper (), 0x00 ))
132+ else :
133+ self .set_digit_raw (pos , DIGIT_VALUES .get (str (digit ).upper (), 0x00 ))
134+
89135 if decimal :
90136 self .set_decimal (pos , True )
91137
0 commit comments