Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ CONFIG_LV_FONT_MONTSERRAT_16=y
# Important for LVGL to work.
CONFIG_MAIN_STACK_SIZE=8192
# CONFIG_LV_MEM_CUSTOM=y
CONFIG_LV_Z_MEM_POOL_SIZE=16384
CONFIG_LV_Z_MEM_POOL_SIZE=32768

# PWM Configurations
CONFIG_PWM=y
Expand Down Expand Up @@ -63,6 +63,7 @@ CONFIG_SYSTEM_WORKQUEUE_PRIORITY=-1
# Input Subsystem Configuration - Disable input warnings
CONFIG_INPUT=y
CONFIG_INPUT_LOG_LEVEL_OFF=y
CONFIG_I2C_LOG_LEVEL_OFF=y

# Flash Configurations
CONFIG_FLASH=y
Expand All @@ -73,4 +74,4 @@ CONFIG_NVS=y
CONFIG_COUNTER=y

# Watchdog Timer
CONFIG_WATCHDOG=y
CONFIG_WATCHDOG=y
57 changes: 38 additions & 19 deletions src/bluetooth/infrastructure.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
* This file manages Bluetooth functionality, including advertising and connection handling.
*
* @license: GNU v3
* @maintainer: electricalgorithm @ github
* @maintainer: electricalgorithm @ github
*/

#include "userinterface/screens/blepairing/blepairing.h"
#include "zephyr/bluetooth/conn.h"
#include <zephyr/settings/settings.h>
#include <zephyr/bluetooth/gatt.h>
#include <zephyr/logging/log.h>
#include <zephyr/settings/settings.h>

LOG_MODULE_REGISTER(ZephyrWatch_BLE, LOG_LEVEL_INF);

Expand All @@ -22,7 +22,8 @@ static const struct bt_data m_sd[] = {
BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
};

static void start_advertisement() {
static void start_advertisement()
{
const int err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, m_ad, ARRAY_SIZE(m_ad), m_sd, ARRAY_SIZE(m_sd));
if (err) {
LOG_DBG("Advertising failed to start (err %d).", err);
Expand All @@ -31,16 +32,19 @@ static void start_advertisement() {
LOG_DBG("Advertising successfully started.");
}

static void process_connection(struct bt_conn *conn, uint8_t err) {
if (err) LOG_ERR("Connection failed (err %u).", err);
static void process_connection(struct bt_conn* conn, uint8_t err)
{
if (err)
LOG_ERR("Connection failed (err %u).", err);
else {
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
LOG_INF("Connection established to %s.", addr);
}
}

static void process_disconnection(struct bt_conn *conn, uint8_t reason) {
static void process_disconnection(struct bt_conn* conn, uint8_t reason)
{
LOG_INF("Disconnected (reason 0x%02x).", reason);
}

Expand All @@ -50,14 +54,16 @@ BT_CONN_CB_DEFINE(connection_callbacks) = {
.recycled = start_advertisement,
};

char* passkey_to_string(const unsigned int passkey) {
char* passkey_to_string(const unsigned int passkey)
{
static char passkey_str[7];
snprintf(passkey_str, sizeof(passkey_str), "%06u", passkey);
return passkey_str;
}

static void process_passkey_display(struct bt_conn *conn, unsigned int passkey){
char addr[BT_ADDR_LE_STR_LEN] = {0};
static void process_passkey_display(struct bt_conn* conn, unsigned int passkey)
{
char addr[BT_ADDR_LE_STR_LEN] = { 0 };
// Write PIN to the screen.
blepairing_screen_init();
blepairing_screen_set_pin(passkey_to_string(passkey));
Expand All @@ -68,19 +74,22 @@ static void process_passkey_display(struct bt_conn *conn, unsigned int passkey){
LOG_DBG("Passkey for %s: %06u", addr, passkey);
}

static void process_auth_cancel(struct bt_conn *conn){
static void process_auth_cancel(struct bt_conn* conn)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
LOG_DBG("Pairing cancelled: %s", addr);
blepairing_screen_unload();
}

static void process_pairing_complete(struct bt_conn *conn, bool bonded) {
static void process_pairing_complete(struct bt_conn* conn, bool bonded)
{
LOG_DBG("Pairing complete. Bonded: %s", bonded ? "OK" : "FAILURE");
blepairing_screen_unload();
}

static void process_pairing_failed(struct bt_conn *conn, enum bt_security_err reason) {
static void process_pairing_failed(struct bt_conn* conn, enum bt_security_err reason)
{
LOG_DBG("Pairing failed. Reason: 0x%02x", reason);
bt_conn_disconnect(conn, BT_HCI_ERR_AUTH_FAIL);
blepairing_screen_unload();
Expand All @@ -98,7 +107,8 @@ static struct bt_conn_auth_cb auth_callbacks = {
};

/* The API function to enable Bluetooth and start advertisement. */
uint8_t enable_bluetooth_subsystem() {
int enable_bluetooth_subsystem()
{
int err;

err = bt_enable(NULL);
Expand Down Expand Up @@ -136,21 +146,30 @@ uint8_t enable_bluetooth_subsystem() {
return 0;
}

uint8_t disable_bluetooth_subsystem() {
int disable_bluetooth_subsystem()
{
int err;

err = bt_le_adv_stop();
if (err) {
LOG_ERR("Advertising failed to stop (err %d).", err);
LOG_ERR("Failed to stop advertising (err %d).", err);
return err;
}
LOG_DBG("Advertising stopped successfully.");

err = bt_conn_auth_info_cb_unregister(&auth_info_callbacks);
if (err) {
LOG_ERR("Failed to unregister authentication information callbacks (err %d).", err);
return err;
}
LOG_DBG("Advertising successfully stopped.");
LOG_DBG("Authentication information callback unregistered successfully.");

err = bt_disable();
if (err) {
LOG_ERR("Bluetooth failed to disable (err %d).", err);
LOG_ERR("Failed to disable Bluetooth subsystem (err %d).", err);
return err;
}
LOG_DBG("Bluetooth disabled.");
LOG_DBG("Bluetooth subsystem disabled successfully.");

return 0;
}
}
10 changes: 5 additions & 5 deletions src/bluetooth/infrastructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This interface manages Bluetooth functionality, including advertising and connection handling.
*
* @license: GNU v3
* @maintainer: electricalgorithm @ github
* @maintainer: electricalgorithm @ github
*/

#ifndef BLUETOOTH_INFRASTRUCTURE_H_
Expand All @@ -12,15 +12,15 @@
extern "C" {
#endif

#include <zephyr/settings/settings.h>
#include <zephyr/bluetooth/gatt.h>
#include <zephyr/logging/log.h>
#include <zephyr/settings/settings.h>

uint8_t enable_bluetooth_subsystem();
uint8_t disable_bluetooth_subsystem();
int enable_bluetooth_subsystem();
int disable_bluetooth_subsystem();

#ifdef __cplusplus
}
#endif

#endif /* BLUETOOTH_INFRASTRUCTURE_H_ */
#endif /* BLUETOOTH_INFRASTRUCTURE_H_ */
45 changes: 38 additions & 7 deletions src/display/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ LOG_MODULE_REGISTER(ZephyrWatch_Display, LOG_LEVEL_INF);
/* ENABLE_DISPLAY_SUBSYSTEM
* Set the Zephyr display device and set backlight.
*/
int enable_display_subsystem() {
int enable_display_subsystem()
{
int ret;

const struct device *display_dev = DEVICE_DT_GET(DISPLAY_DEVICE);
const struct device* display_dev = DEVICE_DT_GET(DISPLAY_DEVICE);
ret = device_is_ready(display_dev);
if (!ret) {
LOG_ERR("Display device is not ready, exiting... (RET: %d)", ret);
Expand Down Expand Up @@ -56,19 +57,49 @@ int enable_display_subsystem() {
return 0;
}


/* ENABLE_DISPLAY_SUBSYSTEM
* Disable the Zephyr display device and set backlight to 0.
*/
int disable_display_subsystem() {
int disable_display_subsystem()
{
LOG_DBG("Not implemented yet.");
return 0;
}

/* CHANGE_BRIGHTNESS
* Change the brightness based on a percentage.
*/
int change_brightness(uint8_t perc) {
LOG_DBG("Not implemented yet.");
int change_brightness(uint8_t perc)
{
int ret;
const struct pwm_dt_spec backlight = PWM_DT_SPEC_GET_BY_IDX(DISPLAY_PWM_DEVICE, 0);

// Check if PWM is ready
if (!pwm_is_ready_dt(&backlight)) {
LOG_ERR("PWM device is not ready");
return -ENODEV;
}
LOG_DBG("PWM device is ready");

// Clamp percentage to max 100% and min 5%
if (perc > 100)
perc = 100;
if (perc < 5)
perc = 5;

// Define a PWM period (in nanoseconds). For example, 20 kHz = 50 us = 50000 ns
uint32_t period_ns = 50000;

// Calculate pulse width (duty cycle) from percentage
uint32_t pulse_ns = (period_ns * perc) / 100;

// Set the PWM signal
ret = pwm_set_dt(&backlight, period_ns, pulse_ns);
if (ret < 0) {
LOG_ERR("Failed to set PWM (ret = %d)", ret);
return ret;
}

LOG_DBG("Brightness set to %d%%", perc);
return 0;
}
}
Loading