rmk_config/
board.rs

1use crate::{InputDeviceConfig, KeyboardTomlConfig, MatrixConfig, MatrixType, SplitConfig};
2
3#[derive(Clone, Debug)]
4pub enum BoardConfig {
5    Split(SplitConfig),
6    UniBody(UniBodyConfig),
7}
8
9#[derive(Clone, Debug, Default)]
10pub struct UniBodyConfig {
11    pub matrix: MatrixConfig,
12    pub input_device: InputDeviceConfig,
13}
14
15impl Default for BoardConfig {
16    fn default() -> Self {
17        BoardConfig::UniBody(UniBodyConfig::default())
18    }
19}
20
21impl BoardConfig {
22    /// Get the number of encoders for each board
23    ///
24    /// - If the board is the unibody board, the returned vector has only one element.
25    /// - If the board is the split board, the number of elements is the number of peripherals + 1 (central),
26    ///   where the first element is the number of encoders on the central.
27    pub fn get_num_encoder(&self) -> Vec<usize> {
28        let mut num_encoder = Vec::new();
29        match self {
30            BoardConfig::Split(split) => {
31                // Central's encoders
32                num_encoder.push(
33                    split
34                        .central
35                        .input_device
36                        .clone()
37                        .unwrap_or_default()
38                        .encoder
39                        .unwrap_or(Vec::new())
40                        .len(),
41                );
42
43                // Peripheral's encoders
44                for peri in &split.peripheral {
45                    num_encoder.push(
46                        peri.input_device
47                            .clone()
48                            .unwrap_or_default()
49                            .encoder
50                            .unwrap_or(Vec::new())
51                            .len(),
52                    );
53                }
54            }
55            BoardConfig::UniBody(uni_body_config) => {
56                num_encoder.push(uni_body_config.input_device.encoder.clone().unwrap_or(Vec::new()).len());
57            }
58        };
59        num_encoder
60    }
61}
62
63impl KeyboardTomlConfig {
64    pub fn get_board_config(&self) -> Result<BoardConfig, String> {
65        let matrix = self.matrix.clone();
66        let split = self.split.clone();
67        let input_device = self.input_device.clone();
68        match (matrix, split) {
69            (None, Some(s)) => {
70                Ok(BoardConfig::Split(s))
71            },
72            (Some(m), None) => {
73                match m.matrix_type {
74                    MatrixType::normal => {
75                        if m.input_pins.is_none() || m.output_pins.is_none() {
76                            return Err("`input_pins` and `output_pins` is required for normal matrix".to_string());
77                        }
78                    },
79                    MatrixType::direct_pin => {
80                        if m.direct_pins.is_none() {
81                            return Err("`direct_pins` is required for direct pin matrix".to_string());
82                        }
83                    },
84                }
85                // FIXME: input device for split keyboard is not supported yet
86                Ok(BoardConfig::UniBody(UniBodyConfig{matrix: m, input_device: input_device.unwrap_or_default()}))
87            },
88            (None, None) => Err("[matrix] section in keyboard.toml is required for non-split keyboard".to_string()),
89            _ => Err("Use at most one of [matrix] or [split] in your keyboard.toml!\n-> [matrix] is used to define a normal matrix of non-split keyboard\n-> [split] is used to define a split keyboard\n".to_string()),
90        }
91    }
92}