serial_stream.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "create/serial_stream.h"
4 #include "create/types.h"
5 
6 namespace create {
7 
8  SerialStream::SerialStream(boost::shared_ptr<Data> d, const uint8_t& header) : Serial(d), headerByte(header){
9  }
10 
12  // Request from Create that we want a stream containing all packets
13  uint8_t numPackets = data->getNumPackets();
14  std::vector<uint8_t> packetIDs = data->getPacketIDs();
15  uint8_t streamReq[2 + numPackets];
16  streamReq[0] = OC_STREAM;
17  streamReq[1] = numPackets;
18  int i = 2;
19  for (std::vector<uint8_t>::iterator it = packetIDs.begin(); it != packetIDs.end(); ++it) {
20  streamReq[i] = *it;
21  i++;
22  }
23 
24  // Start streaming data
25  send(streamReq, 2 + numPackets);
26 
27  expectedNumBytes = data->getTotalDataBytes() + numPackets;
28 
29  return true;
30  }
31 
33  numBytesRead++;
34  byteSum += byteRead;
35  switch (readState) {
36  case READ_HEADER:
37  if (byteRead == headerByte) {
39  byteSum = byteRead;
40  }
41  break;
42 
43  case READ_NBYTES:
44  if (byteRead == expectedNumBytes) {
46  numBytesRead = 0;
47  }
48  else {
49  //notifyDataReady();
51  }
52  break;
53 
54  case READ_PACKET_ID:
56  if (data->isValidPacketID(packetID)) {
57  expectedNumDataBytes = data->getPacket(packetID)->nbytes;
58  assert(expectedNumDataBytes == 1 || expectedNumDataBytes == 2);
59  numDataBytesRead = 0;
60  packetBytes = 0;
62  }
63  else {
64  //notifyDataReady();
66  }
67  break;
68 
69  case READ_PACKET_BYTES:
71  if (expectedNumDataBytes == 2 && numDataBytesRead == 1) {
72  // High byte first
73  packetBytes = (byteRead << 8) & 0xff00;
74  }
75  else {
76  // Low byte
78  }
80  data->getPacket(packetID)->setDataToValidate(packetBytes);
83  else
85  }
86  break;
87 
88  case READ_CHECKSUM:
89  if ((byteSum & 0xFF) == 0) {
91  }
92  else {
93  // Corrupt data
95  }
96  totalPackets++;
97  // Start again
99  break;
100  } // end switch (readState)
101  }
102 
103 } // namespace create
uint8_t byteRead
Definition: serial.h:62
SerialStream(boost::shared_ptr< Data > data, const uint8_t &header=create::util::STREAM_HEADER)
void notifyDataReady()
Definition: serial.cpp:117
boost::shared_ptr< Data > data
Definition: serial.h:74
uint8_t expectedNumDataBytes
Definition: serial_stream.h:69
Definition: create.h:46
uint64_t totalPackets
Definition: serial.h:77
void processByte(uint8_t byteRead)
bool send(const uint8_t *bytes, const uint32_t numBytes)
Definition: serial.cpp:151
uint64_t corruptPackets
Definition: serial.h:76


libcreate
Author(s): Jacob Perron
autogenerated on Sat Jun 8 2019 17:58:17