ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
listen_before_talk.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4
6
21 public:
22 static constexpr uint32_t DEFAULT_MAX_BUSY_MS = 2000;
23
24 void set_max_busy(uint32_t max_busy_ms) { this->max_busy_ms_ = max_busy_ms; }
25 uint32_t get_max_busy() const { return this->max_busy_ms_; }
26 void set_turnaround(uint32_t turnaround_ms) { this->turnaround_ms_ = turnaround_ms; }
27
30 bool may_send(bool busy, uint32_t now, uint32_t pass) {
31 if (!busy) {
32 this->busy_ = false;
33 return !this->turn_held_ || pass == this->sent_pass_ || now - this->sent_ms_ >= this->turnaround_ms_;
34 }
35 if (!this->busy_) {
36 this->busy_ = true;
37 this->busy_since_ms_ = now;
38 }
39 return now - this->busy_since_ms_ >= this->max_busy_ms_;
40 }
42 void end_busy() { this->busy_ = false; }
44 void sent(uint32_t now, uint32_t pass) {
45 this->turn_held_ = this->turnaround_ms_ != 0;
46 this->sent_ms_ = now;
47 this->sent_pass_ = pass;
48 }
50 void received() { this->turn_held_ = false; }
52 uint32_t busy_for(uint32_t now) const { return now - this->busy_since_ms_; }
53
54 protected:
56 uint32_t max_busy_ms_{DEFAULT_MAX_BUSY_MS};
60 bool busy_{false};
61 bool turn_held_{false};
62};
63
64} // namespace esphome::packet_interface
Decides whether a half-duplex radio link may send while its radio reports that it is receiving a pack...
void received()
A packet arrived, so the peer has taken its turn.
void end_busy()
Ends the busy period, when the radio is seen to be free other than by a send.
void sent(uint32_t now, uint32_t pass)
A packet was sent, ending at now, in the loop pass that started at pass.
uint32_t busy_for(uint32_t now) const
How long the current busy period has lasted at now.
bool may_send(bool busy, uint32_t now, uint32_t pass)
True if a packet may be sent at now, in the loop pass that started at pass; busy says whether the rad...
static void uint32_t