@@ -7,6 +7,13 @@ use rppal::gpio::OutputPin;
7
7
8
8
use crate :: { cli:: ConnectOpts , Config } ;
9
9
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
+
10
17
/// Wrapper around SerialPort where platform-specific modifications can be implemented.
11
18
pub struct Interface {
12
19
pub serial_port : Box < dyn SerialPort > ,
@@ -27,26 +34,30 @@ fn write_gpio(gpio: &mut OutputPin, level: bool) {
27
34
28
35
impl Interface {
29
36
#[ 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 {
32
46
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
+ } )
43
50
}
44
51
45
52
#[ 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 {
48
59
serial_port : serial,
49
- }
60
+ } )
50
61
}
51
62
52
63
pub fn write_data_terminal_ready ( & mut self , pin_state : bool ) -> serialport:: Result < ( ) > {
0 commit comments