Advertisement
DarrenHill

Picoscroll Clock

Apr 15th, 2021 (edited)
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.64 KB | None | 0 0
  1. import math
  2. import utime
  3. import picoscroll as scroll
  4. from machine import Pin
  5.  
  6. scroll.init()
  7.  
  8. secupdated = False
  9. led = Pin(25,Pin.OUT)
  10.  
  11. # Initial time, from RTC if set via the shell, or 12:30:01 otherwise (if RTC is unset, epoch is 1/1/21 00:00:01)
  12. if utime.localtime()[0] == 2021 and utime.localtime()[1] == 1 and utime.localtime()[2] == 1:
  13.     hours = 12
  14.     minutes = 30
  15.     seconds = 1
  16.     sync = False
  17.     led.low()
  18. else:
  19.     hours = utime.localtime()[3]
  20.     minutes = utime.localtime()[4]
  21.     seconds = utime.localtime()[5]
  22.     sync = True
  23.     led.high()
  24.  
  25. # LED Brightness (0-255)
  26. brightness = 50
  27.  
  28. # Character pixel maps (3w x 5h font)
  29. zero = [0,1,1,1,0],[1,0,0,0,1],[0,1,1,1,0]
  30. one = [0,1,0,0,1],[1,1,1,1,1],[0,0,0,0,1]
  31. two = [1,0,0,1,1],[1,0,1,0,1],[0,1,0,0,1]
  32. three = [1,0,1,0,1],[1,0,1,0,1],[0,1,0,1,0]
  33. four = [0,1,1,1,0],[1,0,0,1,0],[0,0,1,1,1]
  34. five = [1,1,1,0,1],[1,0,1,0,1],[1,0,0,1,0]
  35. six = [0,1,1,1,0],[1,0,1,0,1],[0,0,0,1,0]
  36. seven = [1,0,0,1,1],[1,0,1,0,0],[1,1,0,0,0]
  37. eight = [0,1,0,1,0],[1,0,1,0,1],[0,1,0,1,0]
  38. nine = [0,1,0,0,0],[1,0,1,0,1],[0,1,1,1,0]
  39. error = [1,1,1,1,1],[1,0,1,0,1],[1,0,1,0,1]
  40. charlist = [zero,one,two,three,four,five,six,seven,eight,nine]
  41.  
  42. # Draw characters using above pixel maps, left to right, top to bottom
  43. def draw_digit(x,y,map):
  44.     global brightness
  45.  
  46.     scroll.set_pixel(x,y,map[0][0]*brightness)
  47.     scroll.set_pixel(x+1,y,map[1][0]*brightness)
  48.     scroll.set_pixel(x+2,y,map[2][0]*brightness)
  49.  
  50.     scroll.set_pixel(x,y+1,map[0][1]*brightness)
  51.     scroll.set_pixel(x+1,y+1,map[1][1]*brightness)
  52.     scroll.set_pixel(x+2,y+1,map[2][1]*brightness)
  53.  
  54.     scroll.set_pixel(x,y+2,map[0][2]*brightness)
  55.     scroll.set_pixel(x+1,y+2,map[1][2]*brightness)
  56.     scroll.set_pixel(x+2,y+2,map[2][2]*brightness)
  57.  
  58.     scroll.set_pixel(x,y+3,map[0][3]*brightness)
  59.     scroll.set_pixel(x+1,y+3,map[1][3]*brightness)
  60.     scroll.set_pixel(x+2,y+3,map[2][3]*brightness)
  61.  
  62.     scroll.set_pixel(x,y+4,map[0][4]*brightness)
  63.     scroll.set_pixel(x+1,y+4,map[1][4]*brightness)
  64.     scroll.set_pixel(x+2,y+4,map[2][4]*brightness)
  65.     return
  66.  
  67. # Process clock characters
  68. def character(x,y,char):
  69.     global charlist
  70.     if char >= 0 and char <=9:
  71.         draw_digit(x,y,charlist[char])
  72.         return
  73.     else:
  74.         draw_digit(x,y,error)
  75.         return
  76.  
  77. # Act on button presses
  78. def buttonhandler():
  79.     global hours, minutes, brightness, led
  80.    
  81.     if scroll.is_pressed(scroll.BUTTON_A):
  82.         led.low()
  83.         if scroll.is_pressed(scroll.BUTTON_X):
  84.             if brightness <= 245 and brightness >= 10:
  85.                 brightness += 10
  86.             elif brightness < 255 or brightness < 10:
  87.                 brightness += 1
  88.         else:
  89.             hours += 1
  90.         return
  91.  
  92.     if scroll.is_pressed(scroll.BUTTON_B):
  93.         led.low()
  94.         if scroll.is_pressed(scroll.BUTTON_Y):
  95.             if brightness >= 11 and brightness <= 250:
  96.                 brightness -= 10
  97.             elif brightness > 3 or brightness > 250 :
  98.                 brightness -= 1
  99.         else:
  100.             hours -= 1
  101.             if hours < 0:
  102.                 hours = 23
  103.         return
  104.  
  105.  
  106.     if scroll.is_pressed(scroll.BUTTON_X):
  107.         led.low()
  108.         minutes += 1
  109.         return
  110.  
  111.     if scroll.is_pressed(scroll.BUTTON_Y):
  112.         led.low()
  113.         minutes -= 1
  114.         if minutes < 0:
  115.             minutes = 59
  116.         return
  117.  
  118. def draw_clock(hrs,mins,secs):
  119.     global brightness
  120.     scroll.clear()
  121.  
  122.     character(0,0,hrs//10) # hour tens (0-2)
  123.     character(4,0,hrs%10) # hour units (0-9)
  124.     character(10,0,mins//10) # minute tens (0-5)
  125.     character(14,0,mins%10) # minute units (0-9)
  126.  
  127.     if(secs % 2 != 0): # draw colon between hours and minutes on odd seconds
  128.         scroll.set_pixel(8,1,brightness)
  129.         scroll.set_pixel(8,3,brightness)
  130.        
  131.     # seconds pixel bar across the bottom - borrowed from the scrollphathd clock example
  132.     seconds_progress = 15 * ((secs % 60) / 59.0)
  133.     for y in range(15):
  134.         current_pixel = min(seconds_progress, 1)
  135.         scroll.set_pixel(y + 1, 6, int(current_pixel * brightness))
  136.         seconds_progress -= 1
  137.         if seconds_progress <= 0:
  138.             break
  139.            
  140.     scroll.update()
  141.  
  142. # Main clock loop
  143. while True:
  144.     utime.sleep(0.5)
  145.     seconds = utime.localtime()[5]
  146.  
  147.     buttonhandler()
  148.    
  149.     if seconds == 0 and secupdated == False:
  150.         minutes += 1
  151.         secupdated = True
  152.        
  153.     if seconds == 1 and secupdated == True:
  154.         secupdated = False
  155.  
  156.     if minutes > 59:
  157.         hours += 1
  158.         minutes = 0
  159.  
  160.     if hours > 23:
  161.         hours = 0
  162.  
  163.     draw_clock(hours,minutes,seconds)
  164.  
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement