Skip to content

Commit

Permalink
Merge pull request #234 from HaoboGu/refactor/rename_trait_fn
Browse files Browse the repository at this point in the history
Update fn names in the input device trait
  • Loading branch information
HaoboGu authored Jan 4, 2025
2 parents acfd3db + 85a9403 commit 68b0025
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
27 changes: 14 additions & 13 deletions docs/src/design_doc/input_device.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The input devices can be key matrix or sensors, which read the physical devices,

```rust
pub trait InputDevice {
/// Event type that input device will send
/// Event type that input device will send
type EventType;

/// Starts the input device task.
Expand All @@ -28,8 +28,8 @@ pub trait InputDevice {
/// It will be executed concurrently with other input devices using the `run_devices` macro.
fn run(&mut self) -> impl Future<Output = ()>;

/// Get the event channel for the input device. All events should be send by this channel.
fn get_channel(&self) -> &Channel<CriticalSectionRawMutex, Self::EventType, EVENT_CHANNEL_SIZE>;
/// Get the event sender for the input device. All events should be send by this channel.
fn event_sender(&self) -> Sender<CriticalSectionRawMutex, Self::EventType, EVENT_CHANNEL_SIZE>;
}
```

Expand Down Expand Up @@ -83,9 +83,12 @@ The input processors receive the event from input devices, process them and conv

```rust
pub trait InputProcessor {
/// Event type that the input processor receives.
/// The event type that the input processor receives.
type EventType;

/// The report type that the input processor sends.
type ReportType;

/// Process the incoming events, convert them to HID report [`KeyboardReportMessage`],
/// then send the report to the USB/BLE.
///
Expand All @@ -94,22 +97,20 @@ pub trait InputProcessor {
/// The input processor implementor should be aware of this.
fn process(&mut self, event: Self::EventType) -> impl Future<Output = ()>;

/// Get the input event channel for the input processor.
/// Get the input event channel receiver for the input processor.
///
/// The input processor receives events from this channel, processes the event,
/// then sends to the report channel.
fn get_event_channel(
fn event_receiver(
&self,
) -> &Channel<CriticalSectionRawMutex, Self::EventType, EVENT_CHANNEL_SIZE>;
) -> Receiver<CriticalSectionRawMutex, Self::EventType, EVENT_CHANNEL_SIZE>;

/// Get the output report channel for the input processor.
/// Get the output report sender for the input processor.
///
/// The input processor sends keyboard reports to this channel.
fn get_report_channel(
fn report_sender(
&self,
) -> &Channel<CriticalSectionRawMutex, KeyboardReportMessage, REPORT_CHANNEL_SIZE> {
&KEYBOARD_REPORT_CHANNEL
}
) -> Sender<CriticalSectionRawMutex, Self::ReportType, REPORT_CHANNEL_SIZE>;

/// Default implementation of the input processor. It wait for a new event from the event channel,
/// then process the event.
Expand All @@ -118,7 +119,7 @@ pub trait InputProcessor {
fn run(&mut self) -> impl Future<Output = ()> {
async {
loop {
let event = self.get_event_channel().receive().await;
let event = self.event_receiver().receive().await;
self.process(event).await;
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/use_rust/nrf52840_ble/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl InputDevice for MyDevice {

type EventType = Event;

fn get_channel(&self) -> Sender<CriticalSectionRawMutex, Self::EventType, EVENT_CHANNEL_SIZE> {
fn event_sender(&self) -> Sender<CriticalSectionRawMutex, Self::EventType, EVENT_CHANNEL_SIZE> {
EVENT_CHANNEL.sender()
}
}
16 changes: 8 additions & 8 deletions rmk/src/input_device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub trait InputDevice {
// FIXME: it's not possible in stable to define an associated const and use it as the channel size in stable Rust.
// It requires #[feature(generic_const_exprs)]:
//
// `fn get_channel(..) -> &Channel<CriticalSectionRawMutex, Self::EventType, { Self::EVENT_CHANNEL_SIZE } >;`
// `fn event_sender(..) -> &Channel<CriticalSectionRawMutex, Self::EventType, { Self::EVENT_CHANNEL_SIZE } >;`
// So this size is commented out
// const EVENT_CHANNEL_SIZE: usize = 32;

Expand All @@ -63,8 +63,8 @@ pub trait InputDevice {
/// It will be executed concurrently with other input devices using the `run_devices` macro.
fn run(&mut self) -> impl Future<Output = ()>;

/// Get the event channel for the input device. All events should be send by this channel.
fn get_channel(&self) -> Sender<CriticalSectionRawMutex, Self::EventType, EVENT_CHANNEL_SIZE>;
/// Get the event sender for the input device. All events should be send by this channel.
fn event_sender(&self) -> Sender<CriticalSectionRawMutex, Self::EventType, EVENT_CHANNEL_SIZE>;
}

/// The trait for input processors.
Expand All @@ -88,18 +88,18 @@ pub trait InputProcessor {
/// The input processor implementor should be aware of this.
fn process(&mut self, event: Self::EventType) -> impl Future<Output = ()>;

/// Get the input event channel for the input processor.
/// Get the input event channel receiver for the input processor.
///
/// The input processor receives events from this channel, processes the event,
/// then sends to the report channel.
fn get_event_channel(
fn event_receiver(
&self,
) -> Receiver<CriticalSectionRawMutex, Self::EventType, EVENT_CHANNEL_SIZE>;

/// Get the output report channel for the input processor.
/// Get the output report sender for the input processor.
///
/// The input processor sends keyboard reports to this channel.
fn get_report_channel(
fn report_sender(
&self,
) -> Sender<CriticalSectionRawMutex, Self::ReportType, REPORT_CHANNEL_SIZE>;

Expand All @@ -110,7 +110,7 @@ pub trait InputProcessor {
fn run(&mut self) -> impl Future<Output = ()> {
async {
loop {
let event = self.get_event_channel().receive().await;
let event = self.event_receiver().receive().await;
self.process(event).await;
}
}
Expand Down
8 changes: 4 additions & 4 deletions rmk/src/input_device/rotary_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl<

let direction = self.update();

self.get_channel()
self.event_sender()
.send(Event::RotaryEncoder(RotaryEncoderEvent {
id: self.id,
direction,
Expand All @@ -172,7 +172,7 @@ impl<
}
}

fn get_channel(&self) -> Sender<CriticalSectionRawMutex, Self::EventType, EVENT_CHANNEL_SIZE> {
fn event_sender(&self) -> Sender<CriticalSectionRawMutex, Self::EventType, EVENT_CHANNEL_SIZE> {
EVENT_CHANNEL.sender()
}
}
Expand All @@ -199,13 +199,13 @@ impl InputProcessor for RotaryEncoderProcessor {
}
}

fn get_event_channel(
fn event_receiver(
&self,
) -> Receiver<CriticalSectionRawMutex, Self::EventType, EVENT_CHANNEL_SIZE> {
EVENT_CHANNEL.receiver()
}

fn get_report_channel(
fn report_sender(
&self,
) -> Sender<CriticalSectionRawMutex, Self::ReportType, REPORT_CHANNEL_SIZE> {
KEYBOARD_REPORT_CHANNEL.sender()
Expand Down

0 comments on commit 68b0025

Please sign in to comment.