Skip to content

Commit

Permalink
Device is now selected based on its name.
Browse files Browse the repository at this point in the history
Static MAC address no longer required. Devices started with "BB-" are
now selected.

The option to use a fixed address is still available through make
menuconfig.
  • Loading branch information
asirinelli committed Sep 29, 2018
1 parent d17ec76 commit 8300cb7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ You should have updated your `PATH` environment variable to add the toolchain
programs.
4. Clone this repository and enter the created directory.
5. Pull the submodules: `git submodule init && git submodule update`
6. Run `make menuconfig` to change your BB-8 bluetooth MAC address
7. Compile: `make`
8. Flash your odroid-go: `make flash`
6. Compile: `make`
7. Flash your odroid-go: `make flash`

## Licence

Expand Down
10 changes: 10 additions & 0 deletions main/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
menu "BB-8 remote control"

config STATIC_MAC
bool "Set a static MAC address"
default n
help
Connect based on a given MAC address to the Sphero BB-8 toy.

Otherwise, it will try to connect to any BLE device with a name
starting with "BB-"

config BB8_MAC
string "BB-8 Bluetooth MAC address"
default "e5:12:88:32:81:75"
depends on STATIC_MAC
help
Bluetooth MAC address of the Spero BB-8 toy to be controlled.

Expand Down
62 changes: 48 additions & 14 deletions main/bb8.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp
static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
esp_ble_gattc_cb_param_t * param);

static esp_bd_addr_t bb8_mac;

static bool Isconnecting = false;

static esp_ble_scan_params_t ble_scan_params = {
Expand Down Expand Up @@ -162,6 +160,9 @@ static void DisplaySpeed()

#endif

#if CONFIG_STATIC_MAC
static esp_bd_addr_t bb8_mac;

static void init_mac()
{
int values[6];
Expand All @@ -180,6 +181,34 @@ static void init_mac()
ESP_LOGE(GATTC_TAG, "Invalid MAC Address!");
}
}
#endif

uint8_t check_if_bb8(struct ble_scan_result_evt_param scan_rst)
{

#if CONFIG_STATIC_MAC
if (memcmp(scan_rst.bda, bb8_mac, ESP_BD_ADDR_LEN) == 0)
return 1;
else
return 0;

#else

uint8_t adv_name_len = 0;
uint8_t *adv_name;

adv_name = esp_ble_resolve_adv_data(scan_rst.ble_adv, ESP_BLE_AD_TYPE_NAME_CMPL, &adv_name_len);
ESP_LOGI(GATTC_TAG, "adv_name_len: %d", adv_name_len);
esp_log_buffer_char(GATTC_TAG, adv_name, adv_name_len);
if (adv_name_len > 3)
if ((adv_name[0] == 'B') && (adv_name[1] == 'B') && (adv_name[2] == '-'))
return 1;

return 0;

#endif

}


static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
Expand Down Expand Up @@ -314,18 +343,22 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t * pa
case ESP_GAP_BLE_SCAN_RESULT_EVT:
ESP_LOGI(GATTC_TAG, "SCAN_RESULTE_EVT");
esp_ble_gap_cb_param_t *scan_result = (esp_ble_gap_cb_param_t *) param;
esp_log_buffer_hex(GATTC_TAG, scan_result->scan_rst.bda, ESP_BD_ADDR_LEN);
if (memcmp(scan_result->scan_rst.bda, bb8_mac, ESP_BD_ADDR_LEN) == 0) {
ESP_LOGI(GATTC_TAG, "BB8 !!!");
if (Isconnecting) {
break;
}
Isconnecting = true;
esp_ble_gap_stop_scanning();
esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, scan_result->scan_rst.bda,
scan_result->scan_rst.ble_addr_type, true);

if (scan_result->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_RES_EVT) {
esp_log_buffer_hex(GATTC_TAG, scan_result->scan_rst.bda, ESP_BD_ADDR_LEN);

if (check_if_bb8(scan_result->scan_rst)) {
ESP_LOGI(GATTC_TAG, "BB8 !!!");
if (Isconnecting) {
break;
}
Isconnecting = true;
esp_ble_gap_stop_scanning();
esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, scan_result->scan_rst.bda,
scan_result->scan_rst.ble_addr_type, true);
}
}

break;

case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT:
Expand Down Expand Up @@ -403,7 +436,7 @@ void send_command(uint8_t did, uint8_t cid, uint8_t data_length, uint8_t * data)
ESP_GATT_WRITE_TYPE_RSP,
ESP_GATT_AUTH_REQ_NONE);
if (write != ESP_GATT_OK)
ESP_LOGE(GATTC_TAG, "Error writing antidos");
ESP_LOGE(GATTC_TAG, "Error writing command");
free(packet);
seq++;
}
Expand Down Expand Up @@ -564,8 +597,9 @@ void test_input()

void app_main()
{

#if CONFIG_STATIC_MAC
init_mac();
#endif

input_init();

Expand Down
2 changes: 1 addition & 1 deletion sdkconfig
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ CONFIG_MONITOR_BAUD=115200
#
# BB-8 remote control
#
CONFIG_BB8_MAC="e5:12:88:32:81:75"
CONFIG_STATIC_MAC=
CONFIG_BB8_GUI=y

#
Expand Down

0 comments on commit 8300cb7

Please sign in to comment.