# ONE HERTZ DISPLAY
# Special for Hackaday One Hertz Challenge contest
# Danjovic 2025

#
# gc9a01_picture_locket.py -- Simple Demo of GC9A01 Round LCD 
#
# 2021 - Tod Kurt - todbot.com
#
# Tested on QTPy (SAMD21), QTPy RP2040, and Raspberry Pi Pico (RP2040)
# running CircuitPython 7.
#
# You'll need to install 'gc9a01' package.
# Easiest way to do this is from Terminal:
#  circup install gc9a01
#

import time
import board
import math
import busio
import terminalio
import displayio
import adafruit_imageload
import gc9a01

# A list of all the BMP images you want displayed, in order
#
# prepare image with ImageMagick like:
# convert input.jpg -resize 240x240 -type palette BMP3:output.bmp

img_filenames = ( "/imgs/pic00.bmp",
                  "/imgs/pic01.bmp",
                  "/imgs/pic02.bmp",
                  "/imgs/pic03.bmp",
                  "/imgs/pic04.bmp",
                  "/imgs/pic05.bmp",
                  )


# time in seconds between images
img_time = 1

# Release any resources currently in use for the displays
displayio.release_displays()

# attempt to auto-detect board type
import os
board_type = os.uname().machine
if 'QT Py M0 Haxpress' in board_type or 'QT Py RP2040' in board_type: 
    tft_clk  = board.SCK
    tft_mosi = board.MOSI
    tft_rst  = board.TX
    tft_dc   = board.RX
    tft_cs   = board.A3
    tft_bl   = board.A2
    spi = busio.SPI(clock=tft_clk, MOSI=tft_mosi)
elif 'ItsyBitsy M4' in board_type:
    tft_clk  = board.SCK
    tft_mosi = board.MOSI
    tft_rst  = board.MISO
    tft_dc   = board.D2
    tft_cs   = board.A5
    tft_bl   = board.A3  # optional
    spi = busio.SPI(clock=tft_clk, MOSI=tft_mosi)
elif 'Pico' in board_type:
    # Raspberry Pi Pico pinout, one possibility, at "southwest" of board
    tft_clk = board.GP2 #10 # must be a SPI CLK
    tft_mosi= board.GP3 #11 # must be a SPI TX
    tft_rst = board.GP5 #12
    tft_dc  = board.GP4 #13
    tft_cs  = board.GP6 #14
    tft_bl  = board.GP15
    spi = busio.SPI(clock=tft_clk, MOSI=tft_mosi)
elif 'Waveshare RP2040-LCD-1.28 with rp2040' in board_type:
    tft_clk = board.LCD_CLK
    tft_mosi = board.LCD_DIN
    tft_rst = board.LCD_RST
    tft_dc = board.LCD_DC
    tft_cs = board.LCD_CS
    tft_bl = board.LCD_BL
    spi = busio.SPI(clock=tft_clk, MOSI=tft_mosi)
else:
    print("ERROR: Unknown board!")
    
# Make the displayio SPI bus and the GC9A01 display
display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=tft_rst)
display = gc9a01.GC9A01(display_bus, width=240, height=240, backlight_pin=tft_bl)

# Make the main display context
main = displayio.Group()
display.root_group = main

i=0
while True:
    print(time.monotonic(),"hello")
    img_filename = img_filenames[i]
    print (img_filename)
    img_bitmap = displayio.OnDiskBitmap(open(img_filename, "rb"))
    img_palette = displayio.ColorConverter()
    #img_bitmap, img_palette = adafruit_imageload.load(img_filename)
    img_tilegrid = displayio.TileGrid(img_bitmap, pixel_shader=img_palette)
    main.append(img_tilegrid)
    time.sleep(img_time)
    main.pop()  # remove image
    i = (i+1) % len(img_filenames)  # go to next file
    

    
