from telemetrix_rpi_pico import telemetrix_rpi_pico
from kuukar.kuukar_config import DHT22_PIN, SONAR_ADC_PIN, LIGHT_SENSOR_PIN


class sensors:
    __sonar_distances = 0
    __temperature = 0
    __humidity = 0
    __brightness = 0

    def __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico) -> None:
        self.mcu = mcu
        self.mcu.set_pin_mode_analog_input(SONAR_ADC_PIN, 100, self.__sonar_callback)
        self.mcu.set_pin_mode_dht(DHT22_PIN, self.__dht22_callback) 
        self.mcu.set_pin_mode_analog_input(LIGHT_SENSOR_PIN, 100, self.__light_sensor_callback)

    def sonar_get_distance(self) -> float:
        return self.__sonar_distances

    def __sonar_callback(self, data):
        distance = data[2]
        self.__sonar_distances = distance

    def get_brightness_pct(self) -> float:
        return self.__brightness

    def get_temperature(self) -> float:
        return self.__temperature

    def get_humidity_pct(self) -> float:
        return self.__humidity

    def __light_sensor_callback(self, data):
        self.__brightness = (4096-float(data[2]))/4096*100

    def __dht22_callback(self, data):
        humidity = data[2]
        temperature = data[3]
        self.__humidity = humidity
        self.__temperature = temperature
