From cff1039a6dfde4d515c439c81c5201cf50cda0c0 Mon Sep 17 00:00:00 2001 From: Simon Wood Date: Thu, 1 Jan 2026 20:19:24 -0700 Subject: [PATCH 1/2] First pass at rotation Mostly works on ECBUYING, but weirdness with diagonals (ie 'V' doesn't look right) --- ht16k33/ht16k33segment14.py | 63 +++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/ht16k33/ht16k33segment14.py b/ht16k33/ht16k33segment14.py index 35d0667..3ce40a3 100644 --- a/ht16k33/ht16k33segment14.py +++ b/ht16k33/ht16k33segment14.py @@ -48,6 +48,7 @@ class HT16K33Segment14(HT16K33): def __init__(self, i2c, i2c_address=0x70, is_ht16k33=False, board=UNKNOWN, do_enable_display=True): self.buffer = bytearray(16) + self.is_rotated = False # FROM 4.1.0 # Provide backwards compatibility with 4.0.x @@ -64,6 +65,16 @@ def __init__(self, i2c, i2c_address=0x70, is_ht16k33=False, board=UNKNOWN, do_en # *********** PUBLIC FUNCTIONS ********** + def rotate(self): + """ + Rotate/flip the segment display. + + Returns: + The instance (self) + """ + self.is_rotated = not self.is_rotated + return self + def set_glyph(self, glyph, digit=0, has_dot=False): """ Puts the input character matrix (a 16-bit integer) into the specified row, @@ -228,6 +239,58 @@ def set_decimal(self, is_on=True): # This only works on SparkFun Alphanumeric. return self._set_furniture(self.VK16K33_SEG14_DECIMAL_BYTE, is_on) + def draw(self): + """ + Writes the current display buffer to the display itself. + + Call this method after updating the buffer to update + the LED itself. Rotation handled here. + """ + if self.is_rotated: + # Swap msb/lsb for digits 0,3 and 1,2 + a = self.buffer[0] + b = self.buffer[1] + self.buffer[0] = self.buffer[6] + self.buffer[1] = self.buffer[7] + self.buffer[6] = a + self.buffer[7] = b + + a = self.buffer[2] + b = self.buffer[3] + self.buffer[2] = self.buffer[4] + self.buffer[3] = self.buffer[5] + self.buffer[4] = a + self.buffer[5] = b + + # Rotate segments for each msb/lsb + for i in range(0, 8, 2): + a = self.buffer[i] + (self.buffer[i+1] << 8) + + ''' + b11 = a & 0x0800 + b13 = a & 0x2000 + a = (a & 0x57FF) | (b11 < 2) | (b13 > 2) + ''' + + b = (a & 0b0000011100000111) << 3 + c = (a & 0b0011100000111000) >> 3 + d = (a & 0b0000000001000000) << 1 + e = (a & 0b0000000010000000) >> 1 + + a &= 0b0100000000000000 # Decimal Point remains + a = (a | b | c | d | e) + + ''' + b11 = a & 0x0800 + b13 = a & 0x2000 + a = (a & 0x57FF) | (b11 < 2) | (b13 > 2) + ''' + + self.buffer[i] = a & 0xFF + self.buffer[i+1] = a >> 8 + + self._render() + # *********** PRIVATE FUNCTIONS (DO NOT CALL) ********** def _set_furniture(self, digit, state): From c0bae6d5e96a6e651f402179f40e28083cc0f356 Mon Sep 17 00:00:00 2001 From: Simon Wood Date: Thu, 1 Jan 2026 21:06:26 -0700 Subject: [PATCH 2/2] Fix diagonals Had a mistype with '<<' and '>>' operators.... --- ht16k33/ht16k33segment14.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/ht16k33/ht16k33segment14.py b/ht16k33/ht16k33segment14.py index 3ce40a3..354cb32 100644 --- a/ht16k33/ht16k33segment14.py +++ b/ht16k33/ht16k33segment14.py @@ -266,11 +266,9 @@ def draw(self): for i in range(0, 8, 2): a = self.buffer[i] + (self.buffer[i+1] << 8) - ''' b11 = a & 0x0800 b13 = a & 0x2000 - a = (a & 0x57FF) | (b11 < 2) | (b13 > 2) - ''' + a = (a & 0x57FF) | (b11 << 2) | (b13 >> 2) b = (a & 0b0000011100000111) << 3 c = (a & 0b0011100000111000) >> 3 @@ -280,11 +278,9 @@ def draw(self): a &= 0b0100000000000000 # Decimal Point remains a = (a | b | c | d | e) - ''' b11 = a & 0x0800 b13 = a & 0x2000 - a = (a & 0x57FF) | (b11 < 2) | (b13 > 2) - ''' + a = (a & 0x57FF) | (b11 << 2) | (b13 >> 2) self.buffer[i] = a & 0xFF self.buffer[i+1] = a >> 8