Skip to content

Commit

Permalink
decouple the different types of backlighting into their own modules
Browse files Browse the repository at this point in the history
still not possible to use the different backlighting types at the same time, but this is the first big step in working towards that
  • Loading branch information
Univa committed Dec 7, 2023
1 parent 99038be commit 0db253a
Show file tree
Hide file tree
Showing 24 changed files with 1,312 additions and 664 deletions.
39 changes: 25 additions & 14 deletions docs/src/content/docs/features/feature-backlight.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ Some drivers may not be able to support all backlight types.

## Required code

To set up backlighting, you must add `backlight(driver = "<driver>")` to your `#[keyboard]` macro invocation,
To set up backlighting, you must add `<backlight_type>(driver = "<driver>")` to your `#[keyboard]` macro invocation,
and your keyboard must implement the `BacklightDevice` trait.

```rust ins={5-7,11-16}
use rumcake::keyboard;

#[keyboard(
// somewhere in your keyboard macro invocation ...
backlight(
simple_backlight_matrix( // TODO: Change this to `rgb_backlight_matrix` or `simple_backlight` if that's what you want.
driver = "is31fl3731", // TODO: change this to your desired backlight driver, and implement the appropriate trait (info below)
)
)]
Expand All @@ -62,7 +62,7 @@ use rumcake::keyboard;

#[keyboard(
// somewhere in your keyboard macro invocation ...
backlight(
simple_backlight_matrix( // TODO: Change this to `rgb_backlight_matrix` or `simple_backlight` if that's what you want.
driver = "is31fl3731", // TODO: change this to your desired backlight driver, and implement the appropriate trait (info below)
use_storage // Optional, if you want to save backlight configuration
),
Expand All @@ -82,7 +82,7 @@ use rumcake::keyboard;

#[keyboard(
// somewhere in your keyboard macro invocation ...
backlight(
simple_backlight_matrix( // TODO: Change this to `rgb_backlight_matrix` or `simple_backlight` if that's what you want.
driver = "is31fl3731", // TODO: change this to your desired backlight driver, and implement the appropriate trait (info below)
)
)]
Expand Down Expand Up @@ -126,20 +126,21 @@ a key at switch matrix position row 0, column 0, will correspond to the LED at r

Lastly, you must also implement the appropriate trait that corresponds to your chosen driver in the `#[keyboard]` macro. For example, with `is31fl3731`, you must implement `IS31FL3731BacklightDriver`:

```rust ins={3-23}
```rust ins={3-24}
// later in your file...

use rumcake::{setup_i2c, is31fl3731_get_led_from_matrix_coordinates};
use rumcake::drivers::is31fl3731::backlight::IS31FL3731BacklightDriver;
impl IS31FL3731BacklightDriver for MyKeyboard {
const LED_DRIVER_ADDR: u32 = 0b1110100; // see https://github.com/qmk/qmk_firmware/blob/d9fa80c0b0044bb951694aead215d72e4a51807c/docs/feature_rgb_matrix.md#is31fl3731-idis31fl3731
setup_i2c! {
I2C1_EV,
I2C1,
PB6,
PB7,
DMA1_CH7,
DMA1_CH6
setup_i2c! { // Note: The arguments of setup_i2c may change depending on platform. This assumes STM32.
I2C1_EV, // Event interrupt
I2C1_ER, // Error interrupt
I2C1, // I2C peripheral
PB6, // SCL
PB7, // SDA
DMA1_CH7, // RX DMA Channel
DMA1_CH6 // TX DMA Channel
}

// This must have the same number of rows and columns as specified in your `BacklightMatrixDevice` implementation.
Expand All @@ -160,7 +161,12 @@ an RGB matrix, there is a separate `is31fl3731_get_led_from_rgb_matrix_coordinat

# Keycodes

In your keyberon layout, you can use any of the enum members defined in `BacklightCommand`:
Depending on the backlight type you chose, you can use certain version of the `BacklightCommand`
enum in your `keyberon` layout:

- [Simple Backlight Commands](/rumcake/api/nrf52840/rumcake/backlight/simple_backlight/animations/enum.BacklightCommand.html)
- [Simple Backlight Matrix Commands](/rumcake/api/nrf52840/rumcake/backlight/simple_backlight_matrix/animations/enum.BacklightCommand.html)
- [RGB Backlight Matrix Commands](/rumcake/api/nrf52840/rumcake/backlight/rgb_backlight_matrix/animations/enum.BacklightCommand.html)

```rust
Toggle,
Expand All @@ -185,6 +191,10 @@ SaveConfig, // normally called internally when the backlight config changes, onl
ResetTime, // normally used internally for syncing LEDs for split keyboards
```

In your `keyberon` layout, you can use `{Custom(SimpleBacklight(<command>))}`,
`{Custom(SimpleBacklightMatrix(<command>))}`, `{Custom(RGBBacklightMatrix(<command>))}`,
depending on what type of backlight system you are using.

Example of usage:

```rust
Expand All @@ -196,11 +206,12 @@ use rumcake::keyboard::{Keyboard, Keycode::*};

build_layout! {
{
[ Escape {Custom(Backlight(Toggle))} A B C]
[ Escape {Custom(SimpleBacklightMatrix(Toggle))} A B C]
}
}
```

# To-do List

- [ ] RGB Backlight animations
- [ ] Allow different backlighting systems to be used at the same time
108 changes: 99 additions & 9 deletions rumcake-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ struct KeyboardSettings {
bluetooth: bool,
usb: bool,
storage: Option<String>,
backlight: Option<LightingSettings>,
simple_backlight: Option<LightingSettings>,
simple_backlight_matrix: Option<LightingSettings>,
rgb_backlight_matrix: Option<LightingSettings>,
underglow: Option<LightingSettings>,
display: Option<DisplaySettings>,
split_peripheral: Option<SplitPeripheralSettings>,
Expand Down Expand Up @@ -286,7 +288,17 @@ fn setup_underglow_driver(kb_name: &Ident, driver: &str) -> Option<TokenStream>
}
}

fn setup_backlight_driver(kb_name: &Ident, driver: &str) -> Option<TokenStream> {
enum BacklightType {
SimpleBacklight,
SimpleBacklightMatrix,
RGBBacklightMatrix,
}

fn setup_backlight_driver(
kb_name: &Ident,
backlight_type: BacklightType,
driver: &str,
) -> Option<TokenStream> {
match driver {
"is31fl3731" => Some(quote! {
let backlight_driver = rumcake::drivers::is31fl3731::backlight::setup_backlight_driver::<#kb_name>().await;
Expand Down Expand Up @@ -607,33 +619,111 @@ pub fn main(
}

// Backlight setup
if let Some(args) = keyboard.backlight {
if let Some(args) = keyboard.simple_backlight {
if args.driver.is_empty() {
initialization.extend(quote_spanned! {
args.driver.span() => compile_error!("You must specify a simple backlight driver.");
})
} else if args.use_storage && keyboard.storage.is_none() {
initialization.extend(quote_spanned! {
args.driver.span() => compile_error!("Simple backlighting uses storage but no `storage` driver was specified. Either specify a `storage` driver, or remove `use_storage` from your simple backlight settings.");
});
} else {
match setup_backlight_driver(
&kb_name,
BacklightType::SimpleBacklight,
args.driver.as_str(),
) {
Some(driver_setup) => {
initialization.extend(driver_setup);

if args.use_storage {
spawning.extend(quote! {
spawner.spawn(rumcake::simple_backlight_storage_task!(&DATABASE)).unwrap();
});
}

spawning.extend(quote! {
spawner.spawn(rumcake::simple_backlight_task!(#kb_name, backlight_driver)).unwrap();
});
}
None => {
initialization.extend(quote_spanned! {
args.driver.span() => compile_error!("Unknown simple backlight driver.");
});
}
}
}
}

if let Some(args) = keyboard.simple_backlight_matrix {
if args.driver.is_empty() {
initialization.extend(quote_spanned! {
args.driver.span() => compile_error!("You must specify a simple backlight matrix driver.");
})
} else if args.use_storage && keyboard.storage.is_none() {
initialization.extend(quote_spanned! {
args.driver.span() => compile_error!("Simple backlight matrix uses storage but no `storage` driver was specified. Either specify a `storage` driver, or remove `use_storage` from your simple backlight matrix settings.");
});
} else {
match setup_backlight_driver(
&kb_name,
BacklightType::SimpleBacklightMatrix,
args.driver.as_str(),
) {
Some(driver_setup) => {
initialization.extend(driver_setup);

if args.use_storage {
spawning.extend(quote! {
spawner.spawn(rumcake::simple_backlight_matrix_storage_task!(&DATABASE)).unwrap();
});
}

spawning.extend(quote! {
spawner.spawn(rumcake::simple_backlight_matrix_task!(#kb_name, backlight_driver)).unwrap();
});
}
None => {
initialization.extend(quote_spanned! {
args.driver.span() => compile_error!("Unknown simple backlight matrix driver.");
});
}
}
}
}

if let Some(args) = keyboard.rgb_backlight_matrix {
if args.driver.is_empty() {
initialization.extend(quote_spanned! {
args.driver.span() => compile_error!("You must specify a backlight driver.");
args.driver.span() => compile_error!("You must specify an RGB backlight matrix driver.");
})
} else if args.use_storage && keyboard.storage.is_none() {
initialization.extend(quote_spanned! {
args.driver.span() => compile_error!("Backlighting uses storage but no `storage` driver was specified. Either specify a `storage` driver, or remove `use_storage` from your backlight settings.");
args.driver.span() => compile_error!("RGB backlight matrix uses storage but no `storage` driver was specified. Either specify a `storage` driver, or remove `use_storage` from your RGB backlight matrix settings.");
});
} else {
match setup_backlight_driver(&kb_name, args.driver.as_str()) {
match setup_backlight_driver(
&kb_name,
BacklightType::RGBBacklightMatrix,
args.driver.as_str(),
) {
Some(driver_setup) => {
initialization.extend(driver_setup);

if args.use_storage {
spawning.extend(quote! {
spawner.spawn(rumcake::backlight_storage_task!(&DATABASE)).unwrap();
spawner.spawn(rumcake::rgb_backlight_matrix_storage_task!(&DATABASE)).unwrap();
});
}

spawning.extend(quote! {
spawner.spawn(rumcake::backlight_task!(#kb_name, backlight_driver)).unwrap();
spawner.spawn(rumcake::rgb_backlight_matrix_task!(#kb_name, backlight_driver)).unwrap();
});
}
None => {
initialization.extend(quote_spanned! {
args.driver.span() => compile_error!("Unknown backlight driver.");
args.driver.span() => compile_error!("Unknown RGB backlight matrix driver.");
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions rumcake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ edition = "2021"
features = [
"drivers",
"storage",
"simple-backlight",
"simple-backlight-matrix",
"rgb-backlight-matrix",
"underglow",
"usb",
Expand Down
Loading

0 comments on commit 0db253a

Please sign in to comment.