Skip to content

added returning to regular hid mode via reboot #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 76 additions & 6 deletions arduino/arduino_nRF52840/arduino_nRF52840.ino
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ volatile uint8_t switchBleConnCurrIndex = 0;
using namespace Adafruit_LittleFS_Namespace;
#define FILENAME "/devNameList.txt"
#define MODE_FILENAME "/config.txt"
#define TMP_FILENAME "/tmp.txt"

File file(InternalFS);

Expand Down Expand Up @@ -272,6 +273,63 @@ void change_mode(char* myLine) {
NVIC_SystemReset();
}

void save_tmp_file() {
file.open(TMP_FILENAME, FILE_O_WRITE);
if(file) {
file.seek(0);
file.write((const uint8_t *)&flag_bleSwapConnProsStarted, 1);
file.write((const uint8_t *)&switchBleConnStartIndex, 1);
file.write((const uint8_t *)&switchBleConnCurrIndex, 1);
file.close();

#ifdef DEBUG
Serial.println("Temp parameters saved");
Serial.print("Previous connection index");
Serial.println(switchBleConnStartIndex);
Serial.print("Previous connection name");
Serial.println( bleDeviceNameList[switchBleConnStartIndex - 1]);
Serial.print("Target connection index");
Serial.println(switchBleConnCurrIndex);
Serial.print("Target connection name");
Serial.println( bleDeviceNameList[switchBleConnCurrIndex - 1]);
#endif
} else {
#ifdef DEBUG
Serial.print(MODE_FILENAME " Write Failed");
#endif
}
}

void load_tmp_file() {
file.open(TMP_FILENAME, FILE_O_READ);
// file existed
if (file)
{
file.read((void *)&flag_bleSwapConnProsStarted, 1);
file.read((void *)&switchBleConnStartIndex, 1);
file.read((void *)&switchBleConnCurrIndex, 1);
file.close();

#ifdef DEBUG
Serial.println("Temp parameters loaded");
Serial.print("Previous connection index");
Serial.println(switchBleConnStartIndex);
Serial.print("Previous connection name");
Serial.println( bleDeviceNameList[switchBleConnStartIndex - 1]);
Serial.print("Target connection index");
Serial.println(switchBleConnCurrIndex);
Serial.print("Target connection name");
Serial.println( bleDeviceNameList[switchBleConnCurrIndex - 1]);
#endif

InternalFS.remove(TMP_FILENAME);
} else {
#ifdef DEBUG
Serial.println(TMP_FILENAME " Read Failed");
#endif
}
}

void setup()
{
Serial.begin(115200);
Expand All @@ -284,6 +342,8 @@ void setup()
InternalFS.begin();

load_mode_file();

load_tmp_file();

pinMode(USER_SW, INPUT_PULLUP);

Expand Down Expand Up @@ -835,11 +895,7 @@ void switchBleConnection(char *myLine)
at_response("ERROR: No other device present in list\n");
break;
}
else
{
connection->disconnect();
}


if (switchBleConnStartIndex >= bleDeviceNameListIndex)
{
switchBleConnCurrIndex = 1;
Expand All @@ -854,6 +910,9 @@ void switchBleConnection(char *myLine)

snprintf(buff, sizeof(buff), "Trying to connect with - %s\n", bleDeviceNameList[switchBleConnCurrIndex - 1]);
at_response(buff);

connection->disconnect();

break;
}
}
Expand Down Expand Up @@ -1227,6 +1286,17 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
//Serial.println();
//Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);

if(blehid.isBootMode()) {
save_tmp_file();

#ifdef DEBUG
Serial.println("Resetting hid mode");
delay(1000);
#endif

NVIC_SystemReset();
}

connection_count--;
// Keep advertising if not reaching max
if (connection_count < max_prph_connection)
Expand Down Expand Up @@ -1254,4 +1324,4 @@ void set_keyboard_led(uint16_t conn_handle, uint8_t led_bitmap)
{
ledOff(LED_RED);
}
}
}
2 changes: 1 addition & 1 deletion arduino/receiver_dongle/receiver_dongle.ino
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
Bluefruit.Central.connect(report);
}
} else {
Serial.println("Name not available");
Serial.println("Name not available");
}
Serial.println();

Expand Down