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){
if(event == ESP_SPP_SRV_OPEN_EVT) {
bt_state = BT_STATE_CONNECTED;
cable_state = CABLE_STATE_DISCONNECTED;
}
if(event == ESP_SPP_CLOSE_EVT ){

View File

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

View File

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