import time
import gc
from weight_sensor import WeightSensor
from height_sensor import HeightSensor
from bmi import BMICalculator
from display import Display
def main():
gc.collect()
print("[MAIN] Boot...")
# --- Init ---
disp = Display()
weight = WeightSensor()
height = HeightSensor()
bmi = BMICalculator()
disp.show_status("System ready!", "Set slider:", "Weight & HC-SR04")
time.sleep(2)
count = 0
while True:
try:
gc.collect()
count += 1
print(f"\n[MAIN] === Measurement #{count} ===")
# ── Body Weight ──────────────────────────────────
disp.show_measuring("Measuring weight...")
weight_kg = weight.get_stable_weight(samples=3)
print("[MAIN] Weight:", weight_kg, "kg")
if weight_kg is None:
disp.show_error("Weight sensor failed")
time.sleep(3)
continue
disp.show_weight_only(weight_kg)
time.sleep(1)
# ── Body Height ─────────────────────────────────
disp.show_measuring("Measuring height...")
height_cm = height.get_stable_height(samples=3)
print("[MAIN] Height:", height_cm, "cm")
if height_cm is None:
disp.show_error("Height sensor failed")
time.sleep(3)
continue
# ── Calculate & Display ───────────────────────────
result = bmi.analyze(weight_kg, height_cm)
print("[MAIN] BMI:", result['bmi'], "→", result['nutrition_status'])
disp.show_result(result)
time.sleep(8)
except KeyboardInterrupt:
print("\n[MAIN] Stop.")
disp.show_status("Program", "stopped.", "")
break
except Exception as e:
print("[MAIN] Error:", e)
disp.show_error(str(e))
time.sleep(4)
main()