/
nkarbofos
/
PressAutomation
Обзор
Документация
Войти
/
nkarbofos
/
PressAutomation
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
code/PressAutomationCode/src/main.cpp
181 строка
5 KB
Nikita
fix: minor fixes, added params to display
25 июн 2026, 13:58
25 июн 2026, 13:58
ab77611
Код
Авторство
О чём код?
#include <Arduino.h> #include "app_state.h" #include "config.h" #include "display.h" #include "encoder.h" #include "interrupts.h" #include "motor.h" static const char* stateToString(SystemState s) { switch (s) { case OFF: return "OFF"; case VALVE_CLOSE: return "VALVE CLOSE"; case PRESS_DOWN: return "PRESS DOWN"; case VALVE_OPEN: return "VALVE OPEN"; case PAUSED: return "PAUSED"; } return "?"; } void setup() { Serial.begin(115200); pinMode(LIMIT_SWITCH_PIN, INPUT_PULLUP); pinMode(BUTTON_PIN, INPUT_PULLUP); initMotors(); potInit(); attachInputInterrupts(); initDisplay(); Serial.println("Setup ready"); } bool prev_tap = true; bool prev_held = true; bool prev_btn = true; bool prev_held_outdated = false; void update_prev(bool tap, bool held, bool btn, bool held_outdated) { if (prev_tap != tap || prev_held != held || prev_btn != btn || prev_held_outdated != held_outdated) { Serial.print("tap "); Serial.print(tap); Serial.print("\theld "); Serial.print(held); Serial.print("\tbtn "); Serial.print(btn); Serial.print("\theld_outdated "); Serial.println(held_outdated); prev_tap = tap; prev_held = held; prev_btn = btn; prev_held_outdated = held_outdated; } } void loop() { static SystemState lastLoggedState = (SystemState)-1; if (currentState != lastLoggedState) { Serial.print("[LOOP] STATE -> "); Serial.println(stateToString(currentState)); lastLoggedState = currentState; } potUpdate(); updateButton(); updateDisplayIfNeeded(); bool tap = isButtonTapPending(); bool held = isButtonHoldActive(); bool btn = isButtonHeld(); bool held_outdated = isButtonHoldOutdated(); if (isLimitHeld() && currentState != VALVE_OPEN) { disableMotors(); currentState = VALVE_OPEN; motor2StepsDone = 0; updateStatus("VALVE OPEN"); } switch (currentState) { case OFF: if (tap || (held && !prev_held)) { consumeButtonTap(); currentState = VALVE_CLOSE; motor2StepsDone = 0; updateStatus("VALVE CLOSE"); } break; case VALVE_CLOSE: { if (tap) { consumeButtonTap(); stateBeforePause = currentState; currentState = PAUSED; disableMotors(); updateStatus("PAUSED"); break; } if (held && !prev_held) { currentState = OFF; disableMotors(); updateStatus("OFF"); break; } int remaining = MOTOR2_STEPS_A - motor2StepsDone; if (btn && remaining <= 0) { remaining = 50; } if (remaining > 0) { int done = rotateMotorSteps(STEP_2, DIR_2, EN_2, remaining, false, true); motor2StepsDone += done; } if (motor2StepsDone >= MOTOR2_STEPS_A) { if (!btn) { currentState = PRESS_DOWN; motor2StepsDone = 0; updateStatus("PRESS DOWN"); } } break; } case PRESS_DOWN: { if (tap) { consumeButtonTap(); stateBeforePause = currentState; currentState = PAUSED; disableMotors(); updateStatus("PAUSED"); break; } if (held && !prev_held) { currentState = VALVE_OPEN; motor2StepsDone = 0; updateStatus("VALVE OPEN"); break; } rotateMotorSteps(STEP_1, DIR_1, EN_1, STEPS_PER_REV, true, false); break; } case VALVE_OPEN: { limitTriggered = false; bool limitPressed = isLimitHeld(); int remaining = MOTOR2_STEPS_A - motor2StepsDone; if (limitPressed && remaining <= 0) { remaining = 50; } if (remaining > 0) { int done = rotateMotorSteps(STEP_2, DIR_2, EN_2, remaining, true, true); motor2StepsDone += done; } if (motor2StepsDone >= MOTOR2_STEPS_A && !limitPressed) { currentState = OFF; motor2StepsDone = 0; disableMotors(); updateStatus("OFF"); } break; } case PAUSED: { if (tap) { consumeButtonTap(); currentState = stateBeforePause; updateStatus(stateToString(currentState)); break; } if (held) { currentState = VALVE_OPEN; motor2StepsDone = 0; updateStatus("VALVE OPEN"); break; } break; } } update_prev(tap, held, btn, held_outdated); }