# Copyright © 2026 Remco van 't Veer
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

import machine, neopixel, time

class Display:
    SEG_A = 3
    SEG_B = 4
    SEG_C = 5
    SEG_D = 0
    SEG_E = 1
    SEG_F = 2
    SEG_G = 6

    DIGITS = [[SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F],
              [SEG_B, SEG_C],
              [SEG_A, SEG_B, SEG_G, SEG_E, SEG_D],
              [SEG_A, SEG_B, SEG_G, SEG_C, SEG_D],
              [SEG_F, SEG_B, SEG_G, SEG_C],
              [SEG_A, SEG_F, SEG_G, SEG_C, SEG_D],
              [SEG_A, SEG_F, SEG_G, SEG_E, SEG_C, SEG_D],
              [SEG_A, SEG_B, SEG_C],
              [SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F, SEG_G],
              [SEG_A, SEG_F, SEG_B, SEG_G, SEG_C, SEG_D]]

    color = (2, 2, 2)

    def __init__(self, *, data_gpio: None):
        self.data_pin = machine.Pin(data_gpio)
        self.pixel_array = neopixel.NeoPixel(self.data_pin, 7 * 4 + 2)

    def commit(self):
        self.pixel_array.write()

    def set_hours(self, hours, color = None):
        for i in range(14):
            self.pixel_array[16 + i] = (0, 0, 0)

        if hours is not None:
            t, n = divmod(hours, 10)
            for i in self.DIGITS[t]:
                self.pixel_array[7 + 7 + 2 + 7 + i] = color or self.color
            for i in self.DIGITS[n]:
                self.pixel_array[7 + 7 + 2 + i] = color or self.color

    def set_minutes(self, minutes, color = None):
        for i in range(14):
            self.pixel_array[i] = (0, 0, 0)

        if minutes is not None:
            t, n = divmod(minutes, 10)
            for i in self.DIGITS[t]:
                self.pixel_array[7 + i] = color or self.color
            for i in self.DIGITS[n]:
                self.pixel_array[i] = color or self.color

    def set_hours_minutes(self, hours, minutes, color = None):
        self.set_hours(hours, color)
        self.set_minutes(minutes, color)

    def dots_on(self, color = None):
        self.pixel_array[14] = color or self.color
        self.pixel_array[15] = color or self.color

    def dots_off(self, color = None):
        self.pixel_array[14] = (0,0,0)
        self.pixel_array[15] = (0,0,0)

    def toggle_dots(self, color = None):
        if self.pixel_array[14] == (0,0,0):
            self.dots_on(color)
        else:
            self.dots_off(color)

    def display_current_time(self, toggle_dots = False, color = None, color_dots = None):
        _, _, _, hours, minutes, _, _, _ = time.gmtime()

        self.set_hours_minutes(hours, minutes, color = color)
        if toggle_dots:
            self.toggle_dots(color = color_dots or color)
        else:
            self.dots_on(color = color_dots or color)
        self.commit()

if __name__ == "__main__":
    t = Display(data_gpio = 0)
    print("running display_time_now")
    t.display_current_time()
    print("running display_time_now with dot toggle")
    t.display_current_time(toggle_dots = True)
    print("running toggle_dots")
    t.toggle_dots()
    print("running toggle_dots other color")
    t.color = (0, 0, 2)
    t.toggle_dots()
