BowlerKernel
SPIChannel.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.dyio.peripherals;
2 
3 import com.neuronrobotics.sdk.commands.bcs.io.SetChannelValueCommand;
4 import com.neuronrobotics.sdk.common.BowlerDatagram;
5 import com.neuronrobotics.sdk.common.BowlerMethod;
6 import com.neuronrobotics.sdk.common.ByteList;
7 import com.neuronrobotics.sdk.common.Log;
8 import com.neuronrobotics.sdk.dyio.DyIO;
9 import com.neuronrobotics.sdk.dyio.DyIOChannelEvent;
10 import com.neuronrobotics.sdk.dyio.DyIOChannelMode;
11 import com.neuronrobotics.sdk.common.DeviceManager;
12 import com.neuronrobotics.sdk.dyio.IChannelEventListener;
13 import com.neuronrobotics.sdk.util.ThreadUtil;
14 // TODO: Auto-generated Javadoc
15 
21 public class SPIChannel implements IChannelEventListener{
22 
24  DyIO dyio;
25 
27  byte [] rx=null;
28 
32  public SPIChannel () {
33  this(((DyIO) DeviceManager.getSpecificDevice(DyIO.class, null)));
34  }
35 
41  public SPIChannel (DyIO d) {
43  dyio = d;
44  dyio.getChannel(0).addChannelEventListener(this);
45  }
52  @Deprecated
53  private BowlerDatagram sendSPIStream(int ss, byte [] stream) {
54 
55  ByteList b = new ByteList();
56  b.add(ss);
57  b.add(stream);
58  return dyio.send(new SetChannelValueCommand(0, b));
59 
60  }
67  public byte [] read(int ss, int numBytes) {
68  byte [] stream = new byte[numBytes];
69  for(int i = 0; i < numBytes; i++) {
70  stream[i]=(byte) 0xff;
71  }
72  return write(ss,stream);
73  }
80  public byte [] write(int ss, byte [] stream) {
81  if(dyio.isLegacyParser()){
82  BowlerDatagram b= sendSPIStream(ss,stream);
83  if(b==null)
84  return new byte[0];
85  return b.getData().getBytes(2);
86  }else{
87  ByteList data = new ByteList(stream);
88  data.insert(0, (byte) ss);
89  dyio.send("bcs.io.*;0.3;;",
91  "strm",
92  new Object[]{0,data});
93  int timeout = 1000;
94  rx=null;
95  while(timeout-->0){
96  ThreadUtil.wait(1);
97  if(rx!=null){
98  return rx;
99  }
100  }
101  return new byte [0];
102  }
103  }
104 
105  /* (non-Javadoc)
106  * @see com.neuronrobotics.sdk.dyio.IChannelEventListener#onChannelEvent(com.neuronrobotics.sdk.dyio.DyIOChannelEvent)
107  */
108  @Override
110  //Log.error("SPI"+e);
111  rx = e.getData().getBytes();
112  }
113 }
synchronized boolean add(byte data)
Definition: ByteList.java:149
void insert(int index, byte val)
Definition: ByteList.java:1051
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
boolean setMode(int channel, DyIOChannelMode mode)
Definition: DyIO.java:182
DyIOChannel getChannel(int channel)
Definition: DyIO.java:160
BowlerDatagram sendSPIStream(int ss, byte[] stream)
Definition: SPIChannel.java:53