[Python] Trying to make my Microdot pHAT script more efficient

Hi all. I’ve cobbled together a clock script for the Microdot pHat. The display is used “upside-down” so I can stand it vertically, and the hours:minutes separator is a blinking colon drawn as text (rather than using the built-in dot separators). This is the code:

#!/usr/bin/env python

import datetime
import time

from microdotphat import write_string, clear, show, set_rotate180

set_rotate180(True)

while True:
    clear()
    t = datetime.datetime.now()
    write_string(t.strftime("%H:%M"), kerning=False)
    show()
    time.sleep(1.0)
    clear()
    write_string(t.strftime("%H %M"), kerning=False)
    show()
    time.sleep(1.0)

It works perfectly well but it seems quite clunky to me. I’d have thought there’d be a way to draw the time only once and have the colon blink independently, but it’s possible I’m not asking Google the right questions, as I haven’t been able to find a solution. Does anyone have any ideas on how to improve efficiency?

Thanks in advance.

We have an example in out GitHub repo. of exactly this, although it’s not really much more efficient in terms of number of lines of code! It does however use the decimal points and also display seconds, in addition to the hours and minutes.

Coming back to this after three months, a bit of code in the new ScrollphatHD clock example led me to a solution:

#!/usr/bin/env python

import datetime
import time
from microdotphat import write_string, clear, show, set_rotate180, WIDTH, HEIGHT, set_pixel         

set_rotate180(True)

while True:
    t = datetime.datetime.now()
    clear()
    write_string(t.strftime("%H:%M"), kerning=False)
    if int(time.time()) % 2 == 0:
      for x in range(WIDTH):
        for y in range(HEIGHT):
          set_pixel(17, y, 0)
          set_pixel(18, y, 0)
    show()

This causes the colon to flash without having to redraw the entire display.