Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial potentiometer support #22449

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions builddefs/common_features.mk
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,10 @@ ifeq ($(strip $(DIP_SWITCH_ENABLE)), yes)
endif
endif

ifeq ($(strip $(POTENTIOMETER_ENABLE)), yes)
ANALOG_DRIVER_REQUIRED = yes
endif

VALID_WS2812_DRIVER_TYPES := bitbang custom i2c pwm spi vendor

WS2812_DRIVER ?= bitbang
Expand Down
1 change: 1 addition & 0 deletions builddefs/generic_features.mk
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ GENERIC_FEATURES = \
MOUSEKEY \
MUSIC \
OS_DETECTION \
POTENTIOMETER \
PROGRAMMABLE_BUTTON \
REPEAT_KEY \
SECURE \
Expand Down
3 changes: 3 additions & 0 deletions data/mappings/info_config.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
"ONESHOT_TIMEOUT": {"info_key": "oneshot.timeout", "value_type": "int"},
"ONESHOT_TAP_TOGGLE": {"info_key": "oneshot.tap_toggle", "value_type": "int"},

// Potentiometer
"POTENTIOMETER_PINS": {"info_key": "potentiometer.pins", "value_type": "array"},

// PS/2
"PS2_CLOCK_PIN": {"info_key": "ps2.clock_pin"},
"PS2_DATA_PIN": {"info_key": "ps2.data_pin"},
Expand Down
1 change: 1 addition & 0 deletions data/mappings/info_rules.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"NO_USB_STARTUP_CHECK": {"info_key": "usb.no_startup_check", "value_type": "bool"},
"PIN_COMPATIBLE": {"info_key": "pin_compatible"},
"PLATFORM_KEY": {"info_key": "platform_key", "to_json": false},
"POTENTIOMETER_ENABLE": {"info_key": "potentiometer.enabled", "value_type": "bool"},
"PS2_DRIVER": {"info_key": "ps2.driver"},
"PS2_ENABLE": {"info_key": "ps2.enabled", "value_type": "bool"},
"PS2_MOUSE_ENABLE": {"info_key": "ps2.mouse_enabled", "value_type": "bool"},
Expand Down
7 changes: 7 additions & 0 deletions data/schemas/keyboard.jsonschema
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,13 @@
"timeout": {"$ref": "qmk.definitions.v1#/unsigned_int"}
}
},
"potentiometer": {
"type": "object",
"properties": {
"enabled": {"type": "boolean"},
"pins": {"$ref": "qmk.definitions.v1#/mcu_pin_array"}
}
},
"led_matrix": {
"type": "object",
"properties": {
Expand Down
1 change: 1 addition & 0 deletions docs/_summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
* [LED Indicators](feature_led_indicators.md)
* [MIDI](feature_midi.md)
* [Pointing Device](feature_pointing_device.md)
* [Potentiometer](feature_potentiometers.md)
* [PS/2 Mouse](feature_ps2_mouse.md)
* [Split Keyboard](feature_split_keyboard.md)
* [Stenography](feature_stenography.md)
Expand Down
35 changes: 35 additions & 0 deletions docs/feature_potentiometers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Potentiometers

Add this to your `rules.mk`:

```make
POTENTIOMETER_ENABLE = yes
```

and this to your `config.h`:

```c
#define POTENTIOMETER_PINS { B0 }
```

## Callbacks

The callback functions can be inserted into your `<keyboard>.c`:

```c
bool potentiometer_update_kb(uint8_t index, uint16_t value) {
if (!potentiometer_update_user(index, value)) {
midi_send_cc(&midi_device, 2, 0x3E, 0x7F + value);
}
return true;
}
```

or `keymap.c`:

```c
bool potentiometer_update_user(uint8_t index, uint16_t value) {
midi_send_cc(&midi_device, 2, 0x3E, 0x7F + value);
return false;
}
```
11 changes: 11 additions & 0 deletions docs/reference_info_json.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,17 @@ Configures [One Shot keys](one_shot_keys.md).
* `timeout`
* The amount of time before the key is released in milliseconds.

## Potentiometer :id=potentiometer

Configures the [Potentiometer](feature_Potentiometers.md) feature.

* `potentiometer`
* `enabled`
* Enable the Potentiometer feature.
* Default: `false`
* `pins` (Required)
* The GPIO pin(s) connected to the Potentiometer(s).

## PS/2 :id=ps2

Configures the [PS/2](feature_ps2_mouse.md) feature.
Expand Down
1 change: 0 additions & 1 deletion keyboards/gmmk/numpad/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#pragma once

#define SLIDER_PIN B0
#define MIDI_ADVANCED

#define LOCKING_SUPPORT_ENABLE
Expand Down
3 changes: 3 additions & 0 deletions keyboards/gmmk/numpad/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
{"pin_a": "A2", "pin_b": "A1"}
]
},
"potentiometer": {
"pins": ["B0"]
},
"rgb_matrix": {
"driver": "aw20216s"
},
Expand Down
17 changes: 0 additions & 17 deletions keyboards/gmmk/numpad/keymaps/default/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include QMK_KEYBOARD_H
#include "analog.h"
#include "qmk_midi.h"

// clang-format off
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
Expand All @@ -43,18 +41,3 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
RGB_TOG, QK_BOOT
)
};

// Potentiometer Slider, MIDI Control

uint8_t divisor = 0;

void slider(void) {
if (divisor++) { /* only run the slider function 1/256 times it's called */
return;
}
midi_send_cc(&midi_device, 2, 0x3E, 0x7F + (analogReadPin(SLIDER_PIN) >> 3));
}

void housekeeping_task_user(void) {
slider();
}
17 changes: 0 additions & 17 deletions keyboards/gmmk/numpad/keymaps/via/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include QMK_KEYBOARD_H
#include "analog.h"
#include "qmk_midi.h"

// clang-format off
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
Expand Down Expand Up @@ -62,18 +60,3 @@ const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS] = {
[2] = { ENCODER_CCW_CW(KC_VOLD, KC_VOLU) },
[3] = { ENCODER_CCW_CW(KC_VOLD, KC_VOLU) }
};

// Potentiometer Slider, MIDI Control

uint8_t divisor = 0;

void slider(void) {
if (divisor++) { /* only run the slider function 1/256 times it's called */
return;
}
midi_send_cc(&midi_device, 2, 0x3E, 0x7F + (analogReadPin(SLIDER_PIN) >> 3));
}

void housekeeping_task_user(void) {
slider();
}
8 changes: 8 additions & 0 deletions keyboards/gmmk/numpad/numpad.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#include "quantum.h"
#include "qmk_midi.h"

#ifdef RGB_MATRIX_ENABLE

Expand Down Expand Up @@ -117,3 +118,10 @@ void keyboard_pre_init_user(void) {
# endif

#endif

bool potentiometer_update_kb(uint8_t index, uint16_t value) {
if (!potentiometer_update_user(index, value)) {
midi_send_cc(&midi_device, 2, 0x3E, 0x7F + value);
}
return true;
}
3 changes: 1 addition & 2 deletions keyboards/gmmk/numpad/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ AUDIO_ENABLE = no # Audio output
ENCODER_ENABLE = yes
KEYBOARD_SHARED_EP = yes
MIDI_ENABLE = yes
POTENTIOMETER_ENABLE = yes

RGB_MATRIX_ENABLE = yes

LTO_ENABLE = yes

ANALOG_DRIVER_REQUIRED = yes

SRC += matrix.c
6 changes: 6 additions & 0 deletions keyboards/handwired/onekey/keymaps/potentiometer/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright 2023 QMK
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

// TODO: Remove reuse of pin
#define POTENTIOMETER_PINS { ADC_PIN }
16 changes: 16 additions & 0 deletions keyboards/handwired/onekey/keymaps/potentiometer/keymap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2023 QMK
// SPDX-License-Identifier: GPL-2.0-or-later
#include QMK_KEYBOARD_H

const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
LAYOUT_ortho_1x1(KC_A)
};

bool potentiometer_update_user(uint8_t index, uint16_t value) {
uprintf("ADC:%u\n", value);
return false;
}

void keyboard_post_init_user(void) {
debug_enable=true;
}
3 changes: 3 additions & 0 deletions keyboards/handwired/onekey/keymaps/potentiometer/rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CONSOLE_ENABLE = yes

POTENTIOMETER_ENABLE = yes
16 changes: 16 additions & 0 deletions quantum/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifdef ENCODER_ENABLE
# include "encoder.h"
#endif
#ifdef POTENTIOMETER_ENABLE
# include "potentiometer.h"
#endif
#ifdef HAPTIC_ENABLE
# include "haptic.h"
#endif
Expand Down Expand Up @@ -143,6 +146,9 @@ uint32_t last_input_activity_time(void) {
uint32_t last_input_activity_elapsed(void) {
return sync_timer_elapsed32(last_input_modification_time);
}
void last_input_activity_trigger(void) {
last_input_modification_time = sync_timer_read32();
}

static uint32_t last_matrix_modification_time = 0;
uint32_t last_matrix_activity_time(void) {
Expand Down Expand Up @@ -440,6 +446,9 @@ void keyboard_init(void) {
#ifdef DIP_SWITCH_ENABLE
dip_switch_init();
#endif
#ifdef POTENTIOMETER_ENABLE
potentiometer_init();
#endif
#ifdef SLEEP_LED_ENABLE
sleep_led_init();
#endif
Expand Down Expand Up @@ -669,6 +678,13 @@ void keyboard_task(void) {
}
#endif

#ifdef POTENTIOMETER_ENABLE
if (potentiometer_task()) {
last_input_activity_trigger();
activity_has_occurred = true;
}
#endif

#ifdef POINTING_DEVICE_ENABLE
if (pointing_device_task()) {
last_pointing_device_activity_trigger();
Expand Down
Loading