Maszyna stanow: przenosny NORMAL LEARN SINGLE z timeoutami

This commit is contained in:
czcionka committed 2026-10-27 17:49:34 +00:00
1 parent e2ffb8d8cf
commit 824b1dbf0e
2 files changed
+208

No files matched your search

+147
View File
@@ -0,0 +1,147 @@
#include "app_state.h"
#include "config.h"
AppState::AppState(const settings_t *settings, RadioSink *radio)
: settings_(settings), radio_(radio)
{
}
void AppState::begin(uint32_t now_ms, bool transmit)
{
set_mode(AppMode::NORMAL, WHEEL_FL, now_ms, transmit);
}
uint32_t AppState::interval_s(bool ignition) const
{
#if PIN_ACC >= 0
return ignition ? settings_->ignition_interval_s : settings_->parking_interval_s;
#else
(void)ignition;
return settings_->normal_interval_s;
#endif
}
bool AppState::transmit(uint8_t wheel)
{
bool ok = radio_ != NULL && radio_->send(wheel);
if (!ok) {
++error_count_;
}
return ok;
}
bool AppState::send_current()
{
return transmit(active_wheel_);
}
bool AppState::send_wheel(uint8_t wheel)
{
return wheel < WHEEL_COUNT && transmit(wheel);
}
bool AppState::send_all()
{
bool ok = true;
for (uint8_t i = 0; i < WHEEL_COUNT; ++i) {
ok = transmit(i) && ok;
}
return ok;
}
void AppState::schedule(uint32_t now_ms, bool immediate)
{
next_tx_ms_ = immediate ? now_ms : now_ms + LEARN_PERIOD_MS;
}
bool AppState::set_mode(AppMode mode, uint8_t wheel, uint32_t now_ms,
bool transmit_now)
{
if (wheel >= WHEEL_COUNT) {
return false;
}
mode_ = mode;
active_wheel_ = wheel;
mode_start_ms_ = now_ms;
wheel_start_ms_ = now_ms;
schedule(now_ms, transmit_now);
if (mode == AppMode::NORMAL) {
next_tx_ms_ = transmit_now ? now_ms : UINT32_MAX;
}
if (transmit_now && mode != AppMode::NORMAL) {
send_current();
schedule(now_ms, false);
} else if (transmit_now) {
send_all();
next_tx_ms_ = now_ms + settings_->normal_interval_s * 1000u;
}
return true;
}
bool AppState::next_wheel(uint32_t now_ms)
{
if (mode_ == AppMode::NORMAL) {
return false;
}
if (mode_ == AppMode::LEARN && active_wheel_ == WHEEL_RL) {
return set_mode(AppMode::NORMAL, WHEEL_FL, now_ms, false);
}
active_wheel_ = (uint8_t)((active_wheel_ + 1u) % WHEEL_COUNT);
wheel_start_ms_ = now_ms;
send_current();
schedule(now_ms, false);
return true;
}
void AppState::short_press(uint32_t now_ms)
{
if (mode_ == AppMode::NORMAL) {
send_all();
next_tx_ms_ = now_ms + settings_->normal_interval_s * 1000u;
} else {
next_wheel(now_ms);
}
}
void AppState::long_press(uint32_t now_ms)
{
if (mode_ == AppMode::LEARN) {
set_mode(AppMode::NORMAL, WHEEL_FL, now_ms, false);
} else {
set_mode(AppMode::LEARN, active_wheel_, now_ms);
}
}
void AppState::very_long_press(uint32_t now_ms)
{
if (mode_ == AppMode::SINGLE) {
set_mode(AppMode::NORMAL, WHEEL_FL, now_ms, false);
} else {
set_mode(AppMode::SINGLE, active_wheel_, now_ms);
}
}
void AppState::tick(uint32_t now_ms, bool ignition)
{
if (mode_ == AppMode::LEARN) {
if ((uint32_t)(now_ms - mode_start_ms_) >= LEARN_TOTAL_MS
|| (uint32_t)(now_ms - wheel_start_ms_) >= LEARN_WINDOW_MS) {
set_mode(AppMode::NORMAL, WHEEL_FL, now_ms, false);
return;
}
} else if (mode_ == AppMode::SINGLE
&& (uint32_t)(now_ms - mode_start_ms_) >= SINGLE_TIMEOUT_MS) {
set_mode(AppMode::NORMAL, WHEEL_FL, now_ms, false);
return;
}
if (next_tx_ms_ != UINT32_MAX && (int32_t)(now_ms - next_tx_ms_) >= 0) {
if (mode_ == AppMode::NORMAL) {
send_all();
next_tx_ms_ = now_ms + interval_s(ignition) * 1000u;
} else {
send_current();
schedule(now_ms, false);
}
}
}
+61
View File
@@ -0,0 +1,61 @@
/* Przenosna maszyna stanow NORMAL/LEARN/SINGLE (fake clock/radio w testach). */
#ifndef TPMS_APP_STATE_H
#define TPMS_APP_STATE_H
#include <stdint.h>
#include "settings.h"
enum class AppMode : uint8_t {
NORMAL = 0,
LEARN = 1,
SINGLE = 2,
};
class RadioSink {
public:
virtual ~RadioSink() = default;
virtual bool send(uint8_t wheel) = 0;
};
class AppState {
public:
AppState(const settings_t *settings, RadioSink *radio);
void begin(uint32_t now_ms, bool transmit = true);
void tick(uint32_t now_ms, bool ignition);
void short_press(uint32_t now_ms);
void long_press(uint32_t now_ms);
void very_long_press(uint32_t now_ms);
bool set_mode(AppMode mode, uint8_t wheel, uint32_t now_ms,
bool transmit = true);
bool next_wheel(uint32_t now_ms);
bool send_current();
bool send_wheel(uint8_t wheel);
bool send_all();
AppMode mode() const { return mode_; }
uint8_t active_wheel() const { return active_wheel_; }
uint32_t interval_s(bool ignition) const;
uint32_t last_error_count() const { return error_count_; }
private:
static const uint32_t LEARN_PERIOD_MS = 2000u;
static const uint32_t LEARN_WINDOW_MS = 60000u;
static const uint32_t LEARN_TOTAL_MS = 900000u;
static const uint32_t SINGLE_TIMEOUT_MS = 300000u;
const settings_t *settings_;
RadioSink *radio_;
AppMode mode_ = AppMode::NORMAL;
uint8_t active_wheel_ = WHEEL_FL;
uint32_t next_tx_ms_ = 0;
uint32_t mode_start_ms_ = 0;
uint32_t wheel_start_ms_ = 0;
uint32_t error_count_ = 0;
bool transmit(uint8_t wheel);
void schedule(uint32_t now_ms, bool immediate);
};
#endif