use postcard::experimental::max_size::MaxSize;
use serde::{Deserialize, Serialize};
#[cfg(feature = "controller")]
use {rmk_types::action::KeyAction, rmk_types::led_indicator::LedIndicator, rmk_types::modifier::ModifierCombination};
use crate::input_device::rotary_encoder::Direction;
#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Copy, Debug, MaxSize)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Event {
Key(KeyboardEvent),
Touchpad(TouchpadEvent),
Joystick([AxisEvent; 3]),
AxisEventStream(AxisEvent),
Battery(u16),
ChargingState(bool),
Eos,
Custom([u8; 16]),
}
#[derive(Serialize, Deserialize, Clone, Copy, Debug, MaxSize, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct KeyboardEvent {
pub(crate) pressed: bool,
pub(crate) pos: KeyboardEventPos,
}
impl KeyboardEvent {
pub fn key(row: u8, col: u8, pressed: bool) -> Self {
Self {
pressed,
pos: KeyboardEventPos::Key(KeyPos { row, col }),
}
}
pub fn rotary_encoder(id: u8, direction: Direction, pressed: bool) -> Self {
Self {
pressed,
pos: KeyboardEventPos::RotaryEncoder(RotaryEncoderPos { id, direction }),
}
}
}
#[derive(Serialize, Deserialize, Clone, Copy, Debug, MaxSize, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum KeyboardEventPos {
Key(KeyPos),
RotaryEncoder(RotaryEncoderPos),
}
impl KeyboardEventPos {
pub(crate) fn key_pos(col: u8, row: u8) -> Self {
Self::Key(KeyPos { row, col })
}
pub(crate) fn rotary_encoder_pos(id: u8, direction: Direction) -> Self {
Self::RotaryEncoder(RotaryEncoderPos { id, direction })
}
}
#[derive(Serialize, Deserialize, Clone, Copy, Debug, MaxSize, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct KeyPos {
pub row: u8,
pub col: u8,
}
#[derive(Serialize, Deserialize, Clone, Copy, Debug, MaxSize, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct RotaryEncoderPos {
pub id: u8,
pub direction: Direction,
}
#[derive(Serialize, Deserialize, Clone, Copy, Debug, MaxSize)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct TouchpadEvent {
pub finger: u8,
pub axis: [AxisEvent; 3],
}
#[derive(Serialize, Deserialize, Clone, Debug, Copy, MaxSize)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct AxisEvent {
pub typ: AxisValType,
pub axis: Axis,
pub value: i16,
}
#[derive(Serialize, Deserialize, Clone, Debug, Copy, MaxSize)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum AxisValType {
Rel,
Abs,
}
#[derive(Serialize, Deserialize, Clone, Copy, Debug, MaxSize)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub enum Axis {
X,
Y,
Z,
H,
V,
}
#[cfg(feature = "controller")]
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum ControllerEvent {
Key(KeyboardEvent, KeyAction),
Battery(u8),
ChargingState(bool),
Layer(u8),
Modifier(ModifierCombination),
Wpm(u16),
ConnectionType(u8),
SplitPeripheral(usize, bool),
SplitCentral(bool),
KeyboardIndicator(LedIndicator),
Sleep(bool),
#[cfg(feature = "_ble")]
BleState(u8, crate::ble::BleState),
#[cfg(feature = "_ble")]
BleProfile(u8),
#[cfg(all(feature = "_ble", feature = "split"))]
ClearPeer,
}