BowlerKernel
PPMReaderChannel.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.dyio.peripherals;
2 
3 import java.util.ArrayList;
4 
5 import com.neuronrobotics.sdk.commands.bcs.io.GetValueCommand;
6 import com.neuronrobotics.sdk.commands.bcs.io.SetChannelValueCommand;
7 import com.neuronrobotics.sdk.common.BowlerDatagram;
8 import com.neuronrobotics.sdk.common.BowlerMethod;
9 import com.neuronrobotics.sdk.common.ByteList;
10 import com.neuronrobotics.sdk.common.Log;
11 import com.neuronrobotics.sdk.dyio.DyIO;
12 import com.neuronrobotics.sdk.dyio.DyIOChannel;
13 import com.neuronrobotics.sdk.dyio.DyIOChannelEvent;
14 import com.neuronrobotics.sdk.dyio.DyIOChannelMode;
15 import com.neuronrobotics.sdk.common.DeviceManager;
16 import com.neuronrobotics.sdk.dyio.IChannelEventListener;
17 
18 // TODO: Auto-generated Javadoc
26 
28  private static final DyIOChannelMode myMode = DyIOChannelMode.PPM_IN;
29 
31  private int [] crossLinks =null;
32 
34  int [] values=null;
35 
37  public static final int NO_CROSSLINK = 0xff;
38 
42  public PPMReaderChannel() {
43  this(((DyIO) DeviceManager.getSpecificDevice(DyIO.class, null)).getChannel(23));
44  }
45 
52  super(channel,myMode,true);
53  if(!getChannel().canBeMode(myMode)) {
54  throw new DyIOPeripheralException("Could not ever be " + channel + " to " + myMode + " mode");
55  }
56  if(!setMode()) {
57  throw new DyIOPeripheralException("Could not set channel " + channel + " to " + myMode + " mode");
58  }
60  }
61 
65  public void stopAllCrossLinks(){
66  if(crossLinks == null)
67  crossLinks = new int[6];
68  for(int i=0;i<crossLinks.length;i++) {
70  }
72  }
78  public void setCrossLink(int [] links){
79  if(links.length != 6)
80  throw new IndexOutOfBoundsException("Array of cross links must be of legnth 6");
81  if(crossLinks == null){
82  throw new RuntimeException("Must get cross link state before setting a new one");
83  }
84  //System.out.print("\nSetting cross link map: [");
85  for(int i=0;i<crossLinks.length;i++) {
86  crossLinks[i] = links[i];
87  //System.out.print(" "+crossLinks[i]);
88  }
89  //System.out.print("]");
90  if(getChannel().getDevice().isLegacyParser()){
92  }else{
93  ByteList data = new ByteList(crossLinks);
94  getChannel().getDevice().send("bcs.io.*;0.3;;",
96  "strm",
97  new Object[]{23,data});
98  }
99  }
100 
106  public int [] getCrossLink(){
107  if(crossLinks == null){
108  updateValues();
109  }
110 // System.out.print("\nGetting cross link map: [");
111 // for(int i=0;i<crossLinks.length;i++) {
112 // System.out.print(" "+crossLinks[i]);
113 // }
114 // System.out.print("]");
115  return crossLinks;
116  }
117 
123  public int [] getValues(){
124  if(values == null){
125  updateValues();
126  }
127  return values;
128  }
129 
133  private void updateValues() {
134  if(getChannel().getDevice().isLegacyParser()){
135  BowlerDatagram b=null;
136  //System.out.println("Updating value map");
137  try {
138  b= getChannel().getDevice().send(new GetValueCommand(23));
139  }catch (Exception e) {
140  e.printStackTrace();
141  }
142  if(b != null) {
143  crossLinks = new int[6];
144  values= new int[6];
145  for(int i=0;i<values.length;i++) {
146  values[i] = b.getData().getUnsigned(1+i);
147  }
148  for(int i=0;i<crossLinks.length;i++) {
149  crossLinks[i] = b.getData().getUnsigned(1+6+i);
150  }
151  }
152  }else{
153  Object[] args = getChannel().getDevice().send("bcs.io.*;0.3;;",
155  "strm",
156  new Object[]{23});
157  crossLinks = new int[6];
158  values= new int[6];
159  ByteList data = (ByteList)args[1];
160  Log.debug("PPM link data: "+data.size());
161  for(int i=0;i<values.length;i++) {
162  values[i] = data.getUnsigned(i);
163  }
164  for(int i=0;i<crossLinks.length;i++) {
165  crossLinks[i] = data.getUnsigned(6+i);
166  }
167  }
168  }
169 
171  ArrayList<IPPMReaderListener> listeners = new ArrayList<IPPMReaderListener> ();
172 
179  if(!listeners.contains(l))
180  listeners.add(l);
181  }
182 
183  /* (non-Javadoc)
184  * @see com.neuronrobotics.sdk.dyio.IChannelEventListener#onChannelEvent(com.neuronrobotics.sdk.dyio.DyIOChannelEvent)
185  */
187  getValues();
188  if(crossLinks == null){
189  crossLinks = new int[6];
190  ByteList data =new ByteList( e.getData().getBytes(6, 6));
191  for(int i=0;i<crossLinks.length;i++) {
192  crossLinks[i] = data.getUnsigned(i);
193  }
194  }
195  for(int i=0;i<values.length;i++) {
196  values[i] = e.getData().getUnsigned(i);
197  }
198  if(values != null) {
199  for (IPPMReaderListener l:listeners) {
200  l.onPPMPacket(values);
201  }
202  }
203  }
204 
205  /* (non-Javadoc)
206  * @see com.neuronrobotics.sdk.dyio.peripherals.DyIOAbstractPeripheral#hasAsync()
207  */
208  public boolean hasAsync() {
209  return true;
210  }
211 }
static Object getSpecificDevice(String name, IDeviceProvider provider)
static void debug(String message)
Definition: Log.java:128
void addChannelEventListener(IChannelEventListener l)
Object[] send(String NS, BowlerMethod method, String rpcString, Object[] arguments)
Definition: DyIO.java:168