37
37
import java .util .List ;
38
38
import java .util .concurrent .*;
39
39
40
- public class PinInput extends DigitalInputBase implements DigitalInput {
41
- private static final Logger logger = LoggerFactory .getLogger (PinInput .class );
40
+ public class DigitalInputFFM extends DigitalInputBase implements DigitalInput {
41
+ private static final Logger logger = LoggerFactory .getLogger (DigitalInputFFM .class );
42
42
private static final Ioctl ioctl = new IoctlNative ();
43
43
private static final FileDescriptor file = new FileDescriptorNative ();
44
44
private static final Poll poll = new PollNative ();
@@ -47,15 +47,15 @@ public class PinInput extends DigitalInputBase implements DigitalInput {
47
47
private final int pin ;
48
48
private final long debounce ;
49
49
private final PullResistance pull ;
50
- private int fd ;
50
+ private int chipFileDescriptor ;
51
51
52
52
private static final ThreadFactory factory = Thread .ofVirtual ().name ("pin-input-event-detection-" , 0 ).factory ();
53
53
// executor services for event watcher
54
54
private static final ExecutorService eventTaskProcessor = Executors .newSingleThreadExecutor (factory );
55
55
56
56
private boolean closed = false ;
57
57
58
- public PinInput (String chipName , DigitalInputProvider provider , DigitalInputConfig config ) {
58
+ public DigitalInputFFM (String chipName , DigitalInputProvider provider , DigitalInputConfig config ) {
59
59
super (provider , config );
60
60
this .pin = config .address ();
61
61
this .chipName = "/dev/" + chipName ;
@@ -75,7 +75,7 @@ public DigitalInput initialize(Context context) throws InitializeException {
75
75
logger .error ("Please, read the documentation <link> to setup right permissions." );
76
76
throw new InitializeException ("Device '" + chipName + "' cannot be accessed with current user." );
77
77
}
78
- logger .info ("{}-{} - setting up GPIO Pin..." , chipName , pin );
78
+ logger .info ("{}-{} - setting up DigitalInput Pin..." , chipName , pin );
79
79
logger .trace ("{}-{} - opening device file." , chipName , pin );
80
80
var fd = file .open (chipName , FileFlag .O_RDONLY | FileFlag .O_CLOEXEC );
81
81
var lineInfo = new LineInfo (new byte []{}, new byte []{}, pin , 0 , 0 , new LineAttribute []{}, new int []{});
@@ -85,7 +85,7 @@ public DigitalInput initialize(Context context) throws InitializeException {
85
85
shutdown (context ());
86
86
throw new InitializeException ("Pin " + pin + " is in use" );
87
87
}
88
- logger .trace ("{}-{} - GPIO Pin line info: {}" , chipName , pin , lineInfo );
88
+ logger .trace ("{}-{} - DigitalInput Pin line info: {}" , chipName , pin , lineInfo );
89
89
var flags = PinFlag .INPUT .getValue () | PinFlag .EDGE_RISING .getValue () | PinFlag .EDGE_FALLING .getValue ();
90
90
var attributes = new ArrayList <LineConfigAttribute >();
91
91
if (debounce > 0 ) {
@@ -98,9 +98,9 @@ public DigitalInput initialize(Context context) throws InitializeException {
98
98
case PULL_UP -> PinFlag .BIAS_PULL_UP .getValue ();
99
99
};
100
100
var lineConfig = new LineConfig (flags , attributes .size (), new int []{}, attributes .toArray (new LineConfigAttribute [0 ]));
101
- var lineRequest = new LineRequest (new int []{pin }, "pi4j FFM" .getBytes (), lineConfig , 1 , 0 , new int []{}, 0 );
101
+ var lineRequest = new LineRequest (new int []{pin }, ( "pi4j." + getClass (). getSimpleName ()) .getBytes (), lineConfig , 1 , 0 , new int []{}, 0 );
102
102
var result = ioctl .call (fd , Command .getGpioV2GetLineIoctl (), lineRequest );
103
- this .fd = result .fd ();
103
+ this .chipFileDescriptor = result .fd ();
104
104
105
105
var watcher = new EventWatcher (fd , PinEvent .BOTH , events -> {
106
106
for (DetectedEvent detectedEvent : events ) {
@@ -109,9 +109,10 @@ public DigitalInput initialize(Context context) throws InitializeException {
109
109
}, 16 );
110
110
eventTaskProcessor .submit (watcher );
111
111
112
- logger .info ("{}-{} - GPIO Pin configured: {}" , chipName , pin , result );
112
+ file .close (fd );
113
+ logger .info ("{}-{} - DigitalInput Pin configured: {}" , chipName , pin , result );
113
114
} catch (NativeMemoryException | IOException e ) {
114
- logger .error ("{}-{} - GPIO Pin Initialization error: {}" , chipName , pin , e .getMessage ());
115
+ logger .error ("{}-{} - DigitalInput Pin Initialization error: {}" , chipName , pin , e .getMessage ());
115
116
throw new InitializeException (e );
116
117
}
117
118
return this ;
@@ -122,8 +123,8 @@ public DigitalInput shutdown(Context context) throws ShutdownException {
122
123
super .shutdown (context );
123
124
logger .info ("{}-{} - closing GPIO Pin." , chipName , pin );
124
125
try {
125
- if (fd > 0 ) {
126
- file .close (fd );
126
+ if (chipFileDescriptor > 0 ) {
127
+ file .close (chipFileDescriptor );
127
128
}
128
129
eventTaskProcessor .awaitTermination (1 , TimeUnit .MILLISECONDS );
129
130
} catch (NativeMemoryException | InterruptedException e ) {
@@ -142,12 +143,12 @@ public DigitalState state() {
142
143
var lineValues = new LineValues (0 , 1 );
143
144
LineValues result ;
144
145
try {
145
- result = ioctl .call (fd , Command .getGpioV2GetValuesIoctl (), lineValues );
146
+ result = ioctl .call (chipFileDescriptor , Command .getGpioV2GetValuesIoctl (), lineValues );
146
147
} catch (NativeMemoryException e ) {
147
148
throw new RuntimeException (e );
148
149
}
149
150
var state = DigitalState .getState (result .bits ());
150
- logger .trace ("{}-{} - new GPIO Pin state is {}." , chipName , pin , state );
151
+ logger .trace ("{}-{} - GPIO Pin state is {}." , chipName , pin , state );
151
152
return state ;
152
153
}
153
154
0 commit comments