Skip to content

Commit

Permalink
use associated type for via
Browse files Browse the repository at this point in the history
  • Loading branch information
Univa committed Mar 30, 2024
1 parent 4ee41ef commit 063ccad
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 185 deletions.
36 changes: 27 additions & 9 deletions docs/src/content/docs/features/feature-via-vial.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ You must enable the following `rumcake` features:

## Required code

To set up Via and Vial support, your keyboard must implement the `ViaKeyboard` trait, and add `via` to your `keyboard` macro invocation.
To set up Via and Vial support, you must add a new type to implement the `ViaKeyboard` trait on.
Then, you can add `via(id = <type>)` to your `keyboard` macro invocation.

Certain Via features need to be configured manually as well:

Expand All @@ -48,18 +49,27 @@ Certain Via features need to be configured manually as well:

For other configurable Via options, see the [`ViaKeyboard` trait](/rumcake/api/nrf52840/rumcake/via/trait.ViaKeyboard.html).

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

#[keyboard(
// somewhere in your keyboard macro invocation ...
via
via(
id = MyKeyboardVia
)
)]
struct MyKeyboard;

impl KeyboardLayout for MyKeyboard {
/* ... */
}

// Via setup
use rumcake::via::{BacklightType, setup_macro_buffer, ViaKeyboard};
impl ViaKeyboard for MyKeyboard {
struct MyKeyboardVia;
impl ViaKeyboard for MyKeyboardVia {
type Layout = MyKeyboard; // Must be a type that implements `KeyboardLayout`

// OPTIONAL, this example assumes you are using simple-backlight-matrix.
const BACKLIGHT_TYPE: Option<BacklightType> = Some(BacklightType::SimpleBacklightMatrix)

Expand All @@ -74,13 +84,14 @@ lighting settings, etc.) will **NOT** be saved by default.

Optionally, you can add `use_storage`, and a `storage` driver to save Via data.

```rust del={5} ins={6-9,22-23}
```rust del={5} ins={6-10,22-23}
use rumcake::keyboard;

#[keyboard(
// somewhere in your keyboard macro invocation ...
via,
via(
id = MyKeyboardVia,
use_storage // Optional, if you want to save Via configuration
),
storage(driver = "internal") // You need to specify a storage driver if you specified `use_storage`. See feature-storage.md for more information.
Expand All @@ -100,15 +111,19 @@ implement `KEYBOARD_DEFINITION`. Please follow the instructions in the [Vial Def

For other configurable Vial options, see the [`VialKeyboard` trait](/rumcake/api/nrf52840/rumcake/vial/trait.VialKeyboard.html)

```rust del={7} ins={1-3,8,24-30}
```rust del={7} ins={1-3,10,30-35}
// GENERATED_KEYBOARD_DEFINITION comes from _generated.rs, which is made by the build.rs script.
#[cfg(vial)]
include!(concat!(env!("OUT_DIR"), "/_generated.rs"));

#[keyboard(
// somewhere in your keyboard macro invocation ...
via
vial
via(
id = MyKeyboardVia
)
vial(
id = MyKeyboardVia
)
)]
struct MyKeyboard;

Expand All @@ -117,6 +132,8 @@ struct MyKeyboard;
// Via setup
use rumcake::via::{setup_macro_buffer, ViaKeyboard};
impl ViaKeyboard for MyKeyboard {
type Layout = MyKeyboard; // Must be a type that implements `KeyboardLayout`

// OPTIONAL, this example assumes you are using simple-backlight-matrix.
const BACKLIGHT_TYPE: Option<BacklightType> = Some(BacklightType::SimpleBacklightMatrix)

Expand All @@ -135,13 +152,14 @@ impl VialKeyboard for MyKeyboard {
:::caution
Similarly to the previous caution, you need to specify `use_storage`, to save Vial data:

```rust del={5} ins={6-9,22-23}
```rust del={5} ins={6-10}
use rumcake::keyboard;

#[keyboard(
// somewhere in your keyboard macro invocation ...
vial,
vial(
id = MyKeyboardVia,
use_storage // Optional, if you want to save Vial configuration
),
storage = "internal" // You need to specify a storage driver if you specified `use_storage`. See feature-storage.md for more information.
Expand Down
17 changes: 8 additions & 9 deletions rumcake-macros/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub(crate) struct KeyboardSettings {
display: Option<DisplaySettings>,
split_peripheral: Option<SplitPeripheralSettings>,
split_central: Option<SplitCentralSettings>,
via: Option<Override<ViaSettings>>,
vial: Option<Override<ViaSettings>>,
via: Option<ViaSettings>,
vial: Option<ViaSettings>,
bootloader_double_tap_reset: Option<Override<LitInt>>,
}

Expand All @@ -52,8 +52,9 @@ pub(crate) struct SplitPeripheralSettings {
driver: LitStr,
}

#[derive(Debug, FromMeta, Default)]
#[derive(Debug, FromMeta)]
pub(crate) struct ViaSettings {
id: Ident,
use_storage: Option<bool>,
}

Expand Down Expand Up @@ -501,15 +502,14 @@ pub(crate) fn keyboard_main(
);
error = true;
} else if let Some(args) = keyboard.via {
let args = args.unwrap_or_default();

if args.use_storage.unwrap_or_default() && keyboard.storage.is_none() {
emit_error!(args.use_storage, "Via uses storage but no `storage` driver was specified. Either specify a `storage` driver, or remove `use_storage` from your Via settings.");
error = true;
} else if args.use_storage.unwrap_or_default() {
let via_device = args.id;
spawning.extend(quote! {
spawner
.spawn(::rumcake::via_storage_task!(#kb_name, &DATABASE))
.spawn(::rumcake::via_storage_task!(#kb_name, #via_device, &DATABASE))
.unwrap();
});
}
Expand All @@ -520,15 +520,14 @@ pub(crate) fn keyboard_main(
.unwrap();
});
} else if let Some(args) = keyboard.vial {
let args = args.unwrap_or_default();

if args.use_storage.unwrap_or_default() && keyboard.storage.is_none() {
emit_error!(args.use_storage, "Vial uses storage but no `storage` driver was specified. Either specify a `storage` driver, or remove `use_storage` from your Vial settings.");
error = true;
} else if args.use_storage.unwrap_or_default() {
let via_device = args.id;
spawning.extend(quote! {
spawner
.spawn(::rumcake::vial_storage_task!(#kb_name, &DATABASE))
.spawn(::rumcake::vial_storage_task!(#kb_name, #via_device, &DATABASE))
.unwrap();
});
}
Expand Down
Loading

0 comments on commit 063ccad

Please sign in to comment.