ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
micronova.cpp
Go to the documentation of this file.
1#include "micronova.h"
3#include "esphome/core/log.h"
4
6
7ESPHOME_LOG_TAG(TAG, "micronova");
8static constexpr uint8_t STOVE_REPLY_SIZE = 2;
9static constexpr uint32_t STOVE_REPLY_TIMEOUT = 200; // ms
10static constexpr uint8_t WRITE_BIT = 1 << 7; // 0x80
11
12bool MicroNovaCommand::is_write() const { return this->memory_location & WRITE_BIT; }
13
15 ESP_LOGCONFIG(TAG,
16 " Memory Location: %02X\n"
17 " Memory Address: %02X",
19}
20
25
29
35
37 ESP_LOGCONFIG(TAG, "MicroNova:");
38 LOG_PIN(" Enable RX Pin: ", this->enable_rx_pin_);
39}
40
41#ifdef MICRONOVA_LISTENER_COUNT
43 this->listeners_.push_back(listener);
44 // Request initial value
45 this->queue_read_request(listener->get_memory_location(), listener->get_memory_address());
46}
47
49 ESP_LOGD(TAG, "Requesting update from all listeners");
50 for (auto *listener : this->listeners_) {
51 this->queue_read_request(listener->get_memory_location(), listener->get_memory_address());
52 }
53}
54#endif
55
57 // Check if we're processing a command and waiting for reply
58 if (this->reply_pending_) {
59 // Check if all reply bytes have arrived
60 if (this->available() >= STOVE_REPLY_SIZE) {
61#ifdef MICRONOVA_LISTENER_COUNT
62 int stove_reply_value = this->read_stove_reply_();
63 if (this->current_command_.is_write()) {
64 if (stove_reply_value == -1) {
65 ESP_LOGW(TAG, "Write to [0x%02X:0x%02X] may have failed (checksum mismatch in reply)",
66 this->current_command_.memory_location & ~WRITE_BIT, this->current_command_.memory_address);
67 }
68 } else {
69 // For READ commands, notify all listeners registered for this address
70 uint8_t loc = this->current_command_.memory_location;
71 uint8_t addr = this->current_command_.memory_address;
72 for (auto *listener : this->listeners_) {
73 if (listener->get_memory_location() == loc && listener->get_memory_address() == addr) {
74 listener->process_value_from_stove(stove_reply_value);
75 }
76 }
77 }
78#else
79 this->read_stove_reply_();
80#endif
81 this->reply_pending_ = false;
82 } else if (millis() - this->transmission_time_ > STOVE_REPLY_TIMEOUT) {
83 // Timeout - no reply received (buffer cleared before next command)
84 ESP_LOGW(TAG, "Timeout waiting for reply from [0x%02X:0x%02X], available: %d",
85 this->current_command_.memory_location, this->current_command_.memory_address, this->available());
86 this->reply_pending_ = false;
87 }
88 return;
89 }
90
91 // No reply pending - process next command (writes have priority over reads)
92#ifdef USE_MICRONOVA_WRITER
93 if (!this->write_queue_.empty()) {
94 this->current_command_ = this->write_queue_.front();
95 this->write_queue_.pop();
97 return;
98 }
99#endif
100#ifdef MICRONOVA_LISTENER_COUNT
101 if (!this->read_queue_.empty()) {
102 this->current_command_ = this->read_queue_.front();
103 this->read_queue_.pop();
104 this->send_current_command_();
105 }
106#endif
107}
108
109#ifdef MICRONOVA_LISTENER_COUNT
110void MicroNova::queue_read_request(uint8_t location, uint8_t address) {
111 // Check if this read is already queued
112 for (const auto &queued : this->read_queue_) {
113 if (queued.memory_location == location && queued.memory_address == address) {
114 ESP_LOGV(TAG, "Read [%02X,%02X] already queued, skipping", location, address);
115 return;
116 }
117 }
118
120 cmd.memory_location = location;
122 cmd.data = 0;
123
124 if (!this->read_queue_.push(cmd)) {
125 ESP_LOGW(TAG, "Read queue full, dropping read [%02X,%02X]", location, address);
126 return;
127 }
128 ESP_LOGV(TAG, "Queued read [%02X,%02X] (queue size: %u)", location, address, this->read_queue_.size());
129}
130#endif
131
133 uint8_t trash_rx;
134
135 // Clear rx buffer - stove hiccups may cause late replies in the rx
136 while (this->available()) {
137 this->read_byte(&trash_rx);
138 ESP_LOGW(TAG, "Reading excess byte 0x%02X", trash_rx);
139 }
140
141 uint8_t write_data[4] = {this->current_command_.memory_location, this->current_command_.memory_address, 0, 0};
142 size_t write_len;
143
144 if (this->current_command_.is_write()) {
145 write_len = 4;
146 write_data[2] = this->current_command_.data;
147 // calculate checksum
148 write_data[3] = write_data[0] + write_data[1] + write_data[2];
149 ESP_LOGV_HEX(TAG, write_data, sizeof(write_data), sizeof(write_data), "Sending write request [%s]");
150 } else {
151 write_len = 2;
152 ESP_LOGV(TAG, "Sending read request [%02X,%02X]", write_data[0], write_data[1]);
153 }
154
155 this->enable_rx_pin_->digital_write(true);
156 this->write_array(write_data, write_len);
157 this->flush();
158 this->enable_rx_pin_->digital_write(false);
159
160 this->transmission_time_ = millis();
161 this->reply_pending_ = true;
162}
163
165 uint8_t reply_data[2] = {0, 0};
166
167 this->read_array(reply_data, 2);
168
169 ESP_LOGV(TAG, "Reply from stove [%02X,%02X]", reply_data[0], reply_data[1]);
170
171 uint8_t checksum = this->current_command_.memory_location + this->current_command_.memory_address + reply_data[1];
172 if (reply_data[0] != checksum) {
173 ESP_LOGE(TAG, "Checksum mismatch! From [0x%02X:0x%02X] received [0x%02X,0x%02X]. Expected 0x%02X, got 0x%02X",
174 this->current_command_.memory_location, this->current_command_.memory_address, reply_data[0],
175 reply_data[1], checksum, reply_data[0]);
176 return -1;
177 }
178 return ((int) reply_data[1]);
179}
180
181#ifdef USE_MICRONOVA_WRITER
182bool MicroNova::queue_write_command(uint8_t location, uint8_t address, uint8_t data) {
184 cmd.memory_location = location | WRITE_BIT;
186 cmd.data = data;
187
188 // Check if a write to the same address is already queued - update data in-place
189 for (auto &queued : this->write_queue_) {
190 if (queued.memory_location == cmd.memory_location && queued.memory_address == cmd.memory_address) {
191 if (queued.data != cmd.data) {
192 ESP_LOGD(TAG, "Updating queued write [%02X,%02X] data 0x%02X -> 0x%02X", location, address, queued.data, data);
193 queued.data = cmd.data;
194 } else {
195 ESP_LOGV(TAG, "Write [%02X,%02X] with data 0x%02X already queued, skipping", location, address, data);
196 }
197 return true;
198 }
199 }
200
201 if (!this->write_queue_.push(cmd)) {
202 ESP_LOGW(TAG, "Write queue full, dropping command");
203 return false;
204 }
205 ESP_LOGD(TAG, "Queued write [%02X,%02X] (queue size: %u)", location, address, this->write_queue_.size());
206#ifdef MICRONOVA_LISTENER_COUNT
207 // Automatically queue sensor updates after write commands
209#endif
210 return true;
211}
212#endif
213
214} // namespace esphome::micronova
uint8_t checksum
Definition bl0906.h:3
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=0
virtual void digital_write(bool value)=0
MicroNovaCommand current_command_
Definition micronova.h:103
StaticVector< MicroNovaListener *, MICRONOVA_LISTENER_COUNT > listeners_
Definition micronova.h:108
bool reply_pending_
True if we are waiting for a reply from the stove.
Definition micronova.h:105
StaticRingBuffer< MicroNovaCommand, MICRONOVA_LISTENER_COUNT > read_queue_
Definition micronova.h:101
void queue_read_request(uint8_t location, uint8_t address)
Queue a read request to the stove (low priority - added at back) All listeners registered for this ad...
bool queue_write_command(uint8_t location, uint8_t address, uint8_t data)
Queue a write command to the stove (processed before reads)
uint32_t transmission_time_
Time when current command was sent.
Definition micronova.h:104
StaticRingBuffer< MicroNovaCommand, WRITE_QUEUE_SIZE > write_queue_
Definition micronova.h:98
void register_micronova_listener(MicroNovaListener *listener)
Definition micronova.cpp:42
UARTFlushResult flush()
Definition uart.h:96
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:43
bool read_byte(uint8_t *data)
Definition uart.h:39
void write_array(const uint8_t *data, size_t len)
Definition uart.h:30
ESPHOME_LOG_TAG(TAG, "micronova")
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t
Represents a command to be sent to the stove Write commands have the high bit (0x80) set in memory_lo...
Definition micronova.h:15
uint8_t data
Only used for write commands.
Definition micronova.h:18