1
1
use crate :: error:: Error ;
2
2
use crate :: { FtInner , PinUse } ;
3
3
use ftdi_mpsse:: { MpsseCmdBuilder , MpsseCmdExecutor } ;
4
- use std:: result :: Result ;
5
- use std:: { cell :: RefCell , sync :: Mutex } ;
4
+ use std:: borrow :: BorrowMut ;
5
+ use std:: sync :: { Arc , Mutex } ;
6
6
7
7
/// FTDI output pin.
8
8
///
@@ -13,7 +13,7 @@ use std::{cell::RefCell, sync::Mutex};
13
13
#[ derive( Debug ) ]
14
14
pub struct OutputPin < ' a , Device : MpsseCmdExecutor > {
15
15
/// Parent FTDI device.
16
- mtx : & ' a Mutex < RefCell < FtInner < Device > > > ,
16
+ mtx : & ' a Arc < Mutex < FtInner < Device > > > ,
17
17
/// GPIO pin index. 0-7 for the FT232H.
18
18
idx : u8 ,
19
19
}
@@ -25,11 +25,11 @@ where
25
25
Error < E > : From < E > ,
26
26
{
27
27
pub ( crate ) fn new (
28
- mtx : & ' a Mutex < RefCell < FtInner < Device > > > ,
28
+ mtx : & ' a Arc < Mutex < FtInner < Device > > > ,
29
29
idx : u8 ,
30
30
) -> Result < OutputPin < ' a , Device > , Error < E > > {
31
- let lock = mtx. lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
32
- let mut inner = lock. borrow_mut ( ) ;
31
+ let mut lock = mtx. lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
32
+ let inner = lock. borrow_mut ( ) ;
33
33
inner. direction |= 1 << idx;
34
34
inner. allocate_pin ( idx, PinUse :: Output ) ;
35
35
let cmd: MpsseCmdBuilder = MpsseCmdBuilder :: new ( )
40
40
}
41
41
42
42
pub ( crate ) fn set ( & self , state : bool ) -> Result < ( ) , Error < E > > {
43
- let lock = self . mtx . lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
44
- let mut inner = lock. borrow_mut ( ) ;
43
+ let mut lock = self . mtx . lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
44
+ let inner = lock. borrow_mut ( ) ;
45
45
46
46
if state {
47
47
inner. value |= self . mask ( ) ;
@@ -65,14 +65,21 @@ impl<'a, Device: MpsseCmdExecutor> OutputPin<'a, Device> {
65
65
}
66
66
}
67
67
68
- impl < ' a , Device , E > embedded_hal:: digital:: v2 :: OutputPin for OutputPin < ' a , Device >
68
+ impl < ' a , Device , E > embedded_hal:: digital:: ErrorType for OutputPin < ' a , Device >
69
69
where
70
70
Device : MpsseCmdExecutor < Error = E > ,
71
71
E : std:: error:: Error ,
72
72
Error < E > : From < E > ,
73
73
{
74
74
type Error = Error < E > ;
75
+ }
75
76
77
+ impl < ' a , Device , E > embedded_hal:: digital:: blocking:: OutputPin for OutputPin < ' a , Device >
78
+ where
79
+ Device : MpsseCmdExecutor < Error = E > ,
80
+ E : std:: error:: Error ,
81
+ Error < E > : From < E > ,
82
+ {
76
83
fn set_low ( & mut self ) -> Result < ( ) , Error < E > > {
77
84
self . set ( false )
78
85
}
91
98
#[ derive( Debug ) ]
92
99
pub struct InputPin < ' a , Device : MpsseCmdExecutor > {
93
100
/// Parent FTDI device.
94
- mtx : & ' a Mutex < RefCell < FtInner < Device > > > ,
101
+ mtx : & ' a Arc < Mutex < FtInner < Device > > > ,
95
102
/// GPIO pin index. 0-7 for the FT232H.
96
103
idx : u8 ,
97
104
}
@@ -103,11 +110,11 @@ where
103
110
Error < E > : From < E > ,
104
111
{
105
112
pub ( crate ) fn new (
106
- mtx : & ' a Mutex < RefCell < FtInner < Device > > > ,
113
+ mtx : & ' a Arc < Mutex < FtInner < Device > > > ,
107
114
idx : u8 ,
108
115
) -> Result < InputPin < ' a , Device > , Error < E > > {
109
- let lock = mtx. lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
110
- let mut inner = lock. borrow_mut ( ) ;
116
+ let mut lock = mtx. lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
117
+ let inner = lock. borrow_mut ( ) ;
111
118
inner. direction &= !( 1 << idx) ;
112
119
inner. allocate_pin ( idx, PinUse :: Input ) ;
113
120
let cmd: MpsseCmdBuilder = MpsseCmdBuilder :: new ( )
@@ -118,8 +125,8 @@ where
118
125
}
119
126
120
127
pub ( crate ) fn get ( & self ) -> Result < bool , Error < E > > {
121
- let lock = self . mtx . lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
122
- let mut inner = lock. borrow_mut ( ) ;
128
+ let mut lock = self . mtx . lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
129
+ let inner = lock. borrow_mut ( ) ;
123
130
124
131
let mut buffer = [ 0u8 ; 1 ] ;
125
132
let cmd: MpsseCmdBuilder = MpsseCmdBuilder :: new ( ) . gpio_lower ( ) . send_immediate ( ) ;
@@ -137,14 +144,21 @@ impl<'a, Device: MpsseCmdExecutor> InputPin<'a, Device> {
137
144
}
138
145
}
139
146
140
- impl < ' a , Device , E > embedded_hal:: digital:: v2 :: InputPin for InputPin < ' a , Device >
147
+ impl < ' a , Device , E > embedded_hal:: digital:: ErrorType for InputPin < ' a , Device >
141
148
where
142
149
Device : MpsseCmdExecutor < Error = E > ,
143
150
E : std:: error:: Error ,
144
151
Error < E > : From < E > ,
145
152
{
146
153
type Error = Error < E > ;
154
+ }
147
155
156
+ impl < ' a , Device , E > embedded_hal:: digital:: blocking:: InputPin for InputPin < ' a , Device >
157
+ where
158
+ Device : MpsseCmdExecutor < Error = E > ,
159
+ E : std:: error:: Error ,
160
+ Error < E > : From < E > ,
161
+ {
148
162
fn is_high ( & self ) -> Result < bool , Self :: Error > {
149
163
self . get ( )
150
164
}
0 commit comments