fix(firmware): 由于 GPIO34,35 上两个按钮没有上拉电阻而无效,调整本机刷卡充电的启动逻辑

This commit is contained in:
2026-04-19 21:09:28 +08:00
parent c11c7f1a4a
commit 2d48724e37

View File

@@ -46,9 +46,6 @@ static unsigned long s_cc2_last_change_ms = 0;
static bool s_cc1_prev_plugged = false; static bool s_cc1_prev_plugged = false;
static bool s_cc2_prev_plugged = false; static bool s_cc2_prev_plugged = false;
static bool s_key1_prev = false;
static bool s_key2_prev = false;
static bool s_auth_in_progress = false; static bool s_auth_in_progress = false;
static bool s_auth_ok = false; static bool s_auth_ok = false;
static unsigned long s_auth_ok_at_ms = 0; static unsigned long s_auth_ok_at_ms = 0;
@@ -56,6 +53,18 @@ static String s_auth_id_tag;
static bool s_remote_start_accepted = false; static bool s_remote_start_accepted = false;
static bool authWindowValid();
static void clearAuthWait(const char *reason)
{
if (s_auth_ok || s_auth_id_tag.length() > 0)
{
Serial.printf("[main] Clear pending authorization: %s\n", reason);
}
s_auth_ok = false;
s_auth_ok_at_ms = 0;
s_auth_id_tag = "";
}
uint8_t mac[6]; uint8_t mac[6];
char cpSerial[13]; char cpSerial[13];
@@ -118,7 +127,20 @@ static void updateConnectorPluggedState()
} }
if ((now - s_cc1_last_change_ms) >= CC_DEBOUNCE_MS) if ((now - s_cc1_last_change_ms) >= CC_DEBOUNCE_MS)
{ {
bool cc1_plugged_old = s_cc1_plugged;
s_cc1_plugged = s_cc1_raw_last; s_cc1_plugged = s_cc1_raw_last;
// If connector1 just connected and auth window is valid, try to start
if (!cc1_plugged_old && s_cc1_plugged && s_auth_ok && (millis() - s_auth_ok_at_ms) <= AUTH_WINDOW_MS && s_auth_id_tag.length() > 0 && isConnectorIdle(1))
{
Serial.printf("[main] Connector 1 plugged in, auto-starting with idTag %s\n", s_auth_id_tag.c_str());
auto tx = beginTransaction_authorized(s_auth_id_tag.c_str(), nullptr, 1);
if (tx != nullptr)
{
s_auth_ok = false;
s_auth_id_tag = "";
}
}
} }
const bool cc2_raw = (digitalRead(PIN_CC2) == HIGH); const bool cc2_raw = (digitalRead(PIN_CC2) == HIGH);
@@ -129,7 +151,20 @@ static void updateConnectorPluggedState()
} }
if ((now - s_cc2_last_change_ms) >= CC_DEBOUNCE_MS) if ((now - s_cc2_last_change_ms) >= CC_DEBOUNCE_MS)
{ {
bool cc2_plugged_old = s_cc2_plugged;
s_cc2_plugged = s_cc2_raw_last; s_cc2_plugged = s_cc2_raw_last;
// If connector2 just connected and auth window is valid, try to start
if (!cc2_plugged_old && s_cc2_plugged && s_auth_ok && (millis() - s_auth_ok_at_ms) <= AUTH_WINDOW_MS && s_auth_id_tag.length() > 0 && isConnectorIdle(2))
{
Serial.printf("[main] Connector 2 plugged in, auto-starting with idTag %s\n", s_auth_id_tag.c_str());
auto tx = beginTransaction_authorized(s_auth_id_tag.c_str(), nullptr, 2);
if (tx != nullptr)
{
s_auth_ok = false;
s_auth_id_tag = "";
}
}
} }
} }
@@ -182,9 +217,8 @@ static void requestAuthorizeByCard(const String &idTag)
return; return;
} }
clearAuthWait("new card swiped");
s_auth_in_progress = true; s_auth_in_progress = true;
s_auth_ok = false;
s_auth_id_tag = "";
Serial.printf("[main] Authorize idTag: %s\n", idTag.c_str()); Serial.printf("[main] Authorize idTag: %s\n", idTag.c_str());
authorize( authorize(
@@ -199,19 +233,40 @@ static void requestAuthorizeByCard(const String &idTag)
s_auth_ok_at_ms = millis(); s_auth_ok_at_ms = millis();
s_auth_id_tag = idTag; s_auth_id_tag = idTag;
Serial.printf("[main] Authorize accepted for idTag %s\n", idTag.c_str()); Serial.printf("[main] Authorize accepted for idTag %s\n", idTag.c_str());
// Check if there's already a connector plugged in; if so, start immediately
unsigned int targetConnector = 0;
if (s_cc1_plugged && isConnectorIdle(1) && isOperative(1))
{
targetConnector = 1;
}
else if (s_cc2_plugged && isConnectorIdle(2) && isOperative(2))
{
targetConnector = 2;
}
if (targetConnector > 0)
{
// Immediately start the transaction on the plugged connector
Serial.printf("[main] Connector %u is plugged in, auto-starting transaction\n", targetConnector);
auto tx = beginTransaction_authorized(idTag.c_str(), nullptr, targetConnector);
if (tx != nullptr)
{
clearAuthWait("transaction started");
}
}
// Otherwise, wait for a connector to be plugged in
} }
else else
{ {
s_auth_ok = false; clearAuthWait("authorize rejected");
s_auth_id_tag = "";
Serial.printf("[main] Authorize rejected, status=%s\n", status); Serial.printf("[main] Authorize rejected, status=%s\n", status);
} }
}, },
[]() []()
{ {
s_auth_in_progress = false; s_auth_in_progress = false;
s_auth_ok = false; clearAuthWait("authorize aborted");
s_auth_id_tag = "";
Serial.println("[main] Authorize aborted"); Serial.println("[main] Authorize aborted");
}); });
} }
@@ -240,49 +295,12 @@ static void pollRfidCard()
requestAuthorizeByCard(idTag); requestAuthorizeByCard(idTag);
} }
static void tryStartByKey(unsigned int connectorId) static void expireAuthWaitIfNeeded()
{ {
if (!authWindowValid()) if (s_auth_ok && s_auth_id_tag.length() > 0 && (millis() - s_auth_ok_at_ms) > AUTH_WINDOW_MS)
{ {
Serial.println("[main] No valid authorization window. Swipe card first."); clearAuthWait("authorization timeout");
return;
} }
if (!isConnectorStartReady(connectorId))
{
Serial.printf("[main] Connector %u not ready for start (needs idle + operative + plugged).\n", connectorId);
return;
}
auto tx = beginTransaction_authorized(s_auth_id_tag.c_str(), nullptr, connectorId);
if (tx)
{
Serial.printf("[main] Local start accepted on connector %u for idTag %s\n", connectorId, s_auth_id_tag.c_str());
s_auth_ok = false;
s_auth_id_tag = "";
}
else
{
Serial.printf("[main] Local start failed on connector %u\n", connectorId);
}
}
static void pollStartKeys()
{
const bool key1 = (digitalRead(PIN_KEY1) == HIGH);
const bool key2 = (digitalRead(PIN_KEY2) == HIGH);
if (key1 && !s_key1_prev)
{
tryStartByKey(1);
}
if (key2 && !s_key2_prev)
{
tryStartByKey(2);
}
s_key1_prev = key1;
s_key2_prev = key2;
} }
/* LED Control Functions */ /* LED Control Functions */
@@ -400,8 +418,6 @@ void setup()
// Initialize CC switches (input) and panel LEDs (low-active output) // Initialize CC switches (input) and panel LEDs (low-active output)
pinMode(PIN_CC1, INPUT); pinMode(PIN_CC1, INPUT);
pinMode(PIN_CC2, INPUT); pinMode(PIN_CC2, INPUT);
pinMode(PIN_KEY1, INPUT);
pinMode(PIN_KEY2, INPUT);
pinMode(PIN_LED1, OUTPUT); pinMode(PIN_LED1, OUTPUT);
pinMode(PIN_LED2, OUTPUT); pinMode(PIN_LED2, OUTPUT);
pinMode(PIN_RELAY1, OUTPUT); pinMode(PIN_RELAY1, OUTPUT);
@@ -756,7 +772,7 @@ void loop()
updateConnectorPluggedState(); updateConnectorPluggedState();
stopIfUnplugged(); stopIfUnplugged();
pollRfidCard(); pollRfidCard();
pollStartKeys(); expireAuthWaitIfNeeded();
mg_mgr_poll(&mgr, 10); mg_mgr_poll(&mgr, 10);
mocpp_loop(); mocpp_loop();