Skip to content

Commit

Permalink
fix animation stability && add tolerance mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
mc-cat-tty committed Jan 5, 2023
1 parent b8bb72b commit 596c76c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
14 changes: 11 additions & 3 deletions lib/hal/rf/rf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,25 @@ namespace hal::rf {
std::vector<uint8_t> sequence;
uint8_t currentMatch = 0;
float currentDuty;
uint8_t tolerance;

public:
RxSequence(
RxPwm receiver,
std::initializer_list<uint8_t> sequence) :
std::initializer_list<uint8_t> sequence,
uint8_t tolerance) :
receiver(receiver),
sequence(sequence) { };
sequence(sequence),
tolerance(tolerance) { };

[[nodiscard]] inline bool rcvdSequenceAsync() {
long currentDutyInt = lroundf(currentDuty * 10.f);

if (receiver.getDutyAsync(currentDuty)) {
if (sequence[currentMatch] != lroundf(currentDuty * 10.f)) {
if (
currentDutyInt < sequence[currentMatch] - tolerance ||
currentDutyInt > sequence[currentMatch] + tolerance
) {
currentMatch = 0;
return false;
}
Expand Down
12 changes: 7 additions & 5 deletions src/main_rx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ void app_main() {

auto rxSequence = RxSequence(
rx,
{1, 3, 5}
{1, 3, 5},
1
);

const auto ledsConfig = (gpio_config_t) {
Expand All @@ -69,15 +70,16 @@ void app_main() {
Led(GPIO_NUM_26, ledsConfig),
};

auto spinnerFw = SpinnerForwardAnimation(ledRingDemux);
auto animator = Animator(spinnerFw, 200_ms);
logger.log(mod, ESP_LOG_DEBUG, "First animation run");

for (EVER) {
if (rxSequence.rcvdSequenceAsync()) {
logger.log(mod, ESP_LOG_INFO, "Sequence matched");
spinnerFw = SpinnerForwardAnimation(ledRingDemux);
animator = Animator(spinnerFw, 200_ms);
auto spinnerFw = new SpinnerForwardAnimation(ledRingDemux);
new Animator(
*spinnerFw,
200_ms
);
}

vTaskDelay(pdMS_TO_TICKS(1_s));
Expand Down

0 comments on commit 596c76c

Please sign in to comment.