Skip to content

Commit

Permalink
Added feature to show if Frame is paired or not on the display
Browse files Browse the repository at this point in the history
  • Loading branch information
siliconwitch committed Aug 28, 2024
1 parent f9ba6bf commit 0dcfcb1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
11 changes: 10 additions & 1 deletion source/application/bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ void SD_EVT_IRQHandler(void)
}
}

void bluetooth_setup(bool factory_reset)
void bluetooth_setup(bool factory_reset, bool *is_paired)
{
// Enable the softdevice using internal RC oscillator
check_error(sd_softdevice_enable(NULL, softdevice_assert_handler));
Expand Down Expand Up @@ -439,6 +439,15 @@ void bluetooth_setup(bool factory_reset)
flash_wait_until_complete();
}

// Check if already paired
ble_gap_enc_info_t zero_struct;
memset(&zero_struct, 0xFF, sizeof(ble_gap_enc_info_t));
if (memcmp((void *)bond_storage, &zero_struct, sizeof(ble_gap_enc_info_t)))
{
LOG("Device is paired");
*is_paired = true;
}

// Read stored encryption key from memory
memcpy(&bond.own_key.enc_info,
(void *)bond_storage,
Expand Down
2 changes: 1 addition & 1 deletion source/application/bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#define BLE_PREFERRED_MAX_MTU 185
extern uint16_t ble_negotiated_mtu;

void bluetooth_setup(bool factory_reset);
void bluetooth_setup(bool factory_reset, bool *is_paired);

bool bluetooth_is_connected(void);

Expand Down
17 changes: 13 additions & 4 deletions source/application/luaport.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void lua_break_signal_interrupt(void)
1);
}

void run_lua(bool factory_reset)
void run_lua(bool factory_reset, bool is_paired)
{
lua_State *L = luaL_newstate();
L_global = L; // Only used for interrupts
Expand Down Expand Up @@ -125,9 +125,18 @@ void run_lua(bool factory_reset)
}

// Show splash screen
status = luaL_dostring(L, "frame.display.text('Ready to Pair', 200, 140);"
"frame.display.text('Frame '..frame.bluetooth.address():sub(-2, -1), 245, 210, { color = 'ORANGE' });"
"frame.display.show();");
if (!is_paired)
{
status = luaL_dostring(L, "frame.display.text('Ready to Pair', 200, 140);"
"frame.display.text('Frame '..frame.bluetooth.address():sub(-2, -1), 245, 210, { color = 'GREEN' });"
"frame.display.show();");
}
else
{
status = luaL_dostring(L, "frame.display.text('Frame is Paired', 185, 140);"
"frame.display.text('Frame '..frame.bluetooth.address():sub(-2, -1), 245, 210, { color = 'ORANGE' });"
"frame.display.show();");
}

if (status != LUA_OK)
{
Expand Down
2 changes: 1 addition & 1 deletion source/application/luaport.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ void lua_write_to_repl(uint8_t *buffer, uint8_t length);

void lua_break_signal_interrupt(void);

void run_lua(bool factory_reset);
void run_lua(bool factory_reset, bool is_paired);
5 changes: 3 additions & 2 deletions source/application/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,16 +402,17 @@ int main(void)
LOG("Frame firmware " BUILD_VERSION " (" GIT_COMMIT ")");

bool factory_reset = false;
bool is_paired = false;

hardware_setup(&factory_reset);

bluetooth_setup(factory_reset);
bluetooth_setup(factory_reset, &is_paired);

while (1)
{
reload_watchdog(NULL, NULL);

run_lua(factory_reset);
run_lua(factory_reset, is_paired);

factory_reset = false;
}
Expand Down

0 comments on commit 0dcfcb1

Please sign in to comment.