refactor config logic

This commit is contained in:
OMGeeky
2025-05-07 22:07:25 +02:00
parent 0fb0566adc
commit c5e02c1d73
15 changed files with 259 additions and 225 deletions

View File

@@ -7,21 +7,28 @@ This example demonstrates how to:
3. Wake up and read sensor data when the button is pressed
4. Display the data on an OLED screen
"""
import time
import sys
import os
# Add the src directory to the Python path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from src.esp_sensors.oled_display import OLEDDisplay
from src.esp_sensors.dht22 import DHT22Sensor
from src.esp_sensors.config import load_config, get_sensor_config, get_display_config, get_button_config
from src.esp_sensors.config import (
load_config,
get_sensor_config,
get_display_config,
get_button_config,
)
# Import hardware-specific modules if available (for ESP32/ESP8266)
try:
from machine import Pin, deepsleep
import esp32
SIMULATION = False
except ImportError:
# Simulation mode for development on non-ESP hardware
@@ -31,10 +38,12 @@ except ImportError:
def simulate_button_press():
"""Simulate a button press in simulation mode."""
print("\nPress Enter to simulate a button press (or 'q' to quit, Ctrl+C to exit)...")
print(
"\nPress Enter to simulate a button press (or 'q' to quit, Ctrl+C to exit)..."
)
try:
user_input = input()
if user_input.lower() == 'q':
if user_input.lower() == "q":
return False
return True
except KeyboardInterrupt:
@@ -49,16 +58,14 @@ def main():
config = load_config()
# Initialize a DHT22 sensor using configuration
dht_sensor = DHT22Sensor(
config=config # Pass the loaded config
)
dht_sensor = DHT22Sensor(sensor_config=config) # Pass the loaded config
print(f"Initialized DHT22 sensor: {dht_sensor.name}, pin: {dht_sensor.pin}")
# Initialize an OLED display using configuration
display = OLEDDisplay(
config=config # Pass the loaded config
display = OLEDDisplay(config=config) # Pass the loaded config
print(
f"Initialized OLED display: {display.name}, size: {display.width}x{display.height}"
)
print(f"Initialized OLED display: {display.name}, size: {display.width}x{display.height}")
# Set up button using configuration
button_config = get_button_config("main_button", config)
@@ -88,7 +95,9 @@ def main():
# Go to light sleep mode to save power
# Wake up on pin change (button press)
print("Entering light sleep mode...")
esp32.wake_on_ext0(pin=button, level=0) # Wake on button press (low)
esp32.wake_on_ext0(
pin=button, level=0
) # Wake on button press (low)
esp32.light_sleep() # Light sleep preserves RAM but saves power
# When we get here, the button was pressed
@@ -105,13 +114,9 @@ def main():
name_str = f"Sensor: {dht_sensor.name}"
# Display values
display.display_values([
name_str,
temp_str,
hum_str,
time_str,
"Press button again"
])
display.display_values(
[name_str, temp_str, hum_str, time_str, "Press button again"]
)
# Print to console
print(f"Updated display with: {temp_str}, {hum_str}")

View File

@@ -9,15 +9,16 @@ Usage:
- Ensure config.json is properly set up with DHT22 sensor configuration
- The script will read temperature and humidity at the specified interval
"""
import time
import sys
import os
# Add the src directory to the Python path if needed
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
# Check if running on MicroPython
if sys.implementation.name == 'micropython':
if sys.implementation.name == "micropython":
from src.esp_sensors.dht22 import DHT22Sensor
from src.esp_sensors.config import load_config, get_sensor_config
@@ -31,7 +32,7 @@ if sys.implementation.name == 'micropython':
print(f"Sensor pin: {dht_config.get('pin')}")
# Initialize the sensor using configuration
sensor = DHT22Sensor(config=config)
sensor = DHT22Sensor(sensor_config=config)
print("Starting sensor readings. Press Ctrl+C to stop.")
@@ -79,7 +80,7 @@ else:
print(f"Sensor pin: {dht_config.get('pin')}")
# Initialize the sensor using configuration
sensor = DHT22Sensor(config=config)
sensor = DHT22Sensor(sensor_config=config)
print("Starting simulated sensor readings. Press Ctrl+C to stop.")

View File

@@ -2,12 +2,13 @@
Example usage of the OLED display with temperature and humidity sensors.
This example demonstrates how to use the configuration system to initialize sensors and displays.
"""
import time
import sys
import os
# Add the src directory to the Python path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from src.esp_sensors.oled_display import OLEDDisplay
from src.esp_sensors.dht22 import DHT22Sensor
@@ -31,10 +32,12 @@ def main():
# Initialize a DHT22 sensor with configuration
dht_sensor = DHT22Sensor(
sensor_type="dht22", # This will load config for this sensor type
config=config # Pass the loaded config
# This will load config for this sensor type
sensor_config=config, # Pass the loaded config
)
print(
f"Created DHT22 sensor: {dht_sensor.name}, pin: {dht_sensor.pin}, interval: {dht_sensor.interval}s"
)
print(f"Created DHT22 sensor: {dht_sensor.name}, pin: {dht_sensor.pin}, interval: {dht_sensor.interval}s")
# Method 2: Initialize with some parameters from config, others specified directly
print("\nMethod 2: Override some config parameters")
@@ -44,11 +47,12 @@ def main():
# These parameters will override the config values
name="Custom Display",
interval=1, # Update every second
# Other parameters will be loaded from config
config=config
config=config,
)
print(
f"Created OLED display: {display.name}, size: {display.width}x{display.height}, interval: {display.interval}s"
)
print(f"Created OLED display: {display.name}, size: {display.width}x{display.height}, interval: {display.interval}s")
# Display initialization message
display.clear()
@@ -72,13 +76,9 @@ def main():
name_str = f"Sensor: {dht_sensor.name}"
# Display values
display.display_values([
name_str,
temp_str,
hum_str,
time_str,
f"Demo ({i+1}/5)"
])
display.display_values(
[name_str, temp_str, hum_str, time_str, f"Demo ({i+1}/5)"]
)
# Print to console in simulation mode
print(f"Updated display with: {temp_str}, {hum_str}")