#!/usr/bin/env python3 # Библиотеки для работы примера import time import datetime from gpiozero import Button from unicornhatmini import UnicornHATMini from random import randrange from signal import pause # Создаём объект для работы с модулем unicornhatmini = UnicornHATMini() # Устанавливаем яркость светодиодов unicornhatmini.set_brightness(0.05) # Переменные red = 0 green = 0 blue = 0 dotson = True previousMinute = -1 mode = "time" # Массив графических чисел numbers = { "0": [ [2, 0], [1, 0], [0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [1, 6], [2, 6], [2, 5], [2, 4], [2, 3], [2, 2], [2, 1], ], "1": [ [0, 1], [1, 0], [1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [0, 6], [2, 6], ], "2": [ [0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [1, 3], [0, 4], [0, 5], [0, 6], [1, 6], [2, 6], ], "3": [ [0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [2, 3], [1, 3], [0, 3], [2, 4], [2, 5], [2, 6], [1, 6], [0, 6], ], "4": [ [0, 0], [0, 1], [0, 2], [0, 3], [1, 3], [2, 3], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], ], "5": [ [0, 0], [1, 0], [2, 0], [0, 1], [0, 2], [0, 3], [1, 3], [2, 3], [2, 4], [2, 5], [2, 6], [1, 6], [0, 6], ], "6": [ [2, 0], [1, 0], [0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [1, 6], [2, 6], [2, 5], [2, 4], [2, 3], [1, 3], ], "7": [[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [2, 3], [1, 4], [1, 5], [1, 6] ], "8": [ [2, 0], [1, 0], [0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [1, 6], [2, 6], [2, 5], [2, 4], [2, 3], [1, 3], [2, 2], [2, 1], ], "9": [ [2, 0], [1, 0], [0, 0], [0, 1], [0, 2], [0, 3], [0, 6], [1, 6], [2, 6], [2, 5], [2, 4], [2, 3], [1, 3], [2, 2], [2, 1], ], ":": [[1, 2], [1, 4]], ".": [[1, 6]], } # Кнопки button_a = Button(5) button_b = Button(6) # Функция отображения времени def enable_show_time(button): global mode mode = "time" # Функция отображения даты def enable_show_date(button): global mode mode = "date" # Функция смена цвета def change_color(): global red global green global blue red = randrange(256) green = randrange(256) blue = randrange(256) # Считываем состояние кнопок button_a.when_pressed = enable_show_date button_a.when_released = enable_show_time button_b.when_pressed = change_color def letter(char, offset): for pixel in numbers[char]: x = pixel[0] + offset y = pixel[1] unicornhatmini.set_pixel(x, y, red, green, blue) # Функция отображения времени def show_time(now): global dotson unicornhatmini.clear() timestring = now.strftime("%H%M") letter(timestring[0], 0) letter(timestring[1], 4) if dotson == True: letter(":", 7) letter(timestring[2], 10) letter(timestring[3], 14) unicornhatmini.show() # Функция отображения даты def show_date(now): global dotson unicornhatmini.clear() timestring = now.strftime("%d%m") letter(timestring[0], 0) letter(timestring[1], 4) letter(".", 7) letter(timestring[2], 10) letter(timestring[3], 14) unicornhatmini.show() while True: # Считываем дату и время now = datetime.datetime.now() # Если прошла минута, меняет цвет if now.minute != previousMinute: change_color() # Если флаг установлен в «time», отображаем время if mode == "time": show_time(now) # Если флаг установлен в «date», отображаем дату if mode == "date": show_date(now) dotson = not dotson previousMinute = now.minute time.sleep(0.5)