# Basic example of clearing and drawing pixels on a SSD1306 OLED display.
# This example and library is meant to work with Adafruit CircuitPython API.
# Author: Tony DiCola
# License: Public Domain

import datetime
import time

from board import SCL, SDA
import busio

import adafruit_ssd1306
from font6_8 import font6_8

# Create the I2C interface.
i2c = busio.I2C(SCL, SDA)

# Create the SSD1306 OLED class.
# The first two parameters are the pixel width and pixel height.  Change these
# to the right size for your display!
display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
# Alternatively you can change the I2C address of the device with an addr parameter:
#display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x31)

# Clear the display.  Always call show after changing pixels to make the display
# update visible!
display.fill(0)

curx = 0


def print_char(c, scale = 1):  
	global curx 
	c = ord(c)
	idx = (c-32)*6 +4
	charmap = font6_8[idx:idx+6]
	for col in charmap:
		for row in range(8):
			setting = 1 if col & 1 else 0
			for x1 in range(scale):
				for y1 in range(scale):
					display.pixel(curx + x1, row*scale + y1, setting)
			col = col >> 1
		curx += scale

def print_str(s, scale = 1): 
	for c in s: print_char(c, scale)


def show_time():
	global curx
	curx = 0
	now = datetime.datetime.now()
	sep = ":" if now.second % 2 == 0 else " "
	fmt = "%H" + sep + "%M"
	s = now.strftime(fmt)
	#print(s)
	print_str(s, scale = 4)
	display.show()

while True:
	show_time()
	time.sleep(0.2)

