mirror of
https://github.com/qmk/qmk_firmware
synced 2025-01-04 22:13:04 +00:00
Compare commits
10 Commits
9236f95fcf
...
68eeb200f3
Author | SHA1 | Date | |
---|---|---|---|
|
68eeb200f3 | ||
|
d5a0424987 | ||
|
1e6eb5e35c | ||
|
57f89e5388 | ||
|
4d182ec117 | ||
|
53680fa6da | ||
|
08c1c045f9 | ||
|
fe2200f73a | ||
|
dbc5bc4f8f | ||
|
93a0dd46de |
@ -930,6 +930,27 @@ ifeq ($(strip $(DIP_SWITCH_ENABLE)), yes)
|
||||
endif
|
||||
endif
|
||||
|
||||
VALID_BATTERY_DRIVER_TYPES := adc custom
|
||||
|
||||
BATTERY_DRIVER ?= adc
|
||||
ifeq ($(strip $(BATTERY_DRIVER_REQUIRED)), yes)
|
||||
ifeq ($(filter $(BATTERY_DRIVER),$(VALID_BATTERY_DRIVER_TYPES)),)
|
||||
$(call CATASTROPHIC_ERROR,Invalid BATTERY_DRIVER,BATTERY_DRIVER="$(BATTERY_DRIVER)" is not a valid battery driver)
|
||||
endif
|
||||
|
||||
OPT_DEFS += -DBATTERY_DRIVER
|
||||
OPT_DEFS += -DBATTERY_$(strip $(shell echo $(BATTERY_DRIVER) | tr '[:lower:]' '[:upper:]'))
|
||||
|
||||
COMMON_VPATH += $(DRIVER_PATH)/battery
|
||||
|
||||
SRC += battery_$(strip $(BATTERY_DRIVER)).c
|
||||
|
||||
# add extra deps
|
||||
ifeq ($(strip $(BATTERY_DRIVER)), adc)
|
||||
ANALOG_DRIVER_REQUIRED = yes
|
||||
endif
|
||||
endif
|
||||
|
||||
VALID_WS2812_DRIVER_TYPES := bitbang custom i2c pwm spi vendor
|
||||
|
||||
WS2812_DRIVER ?= bitbang
|
||||
|
10
drivers/battery/battery.h
Normal file
10
drivers/battery/battery.h
Normal file
@ -0,0 +1,10 @@
|
||||
// Copyright 2024 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void battery_init(void);
|
||||
|
||||
uint8_t battery_get_percent(void);
|
59
drivers/battery/battery_adc.c
Normal file
59
drivers/battery/battery_adc.c
Normal file
@ -0,0 +1,59 @@
|
||||
// Copyright 2024 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "analog.h"
|
||||
#include "gpio.h"
|
||||
|
||||
#ifndef BATTERY_PIN
|
||||
# error("BATTERY_PIN not configured!")
|
||||
#endif
|
||||
|
||||
#ifndef BATTERY_REF_VOLTAGE_MV
|
||||
# define BATTERY_REF_VOLTAGE_MV 3300
|
||||
#endif
|
||||
|
||||
#ifndef BATTERY_VOLTAGE_DIVIDER_R1
|
||||
# define BATTERY_VOLTAGE_DIVIDER_R1 100000
|
||||
#endif
|
||||
|
||||
#ifndef BATTERY_VOLTAGE_DIVIDER_R2
|
||||
# define BATTERY_VOLTAGE_DIVIDER_R2 100000
|
||||
#endif
|
||||
|
||||
// TODO: infer from adc config?
|
||||
#ifndef BATTERY_ADC_RESOLUTION
|
||||
# define BATTERY_ADC_RESOLUTION 10
|
||||
#endif
|
||||
|
||||
void battery_init(void) {
|
||||
gpio_set_pin_input(BATTERY_PIN);
|
||||
}
|
||||
|
||||
__attribute__((weak)) uint16_t battery_raw_to_mv(uint32_t raw) {
|
||||
uint32_t bat_mv = raw * BATTERY_REF_VOLTAGE_MV / (1 << BATTERY_ADC_RESOLUTION);
|
||||
|
||||
#if BATTERY_VOLTAGE_DIVIDER_R1 > 0 && BATTERY_VOLTAGE_DIVIDER_R2 > 0
|
||||
bat_mv = bat_mv * (BATTERY_VOLTAGE_DIVIDER_R1 + BATTERY_VOLTAGE_DIVIDER_R2) / BATTERY_VOLTAGE_DIVIDER_R2;
|
||||
#endif
|
||||
|
||||
return bat_mv;
|
||||
}
|
||||
|
||||
__attribute__((weak)) uint8_t battery_mv_to_percent(uint16_t bat_mv) {
|
||||
// https://github.com/zmkfirmware/zmk/blob/3f7c9d7cc4f46617faad288421025ea2a6b0bd28/app/module/drivers/sensor/battery/battery_common.c#L33
|
||||
if (bat_mv >= 4200) {
|
||||
return 100;
|
||||
} else if (bat_mv <= 3450) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return bat_mv * 2 / 15 - 459;
|
||||
}
|
||||
|
||||
uint8_t battery_get_percent(void) {
|
||||
uint16_t raw = analogReadPin(BATTERY_PIN);
|
||||
|
||||
uint16_t bat_mv = battery_raw_to_mv(raw);
|
||||
|
||||
return battery_mv_to_percent(bat_mv);
|
||||
}
|
6
keyboards/handwired/onekey/keymaps/battery/config.h
Normal file
6
keyboards/handwired/onekey/keymaps/battery/config.h
Normal file
@ -0,0 +1,6 @@
|
||||
// Copyright 2024 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#define BATTERY_PIN ADC_PIN
|
28
keyboards/handwired/onekey/keymaps/battery/keymap.c
Normal file
28
keyboards/handwired/onekey/keymaps/battery/keymap.c
Normal file
@ -0,0 +1,28 @@
|
||||
// Copyright 2024 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include QMK_KEYBOARD_H
|
||||
#include "battery.h"
|
||||
|
||||
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
|
||||
LAYOUT_ortho_1x1(KC_A)
|
||||
};
|
||||
|
||||
void keyboard_post_init_user(void) {
|
||||
// Customise these values to desired behaviour
|
||||
debug_enable=true;
|
||||
// debug_matrix=false;
|
||||
// debug_keyboard=true;
|
||||
// debug_mouse=false;
|
||||
|
||||
battery_init();
|
||||
}
|
||||
|
||||
void housekeeping_task_user(void) {
|
||||
static uint32_t last = 0;
|
||||
if (timer_elapsed32(last) > 2000) {
|
||||
uprintf("Bat: %d!\n", battery_get_percent());
|
||||
|
||||
last = timer_read32();
|
||||
}
|
||||
}
|
7
keyboards/handwired/onekey/keymaps/battery/keymap.json
Normal file
7
keyboards/handwired/onekey/keymaps/battery/keymap.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"config": {
|
||||
"features": {
|
||||
"console": true
|
||||
}
|
||||
}
|
||||
}
|
1
keyboards/handwired/onekey/keymaps/battery/rules.mk
Normal file
1
keyboards/handwired/onekey/keymaps/battery/rules.mk
Normal file
@ -0,0 +1 @@
|
||||
BATTERY_DRIVER_REQUIRED = yes
|
@ -119,8 +119,7 @@
|
||||
"solid_reactive_multinexus": true,
|
||||
"solid_splash": true,
|
||||
"wave_left_right": true,
|
||||
"wave_up_down": true,
|
||||
"effect_max": true
|
||||
"wave_up_down": true
|
||||
},
|
||||
"layout": [
|
||||
{"matrix":[0, 0], "flags":1, "x":0, "y":0},
|
||||
|
@ -136,8 +136,7 @@
|
||||
"solid_reactive_multinexus": true,
|
||||
"solid_splash": true,
|
||||
"wave_left_right": true,
|
||||
"wave_up_down": true,
|
||||
"effect_max": true
|
||||
"wave_up_down": true
|
||||
},
|
||||
"layout": [
|
||||
{"matrix":[0, 0], "flags":1, "x":0, "y":0},
|
||||
|
@ -789,8 +789,8 @@ def _extract_led_config(info_data, keyboard):
|
||||
info_data[feature]['layout'] = ret
|
||||
except Exception as e:
|
||||
_log_warning(info_data, f'led_config: {file.name}: {e}')
|
||||
else:
|
||||
_log_warning(info_data, 'led_config: matrix size required to parse g_led_config')
|
||||
else:
|
||||
_log_warning(info_data, 'led_config: matrix size required to parse g_led_config')
|
||||
|
||||
if info_data[feature].get('layout', None) and not info_data[feature].get('led_count', None):
|
||||
info_data[feature]['led_count'] = len(info_data[feature]['layout'])
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2024 QMK
|
||||
// Copyright 2025 QMK
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
/*******************************************************************************
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user