from machine import Pin, I2C, SoftI2C
from OLED_1inch5 import OLED_1inch5
from SHTC3 import SHTC3
from VOC_SGP40 import SGP40
from VOC_Algorithm import VOC_Algorithm
from time import sleep, ticks_ms, ticks_diff
print("=== LAB 9 REAL VERSION ===")
# ================= ADDRESSES =================
OLED_ADDR = 0x3D
SHTC3_ADDR = 0x70
VOC_ADDR = 0x59
# ================= I2C INIT =================
OLED_i2c = SoftI2C(sda=Pin(6), scl=Pin(7), freq=1_000_000)
sleep(0.1)
Sensors_i2c = I2C(id=0, sda=Pin(8), scl=Pin(9), freq=100_000)
sleep(0.1)
# ================= OBJECTS =================
OLED = OLED_1inch5(OLED_ADDR, OLED_i2c)
sthc3 = SHTC3(Sensors_i2c, SHTC3_ADDR)
sthc3.wakeup()
sgp = SGP40(Sensors_i2c, VOC_ADDR)
VOC = VOC_Algorithm()
# ================= BUTTON =================
button = Pin(3, Pin.IN, Pin.PULL_UP)
running = True
btn_flag = False
last_press = 0
def button_irq(pin):
global btn_flag, last_press
now = ticks_ms()
if ticks_diff(now, last_press) > 300:
btn_flag = True
last_press = now
button.irq(trigger=Pin.IRQ_FALLING, handler=button_irq)
# ================= MAIN LOOP =================
while True:
if btn_flag:
btn_flag = False
running = not running
if not running:
OLED.fill(0)
OLED.text("PROGRAM OFF", 20, 60, 15)
OLED.show()
sleep(0.3)
continue
# ===== Читаємо T та RH =====
temperature, humidity = sthc3.measure()
# ===== Передаємо у SGP40 =====
raw = sgp.measureRaw(temperature, humidity)
# ===== Алгоритм VOC =====
voc_index = VOC.VocAlgorithm_process(raw)
# ===== Вивід на дисплей =====
OLED.fill(0)
OLED.text("Temp: {:.1f}C".format(temperature), 5, 5, 15)
OLED.text("Hum: {:.1f}%".format(humidity), 5, 20, 15)
OLED.text("VOC: {}".format(voc_index), 5, 35, 15)
# ===== Малюємо 5 секторів =====
sector_width = 20
start_x = 10
start_y = 70
height = 40
for i in range(5):
x = start_x + i * (sector_width + 5)
OLED.rect(x, start_y, sector_width, height, 8)
# визначаємо активний сектор
sector = voc_index // 100
if sector > 4:
sector = 4
for i in range(sector + 1):
x = start_x + i * (sector_width + 5)
shade = 5 + i * 2 # різні відтінки сірого
OLED.fill_rect(x+1, start_y+1, sector_width-2, height-2, shade)
OLED.show()
sleep(0.5)