import time
from machine import Pin, I2C, ADC
import math
import ssd1306
temp_adc= ADC(27)
light_adc = ADC(26)
GAMMA = 0.7
RL10 = 33
BETA = 3950
# OLED I2C
i2c = I2C(0, scl=Pin(17), sda=Pin(16), freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
while True:
analogValue = light_adc.read_u16()
voltage = analogValue / 65536. * 3.3;
resistance = 2000 * voltage / (1 - voltage / 3.3);
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
analogValue = temp_adc.read_u16()
celsius = 1 / (math.log(1 / (65535 / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
oled.fill(0)
output_string = f"Light = {lux}lux"
oled.text(output_string, 0, 20, 1)
print(output_string)
output_string = f"Temp = %.2fC"%celsius
oled.text(output_string, 0, 40, 1)
print(output_string)
oled.show()
time.sleep(1)