Глобальная переделка. тест сделан по длине импульса и заданной частоте шим, а не меандру

This commit is contained in:
2026-08-12 18:10:05 +03:00
parent 1db89fca79
commit a17e8962b4
19 changed files with 958 additions and 642 deletions

View File

@@ -1,15 +1,15 @@
#pragma once
#include <Arduino.h>
#include <esp_idf_version.h>
#include <driver/gpio.h>
#include "Config.h"
#include "Core.h"
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
#define OPTICAL_USE_RMT_DMA 1
#include <driver/rmt_rx.h>
#if CONFIG_IDF_TARGET_ESP32S3 && ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
#define OPTICAL_USE_MCPWM_CAPTURE 1
#include <driver/mcpwm_cap.h>
#else
#define OPTICAL_USE_RMT_DMA 0
#include <driver/gpio.h>
#define OPTICAL_USE_MCPWM_CAPTURE 0
#endif
class PulseReceiver {
@@ -19,48 +19,46 @@ class PulseReceiver {
void stop();
void resetStream();
size_t readPeriods(PulsePeriod *periods, size_t capacity, TickType_t waitTicks = 0);
bool overflowed();
uint32_t takeDroppedItems();
uint32_t tickHz() const;
uint32_t pulseTickHz() const { return tickHz(); }
uint32_t plannedTickHz(uint32_t expectedHz, float expectedDutyPct) const;
uint16_t receiveChunkSymbols() const { return receiveChunkSymbols_; }
bool highRateBackend() const {
#if OPTICAL_USE_RMT_DMA && CONFIG_IDF_TARGET_ESP32S3
return true;
#else
return false;
#endif
uint32_t plannedPulseTickHz(uint32_t expectedHz, float expectedDutyPct) const {
return plannedTickHz(expectedHz, expectedDutyPct);
}
uint16_t receiveChunkSymbols() const { return 1; }
bool highRateBackend() const { return OPTICAL_USE_MCPWM_CAPTURE; }
private:
struct Edge { uint32_t tick; uint8_t rising; };
struct TimedEdge { uint64_t tick; bool rising; };
bool consumeEdge(const Edge &edge, PulsePeriod &period);
#if OPTICAL_USE_RMT_DMA
static constexpr size_t BLOCK_SYMBOLS = RMT_MAX_RECEIVE_SYMBOLS;
struct SymbolBlock { uint16_t count; rmt_symbol_word_t symbols[BLOCK_SYMBOLS]; };
static bool IRAM_ATTR onRmt(rmt_channel_handle_t, const rmt_rx_done_event_data_t *, void *);
bool configureRmt(uint32_t resolutionHz);
bool nextRmtEdge(Edge &edge, TickType_t waitTicks);
rmt_channel_handle_t channel_ = nullptr;
bool nextOrderedEdge(Edge &edge, TickType_t waitTicks);
TimedEdge extendEdge(const Edge &edge);
#if OPTICAL_USE_MCPWM_CAPTURE
static bool IRAM_ATTR onCapture(mcpwm_cap_channel_handle_t,
const mcpwm_capture_event_data_t *, void *);
mcpwm_cap_timer_handle_t captureTimer_ = nullptr;
mcpwm_cap_channel_handle_t risingChannel_ = nullptr;
mcpwm_cap_channel_handle_t fallingChannel_ = nullptr;
uint32_t captureResolutionHz_ = 0;
rmt_symbol_word_t receiveBuffer_[RMT_MAX_RECEIVE_SYMBOLS];
uint16_t receiveChunkSymbols_ = 0;
SymbolBlock isrBlock_ = {};
SymbolBlock block_ = {};
uint16_t blockIndex_ = 0;
uint8_t phase_ = 0;
bool haveLevel_ = false;
bool level_ = false;
uint32_t rmtTick_ = 0;
#else
static void IRAM_ATTR onGpio(void *ctx);
uint32_t cpuTickHz_ = 0;
#endif
QueueHandle_t queue_ = nullptr;
volatile bool overflow_ = false;
Edge reorderEdge_ = {};
bool haveReorderEdge_ = false;
volatile uint32_t droppedItems_ = 0;
bool running_ = false;
bool haveRise_ = false, haveFall_ = false, haveRawTick_ = false;
volatile bool running_ = false;
uint32_t expectedHz_ = 0;
float expectedDutyPct_ = 50.0f;
bool polarityKnown_ = false, activeStartRising_ = false;
TimedEdge syncEdges_[3] = {};
uint8_t syncEdgeCount_ = 0;
bool waitingForActiveEnd_ = true;
uint64_t activeStart_ = 0, activeEnd_ = 0;
bool haveRawTick_ = false;
uint32_t lastRawTick_ = 0;
uint64_t tickEpoch_ = 0, rise_ = 0, fall_ = 0;
uint64_t tickEpoch_ = 0;
};