kuukar-rpi/kuukar_sensors.py

45 lines
1.6 KiB
Python

from telemetrix_rpi_pico import telemetrix_rpi_pico
from kuukar_config import DHT22_PIN, SONAR_1_ECHO_PIN, SONAR_1_TRIG_PIN, SONAR_2_ECHO_PIN, SONAR_2_TRIG_PIN, SONAR_3_ECHO_PIN, SONAR_3_TRIG_PIN
class sensors:
__sonar_trig_pins__ = [SONAR_1_TRIG_PIN,
SONAR_2_TRIG_PIN, SONAR_3_TRIG_PIN]
__sonar_distances__ = [0, 0, 0]
__temperature__ = 0
__humidity__ = 0
def __init__(self, aux_board: telemetrix_rpi_pico.TelemetrixRpiPico, driver_board: telemetrix_rpi_pico.TelemetrixRpiPico) -> None:
self.aux = aux_board
self.driver = driver_board
self.driver.set_pin_mode_sonar(
SONAR_1_TRIG_PIN, SONAR_1_ECHO_PIN, self.__sonar_callback__)
self.driver.set_pin_mode_sonar(
SONAR_2_TRIG_PIN, SONAR_2_ECHO_PIN, self.__sonar_callback__)
self.driver.set_pin_mode_sonar(
SONAR_3_TRIG_PIN, SONAR_3_ECHO_PIN, self.__sonar_callback__)
self.driver.set_pin_mode_dht(DHT22_PIN, self.__dht22_callback__)
def sonar_get_distance(self, id: int) -> float:
return self.__sonar_distances__[id]
def __sonar_callback__(self, data):
pin = data[1]
distance = data[2]
sonar_id = self.sonar_pins.index(pin)
self.__sonar_distances__[sonar_id] = distance
def get_temperature(self) -> float:
return self.__temperature__
def get_humidity_pct(self) -> float:
return self.__humidity__
def __dht22_callback__(self, data):
humidity = data[3]
temperature = data[4]
self.__humidity__ = humidity
self.__temperature__ = temperature