diff --git a/radio/src/bluetooth.cpp b/radio/src/bluetooth.cpp index f1550b7fb..82b3b597a 100644 --- a/radio/src/bluetooth.cpp +++ b/radio/src/bluetooth.cpp @@ -34,7 +34,7 @@ #define BLUETOOTH_LINE_LENGTH 32 extern Fifo btTxFifo; -extern Fifo btRxFifo; +extern Fifo btRxFifo; volatile uint8_t bluetoothState; char bluetoothLocalAddr[LEN_BLUETOOTH_ADDR+1]; @@ -442,11 +442,14 @@ void bluetoothWakeup() bluetoothState = BLUETOOTH_STATE_DISCOVER_START; } else if (bluetoothState == BLUETOOTH_STATE_DISCOVER_START && !strncmp(line, "OK+DISC:", 8)) { - strcpy(bluetoothDistantAddr, &line[8]); // TODO quick & dirty + if (strlen(line) < 8 + LEN_BLUETOOTH_ADDR && reusableBuffer.moduleSetup.bt.devicesCount < MAX_BLUETOOTH_DISTANT_ADDR) { + strncpy(reusableBuffer.moduleSetup.bt.devices[reusableBuffer.moduleSetup.bt.devicesCount], &line[8], LEN_BLUETOOTH_ADDR); + ++reusableBuffer.moduleSetup.bt.devicesCount; + } } - else if (bluetoothState == BLUETOOTH_STATE_DISCOVER_START && !strcmp(line, "OK+DISCE")) { + /* 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"); diff --git a/radio/src/bluetooth.h b/radio/src/bluetooth.h index c79938a9a..c1f7a39f4 100644 --- a/radio/src/bluetooth.h +++ b/radio/src/bluetooth.h @@ -43,6 +43,7 @@ enum BluetoothStates { }; #define LEN_BLUETOOTH_ADDR 16 +#define MAX_BLUETOOTH_DISTANT_ADDR 6 extern volatile uint8_t bluetoothState; extern char bluetoothLocalAddr[LEN_BLUETOOTH_ADDR+1]; diff --git a/radio/src/gui/128x64/model_setup.cpp b/radio/src/gui/128x64/model_setup.cpp index d23bc2e39..156e5ee17 100644 --- a/radio/src/gui/128x64/model_setup.cpp +++ b/radio/src/gui/128x64/model_setup.cpp @@ -446,7 +446,18 @@ void startRegisterDialog(uint8_t module) POPUP_INPUT("", runPopupRegister); } - +void onBluetoothConnectMenu(const char * result) +{ + if (result != STR_EXIT) { + uint8_t index = (result - reusableBuffer.moduleSetup.bt.devices[0]) / sizeof(reusableBuffer.moduleSetup.bt.devices[0]); + strncpy(bluetoothDistantAddr, reusableBuffer.moduleSetup.bt.devices[index], LEN_BLUETOOTH_ADDR); + bluetoothState = BLUETOOTH_STATE_BIND_REQUESTED; + } + else { + reusableBuffer.moduleSetup.bt.devicesCount = 0; + bluetoothState = BLUETOOTH_STATE_DISCOVER_END; + } +} void menuModelSetup(event_t event) { @@ -1128,21 +1139,10 @@ void menuModelSetup(event_t event) } 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); - } - else { - lcdDrawText(MODEL_SETUP_2ND_COLUMN, y, BUTTON("Clear"), attr); - } + lcdDrawText(MODEL_SETUP_2ND_COLUMN, y, BUTTON("Clear"), attr); if (attr && event == EVT_KEY_FIRST(KEY_ENTER)) { - if (bluetoothState == BLUETOOTH_STATE_CONNECTED || menuHorizontalPosition == 1) { - bluetoothState = BLUETOOTH_STATE_OFF; - bluetoothDistantAddr[0] = 0; - } - else { - bluetoothState = BLUETOOTH_STATE_BIND_REQUESTED; - } + bluetoothState = BLUETOOTH_STATE_OFF; + memclear(bluetoothDistantAddr, sizeof(bluetoothDistantAddr)); } } else { @@ -1152,10 +1152,22 @@ void menuModelSetup(event_t event) else lcdDrawText(MODEL_SETUP_2ND_COLUMN, y, BUTTON("Discover"), attr); if (attr && event == EVT_KEY_FIRST(KEY_ENTER)) { - if (bluetoothState < BLUETOOTH_STATE_IDLE) + if (bluetoothState < BLUETOOTH_STATE_IDLE) { bluetoothState = BLUETOOTH_STATE_OFF; - else + } + else { + reusableBuffer.moduleSetup.bt.devicesCount = 0; bluetoothState = BLUETOOTH_STATE_DISCOVER_REQUESTED; + } + } + + if (bluetoothState == BLUETOOTH_STATE_DISCOVER_START && reusableBuffer.moduleSetup.bt.devicesCount > 0) { + popupMenuItemsCount = min(reusableBuffer.moduleSetup.bt.devicesCount, 6); + for (uint8_t i=0; i avgJitter[NUM_ANALOGS]; #include "gps.h" #endif -#if defined(BLUETOOTH) - #include "bluetooth.h" -#endif - #if defined(JACK_DETECT_GPIO) enum JackMode { JACK_UNSELECTED_MODE, diff --git a/radio/src/targets/common/arm/stm32/bluetooth_driver.cpp b/radio/src/targets/common/arm/stm32/bluetooth_driver.cpp index 3f2bb898e..381415a35 100644 --- a/radio/src/targets/common/arm/stm32/bluetooth_driver.cpp +++ b/radio/src/targets/common/arm/stm32/bluetooth_driver.cpp @@ -21,7 +21,7 @@ #include "opentx.h" Fifo btTxFifo; -Fifo btRxFifo; +Fifo btRxFifo; #if defined(PCBX7) || defined(PCBXLITE) volatile uint8_t btChipPresent = 0;