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), readState(READ_HEADER), headerByte(header) {
9  }
10 
12  // Request from Create that we want a stream containing all packets
13  const uint8_t numPackets = data->getNumPackets();
14  std::vector<uint8_t> packetIDs = data->getPacketIDs();
15  packetIDs.insert(packetIDs.begin(), numPackets);
16  packetIDs.insert(packetIDs.begin(), OC_STREAM);
17 
18  // Start streaming data
19  send(packetIDs.data(), packetIDs.size());
20 
21  expectedNumBytes = data->getTotalDataBytes() + numPackets;
22 
23  return true;
24  }
25 
27  numBytesRead++;
28  byteSum += byteRead;
29  switch (readState) {
30  case READ_HEADER:
31  if (byteRead == headerByte) {
33  byteSum = byteRead;
34  }
35  break;
36 
37  case READ_NBYTES:
38  if (byteRead == expectedNumBytes) {
40  numBytesRead = 0;
41  }
42  else {
43  //notifyDataReady();
45  }
46  break;
47 
48  case READ_PACKET_ID:
50  if (data->isValidPacketID(packetID)) {
51  expectedNumDataBytes = data->getPacket(packetID)->nbytes;
52  assert(expectedNumDataBytes == 1 || expectedNumDataBytes == 2);
53  numDataBytesRead = 0;
54  packetBytes = 0;
56  }
57  else {
58  //notifyDataReady();
60  }
61  break;
62 
63  case READ_PACKET_BYTES:
65  if (expectedNumDataBytes == 2 && numDataBytesRead == 1) {
66  // High byte first
67  packetBytes = (byteRead << 8) & 0xff00;
68  }
69  else {
70  // Low byte
72  }
74  data->getPacket(packetID)->setDataToValidate(packetBytes);
77  else
79  }
80  break;
81 
82  case READ_CHECKSUM:
83  if ((byteSum & 0xFF) == 0) {
85  }
86  else {
87  // Corrupt data
89  }
90  totalPackets++;
91  // Start again
93  break;
94  } // end switch (readState)
95  }
96 
97 } // namespace create
uint8_t byteRead
Definition: serial.h:64
SerialStream(boost::shared_ptr< Data > data, const uint8_t &header=create::util::STREAM_HEADER)
void notifyDataReady()
Definition: serial.cpp:135
boost::shared_ptr< Data > data
Definition: serial.h:75
uint8_t expectedNumDataBytes
Definition: serial_stream.h:69
Definition: create.h:47
uint64_t totalPackets
Definition: serial.h:78
void processByte(uint8_t byteRead)
bool send(const uint8_t *bytes, const uint32_t numBytes)
Definition: serial.cpp:169
uint64_t corruptPackets
Definition: serial.h:77


libcreate
Author(s): Jacob Perron
autogenerated on Thu Dec 5 2019 04:03:13