Skip to main content

rpi_relay_lib/
config.rs

1use serde::Deserialize;
2use std::fs;
3
4#[derive(Debug, Deserialize, Clone)]
5pub enum LogicMode {
6    High,
7    Low,
8}
9
10#[derive(Debug, Deserialize, Clone)]
11pub struct RelayDef {
12    pub name: String,
13    pub pin: u8,
14    pub logic: LogicMode,
15}
16
17#[derive(Debug, Deserialize, Clone)]
18pub struct TriggerConfig {
19    pub duration_ms: u64,
20}
21
22#[derive(Debug, Deserialize, Clone)]
23pub struct PulseConfig {
24    pub count: u32,
25    pub on_ms: u64,
26    pub gap_ms: u64,
27}
28
29#[derive(Debug, Deserialize, Clone)]
30pub struct RelayConfig {
31    pub relays: Vec<RelayDef>,
32    pub trigger: TriggerConfig,
33    pub pulse: PulseConfig,
34}
35
36impl RelayConfig {
37    pub fn from_file(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
38        let content = fs::read_to_string(path)?;
39        let config: RelayConfig = toml::from_str(&content)?;
40        Ok(config)
41    }
42}