# test program to read in a analog value on Pin 32 on the ESP32 board
# and display it on the 0.96" OLED Display (Heltec ESP32 Web Kit)

from machine import ADC
from time import sleep
from machine import I2C, Pin 
import ssd1306

# ESP32 reset pin for display must be 1 - this is pin16 
# should be done in ssd1306.py - if not uncommend the next 2 lines
#pin16 = Pin(16, Pin.OUT)
#pin16.value(1)

# this is for ESP32
# Pins according the schematic https://heltec.org/project/wifi-kit-32/
i2c = I2C(-1, scl=Pin(15), sda=Pin(4))

# for ESP8266 it should be:
# i2c = I2C(-1, scl=Pin(5), sda=Pin(4))
 

#display size
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)

# for ESP32 setup the ADC on Pin GPIO32
adc = ADC(Pin(32)) 
adc.atten(ADC.ATTN_11DB)    # set 11dB input attenuation (voltage range roughly 0.0v - 3.6v)

# for ESP8266 setup the ADC(0)
#adc = ADC(0) 

while True:
  # read in analog value in v
  v = adc.read()
  # print to serial line
  print(v)
  # empty display
  oled.fill(0)
  oled.show()
  # write v converted to a string onto the display at (0,0)
  oled.text(str(v), 0, 0)
  oled.show()
  sleep(1)



