This should work!

This commit is contained in:
Daniël Koek 2024-03-29 12:58:29 +00:00
parent f39af8d499
commit f024e0dcd7
3 changed files with 322 additions and 25 deletions

View file

@ -0,0 +1,274 @@
#pragma once
#include <utility>
#include <vector>
#include "esphome/core/log.h"
namespace esphome {
namespace ebyte_lora {
static const char *const TAG = "ebyte_lora";
// check your data sheet to see what the values are, since each module does it diffrent
enum ENABLE_BYTE { ENABLED = 0b1, DISABLED = 0b0 };
enum AIR_DATA_RATE {
_2_4kb = 0b000,
_4_8kb = 0b011,
_9_6kb = 0b100,
_19_2kb = 0b101,
_38_4kb = 0b110,
_62_5kb = 0b111
};
enum UART_BPS_SPEED {
_1200 = 0b000,
_2400 = 0b001,
_4800 = 0b010,
_9600 = 0b011,
_19200 = 0b100,
_38400 = 0b101,
_57600 = 0b110,
_115200 = 0b111
};
enum UART_PARITY { _8N1 = 0b00, _8O1 = 0b01, _8E1 = 0b10 };
struct REG0 {
uint8_t air_data_rate : 3;
void air_data_rate_description_() {
switch (this->air_data_rate) {
case _2_4kb:
ESP_LOGD(TAG, "air_data_rate: 2.4kb");
break;
case _4_8kb:
ESP_LOGD(TAG, "air_data_rate: 4.8kb");
break;
case _9_6kb:
ESP_LOGD(TAG, "air_data_rate: 9.6kb");
break;
case _19_2kb:
ESP_LOGD(TAG, "air_data_rate: 19.2kb");
break;
case _38_4kb:
ESP_LOGD(TAG, "air_data_rate: 38.4kb");
break;
case _62_5kb:
ESP_LOGD(TAG, "air_data_rate: 62.5kb");
break;
default:
break;
}
}
uint8_t parity : 2;
void parity_description_() {
switch (this->parity) {
case _8N1:
ESP_LOGD(TAG, "uart_parity: 8N1");
break;
case _8O1:
ESP_LOGD(TAG, "uart_parity: 8O1");
break;
case _8E1:
ESP_LOGD(TAG, "uart_parity: 8E1");
break;
default:
break;
}
}
uint8_t uart_baud : 3;
void uart_baud_description_() {
switch (this->uart_baud) {
case _1200:
ESP_LOGD(TAG, "uart_baud: 1200");
break;
case _2400:
ESP_LOGD(TAG, "uart_baud: 2400");
break;
case _4800:
ESP_LOGD(TAG, "uart_baud: 4800");
break;
case _9600:
ESP_LOGD(TAG, "uart_baud: 9600");
break;
case _19200:
ESP_LOGD(TAG, "uart_baud: 19200");
break;
case _38400:
ESP_LOGD(TAG, "uart_baud: 38400");
break;
case _57600:
ESP_LOGD(TAG, "uart_baud: 57600");
break;
case _115200:
ESP_LOGD(TAG, "uart_baud: 115200");
break;
default:
break;
}
}
};
enum TRANSMISSION_POWER {
_DEFAULT_MAX = 0b00,
_LOWER = 0b01,
_EVEN_LOWER = 0b10,
_LOWEST = 0b11
};
enum SUB_PACKET_SETTING { _200b = 0b00, _128b = 0b01, _64b = 0b10, _32b = 0b11 };
// again in reverse order on the data sheet
struct REG1 {
uint8_t transmission_power : 2;
void transmission_power_description_() {
switch (this->transmission_power) {
case _DEFAULT_MAX:
ESP_LOGD(TAG, "transmission_power: default or max");
break;
case _LOWER:
ESP_LOGD(TAG, "transmission_power: lower");
break;
case _EVEN_LOWER:
ESP_LOGD(TAG, "transmission_power: even lower");
break;
case _LOWEST:
ESP_LOGD(TAG, "transmission_power: Lowest");
break;
default:
break;
}
}
uint8_t reserve : 3;
uint8_t rssi_noise : 1;
void rssi_noise_description_() {
switch (this->rssi_noise) {
case ENABLED:
ESP_LOGD(TAG, "rssi_noise: ENABLED");
break;
case DISABLED:
ESP_LOGD(TAG, "rssi_noise: DISABLED");
break;
default:
break;
}
}
uint8_t sub_packet : 2;
void sub_packet_description_() {
switch (this->sub_packet) {
case _200b:
ESP_LOGD(TAG, "sub_packet: 200 bytes");
break;
case _128b:
ESP_LOGD(TAG, "sub_packet: 128 bytes");
break;
case _64b:
ESP_LOGD(TAG, "sub_packet: 64 bytes");
break;
case _32b:
ESP_LOGD(TAG, "sub_packet: 32 bytes");
break;
default:
break;
}
}
};
enum TRANSMISSION_MODE { TRANSPARENT = 0b0, FIXED = 0b1 };
enum WOR_PERIOD {
_500 = 0b000,
_1000 = 0b001,
_1500 = 0b010,
_2000 = 0b011,
_2500 = 0b100,
_3000 = 0b101,
_3500 = 0b110,
_4000 = 0b111
};
// reverse order on the data sheet
struct REG3 {
uint8_t wor_period : 3;
void wor_period_description_() {
switch (this->wor_period) {
case _500:
ESP_LOGD(TAG, "wor_period: 500");
break;
case _1000:
ESP_LOGD(TAG, "wor_period: 1000");
break;
case _1500:
ESP_LOGD(TAG, "wor_period: 1500");
break;
case _2000:
ESP_LOGD(TAG, "wor_period: 2000");
break;
case _2500:
ESP_LOGD(TAG, "wor_period: 2500");
break;
case _3000:
ESP_LOGD(TAG, "wor_period: 3000");
break;
case _3500:
ESP_LOGD(TAG, "wor_period: 3500");
break;
case _4000:
ESP_LOGD(TAG, "wor_period: 4000");
break;
default:
break;
}
}
uint8_t reserve1 : 1;
uint8_t enable_lbt : 1;
void enable_lbt_description_() {
switch (this->enable_lbt) {
case ENABLED:
ESP_LOGD(TAG, "enable_lbt: ENABLED");
break;
case DISABLED:
ESP_LOGD(TAG, "enable_lbt: DISABLED");
break;
default:
break;
}
}
uint8_t reserve2 : 1;
uint8_t transmission_mode : 1;
void transmission_type_description_() {
switch (this->transmission_mode) {
case TRANSPARENT:
ESP_LOGD(TAG, "transmission_type: TRANSPARENT");
break;
case FIXED:
ESP_LOGD(TAG, "transmission_type: FIXED");
break;
default:
break;
}
}
uint8_t enable_rssi : 1;
void enable_rssi_description_() {
switch (this->enable_rssi) {
case ENABLED:
ESP_LOGD(TAG, "enable_rssi: ENABLED");
break;
case DISABLED:
ESP_LOGD(TAG, "enable_rssi: DISABLED");
break;
default:
break;
}
}
};
static const uint8_t OPERATING_FREQUENCY = 700;
struct RegisterConfig {
uint8_t command = 0;
uint8_t starting_address = 0;
uint8_t length = 0;
uint8_t addh = 0;
void addh_description_() { ESP_LOGD(TAG, "addh: %u", this->addh); }
uint8_t addl = 0;
void addl_description_() { ESP_LOGD(TAG, "addl: %u", this->addh); }
struct REG0 reg_0;
struct REG1 reg_1;
// reg2
uint8_t channel;
void channel_description_() { ESP_LOGD(TAG, "channel: %u", this->channel); }
struct REG3 reg_3;
uint8_t crypt_h;
uint8_t crypt_l;
};
} // namespace ebyte_lora
} // namespace esphome

View file

@ -1,17 +1,46 @@
#include "ebyte_lora.h"
#include "config.h"
namespace esphome {
namespace ebyte_lora {
static const uint8_t SWITCH_PUSH = 0x55;
static const uint8_t SWITCH_INFO = 0x66;
static const uint8_t PROGRAM_CONF = 0xC1;
void EbyteLoraComponent::setup() {
this->pin_aux_->setup();
this->pin_m0_->setup();
this->pin_m1_->setup();
set_mode_(MODE_0_NORMAL);
get_current_config_();
ESP_LOGD(TAG, "Setup success");
}
void EbyteLoraComponent::get_current_config_() {
set_mode_(CONFIGURATION);
uint8_t data[3] = {PROGRAM_CONF, 0x00, 0x08};
this->write_array(data, sizeof(data));
RegisterConfig buffer;
if (!this->available()) {
return;
}
if (read_array((uint8_t *) &buffer, sizeof(buffer))) {
ESP_LOGD(TAG, "Found config");
buffer.addh_description_();
buffer.addl_description_();
buffer.reg_0.air_data_rate_description_();
buffer.reg_0.uart_baud_description_();
buffer.reg_0.parity_description_();
buffer.reg_1.rssi_noise_description_();
buffer.reg_1.sub_packet_description_();
buffer.reg_1.transmission_power_description_();
buffer.channel_description_();
buffer.reg_3.enable_lbt_description_();
buffer.reg_3.wor_period_description_();
buffer.reg_3.enable_rssi_description_();
buffer.reg_3.transmission_type_description_();
set_mode_(NORMAL);
} else {
ESP_LOGW(TAG, "Junk on wire. Throwing away partial message");
}
}
ModeType EbyteLoraComponent::get_mode_() {
ModeType internalMode = MODE_INIT;
if (!EbyteLoraComponent::can_send_message_()) {
@ -22,19 +51,19 @@ ModeType EbyteLoraComponent::get_mode_() {
bool pin2 = this->pin_m1_->digital_read();
if (!pin1 && !pin2) {
ESP_LOGD(TAG, "MODE NORMAL!");
internalMode = MODE_0_NORMAL;
internalMode = NORMAL;
}
if (pin1 && !pin2) {
ESP_LOGD(TAG, "MODE WOR!");
internalMode = MODE_1_WOR_TRANSMITTER;
internalMode = WOR_SEND;
}
if (!pin1 && pin2) {
ESP_LOGD(TAG, "MODE WOR!");
internalMode = MODE_2_WOR_RECEIVER;
internalMode = WOR_RECEIVER;
}
if (pin1 && pin2) {
ESP_LOGD(TAG, "MODE Conf!");
internalMode = MODE_3_CONFIGURATION;
internalMode = CONFIGURATION;
}
if (internalMode != this->mode_) {
ESP_LOGD(TAG, "Modes are not equal, calling the set function!! , checked: %u, expected: %u", internalMode,
@ -51,28 +80,28 @@ void EbyteLoraComponent::set_mode_(ModeType mode) {
ESP_LOGD(TAG, "The M0 and M1 pins is not set, this mean that you are connect directly the pins as you need!");
} else {
switch (mode) {
case MODE_0_NORMAL:
case NORMAL:
// Mode 0 | normal operation
this->pin_m0_->digital_write(false);
this->pin_m1_->digital_write(false);
ESP_LOGD(TAG, "MODE NORMAL!");
break;
case MODE_1_WOR_TRANSMITTER:
case WOR_SEND:
this->pin_m0_->digital_write(true);
this->pin_m1_->digital_write(false);
ESP_LOGD(TAG, "MODE WOR!");
ESP_LOGD(TAG, "MODE WOR SEND!");
break;
case MODE_2_WOR_RECEIVER:
case WOR_RECEIVER:
// case MODE_2_PROGRAM:
this->pin_m0_->digital_write(false);
this->pin_m1_->digital_write(true);
ESP_LOGD(TAG, "MODE RECEIVING!");
break;
case MODE_3_CONFIGURATION:
case CONFIGURATION:
// Mode 3 | Setting operation
this->pin_m0_->digital_write(true);
this->pin_m1_->digital_write(true);
ESP_LOGD(TAG, "MODE SLEEP CONFIG!");
ESP_LOGD(TAG, "MODE SLEEP and CONFIG!");
break;
case MODE_INIT:
ESP_LOGD(TAG, "Don't call this!");
@ -104,6 +133,7 @@ bool EbyteLoraComponent::can_send_message_() {
return false;
}
}
void EbyteLoraComponent::setup_wait_response_(uint32_t timeout) {
if (this->starting_to_check_ != 0 || this->time_out_after_ != 0) {
ESP_LOGD(TAG, "Wait response already set!! %u", timeout);

View file

@ -11,21 +11,13 @@
namespace esphome {
namespace ebyte_lora {
static const char *const TAG = "ebyte_lora";
static const uint8_t MAX_SIZE_TX_PACKET = 200;
// the mode the receiver is in
enum ModeType {
MODE_0_NORMAL = 0,
MODE_0_TRANSMISSION = 0,
MODE_1_WOR_TRANSMITTER = 1,
MODE_1_WOR = 1,
MODE_2_WOR_RECEIVER = 2,
MODE_2_POWER_SAVING = 2,
MODE_3_CONFIGURATION = 3,
MODE_3_PROGRAM = 3,
MODE_3_SLEEP = 3,
MODE_INIT = 0xFF
};
enum ModeType { NORMAL = 0, WOR_SEND = 1, WOR_RECEIVER = 2, CONFIGURATION = 3, MODE_INIT = 0xFF };
// 1 byte, 8 bits in total
// note that the data sheets shows the order in reverse
// has to be defined first, will be implemented later
class EbyteLoraSwitch;
class EbyteLoraComponent : public PollingComponent, public uart::UARTDevice {
@ -52,6 +44,7 @@ class EbyteLoraComponent : public PollingComponent, public uart::UARTDevice {
// checks the aux port to see if it is done setting
void setup_wait_response_(uint32_t timeout = 1000);
bool can_send_message_();
void get_current_config_();
void send_switch_push_(uint8_t pin, bool value);
void send_switch_info_();