2022-08-03 14:53:42 +06:00
|
|
|
/* Copyright 2021 @ lokher (https://www.keychron.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "quantum.h"
|
|
|
|
#include "indicator.h"
|
|
|
|
#include "transport.h"
|
|
|
|
#include "battery.h"
|
|
|
|
#include "eeconfig.h"
|
|
|
|
#include "bluetooth_config.h"
|
|
|
|
#include "config.h"
|
2023-01-12 13:50:37 +06:00
|
|
|
#include "rtc_timer.h"
|
|
|
|
|
2022-08-03 14:53:42 +06:00
|
|
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
|
|
|
# ifdef LED_MATRIX_ENABLE
|
|
|
|
# include "led_matrix.h"
|
|
|
|
# endif
|
|
|
|
# ifdef RGB_MATRIX_ENABLE
|
|
|
|
# include "rgb_matrix.h"
|
|
|
|
# endif
|
|
|
|
# include "i2c_master.h"
|
|
|
|
# include "bat_level_animation.h"
|
2022-09-22 15:33:56 +06:00
|
|
|
# include "eeprom.h"
|
2022-08-03 14:53:42 +06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LED_MATRIX_ENABLE
|
2022-12-06 15:04:10 +06:00
|
|
|
# define DECIDE_TIME(t, duration) (duration == 0 ? LED_MATRIX_TIMEOUT_INFINITE : ((t > duration) ? t : duration))
|
2022-08-03 14:53:42 +06:00
|
|
|
#endif
|
|
|
|
#ifdef RGB_MATRIX_ENABLE
|
2022-12-06 15:04:10 +06:00
|
|
|
# define DECIDE_TIME(t, duration) (duration == 0 ? RGB_MATRIX_TIMEOUT_INFINITE : ((t > duration) ? t : duration))
|
2022-08-03 14:53:42 +06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define LED_ON 0x80
|
2022-09-22 15:33:56 +06:00
|
|
|
#define INDICATOR_SET(s) memcpy(&indicator_config, &s##_config, sizeof(indicator_config_t));
|
2022-08-03 14:53:42 +06:00
|
|
|
|
|
|
|
enum {
|
|
|
|
BACKLIGHT_OFF = 0x00,
|
|
|
|
BACKLIGHT_ON_CONNECTED = 0x01,
|
|
|
|
BACKLIGHT_ON_UNCONNECTED = 0x02,
|
|
|
|
};
|
|
|
|
|
2022-09-22 15:33:56 +06:00
|
|
|
static indicator_config_t pairing_config = INDICATOR_CONFIG_PARING;
|
|
|
|
static indicator_config_t connected_config = INDICATOR_CONFIG_CONNECTD;
|
|
|
|
static indicator_config_t reconnecting_config = INDICATOR_CONFIG_RECONNECTING;
|
|
|
|
static indicator_config_t disconnected_config = INDICATOR_CONFIG_DISCONNECTED;
|
2022-08-03 14:53:42 +06:00
|
|
|
indicator_config_t indicator_config;
|
|
|
|
static bluetooth_state_t indicator_state;
|
|
|
|
static uint16_t next_period;
|
|
|
|
static indicator_type_t type;
|
2022-08-10 13:19:01 +06:00
|
|
|
static uint32_t indicator_timer_buffer = 0;
|
2023-01-12 13:50:37 +06:00
|
|
|
|
2023-03-02 15:31:35 +06:00
|
|
|
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
2023-01-12 13:50:37 +06:00
|
|
|
static uint32_t bat_low_pin_indicator = 0;
|
|
|
|
static uint32_t bat_low_blink_duration = 0;
|
2023-03-02 15:31:35 +06:00
|
|
|
# ifdef BAT_LOW_LED_PIN_STATE
|
|
|
|
bool bat_low_led_pin_state = false;
|
|
|
|
# endif
|
2023-01-12 13:50:37 +06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(LOW_BAT_IND_INDEX)
|
|
|
|
static uint32_t bat_low_backlit_indicator = 0;
|
|
|
|
static uint8_t bat_low_ind_state = 0;
|
|
|
|
static uint32_t rtc_time = 0;
|
|
|
|
#endif
|
2022-08-03 14:53:42 +06:00
|
|
|
|
|
|
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
|
|
|
backlight_state_t original_backlight_state;
|
|
|
|
|
|
|
|
static uint8_t host_led_matrix_list[HOST_DEVICES_COUNT] = HOST_LED_MATRIX_LIST;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HOST_LED_PIN_LIST
|
|
|
|
static pin_t host_led_pin_list[HOST_DEVICES_COUNT] = HOST_LED_PIN_LIST;
|
|
|
|
#endif
|
|
|
|
|
2022-09-22 15:33:56 +06:00
|
|
|
#ifdef LED_MATRIX_ENABLE
|
|
|
|
# define LED_DRIVER led_matrix_driver
|
|
|
|
# define LED_INDICATORS_KB led_matrix_indicators_kb
|
2023-03-02 15:31:35 +06:00
|
|
|
# define LED_INDICATORS_USER led_matrix_indicators_user
|
2022-09-22 15:33:56 +06:00
|
|
|
# define LED_NONE_INDICATORS_KB led_matrix_none_indicators_kb
|
|
|
|
# define SET_ALL_LED_OFF() led_matrix_set_value_all(0)
|
|
|
|
# define SET_LED_OFF(idx) led_matrix_set_value(idx, 0)
|
|
|
|
# define SET_LED_ON(idx) led_matrix_set_value(idx, 255)
|
|
|
|
# define SET_LED_BT(idx) led_matrix_set_value(idx, 255)
|
2023-01-12 13:50:37 +06:00
|
|
|
# define SET_LED_LOW_BAT(idx) led_matrix_set_value(idx, 255)
|
2022-09-22 15:33:56 +06:00
|
|
|
# define LED_DRIVER_IS_ENABLED led_matrix_is_enabled
|
|
|
|
# define LED_DRIVER_EECONFIG_RELOAD() \
|
|
|
|
eeprom_read_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); \
|
|
|
|
if (!led_matrix_eeconfig.mode) { \
|
|
|
|
eeconfig_update_led_matrix_default(); \
|
|
|
|
}
|
|
|
|
# define LED_DRIVER_ALLOW_SHUTDOWN led_matrix_driver_allow_shutdown
|
|
|
|
# define LED_DRIVER_ENABLE_NOEEPROM led_matrix_enable_noeeprom
|
|
|
|
# define LED_DRIVER_DISABLE_NOEEPROM led_matrix_disable_noeeprom
|
|
|
|
# define LED_DRIVER_DISABLE_TIMEOUT_SET led_matrix_disable_timeout_set
|
|
|
|
# define LED_DRIVER_DISABLE_TIME_RESET led_matrix_disable_time_reset
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RGB_MATRIX_ENABLE
|
|
|
|
# define LED_DRIVER rgb_matrix_driver
|
|
|
|
# define LED_INDICATORS_KB rgb_matrix_indicators_kb
|
2023-03-02 15:31:35 +06:00
|
|
|
# define LED_INDICATORS_USER rgb_matrix_indicators_user
|
2022-09-22 15:33:56 +06:00
|
|
|
# define LED_NONE_INDICATORS_KB rgb_matrix_none_indicators_kb
|
|
|
|
# define SET_ALL_LED_OFF() rgb_matrix_set_color_all(0, 0, 0)
|
|
|
|
# define SET_LED_OFF(idx) rgb_matrix_set_color(idx, 0, 0, 0)
|
|
|
|
# define SET_LED_ON(idx) rgb_matrix_set_color(idx, 255, 255, 255)
|
|
|
|
# define SET_LED_BT(idx) rgb_matrix_set_color(idx, 0, 0, 255)
|
2023-01-12 13:50:37 +06:00
|
|
|
# define SET_LED_LOW_BAT(idx) rgb_matrix_set_color(idx, 255, 0, 0)
|
2022-09-22 15:33:56 +06:00
|
|
|
# define LED_DRIVER_IS_ENABLED rgb_matrix_is_enabled
|
|
|
|
# define LED_DRIVER_EECONFIG_RELOAD() \
|
|
|
|
eeprom_read_block(&rgb_matrix_config, EECONFIG_RGB_MATRIX, sizeof(rgb_matrix_config)); \
|
|
|
|
if (!rgb_matrix_config.mode) { \
|
|
|
|
eeconfig_update_rgb_matrix_default(); \
|
|
|
|
}
|
|
|
|
# define LED_DRIVER_ALLOW_SHUTDOWN rgb_matrix_driver_allow_shutdown
|
|
|
|
# define LED_DRIVER_ENABLE_NOEEPROM rgb_matrix_enable_noeeprom
|
|
|
|
# define LED_DRIVER_DISABLE_NOEEPROM rgb_matrix_disable_noeeprom
|
|
|
|
# define LED_DRIVER_DISABLE_TIMEOUT_SET rgb_matrix_disable_timeout_set
|
|
|
|
# define LED_DRIVER_DISABLE_TIME_RESET rgb_matrix_disable_time_reset
|
|
|
|
#endif
|
2022-08-03 14:53:42 +06:00
|
|
|
void indicator_init(void) {
|
2022-08-10 13:19:01 +06:00
|
|
|
memset(&indicator_config, 0, sizeof(indicator_config));
|
2023-01-12 13:50:37 +06:00
|
|
|
|
2022-08-03 14:53:42 +06:00
|
|
|
#ifdef HOST_LED_PIN_LIST
|
|
|
|
for (uint8_t i = 0; i < HOST_DEVICES_COUNT; i++) {
|
|
|
|
setPinOutput(host_led_pin_list[i]);
|
|
|
|
writePin(host_led_pin_list[i], !HOST_LED_PIN_ON_STATE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BAT_LOW_LED_PIN
|
|
|
|
setPinOutput(BAT_LOW_LED_PIN);
|
|
|
|
writePin(BAT_LOW_LED_PIN, !BAT_LOW_LED_PIN_ON_STATE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
|
|
|
void indicator_enable(void) {
|
2022-09-22 15:33:56 +06:00
|
|
|
if (!LED_DRIVER_IS_ENABLED()) {
|
|
|
|
LED_DRIVER_ENABLE_NOEEPROM();
|
2022-08-03 14:53:42 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 15:33:56 +06:00
|
|
|
inline void indicator_disable(void) {
|
|
|
|
LED_DRIVER_DISABLE_NOEEPROM();
|
2022-08-03 14:53:42 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
void indicator_set_backlit_timeout(uint32_t time) {
|
2022-09-22 15:33:56 +06:00
|
|
|
LED_DRIVER_DISABLE_TIMEOUT_SET(time);
|
2022-08-03 14:53:42 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void indicator_reset_backlit_time(void) {
|
2022-09-22 15:33:56 +06:00
|
|
|
LED_DRIVER_DISABLE_TIME_RESET();
|
2022-08-03 14:53:42 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool indicator_is_enabled(void) {
|
2022-09-22 15:33:56 +06:00
|
|
|
return LED_DRIVER_IS_ENABLED();
|
2022-08-03 14:53:42 +06:00
|
|
|
}
|
|
|
|
|
2022-09-22 15:33:56 +06:00
|
|
|
void indicator_eeconfig_reload(void) {
|
|
|
|
LED_DRIVER_EECONFIG_RELOAD();
|
2022-08-03 14:53:42 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-09-22 15:33:56 +06:00
|
|
|
bool indicator_is_running(void) {
|
2023-01-12 13:50:37 +06:00
|
|
|
return
|
2023-03-02 15:31:35 +06:00
|
|
|
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
2023-01-12 13:50:37 +06:00
|
|
|
bat_low_blink_duration ||
|
|
|
|
#endif
|
|
|
|
#if defined(LOW_BAT_IND_INDEX)
|
|
|
|
bat_low_ind_state ||
|
|
|
|
#endif
|
|
|
|
!!indicator_config.value;
|
2022-09-22 15:33:56 +06:00
|
|
|
}
|
2022-08-03 14:53:42 +06:00
|
|
|
|
|
|
|
static void indicator_timer_cb(void *arg) {
|
|
|
|
if (*(indicator_type_t *)arg != INDICATOR_LAST) type = *(indicator_type_t *)arg;
|
|
|
|
|
|
|
|
bool time_up = false;
|
|
|
|
switch (type) {
|
|
|
|
case INDICATOR_NONE:
|
|
|
|
break;
|
|
|
|
case INDICATOR_OFF:
|
|
|
|
next_period = 0;
|
|
|
|
time_up = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INDICATOR_ON:
|
|
|
|
if (indicator_config.value) {
|
|
|
|
if (indicator_config.elapsed == 0) {
|
|
|
|
indicator_config.value |= LED_ON;
|
|
|
|
|
|
|
|
if (indicator_config.duration) {
|
|
|
|
indicator_config.elapsed += indicator_config.duration;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
time_up = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INDICATOR_ON_OFF:
|
|
|
|
if (indicator_config.value) {
|
|
|
|
if (indicator_config.elapsed == 0) {
|
|
|
|
indicator_config.value |= LED_ON;
|
|
|
|
next_period = indicator_config.on_time;
|
|
|
|
} else {
|
|
|
|
indicator_config.value = indicator_config.value & 0x0F;
|
|
|
|
next_period = indicator_config.duration - indicator_config.on_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((indicator_config.duration == 0 || indicator_config.elapsed <= indicator_config.duration) && next_period != 0) {
|
|
|
|
indicator_config.elapsed += next_period;
|
|
|
|
} else {
|
|
|
|
time_up = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INDICATOR_BLINK:
|
|
|
|
if (indicator_config.value) {
|
|
|
|
if (indicator_config.value & LED_ON) {
|
|
|
|
indicator_config.value = indicator_config.value & 0x0F;
|
|
|
|
next_period = indicator_config.off_time;
|
|
|
|
} else {
|
|
|
|
indicator_config.value |= LED_ON;
|
|
|
|
next_period = indicator_config.on_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((indicator_config.duration == 0 || indicator_config.elapsed <= indicator_config.duration) && next_period != 0) {
|
|
|
|
indicator_config.elapsed += next_period;
|
2022-08-26 07:48:51 +06:00
|
|
|
} else {
|
2022-08-03 14:53:42 +06:00
|
|
|
time_up = true;
|
2022-08-26 07:48:51 +06:00
|
|
|
}
|
2022-08-03 14:53:42 +06:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
time_up = true;
|
|
|
|
|
|
|
|
next_period = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-08-26 07:48:51 +06:00
|
|
|
#ifdef HOST_LED_PIN_LIST
|
2022-09-22 15:33:56 +06:00
|
|
|
if (indicator_config.value) {
|
2022-08-26 07:48:51 +06:00
|
|
|
uint8_t idx = (indicator_config.value & 0x0F) - 1;
|
|
|
|
|
|
|
|
if (idx < HOST_DEVICES_COUNT) {
|
|
|
|
if ((indicator_config.value & 0x80) && !time_up) {
|
|
|
|
writePin(host_led_pin_list[idx], HOST_LED_PIN_ON_STATE);
|
|
|
|
} else {
|
|
|
|
writePin(host_led_pin_list[idx], !HOST_LED_PIN_ON_STATE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-08-03 14:53:42 +06:00
|
|
|
if (time_up) {
|
|
|
|
/* Set indicator to off on timeup, avoid keeping light up until next update in raindrop effect */
|
|
|
|
indicator_config.value = indicator_config.value & 0x0F;
|
2022-09-22 15:33:56 +06:00
|
|
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
|
|
|
LED_INDICATORS_KB();
|
2022-08-03 14:53:42 +06:00
|
|
|
#endif
|
|
|
|
indicator_config.value = 0;
|
|
|
|
}
|
|
|
|
|
2022-09-22 15:33:56 +06:00
|
|
|
if (indicator_config.value == 0) {
|
|
|
|
indicator_eeconfig_reload();
|
|
|
|
if (!LED_DRIVER_IS_ENABLED()) indicator_disable();
|
2022-08-03 14:53:42 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void indicator_set(bluetooth_state_t state, uint8_t host_index) {
|
|
|
|
if (get_transport() != TRANSPORT_BLUETOOTH) return;
|
|
|
|
dprintf("indicator set: %d, %d\n", state, host_index);
|
|
|
|
|
|
|
|
static uint8_t current_state = 0;
|
|
|
|
static uint8_t current_host = 0;
|
|
|
|
|
|
|
|
bool host_index_changed = false;
|
|
|
|
if (current_host != host_index && state != BLUETOOTH_DISCONNECTED) {
|
|
|
|
host_index_changed = true;
|
|
|
|
current_host = host_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current_state != state || host_index_changed) {
|
|
|
|
current_state = state;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
indicator_timer_buffer = sync_timer_read32();
|
|
|
|
|
|
|
|
/* Turn on backlight mode for indicator */
|
|
|
|
indicator_enable();
|
|
|
|
indicator_reset_backlit_time();
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case BLUETOOTH_DISCONNECTED:
|
2023-01-12 13:50:37 +06:00
|
|
|
#ifdef HOST_LED_PIN_LIST
|
|
|
|
writePin(host_led_pin_list[host_index - 1], !HOST_LED_PIN_ON_STATE);
|
|
|
|
#endif
|
2022-08-03 14:53:42 +06:00
|
|
|
INDICATOR_SET(disconnected);
|
|
|
|
indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : host_index;
|
|
|
|
indicator_timer_cb((void *)&indicator_config.type);
|
|
|
|
|
|
|
|
if (battery_is_critical_low()) {
|
|
|
|
indicator_set_backlit_timeout(1000);
|
|
|
|
} else {
|
|
|
|
/* Set timer so that user has chance to turn on the backlight when is off */
|
|
|
|
indicator_set_backlit_timeout(DECIDE_TIME(DISCONNECTED_BACKLIGHT_DISABLE_TIMEOUT * 1000, indicator_config.duration));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BLUETOOTH_CONNECTED:
|
|
|
|
if (indicator_state != BLUETOOTH_CONNECTED) {
|
|
|
|
INDICATOR_SET(connected);
|
|
|
|
indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : host_index;
|
|
|
|
indicator_timer_cb((void *)&indicator_config.type);
|
|
|
|
}
|
|
|
|
indicator_set_backlit_timeout(DECIDE_TIME(CONNECTED_BACKLIGHT_DISABLE_TIMEOUT * 1000, indicator_config.duration));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BLUETOOTH_PARING:
|
|
|
|
INDICATOR_SET(pairing);
|
|
|
|
indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : LED_ON | host_index;
|
|
|
|
indicator_timer_cb((void *)&indicator_config.type);
|
|
|
|
indicator_set_backlit_timeout(DECIDE_TIME(DISCONNECTED_BACKLIGHT_DISABLE_TIMEOUT * 1000, indicator_config.duration));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BLUETOOTH_RECONNECTING:
|
|
|
|
INDICATOR_SET(reconnecting);
|
|
|
|
indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : LED_ON | host_index;
|
|
|
|
indicator_timer_cb((void *)&indicator_config.type);
|
|
|
|
indicator_set_backlit_timeout(DECIDE_TIME(DISCONNECTED_BACKLIGHT_DISABLE_TIMEOUT * 1000, indicator_config.duration));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BLUETOOTH_SUSPEND:
|
|
|
|
INDICATOR_SET(disconnected);
|
|
|
|
indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : host_index;
|
|
|
|
indicator_timer_cb((void *)&indicator_config.type);
|
|
|
|
indicator_set_backlit_timeout(100);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
indicator_state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void indicator_stop(void) {
|
|
|
|
indicator_config.value = 0;
|
2022-09-22 15:33:56 +06:00
|
|
|
indicator_eeconfig_reload();
|
2022-08-03 14:53:42 +06:00
|
|
|
|
2022-09-22 15:33:56 +06:00
|
|
|
if (indicator_is_enabled()) {
|
2022-08-03 14:53:42 +06:00
|
|
|
indicator_enable();
|
|
|
|
} else {
|
|
|
|
indicator_disable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 15:31:35 +06:00
|
|
|
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
2022-08-03 14:53:42 +06:00
|
|
|
void indicator_battery_low_enable(bool enable) {
|
2023-01-12 13:50:37 +06:00
|
|
|
if (enable) {
|
|
|
|
if (bat_low_blink_duration == 0) {
|
|
|
|
bat_low_blink_duration = bat_low_pin_indicator = sync_timer_read32() | 1;
|
|
|
|
} else
|
|
|
|
bat_low_blink_duration = sync_timer_read32() | 1;
|
2023-03-02 15:31:35 +06:00
|
|
|
} else {
|
|
|
|
# if defined(BAT_LOW_LED_PIN)
|
2023-01-12 13:50:37 +06:00
|
|
|
writePin(BAT_LOW_LED_PIN, !BAT_LOW_LED_PIN_ON_STATE);
|
2023-03-02 15:31:35 +06:00
|
|
|
# else
|
|
|
|
bat_low_led_pin_state = false;
|
|
|
|
# endif
|
|
|
|
}
|
2023-01-12 13:50:37 +06:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(LOW_BAT_IND_INDEX)
|
|
|
|
void indicator_battery_low_backlit_enable(bool enable) {
|
|
|
|
if (enable) {
|
|
|
|
uint32_t t = rtc_timer_read_ms();
|
|
|
|
/* Check overflow */
|
|
|
|
if (rtc_time > t) {
|
|
|
|
if (bat_low_ind_state == 0)
|
|
|
|
rtc_time = t; // Update rtc_time if indicating is not running
|
|
|
|
else {
|
|
|
|
rtc_time += t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Indicating at first time or after the interval */
|
|
|
|
if ((rtc_time == 0 || t - rtc_time > LOW_BAT_LED_TRIG_INTERVAL) && bat_low_ind_state == 0) {
|
|
|
|
bat_low_backlit_indicator = enable ? (timer_read32() | 1) : 0;
|
|
|
|
rtc_time = rtc_timer_read_ms();
|
|
|
|
bat_low_ind_state = 1;
|
|
|
|
|
|
|
|
indicator_enable();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rtc_time = 0;
|
|
|
|
bat_low_ind_state = 0;
|
|
|
|
|
|
|
|
indicator_eeconfig_reload();
|
|
|
|
if (!LED_DRIVER_IS_ENABLED()) indicator_disable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void indicator_battery_low(void) {
|
2023-03-02 15:31:35 +06:00
|
|
|
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
2023-01-12 13:50:37 +06:00
|
|
|
if (bat_low_pin_indicator && sync_timer_elapsed32(bat_low_pin_indicator) > (LOW_BAT_LED_BLINK_PERIOD)) {
|
2023-03-02 15:31:35 +06:00
|
|
|
# if defined(BAT_LOW_LED_PIN)
|
2023-01-12 13:50:37 +06:00
|
|
|
togglePin(BAT_LOW_LED_PIN);
|
2023-03-02 15:31:35 +06:00
|
|
|
# else
|
|
|
|
bat_low_led_pin_state = !bat_low_led_pin_state;
|
|
|
|
# endif
|
2023-01-12 13:50:37 +06:00
|
|
|
bat_low_pin_indicator = sync_timer_read32() | 1;
|
|
|
|
// Turn off low battery indication if we reach the duration
|
2023-03-02 15:31:35 +06:00
|
|
|
# if defined(BAT_LOW_LED_PIN)
|
2023-01-12 13:50:37 +06:00
|
|
|
if (sync_timer_elapsed32(bat_low_blink_duration) > LOW_BAT_LED_BLINK_DURATION && palReadLine(BAT_LOW_LED_PIN) != BAT_LOW_LED_PIN_ON_STATE) {
|
2023-03-02 15:31:35 +06:00
|
|
|
# elif defined(BAT_LOW_LED_PIN_STATE)
|
|
|
|
if (sync_timer_elapsed32(bat_low_blink_duration) > LOW_BAT_LED_BLINK_DURATION) {
|
|
|
|
# endif
|
2023-01-12 13:50:37 +06:00
|
|
|
bat_low_blink_duration = bat_low_pin_indicator = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(LOW_BAT_IND_INDEX)
|
|
|
|
if (bat_low_ind_state) {
|
|
|
|
if ((bat_low_ind_state & 0x0F) <= (LOW_BAT_LED_BLINK_TIMES) && sync_timer_elapsed32(bat_low_backlit_indicator) > (LOW_BAT_LED_BLINK_PERIOD)) {
|
|
|
|
if (bat_low_ind_state & 0x80) {
|
|
|
|
bat_low_ind_state &= 0x7F;
|
|
|
|
bat_low_ind_state++;
|
|
|
|
} else {
|
|
|
|
bat_low_ind_state |= 0x80;
|
|
|
|
}
|
|
|
|
|
|
|
|
bat_low_backlit_indicator = sync_timer_read32() | 1;
|
|
|
|
|
|
|
|
/* Restore backligth state */
|
|
|
|
if ((bat_low_ind_state & 0x0F) > (LOW_BAT_LED_BLINK_TIMES)) {
|
|
|
|
# if defined(NUM_LOCK_INDEX) || defined(CAPS_LOCK_INDEX) || defined(SCROLL_LOCK_INDEX) || defined(COMPOSE_LOCK_INDEX) || defined(KANA_LOCK_INDEX)
|
|
|
|
if (LED_DRIVER_ALLOW_SHUTDOWN())
|
|
|
|
# endif
|
|
|
|
indicator_disable();
|
|
|
|
}
|
|
|
|
} else if ((bat_low_ind_state & 0x0F) > (LOW_BAT_LED_BLINK_TIMES)) {
|
|
|
|
bat_low_ind_state = 0;
|
|
|
|
}
|
|
|
|
}
|
2022-08-03 14:53:42 +06:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void indicator_task(void) {
|
|
|
|
bat_level_animiation_task();
|
|
|
|
|
|
|
|
if (indicator_config.value && sync_timer_elapsed32(indicator_timer_buffer) >= next_period) {
|
|
|
|
indicator_timer_cb((void *)&type);
|
|
|
|
indicator_timer_buffer = sync_timer_read32();
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:50:37 +06:00
|
|
|
indicator_battery_low();
|
2022-08-03 14:53:42 +06:00
|
|
|
}
|
|
|
|
|
2022-09-22 15:33:56 +06:00
|
|
|
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
|
|
|
static void os_state_indicate(void) {
|
|
|
|
# if defined(NUM_LOCK_INDEX)
|
|
|
|
if (host_keyboard_led_state().num_lock) {
|
|
|
|
SET_LED_ON(NUM_LOCK_INDEX);
|
|
|
|
}
|
2022-08-03 14:53:42 +06:00
|
|
|
# endif
|
2022-09-22 15:33:56 +06:00
|
|
|
# if defined(CAPS_LOCK_INDEX)
|
|
|
|
if (host_keyboard_led_state().caps_lock) {
|
|
|
|
# if defined(DIM_CAPS_LOCK)
|
|
|
|
SET_LED_OFF(CAPS_LOCK_INDEX);
|
|
|
|
# else
|
|
|
|
SET_LED_ON(CAPS_LOCK_INDEX);
|
|
|
|
# endif
|
2022-08-03 14:53:42 +06:00
|
|
|
}
|
2022-09-22 15:33:56 +06:00
|
|
|
# endif
|
|
|
|
# if defined(SCROLL_LOCK_INDEX)
|
|
|
|
if (host_keyboard_led_state().scroll_lock) {
|
|
|
|
SET_LED_ON(SCROLL_LOCK_INDEX);
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
# if defined(COMPOSE_LOCK_INDEX)
|
|
|
|
if (host_keyboard_led_state().compose) {
|
|
|
|
SET_LED_ON(COMPOSE_LOCK_INDEX);
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
# if defined(KANA_LOCK_INDEX)
|
|
|
|
if (host_keyboard_led_state().kana) {
|
|
|
|
SET_LED_ON(KANA_LOCK_INDEX);
|
|
|
|
}
|
|
|
|
# endif
|
2022-08-03 14:53:42 +06:00
|
|
|
}
|
|
|
|
|
2022-12-06 15:04:10 +06:00
|
|
|
bool LED_INDICATORS_KB(void) {
|
2023-03-02 15:31:35 +06:00
|
|
|
if (!LED_INDICATORS_USER()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-22 15:33:56 +06:00
|
|
|
if (get_transport() == TRANSPORT_BLUETOOTH) {
|
|
|
|
/* Prevent backlight flash caused by key activities */
|
2022-08-03 14:53:42 +06:00
|
|
|
if (battery_is_critical_low()) {
|
2022-09-22 15:33:56 +06:00
|
|
|
SET_ALL_LED_OFF();
|
2022-12-06 15:04:10 +06:00
|
|
|
return false;
|
2022-08-03 14:53:42 +06:00
|
|
|
}
|
|
|
|
|
2023-01-12 13:50:37 +06:00
|
|
|
# if (defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)) && defined(LOW_BAT_IND_INDEX)
|
|
|
|
if (battery_is_empty()) SET_ALL_LED_OFF();
|
|
|
|
if (bat_low_ind_state && (bat_low_ind_state & 0x0F) <= LOW_BAT_LED_BLINK_TIMES) {
|
|
|
|
if (bat_low_ind_state & 0x80)
|
|
|
|
SET_LED_LOW_BAT(LOW_BAT_IND_INDEX);
|
|
|
|
else
|
|
|
|
SET_LED_OFF(LOW_BAT_IND_INDEX);
|
|
|
|
}
|
|
|
|
# endif
|
2022-08-03 14:53:42 +06:00
|
|
|
if (bat_level_animiation_actived()) {
|
|
|
|
bat_level_animiation_indicate();
|
|
|
|
}
|
|
|
|
static uint8_t last_host_index = 0xFF;
|
|
|
|
|
|
|
|
if (indicator_config.value) {
|
|
|
|
uint8_t host_index = indicator_config.value & 0x0F;
|
|
|
|
|
|
|
|
if (indicator_config.highlight) {
|
2022-09-22 15:33:56 +06:00
|
|
|
SET_ALL_LED_OFF();
|
2022-08-03 14:53:42 +06:00
|
|
|
} else if (last_host_index != host_index) {
|
2022-09-22 15:33:56 +06:00
|
|
|
SET_LED_OFF(host_led_matrix_list[last_host_index - 1]);
|
2022-08-03 14:53:42 +06:00
|
|
|
last_host_index = host_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (indicator_config.value & 0x80) {
|
2022-09-22 15:33:56 +06:00
|
|
|
SET_LED_BT(host_led_matrix_list[host_index - 1]);
|
2022-08-03 14:53:42 +06:00
|
|
|
} else {
|
2022-09-22 15:33:56 +06:00
|
|
|
SET_LED_OFF(host_led_matrix_list[host_index - 1]);
|
2022-08-03 14:53:42 +06:00
|
|
|
}
|
2022-09-22 15:33:56 +06:00
|
|
|
} else
|
|
|
|
os_state_indicate();
|
|
|
|
|
|
|
|
} else
|
|
|
|
os_state_indicate();
|
2023-01-12 13:50:37 +06:00
|
|
|
|
2022-12-06 15:04:10 +06:00
|
|
|
return false;
|
2022-09-22 15:33:56 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool led_update_user(led_t led_state) {
|
|
|
|
if (!LED_DRIVER_IS_ENABLED()) {
|
2023-01-12 13:50:37 +06:00
|
|
|
# if defined(LED_MATRIX_DRIVER_SHUTDOWN_ENABLE) || defined(RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE)
|
2022-09-22 15:33:56 +06:00
|
|
|
LED_DRIVER.exit_shutdown();
|
|
|
|
# endif
|
|
|
|
SET_ALL_LED_OFF();
|
|
|
|
os_state_indicate();
|
|
|
|
LED_DRIVER.flush();
|
2023-01-12 13:50:37 +06:00
|
|
|
# if defined(LED_MATRIX_DRIVER_SHUTDOWN_ENABLE) || defined(RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE)
|
2022-09-22 15:33:56 +06:00
|
|
|
if (LED_DRIVER_ALLOW_SHUTDOWN()) LED_DRIVER.shutdown();
|
2022-08-03 14:53:42 +06:00
|
|
|
# endif
|
|
|
|
}
|
2022-09-22 15:33:56 +06:00
|
|
|
return true;
|
2022-08-03 14:53:42 +06:00
|
|
|
}
|
2022-09-22 15:33:56 +06:00
|
|
|
|
|
|
|
void LED_NONE_INDICATORS_KB(void) {
|
|
|
|
os_state_indicate();
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:50:37 +06:00
|
|
|
# if defined(LED_MATRIX_DRIVER_SHUTDOWN_ENABLE) || defined(RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE)
|
2022-09-22 15:33:56 +06:00
|
|
|
bool LED_DRIVER_ALLOW_SHUTDOWN(void) {
|
|
|
|
# if defined(NUM_LOCK_INDEX)
|
|
|
|
if (host_keyboard_led_state().num_lock) return false;
|
|
|
|
# endif
|
|
|
|
# if defined(CAPS_LOCK_INDEX) && !defined(DIM_CAPS_LOCK)
|
|
|
|
if (host_keyboard_led_state().caps_lock) return false;
|
|
|
|
# endif
|
|
|
|
# if defined(SCROLL_LOCK_INDEX)
|
|
|
|
if (host_keyboard_led_state().scroll_lock) return false;
|
|
|
|
# endif
|
|
|
|
# if defined(COMPOSE_LOCK_INDEX)
|
|
|
|
if (host_keyboard_led_state().compose) return false;
|
|
|
|
# endif
|
|
|
|
# if defined(KANA_LOCK_INDEX)
|
|
|
|
if (host_keyboard_led_state().kana) return false;
|
|
|
|
# endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
2022-08-03 14:53:42 +06:00
|
|
|
#endif
|