diff --git a/radio/src/bluetooth.cpp b/radio/src/bluetooth.cpp index 6f6f21474..2254c26c4 100644 --- a/radio/src/bluetooth.cpp +++ b/radio/src/bluetooth.cpp @@ -37,17 +37,21 @@ extern Fifo btTxFifo; extern Fifo btRxFifo; volatile uint8_t bluetoothState; -char bluetoothFriend[LEN_BLUETOOTH_FRIEND+1]; +char bluetoothLocalAddr[LEN_BLUETOOTH_ADDR+1]; +char bluetoothDistantAddr[LEN_BLUETOOTH_ADDR+1]; uint8_t bluetoothBuffer[BLUETOOTH_LINE_LENGTH+1]; uint8_t bluetoothBufferIndex = 0; tmr10ms_t bluetoothWakeupTime = 0; void bluetoothWrite(const uint8_t * data, uint8_t length) { + TRACE_NOCRLF("BT>"); for (int i=0; i 2 && bluetoothBuffer[bluetoothBufferIndex-1] == '\r') { + bluetoothBuffer[bluetoothBufferIndex-1] = '\0'; + bluetoothBufferIndex = 0; + TRACE("BT< %s", bluetoothBuffer); + if (error_reset && !strcmp((char *)bluetoothBuffer, "ERROR")) { + TRACE("BT Reset..."); + bluetoothDone(); + bluetoothState = BLUETOOTH_STATE_OFF; + bluetoothWakeupTime = get_tmr10ms() + 100; /* 1s */ + return NULL; + } + else { + if (!strncmp((char *)bluetoothBuffer, "Central:", 8)) + strcpy(bluetoothLocalAddr, (char *)bluetoothBuffer+8); + else if (!strncmp((char *)bluetoothBuffer, "Peripheral:", 11)) + strcpy(bluetoothLocalAddr, (char *)bluetoothBuffer+11); + return (char *)bluetoothBuffer; + } + } + else { + bluetoothBufferIndex = 0; + } } - else if (byte != '\r') { + else { bluetoothBuffer[bluetoothBufferIndex++] = byte; bluetoothBufferIndex &= (BLUETOOTH_LINE_LENGTH-1); } @@ -96,6 +119,22 @@ void bluetoothProcessTrainerFrame(const uint8_t * bluetoothBuffer) ppmInputValidityTimer = PPM_IN_VALID_TIMEOUT; } +void bluetoothAppendTrainerByte(uint8_t data) +{ + if (bluetoothBufferIndex < BLUETOOTH_LINE_LENGTH) { + bluetoothBuffer[bluetoothBufferIndex++] = data; + // we check for "DisConnected", but the first byte could be altered (if received in state STATE_DATA_XOR) + if (data == '\n') { + if (!strncmp((char *)&bluetoothBuffer[bluetoothBufferIndex-13], "isConnected", 11)) { + TRACE("BT< DisConnected"); + bluetoothState = BLUETOOTH_STATE_DISCONNECTED; + bluetoothBufferIndex = 0; + bluetoothWakeupTime += 200; // 1s + } + } + } +} + void bluetoothProcessTrainerByte(uint8_t data) { static uint8_t dataState = STATE_DATA_IDLE; @@ -107,13 +146,7 @@ void bluetoothProcessTrainerByte(uint8_t data) bluetoothBufferIndex = 0; } else { - if (bluetoothBufferIndex < BLUETOOTH_PACKET_SIZE) { - bluetoothBuffer[bluetoothBufferIndex++] = data; - } - if (data == '\n' && !strncmp((char *)bluetoothBuffer, "DisConnected", 12)) { - bluetoothState = BLUETOOTH_STATE_IDLE; - bluetoothBufferIndex = 0; - } + bluetoothAppendTrainerByte(data); } break; @@ -125,15 +158,13 @@ void bluetoothProcessTrainerByte(uint8_t data) dataState = STATE_DATA_IN_FRAME ; bluetoothBufferIndex = 0; } - else if (bluetoothBufferIndex < BLUETOOTH_PACKET_SIZE) { - bluetoothBuffer[bluetoothBufferIndex++] = data; + else { + bluetoothAppendTrainerByte(data); } break; case STATE_DATA_XOR: - if (telemetryRxBufferCount < BLUETOOTH_PACKET_SIZE) { - bluetoothBuffer[bluetoothBufferIndex++] = data ^ STUFF_MASK; - } + bluetoothAppendTrainerByte(data ^ STUFF_MASK); dataState = STATE_DATA_IN_FRAME; break; @@ -142,6 +173,9 @@ void bluetoothProcessTrainerByte(uint8_t data) bluetoothBufferIndex = 0; dataState = STATE_DATA_START; } + else { + bluetoothAppendTrainerByte(data); + } break; } @@ -200,7 +234,7 @@ void bluetoothSendTrainer() void bluetoothForwardTelemetry(uint8_t data) { bluetoothBuffer[bluetoothBufferIndex++] = data; - if (bluetoothBufferIndex == BLUETOOTH_LINE_LENGTH || (data == START_STOP && bluetoothBufferIndex > 2*FRSKY_SPORT_PACKET_SIZE)) { + if (data == START_STOP && bluetoothBufferIndex >= 2*FRSKY_SPORT_PACKET_SIZE) { bluetoothWrite(bluetoothBuffer, bluetoothBufferIndex); bluetoothBufferIndex = 0; } @@ -272,14 +306,15 @@ void bluetoothWakeup(void) bluetoothState = BLUETOOTH_INIT; } } - } else if (IS_BLUETOOTH_TRAINER()){ + } + else if (IS_BLUETOOTH_TRAINER()){ bluetoothState = BLUETOOTH_STATE_CONNECTED; bluetoothWriteWakeup(); bluetoothSendTrainer(); } } } -#else //PCBX9E +#else // PCBX9E void bluetoothWakeup() { tmr10ms_t now = get_tmr10ms(); @@ -287,92 +322,116 @@ void bluetoothWakeup() if (now < bluetoothWakeupTime) return; - if (g_eeGeneral.bluetoothMode == BLUETOOTH_OFF || !IS_BLUETOOTH_TRAINER()) { + bluetoothWakeupTime = now + 5; /* 50ms default */ + + if (g_eeGeneral.bluetoothMode == BLUETOOTH_OFF || (g_eeGeneral.bluetoothMode == BLUETOOTH_TRAINER && !IS_BLUETOOTH_TRAINER())) { if (bluetoothState != BLUETOOTH_STATE_OFF) { bluetoothDone(); bluetoothState = BLUETOOTH_STATE_OFF; } - bluetoothWakeupTime = now + 10; + bluetoothWakeupTime = now + 10; /* 100ms */ } - else { - bluetoothWakeupTime = now + 2; + else if (bluetoothState == BLUETOOTH_STATE_OFF) { + bluetoothInit(BLUETOOTH_FACTORY_BAUDRATE); + bluetoothState = BLUETOOTH_STATE_FACTORY_BAUDRATE_INIT; + } + + if (bluetoothState != BLUETOOTH_STATE_OFF) { bluetoothWriteWakeup(); - if (bluetoothState == BLUETOOTH_STATE_CONNECTED) { - if (g_model.trainerMode == TRAINER_MODE_MASTER_BLUETOOTH) { - bluetoothReceiveTrainer(); - } - else { - bluetoothSendTrainer(); - char * line = bluetoothReadline(); - if (!strcmp(line, "ERROR")) { - bluetoothState = BLUETOOTH_STATE_IDLE; - } - } + if (bluetoothIsWriting()) { + return; + } + } + + if (bluetoothState == BLUETOOTH_STATE_FACTORY_BAUDRATE_INIT) { + bluetoothWriteString("AT+BAUD4\r\n"); + bluetoothState = BLUETOOTH_STATE_BAUDRATE_SENT; + } + else if (bluetoothState == BLUETOOTH_STATE_BAUDRATE_SENT) { + bluetoothInit(BLUETOOTH_DEFAULT_BAUDRATE); + bluetoothState = BLUETOOTH_STATE_BAUDRATE_INIT; + bluetoothReadline(false); + } + else if (bluetoothState == BLUETOOTH_STATE_CONNECTED) { + if (g_eeGeneral.bluetoothMode == BLUETOOTH_TRAINER && g_model.trainerMode == TRAINER_MODE_MASTER_BLUETOOTH) { + bluetoothReceiveTrainer(); } else { - char * line = bluetoothReadline(); - if (bluetoothState == BLUETOOTH_STATE_OFF && IS_BLUETOOTH_TRAINER()) { - char command[32]; - char * cur = strAppend(command, BLUETOOTH_COMMAND_NAME); - uint8_t len = ZLEN(g_eeGeneral.bluetoothName); - if (len > 0) { - for (int i = 0; i < len; i++) { - *cur++ = idx2char(g_eeGeneral.bluetoothName[i]); - } + if (g_eeGeneral.bluetoothMode == BLUETOOTH_TRAINER && g_model.trainerMode == TRAINER_MODE_SLAVE_BLUETOOTH) { + bluetoothSendTrainer(); + bluetoothWakeupTime = now + 2; /* 20ms */ + } + bluetoothReadline(); // to deal with "ERROR" + } + } + else { + char * line = bluetoothReadline(); + if (bluetoothState == BLUETOOTH_STATE_BAUDRATE_INIT) { + char command[32]; + char * cur = strAppend(command, BLUETOOTH_COMMAND_NAME); + uint8_t len = ZLEN(g_eeGeneral.bluetoothName); + if (len > 0) { + for (int i = 0; i < len; i++) { + *cur++ = idx2char(g_eeGeneral.bluetoothName[i]); } - else { + } + else { #if defined(PCBHORUS) - cur = strAppend(cur, "Horus"); + cur = strAppend(cur, "Horus"); #else - cur = strAppend(cur, "Taranis"); + cur = strAppend(cur, "Taranis"); #endif - } - strAppend(cur, "\r\n"); - bluetoothWriteString(command); - bluetoothState = BLUETOOTH_STATE_NAME_SENT; } - else if (bluetoothState == BLUETOOTH_STATE_NAME_SENT && !strncmp(line, "OK+", 3)) { - bluetoothWriteString("AT+TXPW3\r\n"); - bluetoothState = BLUETOOTH_STATE_POWER_SENT; - } - else if (bluetoothState == BLUETOOTH_STATE_POWER_SENT && (!strncmp(line, "Central:", 8) || !strncmp(line, "Peripheral:", 11))) { - if (g_model.trainerMode == TRAINER_MODE_MASTER_BLUETOOTH) - bluetoothWriteString("AT+ROLE1\r\n"); - else - bluetoothWriteString("AT+ROLE0\r\n"); - bluetoothState = BLUETOOTH_STATE_ROLE_SENT; - } - else if (bluetoothState == BLUETOOTH_STATE_ROLE_SENT && (!strncmp(line, "Central:", 8) || !strncmp(line, "Peripheral:", 11))) { - bluetoothState = BLUETOOTH_STATE_IDLE; - } - else if (bluetoothState == BLUETOOTH_STATE_DISCOVER_REQUESTED) { - bluetoothWriteString("AT+DISC?\r\n"); - bluetoothState = BLUETOOTH_STATE_DISCOVER_SENT; - } - else if (bluetoothState == BLUETOOTH_STATE_DISCOVER_SENT && !strcmp(line, "OK+DISCS")) { - bluetoothState = BLUETOOTH_STATE_DISCOVER_START; - } - else if (bluetoothState == BLUETOOTH_STATE_DISCOVER_START && !strncmp(line, "OK+DISC:", 8)) { - TRACE("STOP"); - strcpy(bluetoothFriend, &line[8]); // TODO quick & dirty - } - else if (bluetoothState == BLUETOOTH_STATE_DISCOVER_START && !strcmp(line, "OK+DISCE")) { - bluetoothState = BLUETOOTH_STATE_DISCOVER_END; - } - else if (bluetoothState == BLUETOOTH_STATE_BIND_REQUESTED) { - char command[32]; - strAppend(strAppend(strAppend(command, "AT+CON"), bluetoothFriend), "\r\n"); - bluetoothWriteString(command); - bluetoothState = BLUETOOTH_STATE_BIND_SENT; - } - else if ((bluetoothState == BLUETOOTH_STATE_IDLE || bluetoothState == BLUETOOTH_STATE_BIND_SENT) && !strncmp(line, "Connected:", 10)) { - strcpy(bluetoothFriend, &line[10]); // TODO quick & dirty - bluetoothState = BLUETOOTH_STATE_CONNECTED; - if (g_model.trainerMode == TRAINER_MODE_SLAVE_BLUETOOTH) { - bluetoothWakeupTime += 500; // it seems a 5s delay is needed before sending the 1st frame - } + strAppend(cur, "\r\n"); + bluetoothWriteString(command); + bluetoothState = BLUETOOTH_STATE_NAME_SENT; + } + else if (bluetoothState == BLUETOOTH_STATE_NAME_SENT && (!strncmp(line, "OK+", 3) || !strncmp(line, "Central:", 8) || !strncmp(line, "Peripheral:", 11))) { + bluetoothWriteString("AT+TXPW3\r\n"); + bluetoothState = BLUETOOTH_STATE_POWER_SENT; + } + else if (bluetoothState == BLUETOOTH_STATE_POWER_SENT && (!strncmp(line, "Central:", 8) || !strncmp(line, "Peripheral:", 11))) { + if (g_eeGeneral.bluetoothMode == BLUETOOTH_TRAINER && g_model.trainerMode == TRAINER_MODE_MASTER_BLUETOOTH) + bluetoothWriteString("AT+ROLE1\r\n"); + else + bluetoothWriteString("AT+ROLE0\r\n"); + bluetoothState = BLUETOOTH_STATE_ROLE_SENT; + } + else if (bluetoothState == BLUETOOTH_STATE_ROLE_SENT && (!strncmp(line, "Central:", 8) || !strncmp(line, "Peripheral:", 11))) { + bluetoothState = BLUETOOTH_STATE_IDLE; + } + else if (bluetoothState == BLUETOOTH_STATE_DISCOVER_REQUESTED) { + bluetoothWriteString("AT+DISC?\r\n"); + bluetoothState = BLUETOOTH_STATE_DISCOVER_SENT; + } + else if (bluetoothState == BLUETOOTH_STATE_DISCOVER_SENT && !strcmp(line, "OK+DISCS")) { + bluetoothState = BLUETOOTH_STATE_DISCOVER_START; + } + else if (bluetoothState == BLUETOOTH_STATE_DISCOVER_START && !strncmp(line, "OK+DISC:", 8)) { + strcpy(bluetoothDistantAddr, &line[8]); // TODO quick & dirty + } + else if (bluetoothState == BLUETOOTH_STATE_DISCOVER_START && !strcmp(line, "OK+DISCE")) { + bluetoothState = BLUETOOTH_STATE_DISCOVER_END; + } + else if (bluetoothState == BLUETOOTH_STATE_BIND_REQUESTED) { + char command[32]; + strAppend(strAppend(strAppend(command, "AT+CON"), bluetoothDistantAddr), "\r\n"); + bluetoothWriteString(command); + bluetoothState = BLUETOOTH_STATE_CONNECT_SENT; + } + else if ((bluetoothState == BLUETOOTH_STATE_IDLE || bluetoothState == BLUETOOTH_STATE_DISCONNECTED || bluetoothState == BLUETOOTH_STATE_CONNECT_SENT) && !strncmp(line, "Connected:", 10)) { + strcpy(bluetoothDistantAddr, &line[10]); // TODO quick & dirty + bluetoothState = BLUETOOTH_STATE_CONNECTED; + if (g_model.trainerMode == TRAINER_MODE_SLAVE_BLUETOOTH) { + bluetoothWakeupTime += 500; // it seems a 5s delay is needed before sending the 1st frame } } + else if (bluetoothState == BLUETOOTH_STATE_DISCONNECTED && !line) { + char command[32]; + strAppend(strAppend(strAppend(command, "AT+CON"), bluetoothDistantAddr), "\r\n"); + bluetoothWriteString(command); + bluetoothWakeupTime = now + 200; /* 2s */ + } } } #endif diff --git a/radio/src/bluetooth.h b/radio/src/bluetooth.h index e0597e635..385b81bc1 100644 --- a/radio/src/bluetooth.h +++ b/radio/src/bluetooth.h @@ -25,6 +25,9 @@ enum BluetoothStates { BLUETOOTH_WAIT_BAUDRATE_CHANGE, #endif BLUETOOTH_STATE_OFF, + BLUETOOTH_STATE_FACTORY_BAUDRATE_INIT, + BLUETOOTH_STATE_BAUDRATE_SENT, + BLUETOOTH_STATE_BAUDRATE_INIT, BLUETOOTH_STATE_NAME_SENT, BLUETOOTH_STATE_POWER_SENT, BLUETOOTH_STATE_ROLE_SENT, @@ -34,16 +37,18 @@ enum BluetoothStates { BLUETOOTH_STATE_DISCOVER_START, BLUETOOTH_STATE_DISCOVER_END, BLUETOOTH_STATE_BIND_REQUESTED, - BLUETOOTH_STATE_BIND_SENT, - BLUETOOTH_STATE_CONNECTED + BLUETOOTH_STATE_CONNECT_SENT, + BLUETOOTH_STATE_CONNECTED, + BLUETOOTH_STATE_DISCONNECTED, }; -#define LEN_BLUETOOTH_FRIEND 16 +#define LEN_BLUETOOTH_ADDR 16 extern volatile uint8_t bluetoothState; -extern char bluetoothFriend[LEN_BLUETOOTH_FRIEND+1]; +extern char bluetoothLocalAddr[LEN_BLUETOOTH_ADDR+1]; +extern char bluetoothDistantAddr[LEN_BLUETOOTH_ADDR+1]; -char * bluetoothReadline(); +char * bluetoothReadline(bool error_reset=true); void bluetoothWriteString(const char * command); void bluetoothForwardTelemetry(uint8_t data); void bluetoothWakeup(); diff --git a/radio/src/gui/128x64/model_setup.cpp b/radio/src/gui/128x64/model_setup.cpp index d76a42b27..72a922563 100644 --- a/radio/src/gui/128x64/model_setup.cpp +++ b/radio/src/gui/128x64/model_setup.cpp @@ -95,8 +95,11 @@ enum MenuModelSetupItems { #if defined(PCBX7) ITEM_MODEL_TRAINER_LABEL, ITEM_MODEL_TRAINER_MODE, - ITEM_MODEL_TRAINER_LINE1, - ITEM_MODEL_TRAINER_LINE2, +#if defined(BLUETOOTH) + ITEM_MODEL_TRAINER_BLUETOOTH, +#endif + ITEM_MODEL_TRAINER_CHANNELS, + ITEM_MODEL_TRAINER_PARAMS, #endif ITEM_MODEL_SETUP_MAX }; @@ -153,13 +156,15 @@ enum MenuModelSetupItems { #if defined(PCBX7) #if defined(BLUETOOTH) - #define TRAINER_LINE1_BLUETOOTH_M_ROWS ((bluetoothFriend[0] == 0 || bluetoothState == BLUETOOTH_STATE_CONNECTED) ? (uint8_t)0 : (uint8_t)1) - #define TRAINER_LINE1_ROWS (g_model.trainerMode == TRAINER_MODE_SLAVE ? (uint8_t)1 : (g_model.trainerMode == TRAINER_MODE_MASTER_BLUETOOTH ? TRAINER_LINE1_BLUETOOTH_M_ROWS : (g_model.trainerMode == TRAINER_MODE_SLAVE_BLUETOOTH ? (uint8_t)1 : HIDDEN_ROW))) + #define TRAINER_BLUETOOTH_M_ROW ((bluetoothDistantAddr[0] == '\0' || bluetoothState == BLUETOOTH_STATE_CONNECTED) ? (uint8_t)0 : (uint8_t)1) + #define TRAINER_BLUETOOTH_S_ROW (bluetoothDistantAddr[0] == '\0' ? HIDDEN_ROW : LABEL()) + #define TRAINER_BLUETOOTH_ROW (g_model.trainerMode == TRAINER_MODE_MASTER_BLUETOOTH ? TRAINER_BLUETOOTH_M_ROW : (g_model.trainerMode == TRAINER_MODE_SLAVE_BLUETOOTH ? TRAINER_BLUETOOTH_S_ROW : HIDDEN_ROW)), #else - #define TRAINER_LINE1_ROWS (g_model.trainerMode == TRAINER_MODE_SLAVE ? (uint8_t)1 : HIDDEN_ROW) + #define TRAINER_BLUETOOTH_ROW #endif - #define TRAINER_LINE2_ROWS (g_model.trainerMode == TRAINER_MODE_SLAVE ? (uint8_t)2 : HIDDEN_ROW) - #define TRAINER_ROWS LABEL(Trainer), 0, TRAINER_LINE1_ROWS, TRAINER_LINE2_ROWS +#define TRAINER_CHANNELS_ROW (IS_SLAVE_TRAINER() ? (uint8_t)1 : HIDDEN_ROW) +#define TRAINER_PARAMS_ROW (IS_SLAVE_TRAINER() ? (uint8_t)2 : HIDDEN_ROW) +#define TRAINER_ROWS LABEL(Trainer), 0, TRAINER_BLUETOOTH_ROW TRAINER_CHANNELS_ROW, TRAINER_PARAMS_ROW #else #define TRAINER_ROWS #endif @@ -803,22 +808,21 @@ void menuModelSetup(event_t event) #if defined(BLUETOOTH) if (checkIncDec_Ret) { bluetoothState = BLUETOOTH_STATE_OFF; - bluetoothFriend[0] = 0; + bluetoothDistantAddr[0] = 0; } #endif } break; #endif -#if defined(PCBX7) - case ITEM_MODEL_TRAINER_LINE1: -#if defined(BLUETOOTH) +#if defined(PCBX7) && defined(BLUETOOTH) + case ITEM_MODEL_TRAINER_BLUETOOTH: if (g_model.trainerMode == TRAINER_MODE_MASTER_BLUETOOTH) { if (attr) { s_editMode = 0; } - if (bluetoothFriend[0]) { - lcdDrawText(INDENT_WIDTH, y+1, bluetoothFriend, TINSIZE); + if (bluetoothDistantAddr[0]) { + lcdDrawText(INDENT_WIDTH, y+1, bluetoothDistantAddr, TINSIZE); if (bluetoothState != BLUETOOTH_STATE_CONNECTED) { lcdDrawText(MODEL_SETUP_2ND_COLUMN, y, BUTTON("Bind"), menuHorizontalPosition == 0 ? attr : 0); lcdDrawText(MODEL_SETUP_2ND_COLUMN+5*FW, y, BUTTON("Clear"), menuHorizontalPosition == 1 ? attr : 0); @@ -829,7 +833,7 @@ void menuModelSetup(event_t event) if (attr && event == EVT_KEY_FIRST(KEY_ENTER)) { if (bluetoothState == BLUETOOTH_STATE_CONNECTED || menuHorizontalPosition == 1) { bluetoothState = BLUETOOTH_STATE_OFF; - bluetoothFriend[0] = 0; + bluetoothDistantAddr[0] = 0; } else { bluetoothState = BLUETOOTH_STATE_BIND_REQUESTED; @@ -849,11 +853,19 @@ void menuModelSetup(event_t event) bluetoothState = BLUETOOTH_STATE_DISCOVER_REQUESTED; } } - break; } - // no break + else { + if (bluetoothDistantAddr[0]) + lcdDrawText(INDENT_WIDTH, y+1, bluetoothDistantAddr, TINSIZE); + else + lcdDrawText(INDENT_WIDTH, y, "---"); + lcdDrawText(MODEL_SETUP_2ND_COLUMN, y, bluetoothState == BLUETOOTH_STATE_CONNECTED ? "Connected" : "!Connected"); + } + break; #endif +#if defined(PCBX7) + case ITEM_MODEL_TRAINER_CHANNELS: case ITEM_MODEL_INTERNAL_MODULE_CHANNELS: #endif #if defined(PCBSKY9X) @@ -889,7 +901,7 @@ void menuModelSetup(event_t event) #endif #if defined(PCBX7) - case ITEM_MODEL_TRAINER_LINE2: + case ITEM_MODEL_TRAINER_PARAMS: case ITEM_MODEL_INTERNAL_MODULE_BIND: #endif #if defined(PCBSKY9X) diff --git a/radio/src/gui/128x64/radio_hardware.cpp b/radio/src/gui/128x64/radio_hardware.cpp index 2035d07b5..00018d71b 100644 --- a/radio/src/gui/128x64/radio_hardware.cpp +++ b/radio/src/gui/128x64/radio_hardware.cpp @@ -113,6 +113,8 @@ enum MenuRadioHardwareItems { ITEM_RADIO_HARDWARE_SH, #if defined(BLUETOOTH) ITEM_RADIO_HARDWARE_BLUETOOTH_MODE, + ITEM_RADIO_HARDWARE_BLUETOOTH_LOCAL_ADDR, + ITEM_RADIO_HARDWARE_BLUETOOTH_DISTANT_ADDR, ITEM_RADIO_HARDWARE_BLUETOOTH_NAME, #endif ITEM_RADIO_HARDWARE_JITTER_FILTER, @@ -122,7 +124,7 @@ enum MenuRadioHardwareItems { #define POTS_ROWS NAVIGATION_LINE_BY_LINE|1, NAVIGATION_LINE_BY_LINE|1 #define SWITCHES_ROWS NAVIGATION_LINE_BY_LINE|1, NAVIGATION_LINE_BY_LINE|1, NAVIGATION_LINE_BY_LINE|1, NAVIGATION_LINE_BY_LINE|1, NAVIGATION_LINE_BY_LINE|1, NAVIGATION_LINE_BY_LINE|1 #if defined(BLUETOOTH) -#define BLUETOOTH_ROWS 0, uint8_t(g_eeGeneral.bluetoothMode == BLUETOOTH_OFF ? -1 : 0), +#define BLUETOOTH_ROWS 0, uint8_t(g_eeGeneral.bluetoothMode == BLUETOOTH_OFF ? HIDDEN_ROW : -1), uint8_t(g_eeGeneral.bluetoothMode == BLUETOOTH_OFF ? HIDDEN_ROW : -1), uint8_t(g_eeGeneral.bluetoothMode == BLUETOOTH_OFF ? HIDDEN_ROW : 0), #else #define BLUETOOTH_ROWS #endif @@ -214,6 +216,16 @@ void menuRadioHardware(event_t event) } break; + case ITEM_RADIO_HARDWARE_BLUETOOTH_LOCAL_ADDR: + lcdDrawTextAlignedLeft(y, INDENT "Local addr"); + lcdDrawText(HW_SETTINGS_COLUMN2, y, bluetoothLocalAddr[0] == '\0' ? "---" : bluetoothLocalAddr); + break; + + case ITEM_RADIO_HARDWARE_BLUETOOTH_DISTANT_ADDR: + lcdDrawTextAlignedLeft(y, INDENT "Dist addr"); + lcdDrawText(HW_SETTINGS_COLUMN2, y, bluetoothDistantAddr[0] == '\0' ? "---" : bluetoothDistantAddr); + break; + case ITEM_RADIO_HARDWARE_BLUETOOTH_NAME: lcdDrawText(INDENT_WIDTH, y, STR_NAME); editName(HW_SETTINGS_COLUMN2, y, g_eeGeneral.bluetoothName, LEN_BLUETOOTH_NAME, event, attr); diff --git a/radio/src/gui/212x64/model_setup.cpp b/radio/src/gui/212x64/model_setup.cpp index 5f06587f0..234c4010f 100644 --- a/radio/src/gui/212x64/model_setup.cpp +++ b/radio/src/gui/212x64/model_setup.cpp @@ -242,7 +242,7 @@ int getSwitchWarningsCount() #define PORT_CHANNELS_ROWS(x) (x==INTERNAL_MODULE ? INTERNAL_MODULE_CHANNELS_ROWS : (x==EXTERNAL_MODULE ? EXTERNAL_MODULE_CHANNELS_ROWS : 1)) #if defined(BLUETOOTH) && defined(DEBUG) - #define TRAINER_LINE1_BLUETOOTH_M_ROWS ((bluetoothFriend[0] == 0 || bluetoothState == BLUETOOTH_STATE_CONNECTED) ? (uint8_t)0 : (uint8_t)1) + #define TRAINER_LINE1_BLUETOOTH_M_ROWS ((bluetoothDistantAddr[0] == 0 || bluetoothState == BLUETOOTH_STATE_CONNECTED) ? (uint8_t)0 : (uint8_t)1) #define TRAINER_LINE1_ROWS (g_model.trainerMode == TRAINER_MODE_SLAVE ? (uint8_t)1 : (g_model.trainerMode == TRAINER_MODE_MASTER_BLUETOOTH ? TRAINER_LINE1_BLUETOOTH_M_ROWS : (g_model.trainerMode == TRAINER_MODE_SLAVE_BLUETOOTH ? (uint8_t)1 : HIDDEN_ROW))) #define TRAINER_LINE2_ROWS (g_model.trainerMode == TRAINER_MODE_SLAVE ? (uint8_t)2 : HIDDEN_ROW) #else @@ -724,7 +724,7 @@ void menuModelSetup(event_t event) #if defined(BLUETOOTH) && defined(DEBUG) if (attr && checkIncDec_Ret) { bluetoothState = BLUETOOTH_STATE_OFF; - bluetoothFriend[0] = 0; + bluetoothDistantAddr[0] = 0; } #endif break; @@ -829,8 +829,8 @@ void menuModelSetup(event_t event) if (attr) { s_editMode = 0; } - if (bluetoothFriend[0]) { - lcdDrawText(INDENT_WIDTH, y+1, bluetoothFriend, TINSIZE); + if (bluetoothDistantAddr[0]) { + lcdDrawText(INDENT_WIDTH, y+1, bluetoothDistantAddr, TINSIZE); if (bluetoothState != BLUETOOTH_STATE_CONNECTED) { lcdDrawText(MODEL_SETUP_2ND_COLUMN, y, BUTTON("Bind"), menuHorizontalPosition == 0 ? attr : 0); lcdDrawText(MODEL_SETUP_2ND_COLUMN+5*FW, y, BUTTON("Clear"), menuHorizontalPosition == 1 ? attr : 0); @@ -841,7 +841,7 @@ void menuModelSetup(event_t event) if (attr && event == EVT_KEY_FIRST(KEY_ENTER)) { if (bluetoothState == BLUETOOTH_STATE_CONNECTED || menuHorizontalPosition == 1) { bluetoothState = BLUETOOTH_STATE_OFF; - bluetoothFriend[0] = 0; + bluetoothDistantAddr[0] = 0; } else { bluetoothState = BLUETOOTH_STATE_BIND_REQUESTED; diff --git a/radio/src/gui/480x272/model_setup.cpp b/radio/src/gui/480x272/model_setup.cpp index 1d5dc9876..c594374e2 100644 --- a/radio/src/gui/480x272/model_setup.cpp +++ b/radio/src/gui/480x272/model_setup.cpp @@ -233,7 +233,7 @@ int getSwitchWarningsCount() #define POT_WARN_ITEMS() ((g_model.potsWarnMode) ? uint8_t(NAVIGATION_LINE_BY_LINE|(NUM_POTS-1)) : (uint8_t)0) #define SLIDER_WARN_ITEMS() ((g_model.potsWarnMode) ? uint8_t(NAVIGATION_LINE_BY_LINE|(NUM_SLIDERS-1)) : (uint8_t)0) -#define TRAINER_LINE1_BLUETOOTH_M_ROWS ((bluetoothFriend[0] == 0 || bluetoothState == BLUETOOTH_STATE_CONNECTED) ? (uint8_t)0 : (uint8_t)1) +#define TRAINER_LINE1_BLUETOOTH_M_ROWS ((bluetoothDistantAddr[0] == 0 || bluetoothState == BLUETOOTH_STATE_CONNECTED) ? (uint8_t)0 : (uint8_t)1) #define TRAINER_LINE1_ROWS (g_model.trainerMode == TRAINER_MODE_SLAVE ? (uint8_t)1 : (g_model.trainerMode == TRAINER_MODE_MASTER_BLUETOOTH ? TRAINER_LINE1_BLUETOOTH_M_ROWS : (g_model.trainerMode == TRAINER_MODE_SLAVE_BLUETOOTH ? (uint8_t)1 : HIDDEN_ROW))) #define TRAINER_LINE2_ROWS (g_model.trainerMode == TRAINER_MODE_SLAVE ? (uint8_t)2 : HIDDEN_ROW) @@ -649,7 +649,7 @@ bool menuModelSetup(event_t event) g_model.trainerMode = editChoice(MODEL_SETUP_2ND_COLUMN, y, STR_VTRAINERMODES, g_model.trainerMode, 0, TRAINER_MODE_MAX(), attr, event); if (attr && checkIncDec_Ret) { bluetoothState = BLUETOOTH_STATE_OFF; - bluetoothFriend[0] = 0; + bluetoothDistantAddr[0] = 0; } break; @@ -750,8 +750,8 @@ bool menuModelSetup(event_t event) if (attr) { s_editMode = 0; } - if (bluetoothFriend[0]) { - lcdDrawText(MENUS_MARGIN_LEFT + INDENT_WIDTH, y, bluetoothFriend); + if (bluetoothDistantAddr[0]) { + lcdDrawText(MENUS_MARGIN_LEFT + INDENT_WIDTH, y, bluetoothDistantAddr); if (bluetoothState != BLUETOOTH_STATE_CONNECTED) { drawButton(MODEL_SETUP_2ND_COLUMN, y, "Bind", menuHorizontalPosition == 0 ? attr : 0); drawButton(MODEL_SETUP_2ND_COLUMN+60, y, "Clear", menuHorizontalPosition == 1 ? attr : 0); @@ -762,7 +762,7 @@ bool menuModelSetup(event_t event) if (attr && event == EVT_KEY_FIRST(KEY_ENTER)) { if (bluetoothState == BLUETOOTH_STATE_CONNECTED || menuHorizontalPosition == 1) { bluetoothState = BLUETOOTH_STATE_OFF; - bluetoothFriend[0] = 0; + bluetoothDistantAddr[0] = 0; } else { bluetoothState = BLUETOOTH_STATE_BIND_REQUESTED; diff --git a/radio/src/gui/gui_common_arm.cpp b/radio/src/gui/gui_common_arm.cpp index 0b0ab9a85..e367bbd57 100644 --- a/radio/src/gui/gui_common_arm.cpp +++ b/radio/src/gui/gui_common_arm.cpp @@ -587,6 +587,10 @@ bool isTrainerModeAvailable(int mode) { if (mode == TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE || mode == TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE || mode == TRAINER_MODE_MASTER_BATTERY_COMPARTMENT) return false; +#if defined(BLUETOOTH) + else if (g_eeGeneral.bluetoothMode != BLUETOOTH_TRAINER && (mode == TRAINER_MODE_MASTER_BLUETOOTH || mode == TRAINER_MODE_SLAVE_BLUETOOTH)) + return false; +#endif else return true; } diff --git a/radio/src/opentx.h b/radio/src/opentx.h index 89d55ab0e..021ad088d 100644 --- a/radio/src/opentx.h +++ b/radio/src/opentx.h @@ -227,11 +227,14 @@ #if defined(BLUETOOTH) #if defined(X9E) #define IS_BLUETOOTH_TRAINER() (g_model.trainerMode == TRAINER_MODE_SLAVE_BLUETOOTH) + #define IS_SLAVE_TRAINER() (g_model.trainerMode == TRAINER_MODE_SLAVE) #else #define IS_BLUETOOTH_TRAINER() (g_model.trainerMode == TRAINER_MODE_MASTER_BLUETOOTH || g_model.trainerMode == TRAINER_MODE_SLAVE_BLUETOOTH) + #define IS_SLAVE_TRAINER() (g_model.trainerMode == TRAINER_MODE_SLAVE || g_model.trainerMode == TRAINER_MODE_SLAVE_BLUETOOTH) #endif #else #define IS_BLUETOOTH_TRAINER() false + #define IS_SLAVE_TRAINER() (g_model.trainerMode == TRAINER_MODE_SLAVE) #endif #if defined(CPUARM) diff --git a/radio/src/targets/common/arm/stm32/bluetooth_driver.cpp b/radio/src/targets/common/arm/stm32/bluetooth_driver.cpp index 5ce4bb26d..65caaa252 100644 --- a/radio/src/targets/common/arm/stm32/bluetooth_driver.cpp +++ b/radio/src/targets/common/arm/stm32/bluetooth_driver.cpp @@ -86,6 +86,10 @@ void bluetoothInit(uint32_t baudrate) NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); + btRxFifo.clear(); + btTxFifo.clear(); + bluetoothWriteState = BLUETOOTH_WRITE_IDLE; + GPIO_ResetBits(BT_EN_GPIO, BT_EN_GPIO_PIN); // open bluetooth } @@ -137,3 +141,8 @@ void bluetoothWriteWakeup(void) #endif } } + +uint8_t bluetoothIsWriting(void) +{ + return bluetoothWriteState != BLUETOOTH_WRITE_IDLE; +} diff --git a/radio/src/targets/horus/board.h b/radio/src/targets/horus/board.h index a6710a4ed..55adb10bb 100644 --- a/radio/src/targets/horus/board.h +++ b/radio/src/targets/horus/board.h @@ -526,9 +526,11 @@ void serial2Stop(void); int sbusGetByte(uint8_t * byte); // BT driver +#define BLUETOOTH_FACTORY_BAUDRATE 57600 #define BLUETOOTH_DEFAULT_BAUDRATE 115200 void bluetoothInit(uint32_t baudrate); void bluetoothWriteWakeup(void); +uint8_t bluetoothIsWriting(void); void bluetoothDone(void); extern uint8_t currentTrainerMode; diff --git a/radio/src/targets/taranis/board.h b/radio/src/targets/taranis/board.h index 2a30761df..3a266afff 100644 --- a/radio/src/targets/taranis/board.h +++ b/radio/src/targets/taranis/board.h @@ -549,9 +549,14 @@ void serial2Stop(void); // BT driver #define BLUETOOTH_DEFAULT_BAUDRATE 115200 +#if defined(PCBX9E) #define BLUETOOTH_FACTORY_BAUDRATE 9600 +#else +#define BLUETOOTH_FACTORY_BAUDRATE 57600 +#endif void bluetoothInit(uint32_t baudrate); void bluetoothWriteWakeup(void); +uint8_t bluetoothIsWriting(void); void bluetoothDone(void); // LED driver diff --git a/radio/src/telemetry/frsky.cpp b/radio/src/telemetry/frsky.cpp index 0e855ca31..79c856545 100644 --- a/radio/src/telemetry/frsky.cpp +++ b/radio/src/telemetry/frsky.cpp @@ -52,7 +52,7 @@ NOINLINE void processFrskyTelemetryData(uint8_t data) #endif #if defined(BLUETOOTH) - if (g_eeGeneral.bluetoothMode == BLUETOOTH_TELEMETRY) { + if (g_eeGeneral.bluetoothMode == BLUETOOTH_TELEMETRY && bluetoothState == BLUETOOTH_STATE_CONNECTED) { bluetoothForwardTelemetry(data); } #endif