Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Make dongle leds functional. #330

Merged
merged 1 commit into from
Oct 11, 2024
Merged
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
2 changes: 2 additions & 0 deletions device/src/device_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "shared/slave_protocol.h"
#include "legacy/event_scheduler.h"
#include "legacy/power_mode.h"
#include "dongle_leds.h"

static connection_type_t isConnected[ConnectionId_Count] = {};

Expand Down Expand Up @@ -75,6 +76,7 @@ void handleStateTransition(connection_id_t remoteId, bool connected) {
if (remoteId == ConnectionId_Right && connected) {
StateSync_ResetRightDongleLink(true);
}
DongleLeds_Update();
break;
default:
break;
Expand Down
29 changes: 29 additions & 0 deletions device/src/dongle_leds.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
#include "device.h"
#if DEVICE_IS_UHK_DONGLE
#include "dongle_leds.h"
#include "device_state.h"
#include "settings.h"

const struct pwm_dt_spec red_pwm_led = PWM_DT_SPEC_GET(DT_ALIAS(red_pwm_led));
const struct pwm_dt_spec green_pwm_led = PWM_DT_SPEC_GET(DT_ALIAS(green_pwm_led));
const struct pwm_dt_spec blue_pwm_led = PWM_DT_SPEC_GET(DT_ALIAS(blue_pwm_led));


// There is also the following zero led.
// const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(DT_ALIAS(led0_green), gpios);
// gpio_pin_configure_dt(&led0, GPIO_OUTPUT);
// gpio_pin_set_dt(&led0, true);
// k_sleep(K_MSEC(1000));
// gpio_pin_set_dt(&led0, false);

void set_dongle_led(const struct pwm_dt_spec *device, uint8_t percentage) {
pwm_set_pulse_dt(device, percentage * device->period / 100);
}


void DongleLeds_Set(bool r, bool g, bool b) {
set_dongle_led(&red_pwm_led, r ? 100 : 0);
set_dongle_led(&green_pwm_led, g ? 100 : 0);
set_dongle_led(&blue_pwm_led, b ? 100 : 0);
}

void DongleLeds_Update() {
if (DeviceState_IsConnected(ConnectionId_Right)) {
DongleLeds_Set(false, true, false);
return;
}
if (RightAddressIsSet) {
DongleLeds_Set(false, false, true);
return;
}
DongleLeds_Set(true, false, false);
}

#endif // DEVICE_IS_UHK_DONGLE
6 changes: 6 additions & 0 deletions device/src/dongle_leds.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
// Includes:

#include <zephyr/drivers/pwm.h>
#include <stdbool.h>
#include <stdint.h>

// Typedefs:

// Functions:

extern void DongleLeds_Set(bool r, bool g, bool b);
extern void DongleLeds_Update();
extern void set_dongle_led(const struct pwm_dt_spec *device, uint8_t percentage);

// Variables:
Expand Down
22 changes: 3 additions & 19 deletions device/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
#include "state_sync.h"
#include "keyboard/charger.h"
#include <stdint.h>
#include "dongle_leds.h"
#include "debug_eventloop_timing.h"
// #include <zephyr/drivers/gpio.h>
// #include "dongle_leds.h"
#include <zephyr/drivers/gpio.h>
#include "dongle_leds.h"

k_tid_t Main_ThreadId = 0;

Expand Down Expand Up @@ -98,23 +99,6 @@ int main(void) {
Main_ThreadId = k_current_get();
printk("----------\n" DEVICE_NAME " started\n");

// const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(DT_ALIAS(led0_green), gpios);
// gpio_pin_configure_dt(&led0, GPIO_OUTPUT);
// while (true) {
// gpio_pin_set_dt(&led0, true);
// k_sleep(K_MSEC(1000));
// gpio_pin_set_dt(&led0, false);
// set_dongle_led(&red_pwm_led, 100);
// k_sleep(K_MSEC(1000));
// set_dongle_led(&red_pwm_led, 0);
// set_dongle_led(&green_pwm_led, 100);
// k_sleep(K_MSEC(1000));
// set_dongle_led(&green_pwm_led, 0);
// set_dongle_led(&blue_pwm_led, 100);
// k_sleep(K_MSEC(1000));
// set_dongle_led(&blue_pwm_led, 0);
// }

if (DEVICE_IS_UHK80_RIGHT) {
flash_area_open(FLASH_AREA_ID(hardware_config_partition), &hardwareConfigArea);
flash_area_open(FLASH_AREA_ID(user_config_partition), &userConfigArea);
Expand Down
14 changes: 14 additions & 0 deletions device/src/settings.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#include <zephyr/bluetooth/addr.h>
#include <zephyr/settings/settings.h>
#include "bt_conn.h"
#include "dongle_leds.h"
#include "stubs.h"

bool RightAddressIsSet = false;

static int peerAddressSet(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg)
{
static char foo_val[BT_ADDR_SIZE];
read_cb(cb_arg, &foo_val, len);

bool rightAddressIsSet = false;

for (uint8_t i=0; i<PeerCount; i++) {
if (strcmp(name, Peers[i].name) == 0) {
printk("Settings: Found peer '%s' with address ", name);
Expand All @@ -17,10 +23,18 @@ static int peerAddressSet(const char *name, size_t len, settings_read_cb read_cb
printk("%02x", addr->a.val[j]);
}
printk("\n");
if (i == PeerIdRight) {
rightAddressIsSet = true;
}
break;
}
}

if (rightAddressIsSet != RightAddressIsSet) {
RightAddressIsSet = rightAddressIsSet;
DongleLeds_Update();
}

return 0;
}

Expand Down
9 changes: 9 additions & 0 deletions device/src/settings.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
#ifndef __SETTINGS_H__
#define __SETTINGS_H__

// Includes

#include <stdbool.h>
#include <stdint.h>

// Functions:

extern void InitSettings(void);

// Variables:

extern bool RightAddressIsSet;

#endif // __SETTINGS_H__
1 change: 1 addition & 0 deletions right/src/stubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
ATTRS void Uart_Log(const char *fmt, ...) {};
ATTRS void Log(const char *fmt, ...) {};
ATTRS void LogBt(const char *fmt, ...) {};
ATTRS void DongleLeds_Update() {};

#if DEVICE_HAS_OLED
#define WIDGET_REFRESH(W) Widget_Refresh(W)
Expand Down