Skip to content

Commit

Permalink
ble_gadgetbridge: Refactor code to save stack. Stored pretty much sam…
Browse files Browse the repository at this point in the history
…e struct twice causing large stack usage.
  • Loading branch information
jakkra committed Jul 13, 2024
1 parent d6bf748 commit b7b2774
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 86 deletions.
2 changes: 1 addition & 1 deletion app/child_image/hci_ipc.conf
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC=y
# increase of ble throughput
CONFIG_BT_BUF_ACL_RX_SIZE=502
CONFIG_BT_BUF_ACL_TX_SIZE=502
CONFIG_BT_CTLR_DATA_LENGTH_MAX=251

# Temporary fix as there is a strange behaviour with some Android
# phones.
CONFIG_BT_DATA_LEN_UPDATE=n
#CONFIG_BT_CTLR_DATA_LENGTH_MAX=251
6 changes: 4 additions & 2 deletions app/src/applications/weather/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
FILE(GLOB app_sources *.c)
target_sources(app PRIVATE ${app_sources})
if (CONFIG_ZSWATCH_PCB_REV GREATER_EQUAL 4)
FILE(GLOB app_sources *.c)
target_sources(app PRIVATE ${app_sources})
endif()
22 changes: 16 additions & 6 deletions app/src/applications/weather/weather_app.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <zephyr/kernel.h>
#include <zephyr/init.h>
#include <zephyr/zbus/zbus.h>
#include <zephyr/logging/log.h>
#include "cJSON.h"

#include "managers/zsw_app_manager.h"
Expand All @@ -10,6 +11,8 @@
#include "weather_ui.h"
#include <zsw_clock.h>

LOG_MODULE_REGISTER(weather_app, LOG_LEVEL_DBG);

#define HTTP_REQUEST_URL_FMT "https://api.open-meteo.com/v1/forecast?latitude=%f&longitude=%f&current=wind_speed_10m,temperature_2m,apparent_temperature,weather_code&daily=weather_code,temperature_2m_max,temperature_2m_min,apparent_temperature_max,apparent_temperature_min,precipitation_sum,rain_sum,precipitation_probability_max&wind_speed_unit=ms&timezone=auto&forecast_days=%d"

#define MAX_GPS_AGED_TIME_MS 30 * 60 * 1000
Expand Down Expand Up @@ -86,7 +89,7 @@ static void http_rsp_cb(ble_http_status_code_t status, char *response)
last_update_weather_time = k_uptime_get();
ble_comm_request_gps_status(false);
} else {
printk("HTTP request failed\n");
LOG_ERR("HTTP request failed\n");
}
}

Expand All @@ -95,6 +98,7 @@ static void fetch_weather_data(double lat, double lon)
char weather_url[512];
snprintf(weather_url, sizeof(weather_url), HTTP_REQUEST_URL_FMT, lat, lon, WEATHER_UI_NUM_FORECASTS);
zsw_ble_http_get(weather_url, http_rsp_cb);
// TODO Handle if HTTP requests are not enabled or supported on phone.
}

static void on_zbus_ble_data_callback(const struct zbus_channel *chan)
Expand All @@ -103,9 +107,9 @@ static void on_zbus_ble_data_callback(const struct zbus_channel *chan)

if (event->data.type == BLE_COMM_DATA_TYPE_GPS) {
last_update_gps_time = k_uptime_get();
printk("Got GPS data, fetch weather\n");
printk("Latitude: %f\n", event->data.data.gps.lat);
printk("Longitude: %f\n", event->data.data.gps.lon);
LOG_DBG("Got GPS data, fetch weather\n");
LOG_DBG("Latitude: %f\n", event->data.data.gps.lat);
LOG_DBG("Longitude: %f\n", event->data.data.gps.lon);
last_lat = event->data.data.gps.lat;
last_lon = event->data.data.gps.lon;
fetch_weather_data(event->data.data.gps.lat, event->data.data.gps.lon);
Expand All @@ -115,8 +119,12 @@ static void on_zbus_ble_data_callback(const struct zbus_channel *chan)
static void weather_app_start(lv_obj_t *root, lv_group_t *group)
{
if (last_update_gps_time == 0 || k_uptime_delta(&last_update_gps_time) > MAX_GPS_AGED_TIME_MS) {
printk("GPS data is too old, request GPS\n");
ble_comm_request_gps_status(true);
LOG_DBG("GPS data is too old, request GPS\n");
int res = ble_comm_request_gps_status(true);
if (res != 0) {
LOG_ERR("Failed to request GPS data\n");
}
// TODO Show GPS fetching in progress in app
} else {
fetch_weather_data(last_lat, last_lon);
}
Expand All @@ -139,6 +147,8 @@ static int weather_app_add(void)
{
zsw_app_manager_add_application(&app);

// TODO Add periodic GPS and weather polling in backgrund.

return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/applications/weather/weather_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void weather_ui_show(lv_obj_t *root)
lv_obj_set_x(ui_location, 0);
lv_obj_set_y(ui_location, 25);
lv_obj_set_align(ui_location, LV_ALIGN_TOP_MID);
lv_label_set_text(ui_location, "Location...");
lv_label_set_text(ui_location, "");
lv_obj_set_style_text_font(ui_location, &lv_font_montserrat_18, LV_PART_MAIN | LV_STATE_DEFAULT);

ui_forecast_widget = lv_obj_create(ui_root_container);
Expand Down
2 changes: 1 addition & 1 deletion app/src/ble/ble_comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ int ble_comm_request_gps_status(bool enable)
char gps_status[50];
int len = snprintf(gps_status, sizeof(gps_status), "{\"t\":\"gps_power\", \"status\":%s} \n",
enable ? "true" : "false");
printk("Sending: %s", gps_status);
LOG_DBG("Request GPS: %s", gps_status);
return ble_comm_send(gps_status, len);
}

Expand Down
1 change: 1 addition & 0 deletions app/src/ble/ble_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ int zsw_ble_http_get(char *url, ble_http_callback cb)
char *request;

if (request_pending) {
// TODO implement a queue for requests
return -EBUSY;
}

Expand Down
Loading

0 comments on commit b7b2774

Please sign in to comment.