# Script to demonstrate the Enhanced_Display class and associated packed fonts.
#
# Copyright (C) Mark Gladding 2023.
#
# MIT License (see the accompanying license file)
#
# https://github.com/mark-gladding/packed-font
#

from enhanced_display import Enhanced_Display
import time 
from machine import Pin, I2C
import dht

#Temp Stuff

#sensor = dht.DHT22(Pin(22))
sensor = dht.DHT11(Pin(22))

#Screen Stuff
i2c = I2C(scl=Pin(5), sda=Pin(4))

if __name__ == "__main__":

    display = Enhanced_Display(bus=0,scl=Pin(5),sda=Pin(4))

    # Load the list of fonts to use
    display.load_fonts(['digits-30', 'text-16', 'icons-32', 'icons-128'])

while True:
  try:

    time.sleep(2)
    sensor.measure()
    temp = sensor.temperature()
    hum = sensor.humidity()
    temp_f = temp * (9/5) + 32.0
    print(temp_f)
    print(hum)
    #temp_print = 'Temp: %3.1f F' %temp_f
    #hum_print = 'Humidity: %3.1f %%' %hum
    
    #oled.text(temp_print, 0, 10)
    #oled.text(hum_print, 0, 20)
    ##print('Temperature: %3.1f C' %temp)
    #print('Temperature: %3.1f F' %temp_f)
    ##print('Humidity: %3.1f %%' %hum)
    
      # Display the Temperature screen
    display.fill(0)

    display.select_font('digits-30')
    #degrees = '\u00b0'  # Character code for the degrees symbol
    display.text(f'{temp_f:.1f}', 0, 0, 1, 1)
    display.select_font('icons-32')
    display.text('t', 0, 0, 2)          # The 't' character contains the temperature icon
    display.select_font(None)           # Select the built in 8 pixel font
    display.text('Temperature', 0, 0, 1, 2)

    display.show()
    time.sleep(2)

    # Display the Humidity screen
    display.fill(0)

    display.select_font('digits-30')
    display.text(f'{hum:2.0f}', 0, 0, 1, 1)
    display.select_font('icons-32')
    display.text('h', 0, 0, 2)          # The 'h' character contains the humidity icon
    display.select_font(None)
    display.text('Humidity', 0, 0, 1, 2)

    display.show()
    time.sleep(2)    

  except OSError as e:
    display.fill(1)
    #oled.text('Failed to read sensor.', 0, 30)
    display.show()
    print('Failed to read sensor.')
