Added cable/bluetooth multiplexing

This commit is contained in:
Mark Qvist 2022-10-30 18:35:53 +01:00
parent cf4ad22086
commit bea14dcf05
3 changed files with 148 additions and 119 deletions

View File

@ -62,6 +62,7 @@ char bt_devname[11];
void bt_connection_callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param){ void bt_connection_callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param){
if(event == ESP_SPP_SRV_OPEN_EVT) { if(event == ESP_SPP_SRV_OPEN_EVT) {
bt_state = BT_STATE_CONNECTED; bt_state = BT_STATE_CONNECTED;
cable_state = CABLE_STATE_DISCONNECTED;
} }
if(event == ESP_SPP_CLOSE_EVT ){ if(event == ESP_SPP_CLOSE_EVT ){

View File

@ -114,15 +114,15 @@ void lora_receive() {
} }
inline void kiss_write_packet() { inline void kiss_write_packet() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_DATA); serial_write(CMD_DATA);
for (uint16_t i = 0; i < read_len; i++) { for (uint16_t i = 0; i < read_len; i++) {
uint8_t byte = pbuf[i]; uint8_t byte = pbuf[i];
if (byte == FEND) { Serial.write(FESC); byte = TFEND; } if (byte == FEND) { serial_write(FESC); byte = TFEND; }
if (byte == FESC) { Serial.write(FESC); byte = TFESC; } if (byte == FESC) { serial_write(FESC); byte = TFESC; }
Serial.write(byte); serial_write(byte);
} }
Serial.write(FEND); serial_write(FEND);
read_len = 0; read_len = 0;
#if MCU_VARIANT == MCU_ESP32 #if MCU_VARIANT == MCU_ESP32
packet_ready = false; packet_ready = false;
@ -451,7 +451,7 @@ void serialCallback(uint8_t sbyte) {
if (frame_len == 0 && command == CMD_UNKNOWN) { if (frame_len == 0 && command == CMD_UNKNOWN) {
command = sbyte; command = sbyte;
} else if (command == CMD_DATA) { } else if (command == CMD_DATA) {
cable_state = CABLE_STATE_CONNECTED; if (bt_state != BT_STATE_CONNECTED) cable_state = CABLE_STATE_CONNECTED;
if (sbyte == FESC) { if (sbyte == FESC) {
ESCAPE = true; ESCAPE = true;
} else { } else {
@ -559,7 +559,7 @@ void serialCallback(uint8_t sbyte) {
last_snr_raw = 0x80; last_snr_raw = 0x80;
} }
} else if (command == CMD_RADIO_STATE) { } else if (command == CMD_RADIO_STATE) {
cable_state = CABLE_STATE_CONNECTED; if (bt_state != BT_STATE_CONNECTED) cable_state = CABLE_STATE_CONNECTED;
if (sbyte == 0xFF) { if (sbyte == 0xFF) {
kiss_indicate_radiostate(); kiss_indicate_radiostate();
} else if (sbyte == 0x00) { } else if (sbyte == 0x00) {
@ -584,7 +584,7 @@ void serialCallback(uint8_t sbyte) {
kiss_indicate_random(getRandom()); kiss_indicate_random(getRandom());
} else if (command == CMD_DETECT) { } else if (command == CMD_DETECT) {
if (sbyte == DETECT_REQ) { if (sbyte == DETECT_REQ) {
cable_state = CABLE_STATE_CONNECTED; if (bt_state != BT_STATE_CONNECTED) cable_state = CABLE_STATE_CONNECTED;
kiss_indicate_detect(); kiss_indicate_detect();
} }
} else if (command == CMD_PROMISC) { } else if (command == CMD_PROMISC) {
@ -895,7 +895,16 @@ void buffer_serial() {
serial_buffering = true; serial_buffering = true;
uint8_t c = 0; uint8_t c = 0;
while (c < MAX_CYCLES && Serial.available()) {
#if HAS_BLUETOOTH
while (
c < MAX_CYCLES &&
( (bt_state != BT_STATE_CONNECTED && Serial.available()) || (bt_state == BT_STATE_CONNECTED && SerialBT.available()) )
)
#else
while (c < MAX_CYCLES && Serial.available())
#endif
{
c++; c++;
#if MCU_VARIANT != MCU_ESP32 #if MCU_VARIANT != MCU_ESP32
@ -903,8 +912,14 @@ void buffer_serial() {
fifo_push_locked(&serialFIFO, Serial.read()); fifo_push_locked(&serialFIFO, Serial.read());
} }
#else #else
if (!fifo_isfull(&serialFIFO)) { if (HAS_BLUETOOTH && bt_state == BT_STATE_CONNECTED) {
fifo_push(&serialFIFO, Serial.read()); if (!fifo_isfull(&serialFIFO)) {
fifo_push(&serialFIFO, SerialBT.read());
}
} else {
if (!fifo_isfull(&serialFIFO)) {
fifo_push(&serialFIFO, Serial.read());
}
} }
#endif #endif
} }

View File

@ -456,220 +456,233 @@ int8_t led_standby_direction = 0;
#endif #endif
#endif #endif
void serial_write(uint8_t byte) {
#if HAS_BLUETOOTH
if (bt_state != BT_STATE_CONNECTED) {
Serial.write(byte);
} else {
Serial.printf("BT Echo: %02X\n", byte);
SerialBT.write(byte);
}
#else
Serial.write(byte);
#endif
}
void escapedSerialWrite(uint8_t byte) { void escapedSerialWrite(uint8_t byte) {
if (byte == FEND) { Serial.write(FESC); byte = TFEND; } if (byte == FEND) { serial_write(FESC); byte = TFEND; }
if (byte == FESC) { Serial.write(FESC); byte = TFESC; } if (byte == FESC) { serial_write(FESC); byte = TFESC; }
Serial.write(byte); serial_write(byte);
} }
void kiss_indicate_reset() { void kiss_indicate_reset() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_RESET); serial_write(CMD_RESET);
Serial.write(CMD_RESET_BYTE); serial_write(CMD_RESET_BYTE);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_error(uint8_t error_code) { void kiss_indicate_error(uint8_t error_code) {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_ERROR); serial_write(CMD_ERROR);
Serial.write(error_code); serial_write(error_code);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_radiostate() { void kiss_indicate_radiostate() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_RADIO_STATE); serial_write(CMD_RADIO_STATE);
Serial.write(radio_online); serial_write(radio_online);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_stat_rx() { void kiss_indicate_stat_rx() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_STAT_RX); serial_write(CMD_STAT_RX);
escapedSerialWrite(stat_rx>>24); escapedSerialWrite(stat_rx>>24);
escapedSerialWrite(stat_rx>>16); escapedSerialWrite(stat_rx>>16);
escapedSerialWrite(stat_rx>>8); escapedSerialWrite(stat_rx>>8);
escapedSerialWrite(stat_rx); escapedSerialWrite(stat_rx);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_stat_tx() { void kiss_indicate_stat_tx() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_STAT_TX); serial_write(CMD_STAT_TX);
escapedSerialWrite(stat_tx>>24); escapedSerialWrite(stat_tx>>24);
escapedSerialWrite(stat_tx>>16); escapedSerialWrite(stat_tx>>16);
escapedSerialWrite(stat_tx>>8); escapedSerialWrite(stat_tx>>8);
escapedSerialWrite(stat_tx); escapedSerialWrite(stat_tx);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_stat_rssi() { void kiss_indicate_stat_rssi() {
uint8_t packet_rssi_val = (uint8_t)(last_rssi+rssi_offset); uint8_t packet_rssi_val = (uint8_t)(last_rssi+rssi_offset);
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_STAT_RSSI); serial_write(CMD_STAT_RSSI);
escapedSerialWrite(packet_rssi_val); escapedSerialWrite(packet_rssi_val);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_stat_snr() { void kiss_indicate_stat_snr() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_STAT_SNR); serial_write(CMD_STAT_SNR);
escapedSerialWrite(last_snr_raw); escapedSerialWrite(last_snr_raw);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_radio_lock() { void kiss_indicate_radio_lock() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_RADIO_LOCK); serial_write(CMD_RADIO_LOCK);
Serial.write(radio_locked); serial_write(radio_locked);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_spreadingfactor() { void kiss_indicate_spreadingfactor() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_SF); serial_write(CMD_SF);
Serial.write((uint8_t)lora_sf); serial_write((uint8_t)lora_sf);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_codingrate() { void kiss_indicate_codingrate() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_CR); serial_write(CMD_CR);
Serial.write((uint8_t)lora_cr); serial_write((uint8_t)lora_cr);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_implicit_length() { void kiss_indicate_implicit_length() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_IMPLICIT); serial_write(CMD_IMPLICIT);
Serial.write(implicit_l); serial_write(implicit_l);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_txpower() { void kiss_indicate_txpower() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_TXPOWER); serial_write(CMD_TXPOWER);
Serial.write((uint8_t)lora_txp); serial_write((uint8_t)lora_txp);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_bandwidth() { void kiss_indicate_bandwidth() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_BANDWIDTH); serial_write(CMD_BANDWIDTH);
escapedSerialWrite(lora_bw>>24); escapedSerialWrite(lora_bw>>24);
escapedSerialWrite(lora_bw>>16); escapedSerialWrite(lora_bw>>16);
escapedSerialWrite(lora_bw>>8); escapedSerialWrite(lora_bw>>8);
escapedSerialWrite(lora_bw); escapedSerialWrite(lora_bw);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_frequency() { void kiss_indicate_frequency() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_FREQUENCY); serial_write(CMD_FREQUENCY);
escapedSerialWrite(lora_freq>>24); escapedSerialWrite(lora_freq>>24);
escapedSerialWrite(lora_freq>>16); escapedSerialWrite(lora_freq>>16);
escapedSerialWrite(lora_freq>>8); escapedSerialWrite(lora_freq>>8);
escapedSerialWrite(lora_freq); escapedSerialWrite(lora_freq);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_random(uint8_t byte) { void kiss_indicate_random(uint8_t byte) {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_RANDOM); serial_write(CMD_RANDOM);
Serial.write(byte); serial_write(byte);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_fbstate() { void kiss_indicate_fbstate() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_FB_EXT); serial_write(CMD_FB_EXT);
#if HAS_DISPLAY #if HAS_DISPLAY
if (disp_ext_fb) { if (disp_ext_fb) {
Serial.write(0x01); serial_write(0x01);
} else { } else {
Serial.write(0x00); serial_write(0x00);
} }
#else #else
Serial.write(0xFF) serial_write(0xFF)
#endif #endif
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_fb() { void kiss_indicate_fb() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_FB_READ); serial_write(CMD_FB_READ);
#if HAS_DISPLAY #if HAS_DISPLAY
for (int i = 0; i < 512; i++) { for (int i = 0; i < 512; i++) {
uint8_t byte = fb[i]; uint8_t byte = fb[i];
if (byte == FEND) { Serial.write(FESC); byte = TFEND; } if (byte == FEND) { serial_write(FESC); byte = TFEND; }
if (byte == FESC) { Serial.write(FESC); byte = TFESC; } if (byte == FESC) { serial_write(FESC); byte = TFESC; }
Serial.write(byte); serial_write(byte);
} }
#else #else
Serial.write(0xFF) serial_write(0xFF)
#endif #endif
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_ready() { void kiss_indicate_ready() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_READY); serial_write(CMD_READY);
Serial.write(0x01); serial_write(0x01);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_not_ready() { void kiss_indicate_not_ready() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_READY); serial_write(CMD_READY);
Serial.write(0x00); serial_write(0x00);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_promisc() { void kiss_indicate_promisc() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_PROMISC); serial_write(CMD_PROMISC);
if (promisc) { if (promisc) {
Serial.write(0x01); serial_write(0x01);
} else { } else {
Serial.write(0x00); serial_write(0x00);
} }
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_detect() { void kiss_indicate_detect() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_DETECT); serial_write(CMD_DETECT);
Serial.write(DETECT_RESP); serial_write(DETECT_RESP);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_version() { void kiss_indicate_version() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_FW_VERSION); serial_write(CMD_FW_VERSION);
Serial.write(MAJ_VERS); serial_write(MAJ_VERS);
Serial.write(MIN_VERS); serial_write(MIN_VERS);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_platform() { void kiss_indicate_platform() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_PLATFORM); serial_write(CMD_PLATFORM);
Serial.write(PLATFORM); serial_write(PLATFORM);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_board() { void kiss_indicate_board() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_BOARD); serial_write(CMD_BOARD);
Serial.write(BOARD_MODEL); serial_write(BOARD_MODEL);
Serial.write(FEND); serial_write(FEND);
} }
void kiss_indicate_mcu() { void kiss_indicate_mcu() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_MCU); serial_write(CMD_MCU);
Serial.write(MCU_VARIANT); serial_write(MCU_VARIANT);
Serial.write(FEND); serial_write(FEND);
} }
inline bool isSplitPacket(uint8_t header) { inline bool isSplitPacket(uint8_t header) {
@ -796,10 +809,10 @@ void eeprom_dump_all() {
} }
void kiss_dump_eeprom() { void kiss_dump_eeprom() {
Serial.write(FEND); serial_write(FEND);
Serial.write(CMD_ROM_READ); serial_write(CMD_ROM_READ);
eeprom_dump_all(); eeprom_dump_all();
Serial.write(FEND); serial_write(FEND);
} }
void eeprom_update(int mapped_addr, uint8_t byte) { void eeprom_update(int mapped_addr, uint8_t byte) {