From 0f55bfdecc6b36bb911100824a949ba0a18a534f Mon Sep 17 00:00:00 2001 From: caternuson Date: Tue, 19 Jun 2018 11:35:17 -0700 Subject: [PATCH 1/3] add print, privatize old funcs --- adafruit_ht16k33/segments.py | 52 +++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/adafruit_ht16k33/segments.py b/adafruit_ht16k33/segments.py index 3aab115..c675351 100644 --- a/adafruit_ht16k33/segments.py +++ b/adafruit_ht16k33/segments.py @@ -148,6 +148,17 @@ class Seg14x4(HT16K33): """Alpha-numeric, 14-segment display.""" + def print(self, value): + if isinstance(value, (str)): + self._text(value) + elif isinstance(value, (int, float)): + self._number(value) + else: + raise ValueError('Unsupported display value type: {}'.format(type(value))) + + def __setitem__(self, key, value): + self._put(value, key) + def scroll(self, count=1): """Scroll the display by specified number of places.""" if count >= 0: @@ -157,7 +168,7 @@ def scroll(self, count=1): for i in range(6): self._set_buffer(i + offset, self._get_buffer(i + 2 * count)) - def put(self, char, index=0): + def _put(self, char, index=0): """Put a character at the specified place.""" if not 0 <= index <= 3: return @@ -170,21 +181,21 @@ def put(self, char, index=0): self._set_buffer(index * 2, CHARS[1 + character]) self._set_buffer(index * 2 + 1, CHARS[character]) - def push(self, char): + def _push(self, char): """Scroll the display and add a character at the end.""" if char != '.' or self._get_buffer(7) & 0b01000000: self.scroll() - self.put(' ', 3) - self.put(char, 3) + self._put(' ', 3) + self._put(char, 3) - def text(self, text): + def _text(self, text): """Display the specified text.""" for character in text: - self.push(character) + self._push(character) - def number(self, number): + def _number(self, number): """Display the specified decimal number.""" - string = "{:f}".format(number) + string = "{}".format(number) if len(string) > 4: if string.find('.') > 4: raise ValueError("Overflow") @@ -192,22 +203,19 @@ def number(self, number): places = 4 if '.' in string: places += 1 - self.text(string[:places]) - - def hex(self, number): - """Display the specified hexadecimal number.""" - string = "{:x}".format(number) - if len(string) > 4: - raise ValueError("Overflow") - self.fill(False) - self.text(string) - + self._text(string[:places]) class Seg7x4(Seg14x4): """Numeric 7-segment display. It has the same methods as the alphanumeric display, but only supports displaying decimal and hex digits, period and a minus sign.""" POSITIONS = [0, 2, 6, 8] # The positions of characters. + def print(self, value): + if isinstance(value, (int, float)): + self._number(value) + else: + raise ValueError('Unsupported display value type: {}'.format(type(value))) + def scroll(self, count=1): """Scroll the display by specified number of places.""" if count >= 0: @@ -218,14 +226,14 @@ def scroll(self, count=1): self._set_buffer(self.POSITIONS[i + offset], self._get_buffer(self.POSITIONS[i + count])) - def push(self, char): + def _push(self, char): """Scroll the display and add a character at the end.""" if char in ':;': - self.put(char) + self._put(char) else: - super().push(char) + super()._push(char) - def put(self, char, index=0): + def _put(self, char, index=0): """Put a character at the specified place.""" if not 0 <= index <= 3: return From 04e7eceb7b61e0f0cc7331fe251517b1814c055b Mon Sep 17 00:00:00 2001 From: caternuson Date: Tue, 19 Jun 2018 11:46:27 -0700 Subject: [PATCH 2/3] add doc string for print --- adafruit_ht16k33/segments.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/adafruit_ht16k33/segments.py b/adafruit_ht16k33/segments.py index c675351..06610f0 100644 --- a/adafruit_ht16k33/segments.py +++ b/adafruit_ht16k33/segments.py @@ -149,6 +149,7 @@ class Seg14x4(HT16K33): """Alpha-numeric, 14-segment display.""" def print(self, value): + """Print the value to the display.""" if isinstance(value, (str)): self._text(value) elif isinstance(value, (int, float)): @@ -211,6 +212,7 @@ class Seg7x4(Seg14x4): POSITIONS = [0, 2, 6, 8] # The positions of characters. def print(self, value): + """Print the value to the display.""" if isinstance(value, (int, float)): self._number(value) else: From 10e8b74a3f2c02313bda0596c79012c93e5f64fb Mon Sep 17 00:00:00 2001 From: caternuson Date: Tue, 19 Jun 2018 12:00:08 -0700 Subject: [PATCH 3/3] update example --- examples/segments.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/segments.py b/examples/segments.py index 65c933e..f9e5abf 100644 --- a/examples/segments.py +++ b/examples/segments.py @@ -28,12 +28,12 @@ display.show() # Set the first character to '1': -display.put('1', 0) +display[0] = '1' # Set the second character to '2': -display.put('2', 1) +display[1] = '2' # Set the third character to 'A': -display.put('A', 2) +display[2] = 'A' # Set the forth character to 'B': -display.put('B', 3) +display[3] = 'B' # Make sure to call show to see the changes above on the display! display.show()