ESPHome 2025.12.1
Loading...
Searching...
No Matches
micronova.cpp
Go to the documentation of this file.
1#include "micronova.h"
2#include "esphome/core/log.h"
3
4namespace esphome::micronova {
5
6static const int STOVE_REPLY_DELAY = 60;
7static const uint8_t WRITE_BIT = 1 << 7; // 0x80
8
10 ESP_LOGCONFIG(TAG,
11 " Memory Location: %02X\n"
12 " Memory Address: %02X",
14}
15
20
33
35 ESP_LOGCONFIG(TAG, "MicroNova:");
36 if (this->enable_rx_pin_ != nullptr) {
37 LOG_PIN(" Enable RX Pin: ", this->enable_rx_pin_);
38 }
39}
40
42 ESP_LOGD(TAG, "Schedule listener update");
43 for (auto &mv_listener : this->micronova_listeners_) {
44 mv_listener->set_needs_update(true);
45 }
46}
47
49 // Only read one sensor that needs update per loop
50 // If STOVE_REPLY_DELAY time has passed since last loop()
51 // check for a reply from the stove
53 (millis() - this->current_transmission_.request_transmission_time > STOVE_REPLY_DELAY)) {
54 int stove_reply_value = this->read_stove_reply();
55 if (this->current_transmission_.initiating_listener != nullptr) {
58 }
60 return;
61 } else if (!this->current_transmission_.reply_pending) {
62 for (auto &mv_listener : this->micronova_listeners_) {
63 if (mv_listener->get_needs_update()) {
64 mv_listener->set_needs_update(false);
66 mv_listener->request_value_from_stove();
67 return;
68 }
69 }
70 }
71}
72
73void MicroNova::request_address(uint8_t location, uint8_t address, MicroNovaListener *listener) {
74 uint8_t write_data[2] = {0, 0};
75 uint8_t trash_rx;
76
77 if (this->reply_pending_mutex_.try_lock()) {
78 // clear rx buffer.
79 // Stove hickups may cause late replies in the rx
80 while (this->available()) {
81 this->read_byte(&trash_rx);
82 ESP_LOGW(TAG, "Reading excess byte 0x%02X", trash_rx);
83 }
84
85 write_data[0] = location;
86 write_data[1] = address;
87 ESP_LOGV(TAG, "Request from stove [%02X,%02X]", write_data[0], write_data[1]);
88
89 this->enable_rx_pin_->digital_write(true);
90 this->write_array(write_data, 2);
91 this->flush();
92 this->enable_rx_pin_->digital_write(false);
93
99 } else {
100 ESP_LOGE(TAG, "Reply is pending, skipping read request");
101 }
102}
103
105 uint8_t reply_data[2] = {0, 0};
106 uint8_t checksum = 0;
107
108 // assert enable_rx_pin is false
109 this->read_array(reply_data, 2);
110
112 ESP_LOGV(TAG, "Reply from stove [%02X,%02X]", reply_data[0], reply_data[1]);
113
115 (uint16_t) this->current_transmission_.memory_address + (uint16_t) reply_data[1]) &
116 0xFF;
117 if (reply_data[0] != checksum) {
118 ESP_LOGE(TAG, "Checksum missmatch! From [0x%02X:0x%02X] received [0x%02X,0x%02X]. Expected 0x%02X, got 0x%02X",
119 this->current_transmission_.memory_location, this->current_transmission_.memory_address, reply_data[0],
120 reply_data[1], checksum, reply_data[0]);
121 return -1;
122 }
123 return ((int) reply_data[1]);
124}
125
126void MicroNova::write_address(uint8_t location, uint8_t address, uint8_t data) {
127 uint8_t write_data[4] = {0, 0, 0, 0};
128 uint16_t checksum = 0;
129
130 if (this->reply_pending_mutex_.try_lock()) {
131 uint8_t write_location = location | WRITE_BIT;
132 write_data[0] = write_location;
133 write_data[1] = address;
134 write_data[2] = data;
135
136 checksum = ((uint16_t) write_data[0] + (uint16_t) write_data[1] + (uint16_t) write_data[2]) & 0xFF;
137 write_data[3] = checksum;
138
139 ESP_LOGV(TAG, "Write 4 bytes [%02X,%02X,%02X,%02X]", write_data[0], write_data[1], write_data[2], write_data[3]);
140
141 this->enable_rx_pin_->digital_write(true);
142 this->write_array(write_data, 4);
143 this->flush();
144 this->enable_rx_pin_->digital_write(false);
145
147 this->current_transmission_.memory_location = write_location;
151 } else {
152 ESP_LOGE(TAG, "Reply is pending, skipping write");
153 }
154}
155
156} // namespace esphome::micronova
uint8_t checksum
Definition bl0906.h:3
uint8_t address
Definition bl0906.h:4
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=0
virtual void digital_write(bool value)=0
void unlock()
Definition helpers.cpp:27
bool try_lock()
Definition helpers.cpp:26
std::vector< MicroNovaListener * > micronova_listeners_
Definition micronova.h:98
void write_address(uint8_t location, uint8_t address, uint8_t data)
MicroNovaSerialTransmission current_transmission_
Definition micronova.h:96
void request_address(uint8_t location, uint8_t address, MicroNovaListener *listener)
Definition micronova.cpp:73
virtual void process_value_from_stove(int value_from_stove)=0
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:38
bool read_byte(uint8_t *data)
Definition uart.h:34
void write_array(const uint8_t *data, size_t len)
Definition uart.h:26
@ FLAG_OUTPUT
Definition gpio.h:19
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:30