Advertisement
realbuxtehuder

picoscroll scrollphathd like clock

Nov 14th, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | Software | 0 0
  1. import time
  2. from picoscroll import PicoScroll
  3. from picographics import PicoGraphics, DISPLAY_SCROLL_PACK, PEN_P8
  4.  
  5. graphics          = PicoGraphics(DISPLAY_SCROLL_PACK, pen_type=PEN_P8)
  6. scroll            = PicoScroll()
  7. scroll_brightness = 32
  8.  
  9. # Character pixel maps (3w x 5h font)
  10. chr_3x5_zero =  [0,1,1,1,0],[1,0,0,0,1],[0,1,1,1,0]
  11. chr_3x5_one =   [0,1,0,0,1],[1,1,1,1,1],[0,0,0,0,1]
  12. chr_3x5_two =   [1,0,0,1,1],[1,0,1,0,1],[0,1,0,0,1]
  13. chr_3x5_three = [1,0,0,0,1],[1,0,1,0,1],[0,1,0,1,0]
  14. chr_3x5_four =  [0,1,1,1,0],[1,0,0,1,0],[0,0,1,1,1]
  15. chr_3x5_five =  [1,1,1,0,1],[1,0,1,0,1],[1,0,0,1,0]
  16. chr_3x5_six =   [0,1,1,1,0],[1,0,1,0,1],[0,0,0,1,0]
  17. chr_3x5_seven = [1,0,0,1,1],[1,0,1,0,0],[1,1,0,0,0]
  18. chr_3x5_eight = [0,1,0,1,0],[1,0,1,0,1],[0,1,0,1,0]
  19. chr_3x5_nine =  [0,1,0,0,0],[1,0,1,0,1],[0,1,1,1,0]
  20. chr_3x5_error = [1,1,1,1,1],[1,0,1,0,1],[1,0,1,0,1]
  21. chr_3x5_list =  [chr_3x5_zero, chr_3x5_one, chr_3x5_two, chr_3x5_three, chr_3x5_four,
  22.                  chr_3x5_five, chr_3x5_six, chr_3x5_seven, chr_3x5_eight, chr_3x5_nine]
  23.  
  24.  
  25. # draw clock digit using above pixel map, left to right, top to bottom
  26. def draw_3x5_character(x, y, digit):
  27.     if digit >= 0 and digit <=9:
  28.         _chr = chr_3x5_list[digit]
  29.     else:
  30.         _chr = chr_3x5_error
  31.     graphics.set_pen(scroll_brightness)    
  32.     for _hor in range(3):
  33.         for _vert in range(5):
  34.             if(_chr[_hor][_vert] == 1):
  35.                 graphics.pixel(x + _hor, y + _vert)
  36.  
  37.  
  38. def draw_clock():
  39.     # save time to local list
  40.     _time = time.localtime()                 # [0]=year, [1]=month, [2]=day, [3]=hour, [4]=minute, [5]=second
  41.     # clear canvas
  42.     graphics.set_pen(0)
  43.     graphics.clear()
  44.     # draw digits
  45.     draw_3x5_character(0,0,_time[3] // 10)   # hour tens (0-2)
  46.     draw_3x5_character(4,0,_time[3] % 10)    # hour units (0-9)
  47.     draw_3x5_character(10,0,_time[4] // 10)  # minute tens (0-5)
  48.     draw_3x5_character(14,0,_time[4] % 10)   # minute units (0-9)
  49.     # draw colon between hours and minutes on odd seconds
  50.     if(_time[5] % 2 != 0):
  51.         graphics.set_pen(scroll_brightness)
  52.         graphics.pixel(8,1)
  53.         graphics.pixel(8,3)
  54.     # seconds pixel bar across the bottom - borrowed from the scrollphathd clock example
  55.     seconds_progress = 15 * ((_time[5] % 60) / 59.0)
  56.     for y in range(15):
  57.         current_pixel = min(seconds_progress, 1)
  58.         graphics.set_pen(int(current_pixel * scroll_brightness))
  59.         graphics.pixel(y + 1, 6)
  60.         seconds_progress -= 1
  61.         if seconds_progress <= 0:
  62.             break
  63.     # transfer canvas to display
  64.     scroll.update(graphics)
  65.    
  66.  
  67. while True:
  68.     draw_clock()
  69.     time.sleep(0.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement