Skip to content

Commit 30fd01d

Browse files
let's start from scratch with a better implementation
2 parents 9bdd146 + 403eb49 commit 30fd01d

37 files changed

+791
-1011
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
set(BOARD esp32s3_touch_lcd_1_28/esp32s3/procpu)
2+
set(DTC_OVERLAY_FILE boards/esp32.overlay)
23

34
cmake_minimum_required(VERSION 3.20.0)
45
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
@@ -7,5 +8,4 @@ project(Esp32SmartWatch)
78
file(GLOB_RECURSE app_sources src/*.c)
89

910
target_sources(app PRIVATE ${app_sources})
10-
target_include_directories(app PRIVATE inc)
11-
11+
target_include_directories(app PRIVATE src/)

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
```

boards/esp32.overlay

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
&timer0 {
2+
status = "okay";
3+
};

prj.conf

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
# Important for LVGL to work.
2-
CONFIG_MAIN_STACK_SIZE=4096
3-
CONFIG_LV_MEM_CUSTOM=y
4-
CONFIG_LV_Z_MEM_POOL_SIZE=16384
5-
61
# Display Configurations
72
CONFIG_DISPLAY=y
83
CONFIG_DISPLAY_LOG_LEVEL_ERR=y
@@ -20,25 +15,25 @@ CONFIG_LV_USE_LOG=n
2015
CONFIG_LVGL=y
2116
CONFIG_LV_FONT_MONTSERRAT_46=y
2217
CONFIG_LV_FONT_MONTSERRAT_18=y
18+
# Important for LVGL to work.
19+
CONFIG_MAIN_STACK_SIZE=4096
20+
# CONFIG_LV_MEM_CUSTOM=y
21+
CONFIG_LV_Z_MEM_POOL_SIZE=16384
2322

2423
# PWM Configurations
2524
CONFIG_PWM=y
2625

26+
# Bluetooth Settings
2727
# Bluetooth Configurations
2828
CONFIG_BT=y
2929
CONFIG_BT_PERIPHERAL=y
30-
CONFIG_BT_SETTINGS=y
3130
CONFIG_BT_DEVICE_NAME="ZephyrWatch"
32-
CONFIG_BT_SMP=y
33-
CONFIG_BT_SIGNING=y
34-
CONFIG_BT_KEYS_OVERWRITE_OLDEST=y
35-
CONFIG_BT_PRIVACY=y
3631

3732
# Bluetooth GATT Device Information Service Configuration
3833
CONFIG_BT_DIS=y
3934
CONFIG_BT_DIS_PNP=n
4035
CONFIG_BT_DIS_MODEL="ZephyrWatch_001"
41-
CONFIG_BT_DIS_MANUF="github.com/electricalgorithm"
36+
CONFIG_BT_DIS_MANUF="electricalgorithm"
4237
CONFIG_BT_DIS_SERIAL_NUMBER=y
4338
CONFIG_BT_DIS_FW_REV=y
4439
CONFIG_BT_DIS_HW_REV=y
@@ -50,10 +45,28 @@ CONFIG_BT_DIS_SW_REV_STR="0.0.1"
5045
CONFIG_BT_DIS_SETTINGS=y
5146
CONFIG_BT_DIS_STR_MAX=21
5247

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
65+
5366
# Flash Configurations
5467
CONFIG_FLASH=y
5568
CONFIG_FLASH_MAP=y
5669
CONFIG_NVS=y
5770

58-
# RTC
59-
CONFIG_RTC=y
71+
# Counter (using correct config name)
72+
CONFIG_COUNTER=y

src/bluetooth/bluetooth_infra.h

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/bluetooth/current_time_service.c

Lines changed: 0 additions & 83 deletions
This file was deleted.

src/bluetooth/current_time_service.h

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
#include <zephyr/bluetooth/gatt.h>
33
#include <zephyr/logging/log.h>
44

5-
65
LOG_MODULE_REGISTER(ZephyrWatch_BluetoothInfra, LOG_LEVEL_INF);
76

8-
97
static const struct bt_data m_ad[] = {
108
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
119
BT_DATA_BYTES(BT_DATA_UUID16_ALL, BT_UUID_16_ENCODE(BT_UUID_CTS_VAL)),
@@ -36,7 +34,7 @@ BT_CONN_CB_DEFINE(conn_callbacks) = {
3634
/* The API function to enable Bluetooth and start advertisement. */
3735
uint8_t enable_bluetooth_and_start_advertisement() {
3836
int err;
39-
37+
4038
err = bt_enable(NULL);
4139
if (err) {
4240
LOG_ERR("Bluetooth init failed (err %d).", err);

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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <zephyr/sys/byteorder.h>
2+
#include <zephyr/logging/log.h>
3+
#include <zephyr/bluetooth/gatt.h>
4+
#include <string.h>
5+
6+
#include "current_time_service.h"
7+
#include "timeutils/timeutils.h"
8+
#include "devicetwin/devicetwin.h"
9+
10+
LOG_MODULE_REGISTER(ZephyrWatch_CurrentTimeService, LOG_LEVEL_INF);
11+
12+
/* Current Time Service Write Callback */
13+
static ssize_t m_time_write_callback(
14+
struct bt_conn *conn,
15+
const struct bt_gatt_attr *attr,
16+
const void *buf,
17+
uint16_t len,
18+
uint16_t offset,
19+
uint8_t flags) {
20+
21+
// Check if we received exactly 4 bytes for UNIX timestamp
22+
if (len != 4 || offset != 0) {
23+
LOG_ERR("Invalid write length or offset. Expected 4 bytes at offset 0, got %d bytes at offset %d", len, offset);
24+
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
25+
}
26+
27+
// Extract the UNIX timestamp from the buffer (assuming little-endian)
28+
uint32_t unix_timestamp = sys_le32_to_cpu(*(uint32_t *)buf);
29+
LOG_INF("Received UNIX timestamp: %u", unix_timestamp);
30+
31+
// Get the device twin instance to get the UTC zone
32+
device_twin_t *device_twin = get_device_twin_instance();
33+
if (device_twin == NULL) {
34+
LOG_ERR("Failed to get device twin instance.");
35+
return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY);
36+
}
37+
38+
// Convert UNIX timestamp to local time using the device's UTC zone
39+
utc_time_t local_time = unix_to_localtime(unix_timestamp, device_twin->utc_zone);
40+
41+
// Update the device twin's current time with local time
42+
device_twin->current_time = local_time;
43+
44+
// Update the global unix time used by the UI system
45+
update_global_unix_time(unix_timestamp);
46+
47+
LOG_INF("Current time updated to local time: %04d-%02d-%02d %02d:%02d:%02d (UTC%+d)",
48+
local_time.year, local_time.month, local_time.day,
49+
local_time.hour, local_time.minute, local_time.second,
50+
device_twin->utc_zone);
51+
52+
return len;
53+
}
54+
55+
/* Dummy data for GATT characteristic - not used for actual data storage */
56+
static uint8_t dummy_data[4] = {0};
57+
58+
/* Current Time Service Declaration */
59+
BT_GATT_SERVICE_DEFINE(cts_cvs,
60+
BT_GATT_PRIMARY_SERVICE(BT_UUID_CTS),
61+
BT_GATT_CHARACTERISTIC(
62+
BT_UUID_CTS_CURRENT_TIME,
63+
BT_GATT_CHRC_WRITE,
64+
BT_GATT_PERM_WRITE,
65+
NULL, m_time_write_callback, dummy_data),
66+
);

0 commit comments

Comments
 (0)