pdwerryhouse / max7219_8digit

Micropython driver for the max7219 with 8 x 7segment display
GNU General Public License v3.0
23 stars 6 forks source link

[Patch] write_to_buffer and write_to_buffer_with_dots (un-reverse order) #6

Open GM-Script-Writer-62850 opened 2 weeks ago

GM-Script-Writer-62850 commented 2 weeks ago

EDIT: i made a fork of this and made a bunch of improvements if you want to make any changes based on it


Existing version prints in the reverse order based on the pin out for the MAX7219 datasheet Changes:

    def write_to_buffer(self, s):
        l = len(s)
        if l < 8:
            s = "%-8s" % s
        for i in range(0,8):
            self.buffer[i] = self.decode_char(s[i])

    def write_to_buffer_with_dots(self, s):
      i = len(s)
      x=7

      while i > 0:
        i -= 1
        if i > 0 and s[i] == '.':
          i -= 1
          self.buffer[x] = self.decode_char(s[i]) | 0x80
        else:
          self.buffer[x] = self.decode_char(s[i])
        x -= 1

      while x > -1:
        self.buffer[x] = 0x00 #self.decode_char(' ')
        x -= 1
GM-Script-Writer-62850 commented 2 weeks ago

whet ahead and made a new write method

    def write_digits(self, s, x=7):
        # s is the value to show
        # x is the last digit to show, starts at 0
        s=str(s)
        i = len(s)

        while i > 0 and x > -1:
            i -= 1
            if i > 0 and s[i] == '.':
                i -= 1
                while  i > -1 and s[i] == '.' and x > -1:
                    self.set_register(REG_DIGIT_BASE + x, 0x80)
                    x -= 1
                    i -= 1
                digit = self.decode_char(s[i]) | 0x80
            else:
                digit = self.decode_char(s[i])
            self.set_register(REG_DIGIT_BASE + x, digit)
            x -= 1
        else:
            if i > 0:
                print(s,'has',i,'digits too many to display...')

All this works as expected

while 1:
  display.write_digits("........")
  sleep(d)
  display.write_digits("..1..2..3..4.")
  sleep(d)
  display.write_digits("12.34",3)
  display.write_digits("56.78")
  sleep(d)
  display.write_digits("0123456789")
  sleep(d)
  display.write_digits(".1.2.3.4.5.6.7.")
  sleep(d)
  display.write_digits("        ")
  sleep(d)