1- use crate :: { config:: Config , interface:: Mode } ;
1+ //! Ebyte module control.
2+
3+ use crate :: { cli:: Mode , config:: Config } ;
4+ use anyhow:: Context ;
5+ use anyhow:: Result ;
6+ use cli:: App ;
27use ebyte_e32:: { parameters:: Parameters , Ebyte } ;
38use embedded_hal:: blocking:: delay:: DelayMs ;
49use embedded_hal:: digital:: v2:: InputPin ;
510use embedded_hal:: digital:: v2:: OutputPin ;
611use embedded_hal:: prelude:: * ;
712use embedded_hal:: serial;
8- use interface:: App ;
913use linux_embedded_hal:: Delay ;
1014use nb:: block;
1115use rppal:: { gpio:: Gpio , uart:: Uart } ;
@@ -14,37 +18,50 @@ use std::fmt::Debug;
1418use std:: fs:: read_to_string;
1519use std:: io:: { self , Write } ;
1620
21+ /// Configuration from `Config.toml`.
1722pub mod config;
18- pub mod interface;
1923
20- pub fn load_default_config ( ) -> Config {
21- let config = read_to_string ( "Config.toml" ) . unwrap_or_else ( |e| {
22- panic ! (
23- "Failed to open Config.toml [{e:?}] \n Here's an example: {:#?}" ,
24- Config :: example ( )
25- )
26- } ) ;
27- toml:: from_str ( & config) . expect ( "Failed to parse config" )
24+ /// Command line interface.
25+ pub mod cli ;
26+
27+ /// Load a configuration from ` Config.toml` ,
28+ /// returning an error if something goes wrong.
29+ pub fn load_config ( ) -> Result < Config > {
30+ let config = read_to_string ( "Config.toml" ) . context ( "Failed to open Config.toml" ) ? ;
31+ toml:: from_str ( & config) . context ( "Failed to parse config" )
2832}
2933
30- pub fn process ( config : Config , args : App ) {
34+ /// Setup the hardware, then load some parameters,
35+ /// update them if needed, then listen, send, or read model data.
36+ pub fn process ( config : Config , args : App ) -> anyhow:: Result < ( ) > {
3137 let serial = Uart :: with_path (
3238 config. serial_path ,
3339 config. baudrate ,
3440 config. parity . into ( ) ,
3541 config. data_bits ,
3642 config. stop_bits ,
3743 )
38- . expect ( "Failed to set up serial port" ) ;
44+ . context ( "Failed to set up serial port" ) ? ;
3945
40- let gpio = Gpio :: new ( ) . unwrap ( ) ;
41- let aux = gpio. get ( config. aux_pin ) . unwrap ( ) . into_input ( ) ;
42- let m0 = gpio. get ( config. m0_pin ) . unwrap ( ) . into_output ( ) ;
43- let m1 = gpio. get ( config. m1_pin ) . unwrap ( ) . into_output ( ) ;
46+ let gpio = Gpio :: new ( ) . context ( "Failed to open Gpio" ) ?;
47+ let aux = gpio
48+ . get ( config. aux_pin )
49+ . context ( "Failed to open AUX pin" ) ?
50+ . into_input ( ) ;
51+ let m0 = gpio
52+ . get ( config. m0_pin )
53+ . context ( "Failed to open m0 pin" ) ?
54+ . into_output ( ) ;
55+ let m1 = gpio
56+ . get ( config. m1_pin )
57+ . context ( "Failed to open m1 pin" ) ?
58+ . into_output ( ) ;
4459
4560 let mut ebyte = Ebyte :: new ( serial, aux, m0, m1, Delay ) . unwrap ( ) ;
4661
47- let old_params = ebyte. parameters ( ) . unwrap ( ) ;
62+ let old_params = ebyte
63+ . parameters ( )
64+ . expect ( "Failed to read current parameters" ) ;
4865 println ! ( "Loaded parameters: {old_params:#?}" ) ;
4966
5067 let new_params = Parameters :: from ( & args) ;
@@ -53,8 +70,12 @@ pub fn process(config: Config, args: App) {
5370 println ! ( "Leaving parameters unchanged" ) ;
5471 } else {
5572 println ! ( "Updating parameters (persistence: {:?})" , args. persistence) ;
56- ebyte. set_parameters ( & new_params, args. persistence ) . unwrap ( ) ;
57- let current_params = ebyte. parameters ( ) . unwrap ( ) ;
73+ ebyte
74+ . set_parameters ( & new_params, args. persistence )
75+ . expect ( "Failed to set new parameters" ) ;
76+ let current_params = ebyte
77+ . parameters ( )
78+ . expect ( "Failed to read current parameters" ) ;
5879 if current_params != new_params {
5980 eprintln ! ( "Error: parameters unchanged: {current_params:#?}" ) ;
6081 }
@@ -64,8 +85,9 @@ pub fn process(config: Config, args: App) {
6485 Mode :: Send => send ( ebyte) ,
6586 Mode :: ReadModelData => {
6687 println ! ( "Reading model data" ) ;
67- let model_data = ebyte. model_data ( ) . unwrap ( ) ;
88+ let model_data = ebyte. model_data ( ) . expect ( "Failed to read model data" ) ;
6889 println ! ( "{model_data:#?}" ) ;
90+ Ok ( ( ) )
6991 }
7092 Mode :: Listen => loop {
7193 let b = block ! ( ebyte. read( ) ) . unwrap ( ) ;
@@ -75,7 +97,9 @@ pub fn process(config: Config, args: App) {
7597 }
7698}
7799
78- fn send < S , Aux , M0 , M1 , D > ( mut ebyte : Ebyte < S , Aux , M0 , M1 , D , ebyte_e32:: mode:: Normal > )
100+ fn send < S , Aux , M0 , M1 , D > (
101+ mut ebyte : Ebyte < S , Aux , M0 , M1 , D , ebyte_e32:: mode:: Normal > ,
102+ ) -> anyhow:: Result < ( ) >
79103where
80104 S : serial:: Read < u8 > + serial:: Write < u8 > ,
81105 <S as serial:: Write < u8 > >:: Error : Debug ,
@@ -115,4 +139,5 @@ where
115139 }
116140 }
117141 }
142+ Ok ( ( ) )
118143}
0 commit comments