BowlerKernel
CounterOutputChannel.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 
18 import java.util.ArrayList;
19 
20 import com.neuronrobotics.sdk.common.ByteList;
21 import com.neuronrobotics.sdk.common.Log;
22 import com.neuronrobotics.sdk.dyio.DyIO;
23 import com.neuronrobotics.sdk.dyio.DyIOChannel;
24 import com.neuronrobotics.sdk.dyio.DyIOChannelEvent;
25 import com.neuronrobotics.sdk.dyio.DyIOChannelMode;
26 import com.neuronrobotics.sdk.common.DeviceManager;
27 import com.neuronrobotics.sdk.dyio.IChannelEventListener;
28 
29 
30 // TODO: Auto-generated Javadoc
35 
37  private ArrayList<ICounterOutputListener> listeners = new ArrayList<ICounterOutputListener>();
38 
46  this(((DyIO) DeviceManager.getSpecificDevice(DyIO.class, null)).getChannel(channel));
47  }
48 
56  public CounterOutputChannel(DyIO dyio,int channel){
57  this(dyio.getChannel(channel));
58  }
59 
67  this(channel,true);
68  }
69 
76  public CounterOutputChannel(DyIOChannel channel,boolean isAsync) {
77  super(channel,DyIOChannelMode.COUNT_OUT_INT,isAsync);
78  init(channel,isAsync);
79  }
80 
87  private void init(DyIOChannel channel,boolean isAsync){
90  if(!channel.setMode(mode, isAsync)) {
91  throw new DyIOPeripheralException("Could not set channel " + channel + " to " + mode + " mode.");
92  }
93  channel.resync(true);
94  }
95 
102  public boolean SetPosition(int pos){
103  return SetPosition(pos, 0);
104  }
105 
113  public boolean SetPosition(int pos, float time){
114  if(!validate()) {
115  return false;
116  }
117  getChannel().setCachedValue(pos);
118  getChannel().setCachedTime(time);
119  if(getChannel().getCachedMode()) {
120  return true;
121  }
122  return flush();
123  }
124 
125 
133  if(listeners.contains(l)) {
134  return;
135  }
136 
137  listeners.add(l);
138  }
139 
147  if(!listeners.contains(l)) {
148  return;
149  }
150 
151  listeners.add(l);
152  }
153 
158  listeners.clear();
159  }
160 
166  protected void fireOnCounterOutput(int value) {
168  l.onCounterValueChange(this, value);
169  }
170  }
171 
172  /* (non-Javadoc)
173  * @see com.neuronrobotics.sdk.dyio.peripherals.DyIOAbstractPeripheral#setValue(int)
174  */
175 
176  public boolean setValue(int value){
177  Log.info("Setting counter set point");
178  ByteList b = new ByteList();
179  b.addAs32(value);
180  return setValue(b);
181  }
182 
191  }
192 
193  /* (non-Javadoc)
194  * @see com.neuronrobotics.sdk.dyio.peripherals.DyIOAbstractPeripheral#hasAsync()
195  */
196  public boolean hasAsync() {
197  return true;
198  }
199 
205  public void setAsync(boolean isAsync) {
207  }
208 
214  private boolean validate() {
215  if(!isEnabled()) {
216  //return false;
217  }
219  }
220 
221 }
static Object getSpecificDevice(String name, IDeviceProvider provider)
static void info(String message)
Definition: Log.java:110
boolean setMode(DyIOChannelMode mode)
void addChannelEventListener(IChannelEventListener l)
DyIOChannel getChannel(int channel)
Definition: DyIO.java:160