BowlerKernel
AnalogInputChannel.java
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright 2010 Neuron Robotics, LLC
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  ******************************************************************************/
15 package com.neuronrobotics.sdk.dyio.peripherals;
16 
17 import java.util.ArrayList;
18 
19 import com.neuronrobotics.sdk.common.DeviceManager;
20 import com.neuronrobotics.sdk.dyio.DyIO;
21 import com.neuronrobotics.sdk.dyio.DyIOChannel;
22 import com.neuronrobotics.sdk.dyio.DyIOChannelEvent;
23 import com.neuronrobotics.sdk.dyio.DyIOChannelMode;
24 import com.neuronrobotics.sdk.dyio.IChannelEventListener;
25 
26 // TODO: Auto-generated Javadoc
31 
33  public static final int ADCRESOLUTION = 1024;
34 
36  public static final int ADCVOLTAGE = 5;
37 
39  private ArrayList<IAnalogInputListener> listeners = new ArrayList<IAnalogInputListener>();
40 
48  this(((DyIO) DeviceManager.getSpecificDevice(DyIO.class, null)).getChannel(channel));
49  }
50 
58  public AnalogInputChannel(DyIO dyio,int channel){
59  this(dyio.getChannel(channel));
60  }
61 
69  this(channel,true);
70  }
71 
79  public AnalogInputChannel(DyIOChannel channel, boolean async){
80  super(channel,DyIOChannelMode.ANALOG_IN,async);
82 
83  if(!setMode(async)) {
84  throw new DyIOPeripheralException("Could not set channel " + getChannel() + " to " + DyIOChannelMode.ANALOG_IN + " mode.");
85  }
86  }
87 
88 
89 
95  public double getScaledValue() {
96  return scaleValue(getValue());
97  }
98 
104  public double getVoltage(){
105  return getScaledValue() * ADCVOLTAGE;
106  }
107 
113  public void setAsync(boolean isAsync) {
115  }
116 
121  listeners.clear();
122  }
123 
131  if(!listeners.contains(l)) {
132  return;
133  }
134 
135  listeners.remove(l);
136  }
137 
145  if(listeners.contains(l)) {
146  return;
147  }
148 
149  listeners.add(l);
150  }
151 
157  private void fireValueChanged(double value) {
158  try{
160  l.onAnalogValueChange(this, value);
161  }
162  }catch(Exception e){
163  e.printStackTrace();
164  }
165  }
166 
173  private static double scaleValue(int value) {
174  return (((double) value)/ADCRESOLUTION);
175  }
176 
177  /* (non-Javadoc)
178  * @see com.neuronrobotics.sdk.dyio.IChannelEventListener#onChannelEvent(com.neuronrobotics.sdk.dyio.DyIOChannelEvent)
179  */
180 
183  }
184 
185 
186  /* (non-Javadoc)
187  * @see com.neuronrobotics.sdk.dyio.peripherals.DyIOAbstractPeripheral#hasAsync()
188  */
189  public boolean hasAsync() {
190  return true;
191  }
192 }
static Object getSpecificDevice(String name, IDeviceProvider provider)
void addChannelEventListener(IChannelEventListener l)
DyIOChannel getChannel(int channel)
Definition: DyIO.java:160