-
Notifications
You must be signed in to change notification settings - Fork 52
/
DigitalOutputBase.java
177 lines (154 loc) · 6.18 KB
/
DigitalOutputBase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.pi4j.io.gpio.digital;
/*-
* #%L
* **********************************************************************
* ORGANIZATION : Pi4J
* PROJECT : Pi4J :: LIBRARY :: Java Library (CORE)
* FILENAME : DigitalOutputBase.java
*
* This file is part of the Pi4J project. More information about
* this project can be found here: https://pi4j.com/
* **********************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import com.pi4j.context.Context;
import com.pi4j.exception.InitializeException;
import com.pi4j.exception.ShutdownException;
import com.pi4j.io.exception.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
/**
* <p>Abstract DigitalOutputBase class.</p>
*
* @author Robert Savage (<a href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>)
* @version $Id: $Id
*/
public abstract class DigitalOutputBase extends DigitalBase<DigitalOutput, DigitalOutputConfig, DigitalOutputProvider> implements DigitalOutput {
protected DigitalState state = DigitalState.UNKNOWN;
/**
* <p>Constructor for DigitalOutputBase.</p>
*
* @param provider a {@link com.pi4j.io.gpio.digital.DigitalOutputProvider} object.
* @param config a {@link com.pi4j.io.gpio.digital.DigitalOutputConfig} object.
*/
public DigitalOutputBase(DigitalOutputProvider provider, DigitalOutputConfig config){
super(provider, config);
}
/** {@inheritDoc} */
@Override
public DigitalOutput initialize(Context context) throws InitializeException {
super.initialize(context);
// update the analog value to the initial value if an initial value was configured
if(config().initialState() != null){
try {
state(config().initialState());
} catch (IOException e) {
throw new InitializeException(e);
}
}
return this;
}
/** {@inheritDoc} */
@Override
public DigitalOutput state(DigitalState state) throws IOException {
if(!this.equals(state)){
this.state = state;
this.dispatch(new DigitalStateChangeEvent<DigitalOutputBase>(this, this.state));
}
return this;
}
/** {@inheritDoc} */
@Override
public DigitalOutput pulse(int interval, TimeUnit unit, DigitalState state, Callable<Void> callback) throws IOException {
int millis = 0;
// validate arguments
if(interval <= 0) throw new IllegalArgumentException("A time interval of zero or less is not supported.");
if(unit == TimeUnit.MICROSECONDS) throw new IllegalArgumentException("TimeUnit.MICROSECONDS is not supported.");
else if(unit == TimeUnit.DAYS) throw new IllegalArgumentException("TimeUnit.DAYS is not supported.");
else if(unit == TimeUnit.MILLISECONDS) millis = interval;
else if(unit == TimeUnit.SECONDS) millis = interval * 1000;
else if(unit == TimeUnit.MINUTES) millis = interval * 60000;
else if(unit == TimeUnit.HOURS) millis = interval * 360000;
else throw new IllegalArgumentException("TimeUnit provided is not supported.");
// start the pulse state
this.state(state);
// block the current thread for the pulse duration
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
throw new RuntimeException("Pulse blocking thread interrupted.", e);
}
// end the pulse state
this.state(DigitalState.getInverseState(state));
// invoke callback if one was defined
if (callback != null) {
try {
callback.call();
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
return this;
}
/** {@inheritDoc} */
@Override
public Future<?> pulseAsync(int interval, TimeUnit unit, DigitalState state, Callable<Void> callback) {
// TODO :: IMPLEMENT DIGITAL OUTPUT PULSE ASYNC
throw new UnsupportedOperationException("PULSE ASYNC has not yet been implemented!");
}
/** {@inheritDoc} */
@Override
public DigitalOutput blink(int delay, int duration, TimeUnit unit, DigitalState state, Callable<Void> callback) {
// TODO :: IMPLEMENT DIGITAL OUTPUT BLINK
throw new UnsupportedOperationException("BLINK has not yet been implemented!");
}
/** {@inheritDoc} */
@Override
public Future<?> blinkAsync(int delay, int duration, TimeUnit unit, DigitalState state, Callable<Void> callback) {
// TODO :: IMPLEMENT DIGITAL OUTPUT BLINK ASYNC
throw new UnsupportedOperationException("BLINK ASYNC has not yet been implemented!");
}
/** {@inheritDoc} */
@Override
public DigitalState state() {
return this.state;
}
/** {@inheritDoc} */
@Override
public DigitalOutput shutdown(Context context) throws ShutdownException {
// set pin state to shutdown state if a shutdown state is configured
if(config().shutdownState() != null && config().shutdownState() != DigitalState.UNKNOWN){
try {
state(config().shutdownState());
} catch (IOException e) {
throw new ShutdownException(e);
}
}
return super.shutdown(context);
}
/** {@inheritDoc} */
@Override
public DigitalOutput on() throws IOException {
// TODO :: REVISIT STATE VS ON/OFF
return high();
}
/** {@inheritDoc} */
@Override
public DigitalOutput off() throws IOException {
// TODO :: REVISIT STATE VS ON/OFF
return low();
}
}