import time from datetime import datetime from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics import json import pytz try: with open('clock_screensaver.json', 'r') as f: settings = json.load(f)['clock1'] except: settings1 = {"clock1": {"time_color": "White", "weekday_color": "Cyan", "date_color": "Blue", "timezone": "Etc/GMT+4", "display_seconds": True, "display_pm": True, "12hour": True, "brightness": "10"}, "clock2": {"time_color": "Orange", "date_color": "White", "timezone": "Etc/GMT+4", "display_seconds": True, "display_pm": True, "12hour": True, "brightness": "10"}, "world_clock": {"city_color": "White", "display_seconds": True, "display_pm": True, "12hour": True, "brightness": "10"}} with open('clock_screensaver.json', 'w') as f: json.dump(settings1, f) settings = settings1['clock1'] # Configuration for the LED matrix options = RGBMatrixOptions() options.rows = 32 options.cols = 128 options.chain_length = 1 options.parallel = 1 options.hardware_mapping = 'adafruit-hat' # Change this if you have a different GPIO mapping options.gpio_slowdown = 2 options.brightness = int(settings['brightness'])*10 # Create the RGBMatrix object matrix = RGBMatrix(options=options) # Create a canvas to draw on canvas = matrix.CreateFrameCanvas() # Set the font and text color font = graphics.Font() font.LoadFont("fonts/6x12.bdf") # Change this to the path of your desired font file font2 = graphics.Font() font2.LoadFont("fonts/8x13B.bdf") color = graphics.Color(255, 255, 255) # White color timezone = pytz.timezone(settings['timezone']) time_colors = { "White": graphics.Color(255,255,255), "Red": graphics.Color(255, 0,0), "Green": graphics.Color(0,255,0), "Dark Green": graphics.Color(0, 100,0), "Blue": graphics.Color(0,0,255), "Purple": graphics.Color(145,0,255), "Pink": graphics.Color(255,0,255), "Yellow": graphics.Color(255,255,0), "Orange": graphics.Color(255,130,0), "Gold": graphics.Color(255,190,0), "Gray": graphics.Color(100,100,100), "Cyan": graphics.Color(0,255,255) } colon_visible = False while True: # Clear the canvas canvas.Clear() # Get the current time, weekday, and date if settings['12hour']: if settings['display_pm'] and settings['display_seconds']: current_time = datetime.now(timezone).strftime("%I:%M:%S %p") elif settings['display_pm'] and not settings['display_seconds']: current_time = datetime.now(timezone).strftime("%I:%M %p") elif not settings['display_pm'] and settings['display_seconds']: current_time = datetime.now(timezone).strftime("%I:%M:%S") else: current_time = datetime.now(timezone).strftime("%I:%M") else: if settings['display_pm'] and settings['display_seconds']: current_time = datetime.now(timezone).strftime("%H:%M:%S %p") elif settings['display_pm'] and not settings['display_seconds']: current_time = datetime.now(timezone).strftime("%H:%M %p") elif not settings['display_pm'] and settings['display_seconds']: current_time = datetime.now(timezone).strftime("%H:%M:%S") else: current_time = datetime.now(timezone).strftime("%H:%M") if colon_visible: current_time = current_time.replace(":", " ") colon_visible = False else: colon_visible = True current_weekday = datetime.now(timezone).strftime("%A") current_date = datetime.now(timezone).strftime("%d %b %Y") weekday_width = graphics.DrawText(canvas, font, 0, 0, color, current_weekday.upper()) time_width = graphics.DrawText(canvas, font2, 0, 0, time_colors[settings['time_color']], current_time) date_width = graphics.DrawText(canvas, font, 0, 0, time_colors[settings['date_color']], current_date.upper()) canvas.Clear() x_coordinate_weekday = int((options.cols - weekday_width) / 2) x_coordinate_time = int((options.cols - time_width) / 2) x_coordinate_date = int((options.cols - date_width) /2) # Draw the time, weekday, and date on the canvas graphics.DrawText(canvas, font2, x_coordinate_time, 12, time_colors[settings['time_color']], current_time) graphics.DrawText(canvas, font, x_coordinate_weekday, 22, time_colors[settings['weekday_color']], current_weekday.upper()) graphics.DrawText(canvas, font, x_coordinate_date, 31, time_colors[settings['date_color']], current_date.upper()) # Swap the canvas onto the matrix canvas = matrix.SwapOnVSync(canvas) # Wait for 1 second before updating the clock time.sleep(0.5)