silly_clock.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env python
  2. import time
  3. from datetime import datetime
  4. from luma.led_matrix.device import max7219
  5. from luma.core.interface.serial import spi, noop
  6. from luma.core.render import canvas
  7. from luma.core.legacy import text, show_message
  8. from luma.core.legacy.font import proportional, CP437_FONT, TINY_FONT
  9. jours = ["Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche"]
  10. mois = ["Janvier", u"Février", "Mars", "Avril", "Mai", "Juin", "Juillet", u"Août", "Septembtre", "Octobre"]
  11. def minute_change(device):
  12. '''When we reach a minute change, animate it.'''
  13. hours = datetime.now().strftime('%H')
  14. minutes = datetime.now().strftime('%M')
  15. def helper(current_y):
  16. with canvas(device) as draw:
  17. text(draw, (0, 1), hours, fill="white", font=proportional(CP437_FONT))
  18. text(draw, (15, 1), ":", fill="white", font=proportional(TINY_FONT))
  19. text(draw, (17, current_y), minutes, fill="white", font=proportional(CP437_FONT))
  20. time.sleep(0.1)
  21. for current_y in range(1, 9):
  22. helper(current_y)
  23. minutes = datetime.now().strftime('%M')
  24. for current_y in range(9, 1, -1):
  25. helper(current_y)
  26. def animation(device, from_y, to_y):
  27. '''Animate the whole thing, moving it into/out of the abyss.'''
  28. hourstime = datetime.now().strftime('%H')
  29. mintime = datetime.now().strftime('%M')
  30. current_y = from_y
  31. while current_y != to_y:
  32. with canvas(device) as draw:
  33. text(draw, (0, current_y), hourstime, fill="white", font=proportional(CP437_FONT))
  34. text(draw, (15, current_y), ":", fill="white", font=proportional(TINY_FONT))
  35. text(draw, (17, current_y), mintime, fill="white", font=proportional(CP437_FONT))
  36. time.sleep(0.1)
  37. current_y += 1 if to_y > from_y else -1
  38. def main():
  39. # Setup for Banggood version of 4 x 8x8 LED Matrix (https://bit.ly/2Gywazb)
  40. serial = spi(port=0, device=0, gpio=noop())
  41. device = max7219(serial, cascaded=4, block_orientation=-90, blocks_arranged_in_reverse_order=False)
  42. device.contrast(16)
  43. # The time ascends from the abyss...
  44. animation(device, 8, 1)
  45. toggle = False # Toggle the second indicator every second
  46. while True:
  47. toggle = not toggle
  48. sec = datetime.now().second
  49. if sec == 59:
  50. # When we change minutes, animate the minute change
  51. minute_change(device)
  52. else:
  53. # Do the following twice a second (so the seconds' indicator blips).
  54. # I'd optimize if I had to - but what's the point?
  55. # Even my Raspberry PI2 can do this at 4% of a single one of the 4 cores!
  56. hours = datetime.now().strftime('%H')
  57. minutes = datetime.now().strftime('%M')
  58. with canvas(device) as draw:
  59. text(draw, (0, 1), hours, fill="white", font=proportional(CP437_FONT))
  60. text(draw, (15, 1), ":" if toggle else " ", fill="white", font=proportional(TINY_FONT))
  61. text(draw, (17, 1), minutes, fill="white", font=proportional(CP437_FONT))
  62. time.sleep(0.5)
  63. if __name__ == "__main__":
  64. main()