mirror of
https://github.com/esphome/esphome.git
synced 2024-11-28 01:34:18 +01:00
better now
This commit is contained in:
parent
4a1e9adbab
commit
a27a490b2a
2 changed files with 77 additions and 83 deletions
|
@ -14,11 +14,6 @@ void IRAM_ATTR HOT EbyteAuxStore::gpio_intr(EbyteAuxStore *arg) {
|
||||||
|
|
||||||
arg->last_interrupt = now;
|
arg->last_interrupt = now;
|
||||||
}
|
}
|
||||||
union FuData {
|
|
||||||
uint32_t u32;
|
|
||||||
float f32;
|
|
||||||
};
|
|
||||||
|
|
||||||
// when this is called it is asking peers to say something about repeater
|
// when this is called it is asking peers to say something about repeater
|
||||||
static const uint8_t REQUEST_REPEATER_INFO = 0x88;
|
static const uint8_t REQUEST_REPEATER_INFO = 0x88;
|
||||||
static const uint8_t REPEATER_INFO = 0x99;
|
static const uint8_t REPEATER_INFO = 0x99;
|
||||||
|
@ -27,20 +22,6 @@ static const uint8_t REPEATER_KEY = 0x99;
|
||||||
static const uint8_t PROGRAM_CONF = 0xC1;
|
static const uint8_t PROGRAM_CONF = 0xC1;
|
||||||
static const uint8_t BINARY_SENSOR_KEY = 0x66;
|
static const uint8_t BINARY_SENSOR_KEY = 0x66;
|
||||||
static const uint8_t SENSOR_KEY = 0x77;
|
static const uint8_t SENSOR_KEY = 0x77;
|
||||||
|
|
||||||
static inline uint32_t get_uint32(uint8_t *&buf) {
|
|
||||||
uint32_t data = *buf++;
|
|
||||||
data += *buf++ << 8;
|
|
||||||
data += *buf++ << 16;
|
|
||||||
data += *buf++ << 24;
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint16_t get_uint16(uint8_t *&buf) {
|
|
||||||
uint16_t data = *buf++;
|
|
||||||
data += *buf++ << 8;
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
bool EbyteLoraComponent::check_config_() {
|
bool EbyteLoraComponent::check_config_() {
|
||||||
bool success = true;
|
bool success = true;
|
||||||
if (this->current_config_.addh != this->expected_config_.addh) {
|
if (this->current_config_.addh != this->expected_config_.addh) {
|
||||||
|
@ -423,12 +404,7 @@ void EbyteLoraComponent::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG, " Normal mode");
|
ESP_LOGCONFIG(TAG, " Normal mode");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
void EbyteLoraComponent::process_(uint8_t *buf, const size_t len) {
|
void EbyteLoraComponent::process_(std::vector<uint8_t> data) {
|
||||||
uint8_t *start_ptr = buf;
|
|
||||||
uint8_t byte;
|
|
||||||
// -1 cause the last one is always RSSI
|
|
||||||
const uint8_t *end = buf + len - 1;
|
|
||||||
FuData rdata{};
|
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
auto &sensors = this->remote_sensors_[network_id_];
|
auto &sensors = this->remote_sensors_[network_id_];
|
||||||
#endif
|
#endif
|
||||||
|
@ -436,96 +412,114 @@ void EbyteLoraComponent::process_(uint8_t *buf, const size_t len) {
|
||||||
auto &binary_sensors = this->remote_binary_sensors_[network_id_];
|
auto &binary_sensors = this->remote_binary_sensors_[network_id_];
|
||||||
#endif
|
#endif
|
||||||
ESP_LOGD(TAG, "GOT new data to process");
|
ESP_LOGD(TAG, "GOT new data to process");
|
||||||
while (buf < end) {
|
uint8_t first_byte = data[0];
|
||||||
byte = *buf++;
|
// rssi is always the last one, except for when it is a program conf
|
||||||
if (byte == REQUEST_REPEATER_INFO) {
|
if (first_byte == REQUEST_REPEATER_INFO) {
|
||||||
ESP_LOGD(TAG, "Got request for repeater info from network id %u", buf[1]);
|
ESP_LOGD(TAG, "Got request for repeater info from network id %u", data[1]);
|
||||||
this->send_repeater_info_();
|
this->send_repeater_info_();
|
||||||
break;
|
}
|
||||||
}
|
if (first_byte == REPEATER_INFO) {
|
||||||
|
ESP_LOGD(TAG, "Got some repeater info from network %u setting rssi next", data[2]);
|
||||||
if (byte == REPEATER_INFO) {
|
}
|
||||||
ESP_LOGD(TAG, "Got some repeater info from network %u setting rssi next", buf[2]);
|
if (first_byte == PROGRAM_CONF) {
|
||||||
break;
|
ESP_LOGD(TAG, "GOT PROGRAM_CONF");
|
||||||
}
|
this->setup_conf_(data);
|
||||||
if (byte == PROGRAM_CONF) {
|
this->set_mode_(NORMAL);
|
||||||
ESP_LOGD(TAG, "GOT PROGRAM_CONF");
|
}
|
||||||
this->setup_conf_(buf);
|
// Do all the stuff if they are sensors
|
||||||
this->set_mode_(NORMAL);
|
if (first_byte == BINARY_SENSOR_KEY || first_byte == SENSOR_KEY) {
|
||||||
break;
|
for (size_t i = 0; i < data.size() - 1; i++) {
|
||||||
}
|
uint8_t key = data[i];
|
||||||
byte = *buf++;
|
uint32_t u32;
|
||||||
|
if (key == BINARY_SENSOR_KEY) {
|
||||||
// Do all the stuff if they are sensors
|
|
||||||
if (byte == BINARY_SENSOR_KEY || byte == SENSOR_KEY) {
|
|
||||||
if (byte == BINARY_SENSOR_KEY) {
|
|
||||||
// 1 byte for the length of the sensor name, one for the name, 1 for the data
|
// 1 byte for the length of the sensor name, one for the name, 1 for the data
|
||||||
if (end - buf < 3) {
|
if (data.size() - i < 3) {
|
||||||
return ESP_LOGV(TAG, "Binary sensor key requires at least 3 more bytes");
|
return ESP_LOGV(TAG, "Binary sensor key requires at least 3 more bytes");
|
||||||
}
|
}
|
||||||
// grab the first bite, that is the state, there will be 2 at least two more
|
// grab the first bite, that is the state, there will be 2 at least two more
|
||||||
rdata.u32 = *buf++;
|
i++;
|
||||||
} else if (byte == SENSOR_KEY) {
|
u32 = data[i];
|
||||||
|
} else if (key == SENSOR_KEY) {
|
||||||
// same as before but we need 4 for sensor data
|
// same as before but we need 4 for sensor data
|
||||||
if (end - buf < 6) {
|
if (data.size() - i < 6) {
|
||||||
return ESP_LOGV(TAG, "Sensor key requires at least 6 more bytes");
|
return ESP_LOGV(TAG, "Sensor key requires at least 6 more bytes");
|
||||||
}
|
}
|
||||||
// next 4 bytes are the int32
|
i++;
|
||||||
rdata.u32 = get_uint32(buf);
|
u32 = data[i];
|
||||||
|
i++;
|
||||||
|
u32 += data[i] << 8;
|
||||||
|
i++;
|
||||||
|
u32 += data[i] << 16;
|
||||||
|
i++;
|
||||||
|
u32 += data[i] << 24;
|
||||||
}
|
}
|
||||||
|
|
||||||
// key length for the sensor data
|
// key length for the sensor data
|
||||||
auto sensor_name_length = *buf++;
|
i++;
|
||||||
if (end - buf < sensor_name_length) {
|
auto sensor_name_length = data[i];
|
||||||
|
if (data.size() - i < sensor_name_length) {
|
||||||
return ESP_LOGV(TAG, "Name length of %u not available", sensor_name_length);
|
return ESP_LOGV(TAG, "Name length of %u not available", sensor_name_length);
|
||||||
}
|
}
|
||||||
// max length of sensors name
|
// max length of sensors name
|
||||||
char sensor_name[sensor_name_length]{};
|
char sensor_name[sensor_name_length]{};
|
||||||
// get the memory cleared and set
|
// get the memory cleared and set
|
||||||
memset(sensor_name, 0, sizeof sensor_name);
|
memset(sensor_name, 0, sizeof sensor_name);
|
||||||
// copy from buffer to sensor_name
|
for (size_t s = 0; s < sensor_name_length; s++) {
|
||||||
memcpy(sensor_name, buf, sensor_name_length);
|
// set each
|
||||||
ESP_LOGV(TAG, "Found sensor key %d, id %s, data %lX", byte, sensor_name, (unsigned long) rdata.u32);
|
sensor_name[s] = data[s];
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_LOGV(TAG, "Found sensor key %d, id %s, data %lX", key, sensor_name, (unsigned long) u32);
|
||||||
// move the buffer to after sensor name length
|
// move the buffer to after sensor name length
|
||||||
buf += sensor_name_length;
|
i += sensor_name_length;
|
||||||
|
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
if (byte == SENSOR_KEY && sensors.count(sensor_name) != 0)
|
if (key == SENSOR_KEY && sensors.count(sensor_name) != 0)
|
||||||
sensors[sensor_name]->publish_state(rdata.f32);
|
sensors[sensor_name]->publish_state(u32);
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_BINARY_SENSOR
|
#ifdef USE_BINARY_SENSOR
|
||||||
if (byte == BINARY_SENSOR_KEY && binary_sensors.count(sensor_name) != 0)
|
if (key == BINARY_SENSOR_KEY && binary_sensors.count(sensor_name) != 0)
|
||||||
binary_sensors[sensor_name]->publish_state(rdata.u32 != 0);
|
binary_sensors[sensor_name]->publish_state(u32 != 0);
|
||||||
#endif
|
#endif
|
||||||
} else {
|
|
||||||
return ESP_LOGW(TAG, "Unknown key byte %X", byte);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return ESP_LOGW(TAG, "Unknown key byte %X", first_byte);
|
||||||
}
|
}
|
||||||
|
|
||||||
// RSSI is always found whenever it is not program info
|
// RSSI is always found whenever it is not program info
|
||||||
if (buf[0] != PROGRAM_CONF) {
|
if (first_byte != PROGRAM_CONF) {
|
||||||
|
float rssi = (data[data.size() - 1] / 255.0) * 100;
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
this->rssi_sensor_->publish_state((buf[sizeof(buf) - 1] / 255.0) * 100);
|
this->rssi_sensor_->publish_state(rssi);
|
||||||
#endif
|
#endif
|
||||||
ESP_LOGD(TAG, "RSSI: %f", (buf[sizeof(buf) - 1] / 255.0) * 100);
|
ESP_LOGD(TAG, "RSSI: %f", rssi);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
void EbyteLoraComponent::loop() {
|
void EbyteLoraComponent::loop() {
|
||||||
this->store_.can_send = !this->pin_aux_->digital_read();
|
this->store_.can_send = !this->pin_aux_->digital_read();
|
||||||
if (auto len = this->available()) {
|
std::vector<uint8_t> data;
|
||||||
uint8_t buf[len];
|
if (!this->available())
|
||||||
this->read_array(buf, len);
|
return;
|
||||||
if (this->repeater_enabled_) {
|
ESP_LOGD(TAG, "Reading serial");
|
||||||
this->repeat_message_(buf);
|
while (this->available()) {
|
||||||
}
|
uint8_t c;
|
||||||
this->process_(buf, len);
|
this->read_byte(&c);
|
||||||
|
data.push_back(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this->repeater_enabled_) {
|
||||||
|
this->repeat_message_(data);
|
||||||
|
}
|
||||||
|
this->process_(data);
|
||||||
|
|
||||||
if (this->resend_repeater_request_)
|
if (this->resend_repeater_request_)
|
||||||
this->request_repeater_info_();
|
this->request_repeater_info_();
|
||||||
if (this->updated_) {
|
if (this->updated_) {
|
||||||
this->send_data_(true);
|
this->send_data_(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void EbyteLoraComponent::setup_conf_(uint8_t const *conf) {
|
void EbyteLoraComponent::setup_conf_(std::vector<uint8_t> conf) {
|
||||||
ESP_LOGD(TAG, "Config set");
|
ESP_LOGD(TAG, "Config set");
|
||||||
this->current_config_.config_set = 1;
|
this->current_config_.config_set = 1;
|
||||||
// 3 is addh
|
// 3 is addh
|
||||||
|
@ -617,13 +611,13 @@ void EbyteLoraComponent::request_repeater_info_() {
|
||||||
ESP_LOGD(TAG, "Asking for repeater info");
|
ESP_LOGD(TAG, "Asking for repeater info");
|
||||||
this->write_array(data, sizeof(data));
|
this->write_array(data, sizeof(data));
|
||||||
}
|
}
|
||||||
void EbyteLoraComponent::repeat_message_(uint8_t *buf) {
|
void EbyteLoraComponent::repeat_message_(std::vector<uint8_t> data) {
|
||||||
ESP_LOGD(TAG, "Got some info that i need to repeat for network %u", buf[1]);
|
ESP_LOGD(TAG, "Got some info that i need to repeat for network %u", data[1]);
|
||||||
if (!this->store_.can_send) {
|
if (!this->store_.can_send) {
|
||||||
ESP_LOGD(TAG, "Can't sent it right now");
|
ESP_LOGD(TAG, "Can't sent it right now");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this->write_array(buf, sizeof(buf));
|
this->write_array(data.data(), data.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ebyte_lora
|
} // namespace ebyte_lora
|
||||||
|
|
|
@ -120,9 +120,9 @@ class EbyteLoraComponent : public PollingComponent, public uart::UARTDevice {
|
||||||
bool resend_repeater_request_{};
|
bool resend_repeater_request_{};
|
||||||
uint32_t repeater_request_recyle_time_{};
|
uint32_t repeater_request_recyle_time_{};
|
||||||
uint32_t last_key_time_{};
|
uint32_t last_key_time_{};
|
||||||
void setup_conf_(uint8_t const *conf);
|
void setup_conf_(std::vector<uint8_t> conf);
|
||||||
void process_(uint8_t *buf, size_t len);
|
void process_(std::vector<uint8_t> data);
|
||||||
void repeat_message_(uint8_t *buf);
|
void repeat_message_(std::vector<uint8_t> data);
|
||||||
// set if there is some sensor info available or it is in repeater mode
|
// set if there is some sensor info available or it is in repeater mode
|
||||||
bool should_send_{};
|
bool should_send_{};
|
||||||
// if set it will function as a repeater only
|
// if set it will function as a repeater only
|
||||||
|
|
Loading…
Reference in a new issue