kuukar-rpi/kuukar/kuukar_sensors.py

41 lines
1.2 KiB
Python
Raw Normal View History

from telemetrix_rpi_pico import telemetrix_rpi_pico
2022-12-08 14:36:11 +00:00
from kuukar.kuukar_config import DHT22_PIN, SONAR_ADC_PIN, LIGHT_SENSOR_PIN
2022-10-25 14:53:05 +00:00
2022-10-23 06:49:59 +00:00
class sensors:
2022-12-08 14:36:11 +00:00
__sonar_distances = 0
2022-10-28 07:14:00 +00:00
__temperature = 0
__humidity = 0
__brightness = 0
2022-10-25 14:53:05 +00:00
2022-12-08 14:36:11 +00:00
def __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico) -> None:
2022-11-04 04:58:20 +00:00
self.mcu = mcu
2022-12-08 14:36:11 +00:00
self.mcu.set_pin_mode_analog_input(SONAR_ADC_PIN, 100, self.__sonar_callback)
2022-12-09 03:44:15 +00:00
self.mcu.set_pin_mode_dht(DHT22_PIN, self.__dht22_callback)
2022-12-08 15:08:05 +00:00
self.mcu.set_pin_mode_analog_input(LIGHT_SENSOR_PIN, 100, self.__light_sensor_callback)
2022-10-25 14:53:05 +00:00
2022-12-08 14:36:11 +00:00
def sonar_get_distance(self) -> float:
return self.__sonar_distances
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
distance = data[2]
2022-12-08 14:36:11 +00:00
self.__sonar_distances = distance
2022-10-28 07:14:00 +00:00
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):
2022-11-10 15:32:59 +00:00
self.__brightness = (4096-float(data[2]))/4096*100
2022-10-25 14:53:05 +00:00
2022-10-28 07:14:00 +00:00
def __dht22_callback(self, data):
2022-12-09 03:45:59 +00:00
humidity = data[2]
temperature = data[3]
2022-10-28 07:14:00 +00:00
self.__humidity = humidity
self.__temperature = temperature