diff --git a/esp32-lora/main.py b/esp32-lora/main.py index 56628d4b..24b0ba4c 100644 --- a/esp32-lora/main.py +++ b/esp32-lora/main.py @@ -1,15 +1,13 @@ -from machine import Pin, ADC, I2C import time -from ssd1306 import SSD1306_I2C +from machine import Pin, ADC, I2C +from screen import Screen # ----- ADC (Grove Light Sensor) ----- light_adc = ADC(Pin(1)) light_adc.atten(ADC.ATTN_11DB) # 0–3.3V range light_adc.width(ADC.WIDTH_12BIT) # 0–4095 -# ----- I2C (OLED) ----- -i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000) -oled = SSD1306_I2C(128, 64, i2c) +screen = Screen() def read_light(samples=8): total = 0 @@ -22,10 +20,8 @@ while True: raw = read_light() percent = int((raw / 4095) * 100) - oled.fill(0) - oled.text("Light Sensor", 0, 0) - oled.text("Raw: {}".format(raw), 0, 20) - oled.text("Level: {}%".format(percent), 0, 40) - oled.show() + screen.display("Light Sensor", 15, 0) + screen.display("Raw: {}".format(raw), 15, 20) + screen.display("Level: {}%".format(percent), 15, 40) time.sleep(0.5) \ No newline at end of file diff --git a/esp32-lora/screen.py b/esp32-lora/screen.py new file mode 100644 index 00000000..d4991b16 --- /dev/null +++ b/esp32-lora/screen.py @@ -0,0 +1,36 @@ +from machine import Pin, SoftI2C +from ssd1306 import SSD1306_I2C +import time + +class Screen : + + sda = None + scl = None + rst = None + oled = None + + def __init__(self) : + self.sda = Pin(17) + self.scl = Pin(18) + self.rst = Pin(21, Pin.OUT) + self.oled = SSD1306_I2C( + 128, + 64, + SoftI2C(scl=self.scl, sda=self.sda, freq=100000), + addr=0x3C + ) + self.__prepare() + + def reset(self): + self.oled.fill(0) + self.oled.show() + + def display(self, message, col, row): + self.oled.text(message, col, row) + self.oled.show() + + def __prepare(self): + self.rst.value(0) + time.sleep_ms(150) + self.rst.value(1) + time.sleep_ms(150) \ No newline at end of file