Skip to content

Commit 1802d09

Browse files
feat: add bluetooth support
Within this commit, we address the bluetooths and touch screens input work queue issue more gracefully. This commit brings the new system all the capabilities old system have had.
1 parent ffa70a6 commit 1802d09

File tree

9 files changed

+397
-70
lines changed

9 files changed

+397
-70
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
1-
west build -p always . --board esp32s3_touch_lcd_1_28/esp32s3/procpu
2-
west blobs fetch hal_espressif
1+
# ESP32 Zephyr RTOS Watch
2+
3+
**Supported Boards:**
4+
- [ESP32-S3-Touch-LCD-1.28](https://www.waveshare.com/wiki/ESP32-S3-Touch-LCD-1.28)
5+
6+
## Build Instructions
7+
1. Install the Zephyr according the information in their website.
8+
2. Get the ESP32 HAL using `west blobs fetch hal_espressif`.
9+
3. Clone the package and build it using the instructions provided.
10+
```sh
11+
$ west build -p always . --board esp32s3_touch_lcd_1_28/esp32s3/procpu
12+
```
13+
4. After the build, flash the firmware to the board.
14+
```sh
15+
$ west flash
16+
```
17+
5. All done!
18+
19+
## Monitoring
20+
```sh
21+
$ west espressif monitor
22+
```

prj.conf

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,50 @@ CONFIG_LV_Z_MEM_POOL_SIZE=16384
2323
# PWM Configurations
2424
CONFIG_PWM=y
2525

26+
# Bluetooth Settings
2627
# Bluetooth Configurations
27-
# CONFIG_BT=y
28-
# CONFIG_BT_PERIPHERAL=y
29-
# CONFIG_BT_SETTINGS=y
30-
# CONFIG_BT_DEVICE_NAME="ZephyrWatch"
31-
# CONFIG_BT_SMP=y
32-
# CONFIG_BT_SIGNING=y
33-
# CONFIG_BT_KEYS_OVERWRITE_OLDEST=y
34-
# CONFIG_BT_PRIVACY=y
28+
CONFIG_BT=y
29+
CONFIG_BT_PERIPHERAL=y
30+
CONFIG_BT_DEVICE_NAME="ZephyrWatch"
3531

3632
# Bluetooth GATT Device Information Service Configuration
37-
# CONFIG_BT_DIS=y
38-
# CONFIG_BT_DIS_PNP=n
39-
# CONFIG_BT_DIS_MODEL="ZephyrWatch_001"
40-
# CONFIG_BT_DIS_MANUF="github.com/electricalgorithm"
41-
# CONFIG_BT_DIS_SERIAL_NUMBER=y
42-
# CONFIG_BT_DIS_FW_REV=y
43-
# CONFIG_BT_DIS_HW_REV=y
44-
# CONFIG_BT_DIS_SW_REV=y
45-
# CONFIG_BT_DIS_SERIAL_NUMBER_STR="0.0.1"
46-
# CONFIG_BT_DIS_FW_REV_STR="0.0.1"
47-
# CONFIG_BT_DIS_HW_REV_STR="0.0.1"
48-
# CONFIG_BT_DIS_SW_REV_STR="0.0.1"
49-
# CONFIG_BT_DIS_SETTINGS=y
50-
# CONFIG_BT_DIS_STR_MAX=21
33+
CONFIG_BT_DIS=y
34+
CONFIG_BT_DIS_PNP=n
35+
CONFIG_BT_DIS_MODEL="ZephyrWatch_001"
36+
CONFIG_BT_DIS_MANUF="electricalgorithm"
37+
CONFIG_BT_DIS_SERIAL_NUMBER=y
38+
CONFIG_BT_DIS_FW_REV=y
39+
CONFIG_BT_DIS_HW_REV=y
40+
CONFIG_BT_DIS_SW_REV=y
41+
CONFIG_BT_DIS_SERIAL_NUMBER_STR="0.0.1"
42+
CONFIG_BT_DIS_FW_REV_STR="0.0.1"
43+
CONFIG_BT_DIS_HW_REV_STR="0.0.1"
44+
CONFIG_BT_DIS_SW_REV_STR="0.0.1"
45+
CONFIG_BT_DIS_SETTINGS=y
46+
CONFIG_BT_DIS_STR_MAX=21
47+
48+
# If not set BT_SMP & BT_SIGNING, the device will not be able to pair with other devices.
49+
CONFIG_BT_SMP=y
50+
CONFIG_BT_SIGNING=y
51+
CONFIG_BT_KEYS_OVERWRITE_OLDEST=y
52+
CONFIG_BT_PRIVACY=y
53+
54+
# Settings Subsystem
55+
CONFIG_SETTINGS=y
56+
CONFIG_BT_SETTINGS=y
57+
58+
# System Work Queue Configuration - Increased for input event handling
59+
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=32768
60+
CONFIG_SYSTEM_WORKQUEUE_PRIORITY=-1
61+
62+
# Input Subsystem Configuration - Disable input warnings
63+
CONFIG_INPUT=y
64+
CONFIG_INPUT_LOG_LEVEL_OFF=y
5165

5266
# Flash Configurations
5367
CONFIG_FLASH=y
5468
CONFIG_FLASH_MAP=y
5569
CONFIG_NVS=y
5670

57-
# Counter
58-
CONFIG_TIMER=y
59-
60-
# RTC
61-
# CONFIG_RTC=y
62-
# CONFIG_CLOCK_CONTROL=y
63-
# CONFIG_CLOCK_CONTROL_ESP32=y
64-
# CONFIG_RTC_ALARM=y
65-
# CONFIG_RTC_UPDATE=y
66-
# CONFIG_RTC_CALIBRATION=y
71+
# Counter (using correct config name)
72+
CONFIG_COUNTER=y

src/bluetooth/infrastructure.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <zephyr/settings/settings.h>
2+
#include <zephyr/bluetooth/gatt.h>
3+
#include <zephyr/logging/log.h>
4+
5+
LOG_MODULE_REGISTER(ZephyrWatch_BluetoothInfra, LOG_LEVEL_INF);
6+
7+
static const struct bt_data m_ad[] = {
8+
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
9+
BT_DATA_BYTES(BT_DATA_UUID16_ALL, BT_UUID_16_ENCODE(BT_UUID_CTS_VAL)),
10+
};
11+
12+
static const struct bt_data m_sd[] = {
13+
BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
14+
};
15+
16+
static void m_connected_callback(struct bt_conn *conn, uint8_t err) {
17+
if (err) LOG_ERR("Connection failed (err %u).", err);
18+
else {
19+
char addr[BT_ADDR_LE_STR_LEN];
20+
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
21+
LOG_INF("Connection established to %s.", addr);
22+
}
23+
}
24+
25+
static void m_disconnected_callback(struct bt_conn *conn, uint8_t reason) {
26+
LOG_INF("Disconnected (reason 0x%02x).", reason);
27+
}
28+
29+
BT_CONN_CB_DEFINE(conn_callbacks) = {
30+
.connected = m_connected_callback,
31+
.disconnected = m_disconnected_callback,
32+
};
33+
34+
/* The API function to enable Bluetooth and start advertisement. */
35+
uint8_t enable_bluetooth_and_start_advertisement() {
36+
int err;
37+
38+
err = bt_enable(NULL);
39+
if (err) {
40+
LOG_ERR("Bluetooth init failed (err %d).", err);
41+
return err;
42+
}
43+
44+
LOG_INF("Bluetooth initialized.");
45+
46+
if (IS_ENABLED(CONFIG_SETTINGS)) {
47+
settings_load();
48+
}
49+
50+
err = bt_le_adv_start(BT_LE_ADV_CONN, m_ad, ARRAY_SIZE(m_ad), m_sd, ARRAY_SIZE(m_sd));
51+
if (err) {
52+
LOG_ERR("Advertising failed to start (err %d).", err);
53+
return err;
54+
}
55+
56+
LOG_INF("Advertising successfully started.");
57+
return 0;
58+
}
59+
60+
uint8_t disable_bluetooth_and_stop_advertisement() {
61+
int err;
62+
63+
err = bt_le_adv_stop();
64+
if (err) {
65+
LOG_ERR("Advertising failed to stop (err %d).", err);
66+
return err;
67+
}
68+
69+
LOG_INF("Advertising successfully stopped.");
70+
71+
err = bt_disable();
72+
if (err) {
73+
LOG_ERR("Bluetooth failed to disable (err %d).", err);
74+
return err;
75+
}
76+
77+
LOG_INF("Bluetooth disabled.");
78+
return 0;
79+
}

src/bluetooth/infrastructure.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef BLUETOOTH_INFRASTRUCTURE_H_
2+
#define BLUETOOTH_INFRASTRUCTURE_H_
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#include <zephyr/settings/settings.h>
9+
#include <zephyr/bluetooth/gatt.h>
10+
#include <zephyr/logging/log.h>
11+
12+
uint8_t enable_bluetooth_and_start_advertisement();
13+
uint8_t disable_bluetooth_and_stop_advertisement();
14+
15+
#ifdef __cplusplus
16+
}
17+
#endif
18+
19+
#endif /* BLUETOOTH_INFRASTRUCTURE_H_ */
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <zephyr/sys/byteorder.h>
2+
#include <zephyr/logging/log.h>
3+
#include <zephyr/bluetooth/gatt.h>
4+
5+
#include "current_time_service.h"
6+
7+
LOG_MODULE_REGISTER(ZephyrWatch_CurrentTimeService, LOG_LEVEL_INF);
8+
9+
10+
/* Holds current time data. */
11+
static uint8_t m_current_time_data[10] = {
12+
232U, // Year (2024) last byte
13+
7U, // Year (2024) first byte
14+
5U, // Month (May)
15+
13U, // Day (13th)
16+
23U, // Hours (23)
17+
45U, // Minutes (45)
18+
30U, // Seconds (30)
19+
2U, // Day of week (Tuesday)
20+
0U, // Fractions 256 part of 'Exact Time 256'
21+
0U, // Adjust reason
22+
};
23+
24+
/* Current Time Service Declaration */
25+
static ssize_t m_time_write_callback(
26+
struct bt_conn *conn,
27+
const struct bt_gatt_attr *attr,
28+
const void *buf,
29+
uint16_t len,
30+
uint16_t offset,
31+
uint8_t flags) {
32+
33+
// Get the value of the attribute.
34+
uint8_t *value = attr->user_data;
35+
36+
// Check if the value written is valid.
37+
if (offset + len > sizeof(m_current_time_data)) {
38+
LOG_ERR("Write request too long.");
39+
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
40+
}
41+
42+
// Copy the new value into the m_current_time_data array (given as buffer).
43+
memcpy(value + offset, buf, len);
44+
LOG_INF("Current time updated from the Bluetooth client.");
45+
return len;
46+
}
47+
48+
/* Current Time Service Declaration */
49+
/*
50+
BT_GATT_SERVICE_DEFINE(cts_cvs,
51+
BT_GATT_PRIMARY_SERVICE(BT_UUID_CTS),
52+
BT_GATT_CHARACTERISTIC(
53+
BT_UUID_CTS_CURRENT_TIME,
54+
BT_GATT_CHRC_WRITE,
55+
BT_GATT_PERM_WRITE,
56+
NULL, m_time_write_callback, m_current_time_data),
57+
);
58+
*/
59+
60+
/* API for the Current Time Service application layer consumers.
61+
* - get_current_time: Get the current time. Returns a struct with
62+
* proper key-value pairs for year, month, hours, minutes, and seconds.
63+
* - set_current_time: Set the current time. Takes a struct with
64+
* proper key-value pairs for year, month, hours, minutes, and seconds.
65+
*/
66+
67+
struct current_time get_current_time(void) {
68+
struct current_time time;
69+
time.year = sys_le16_to_cpu(*(uint16_t *) &m_current_time_data[0]);
70+
time.month = m_current_time_data[2];
71+
time.day = m_current_time_data[3];
72+
time.hours = m_current_time_data[4];
73+
time.minutes = m_current_time_data[5];
74+
time.seconds = m_current_time_data[6];
75+
return time;
76+
}
77+
78+
void set_current_time(struct current_time time) {
79+
*(uint16_t *) &m_current_time_data[0] = sys_cpu_to_le16(time.year);
80+
m_current_time_data[2] = time.month;
81+
m_current_time_data[3] = time.day;
82+
m_current_time_data[4] = time.hours;
83+
m_current_time_data[5] = time.minutes;
84+
m_current_time_data[6] = time.seconds;
85+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef CURRENT_TIME_SERVICE_H
2+
#define CURRENT_TIME_SERVICE_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#include <zephyr/sys/byteorder.h>
9+
#include <zephyr/logging/log.h>
10+
#include <zephyr/bluetooth/gatt.h>
11+
12+
struct current_time {
13+
uint16_t year;
14+
uint8_t month;
15+
uint8_t day;
16+
uint8_t hours;
17+
uint8_t minutes;
18+
uint8_t seconds;
19+
};
20+
21+
struct current_time get_current_time(void);
22+
void set_current_time(struct current_time time);
23+
24+
#ifdef __cplusplus
25+
}
26+
#endif
27+
28+
#endif // CURRENT_TIME_SERVICE_H

src/display/styles/widgetstyle.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "lvgl.h"
2+
#include "display/styles/widgetstyle.h"
3+
4+
static lv_style_t style_no_border;
5+
6+
void widget_style_init() {
7+
lv_style_init(&style_no_border);
8+
9+
// Remove borders
10+
lv_style_set_border_width(&style_no_border, 0);
11+
12+
// Transparent background
13+
lv_style_set_bg_opa(&style_no_border, LV_OPA_TRANSP);
14+
15+
// Remove outline and shadow just in case
16+
lv_style_set_outline_width(&style_no_border, 0);
17+
lv_style_set_shadow_width(&style_no_border, 0);
18+
19+
// Optional: remove padding if undesired
20+
lv_style_set_pad_all(&style_no_border, 0);
21+
}
22+
23+
const lv_style_t* get_widget_style_no_border() {
24+
return &style_no_border;
25+
}
26+

src/display/styles/widgetstyle.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef _DISPLAY_STYLES_WIDGETSTYLE_H
2+
#define _DISPLAY_STYLES_WIDGETSTYLE_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#include "lvgl.h"
9+
10+
// Initializes shared widget styles (must be called once at startup).
11+
void widget_style_init();
12+
13+
// Returns a pointer to a style without borders, for clean widget appearance
14+
const lv_style_t* get_widget_style_no_border();
15+
16+
#ifdef __cplusplus
17+
}
18+
#endif
19+
20+
#endif // _DISPLAY_STYLES_WIDGETSTYLE_H

0 commit comments

Comments
 (0)