kuukar-rpi/kuukar_sensors.py

55 lines
1.9 KiB
Python
Raw Normal View History

from telemetrix_rpi_pico import telemetrix_rpi_pico
2022-10-25 14:53:05 +00:00
2022-10-28 07:14:00 +00:00
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,\
LIGHT_ANALOG_PIN
2022-10-25 14:53:05 +00:00
2022-10-23 06:49:59 +00:00
class sensors:
2022-10-25 14:53:05 +00:00
2022-10-28 07:14:00 +00:00
__sonar_trig_pins = [SONAR_1_TRIG_PIN,
SONAR_2_TRIG_PIN, SONAR_3_TRIG_PIN]
__sonar_distances = [0, 0, 0]
__temperature = 0
__humidity = 0
__brightness = 0
2022-10-25 14:53:05 +00:00
def __init__(self, aux_board: telemetrix_rpi_pico.TelemetrixRpiPico, driver_board: telemetrix_rpi_pico.TelemetrixRpiPico) -> None:
self.aux = aux_board
2022-10-25 14:53:05 +00:00
self.driver = driver_board
self.driver.set_pin_mode_sonar(
2022-10-28 07:14:00 +00:00
SONAR_1_TRIG_PIN, SONAR_1_ECHO_PIN, self.__sonar_callback)
2022-10-25 14:53:05 +00:00
self.driver.set_pin_mode_sonar(
2022-10-28 07:14:00 +00:00
SONAR_2_TRIG_PIN, SONAR_2_ECHO_PIN, self.__sonar_callback)
2022-10-25 14:53:05 +00:00
self.driver.set_pin_mode_sonar(
2022-10-28 07:14:00 +00:00
SONAR_3_TRIG_PIN, SONAR_3_ECHO_PIN, self.__sonar_callback)
self.driver.set_pin_mode_dht(DHT22_PIN, self.__dht22_callback)
self.aux.set_pin_mode_analog_input(LIGHT_ANALOG_PIN, 0, self.__light_sensor_callback())
2022-10-25 14:53:05 +00:00
def sonar_get_distance(self, id: int) -> float:
2022-10-28 07:14:00 +00:00
return self.__sonar_distances[id]
2022-10-25 14:53:05 +00:00
2022-10-28 07:14:00 +00:00
def __sonar_callback(self, data):
2022-10-25 14:53:05 +00:00
pin = data[1]
distance = data[2]
2022-10-28 07:14:00 +00:00
sonar_id = self.__sonar_trig_pins.index(pin)
self.__sonar_distances[sonar_id] = distance
def get_brightness_pct(self) -> float:
return self.__brightness
2022-10-25 14:53:05 +00:00
def get_temperature(self) -> float:
2022-10-28 07:14:00 +00:00
return self.__temperature
2022-10-25 14:53:05 +00:00
def get_humidity_pct(self) -> float:
2022-10-28 07:14:00 +00:00
return self.__humidity
def __light_sensor_callback(self, data):
self.__brightness = data[2]
2022-10-25 14:53:05 +00:00
2022-10-28 07:14:00 +00:00
def __dht22_callback(self, data):
2022-10-25 14:53:05 +00:00
humidity = data[3]
temperature = data[4]
2022-10-28 07:14:00 +00:00
self.__humidity = humidity
self.__temperature = temperature