@@ -7,6 +7,13 @@ use rppal::gpio::OutputPin;
77
88use crate :: { cli:: ConnectOpts , Config } ;
99
10+ #[ derive( thiserror:: Error , Debug ) ]
11+ pub enum SerialConfigError {
12+ #[ cfg( feature = "raspberry" ) ]
13+ #[ error( "You need to specify DTR when using an internal UART peripheral" ) ]
14+ MissingDtrForInternalUart ,
15+ }
16+
1017/// Wrapper around SerialPort where platform-specific modifications can be implemented.
1118pub struct Interface {
1219 pub serial_port : Box < dyn SerialPort > ,
@@ -27,26 +34,30 @@ fn write_gpio(gpio: &mut OutputPin, level: bool) {
2734
2835impl Interface {
2936 #[ cfg( feature = "raspberry" ) ]
30- pub ( crate ) fn new ( serial : Box < dyn SerialPort > , opts : & ConnectOpts , config : & Config ) -> Self {
31- Self {
37+ pub ( crate ) fn new (
38+ serial : Box < dyn SerialPort > ,
39+ opts : & ConnectOpts ,
40+ config : & Config ,
41+ ) -> Result < Self , SerialConfigError > {
42+ let rts_gpio = opts. rts . or ( config. rts ) ;
43+ let dtr_gpio = opts. dtr . or ( config. dtr ) ;
44+
45+ Ok ( Self {
3246 serial_port : serial,
33- rts : opts
34- . rts
35- . or ( config. rts )
36- . map ( |num| gpios. get ( num) . into_output ( ) ) ,
37-
38- dtr : opts
39- . dtr
40- . or ( config. dtr )
41- . map ( |num| gpios. get ( num) . into_output ( ) ) ,
42- }
47+ rts : rts_gpio. map ( |num| gpios. get ( num) . into_output ( ) ) ,
48+ dtr : dtr_gpio. map ( |num| gpios. get ( num) . into_output ( ) ) ,
49+ } )
4350 }
4451
4552 #[ cfg( not( feature = "raspberry" ) ) ]
46- pub ( crate ) fn new ( serial : Box < dyn SerialPort > , _opts : & ConnectOpts , _config : & Config ) -> Self {
47- Self {
53+ pub ( crate ) fn new (
54+ serial : Box < dyn SerialPort > ,
55+ _opts : & ConnectOpts ,
56+ _config : & Config ,
57+ ) -> Result < Self , SerialConfigError > {
58+ Ok ( Self {
4859 serial_port : serial,
49- }
60+ } )
5061 }
5162
5263 pub fn write_data_terminal_ready ( & mut self , pin_state : bool ) -> serialport:: Result < ( ) > {
0 commit comments