BowlerKernel
UARTChannel.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.io.IOException;
18 import java.util.ArrayList;
19 
20 import com.neuronrobotics.sdk.commands.bcs.io.SetChannelValueCommand;
21 import com.neuronrobotics.sdk.commands.bcs.io.SetUARTBaudrateCommand;
22 import com.neuronrobotics.sdk.common.BowlerMethod;
23 import com.neuronrobotics.sdk.common.ByteList;
24 import com.neuronrobotics.sdk.common.ISendable;
25 import com.neuronrobotics.sdk.dyio.DyIO;
26 import com.neuronrobotics.sdk.dyio.DyIOChannel;
27 import com.neuronrobotics.sdk.dyio.DyIOChannelEvent;
28 import com.neuronrobotics.sdk.dyio.DyIOChannelMode;
29 import com.neuronrobotics.sdk.dyio.DyIOInputStream;
30 import com.neuronrobotics.sdk.dyio.DyIOOutputStream;
31 import com.neuronrobotics.sdk.common.DeviceManager;
32 import com.neuronrobotics.sdk.dyio.IChannelEventListener;
33 import com.neuronrobotics.sdk.dyio.InvalidChannelOperationException;
34 
35 
36 // TODO: Auto-generated Javadoc
40 public class UARTChannel implements ISendable {
41 
43  private ArrayList<IUARTStreamListener> listeners = new ArrayList<IUARTStreamListener>();
44 
46  public static final int UART_IN = 17;
47 
49  public static final int UART_OUT = 16;
50 
52  DyIO device;
53 
55  UARTTxChannel tx;
56 
58  UARTRxChannel rx;
59 
63  public UARTChannel() {
64  this(((DyIO) DeviceManager.getSpecificDevice(DyIO.class, null)));
65  }
66 
72  public UARTChannel(DyIO d){
73  device = d;
74  tx=new UARTTxChannel(d.getChannel(16));
75  rx=new UARTRxChannel(d.getChannel(17));
76  }
77 
78 
79  /* (non-Javadoc)
80  * @see com.neuronrobotics.sdk.common.ISendable#getBytes()
81  */
82  public byte[] getBytes() {
83  return getBytes(rx.getInStreamSize());
84  }
85 
92  public byte[] getBytes(int size) {
93  return rx.getBytes(size);
94  }
95 
96 
104  public boolean sendBytes(ByteList stream) throws IOException{
105  return tx.putStream(stream);
106  }
107 
108 
114  public int getInStreamSize(){
115  return rx.getInStreamSize();
116  }
117 
123  public boolean inStreamDataReady(){
124  return (getInStreamSize()>0);
125  }
126 
133  public boolean setUARTBaudrate(int baudrate) {
134  switch(baudrate){
135  case 2400:
136  case 4800:
137  case 9600:
138  case 14400:
139  case 19200:
140  case 28800:
141  case 38400:
142  case 57600:
143  case 76800:
144  case 115200:
145  case 230400:
146  break;
147  default:
148  throw new InvalidChannelOperationException("This channel can not be set to that baudrate");
149  }
150  if(device.isLegacyParser()){
151  device.send(new SetUARTBaudrateCommand(UART_IN, baudrate));
152  }else{
153 
154  device.send("bcs.io.*;0.3;;",
156  "cchn",
157  new Object[]{ 17,
158  true,
159  new Integer[]{baudrate}
160  });
161  }
162 
163  return true;
164 
165  }
166 
173  return tx.getOutStream();
174  }
175 
182  return rx.getInputStream();
183  }
184 
189  listeners.clear();
190  }
191 
199  if(!listeners.contains(l)) {
200  return;
201  }
202  listeners.remove(l);
203  }
204 
212  if(listeners.contains(l)) {
213  return;
214  }
215  listeners.add(l);
216  }
217 
223  protected void fireChannelEvent(DyIOChannelEvent e) {
224  for(IUARTStreamListener l : listeners) {
225  l.onChannelEvent(e);
226  }
227  }
228 
232  private class UARTTxChannel extends DyIOAbstractPeripheral {
233 
235  DyIOOutputStream out;
236 
242  public UARTTxChannel(DyIOChannel channel) {
243  super(channel,DyIOChannelMode.USART_TX,true);
244  if(!setMode()) {
245  throw new DyIOPeripheralException("Could not set channel " + channel + " to " + DyIOChannelMode.USART_TX + " mode");
246  }
247  out = new DyIOOutputStream(channel);
248  }
249 
257  public boolean putStream(ByteList stream) throws IOException {
258  out.write(stream);
259  return false;
260  }
261 
268  return out;
269  }
270 
271  /* (non-Javadoc)
272  * @see com.neuronrobotics.sdk.dyio.peripherals.DyIOAbstractPeripheral#hasAsync()
273  */
274  public boolean hasAsync() {
275  // TODO Auto-generated method stub
276  return true;
277  }
278 
279  }
280 
284  private class UARTRxChannel extends DyIOAbstractPeripheral implements IChannelEventListener {
285 
287  DyIOInputStream in;
288 
294  public UARTRxChannel(DyIOChannel channel) {
295  super(channel,DyIOChannelMode.USART_RX,true);
296  channel.addChannelEventListener(this);
297  if(!setMode()) {
298  throw new DyIOPeripheralException("Could not set channel " + channel + " to " + DyIOChannelMode.USART_RX + " mode");
299  }
300  in = new DyIOInputStream(channel);
301  }
302 
303 
304  /* (non-Javadoc)
305  * @see com.neuronrobotics.sdk.dyio.IChannelEventListener#onChannelEvent(com.neuronrobotics.sdk.dyio.DyIOChannelEvent)
306  */
308  in.write(e.getData());
309  fireChannelEvent(e);
310  }
311 
317  public int getInStreamSize(){
318  return in.available();
319  }
320 
327  public byte[] getBytes(int inStreamSize) {
328  if (inStreamSize > getInStreamSize())
329  throw new IndexOutOfBoundsException();
330  byte [] b = new byte [inStreamSize];
331 
332  for (int i=0;i<inStreamSize;i++){
333  try {
334  b[i]=(byte) in.read();
335  } catch (IOException e) {
336 
337  }
338  }
339 
340  return b;
341  }
342 
349  return in;
350  }
351 
352 
353  /* (non-Javadoc)
354  * @see com.neuronrobotics.sdk.dyio.peripherals.DyIOAbstractPeripheral#hasAsync()
355  */
356  public boolean hasAsync() {
357  // TODO Auto-generated method stub
358  return true;
359  }
360 
361  }
362 }
static Object getSpecificDevice(String name, IDeviceProvider provider)
void addChannelEventListener(IChannelEventListener l)
Object[] send(String NS, BowlerMethod method, String rpcString, Object[] arguments)
Definition: DyIO.java:168
DyIOChannel getChannel(int channel)
Definition: DyIO.java:160
ArrayList< IUARTStreamListener > listeners
void removeUARTStreamListener(IUARTStreamListener l)