serial_stream.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <memory>
3 
4 #include "create/serial_stream.h"
5 #include "create/types.h"
6 
7 namespace create {
8 
9  SerialStream::SerialStream(std::shared_ptr<Data> d, const uint8_t& header, bool install_signal_handler) : Serial(d, install_signal_handler), readState(READ_HEADER), headerByte(header) {
10  }
11 
13  // Request from Create that we want a stream containing all packets
14  const uint8_t numPackets = data->getNumPackets();
15  std::vector<uint8_t> packetIDs = data->getPacketIDs();
16  packetIDs.insert(packetIDs.begin(), numPackets);
17  packetIDs.insert(packetIDs.begin(), OC_STREAM);
18 
19  // Start streaming data
20  send(packetIDs.data(), packetIDs.size());
21 
22  expectedNumBytes = data->getTotalDataBytes() + numPackets;
23 
24  return true;
25  }
26 
28  numBytesRead++;
29  byteSum += byteRead;
30  switch (readState) {
31  case READ_HEADER:
32  if (byteRead == headerByte) {
34  byteSum = byteRead;
35  }
36  break;
37 
38  case READ_NBYTES:
39  if (byteRead == expectedNumBytes) {
41  numBytesRead = 0;
42  }
43  else {
44  //notifyDataReady();
46  }
47  break;
48 
49  case READ_PACKET_ID:
51  if (data->isValidPacketID(packetID)) {
52  expectedNumDataBytes = data->getPacket(packetID)->nbytes;
53  assert(expectedNumDataBytes == 1 || expectedNumDataBytes == 2);
54  numDataBytesRead = 0;
55  packetBytes = 0;
57  }
58  else {
59  //notifyDataReady();
61  }
62  break;
63 
64  case READ_PACKET_BYTES:
66  if (expectedNumDataBytes == 2 && numDataBytesRead == 1) {
67  // High byte first
68  packetBytes = (byteRead << 8) & 0xff00;
69  }
70  else {
71  // Low byte
73  }
75  data->getPacket(packetID)->setDataToValidate(packetBytes);
78  else
80  }
81  break;
82 
83  case READ_CHECKSUM:
84  if ((byteSum & 0xFF) == 0) {
86  }
87  else {
88  // Corrupt data
90  }
91  totalPackets++;
92  // Start again
94  break;
95  } // end switch (readState)
96  }
97 
98 } // namespace create
uint8_t byteRead
Definition: serial.h:65
void notifyDataReady()
Definition: serial.cpp:147
uint8_t expectedNumDataBytes
Definition: serial_stream.h:65
Definition: create.h:48
uint64_t totalPackets
Definition: serial.h:79
std::shared_ptr< Data > data
Definition: serial.h:76
void processByte(uint8_t byteRead)
bool send(const uint8_t *bytes, const uint32_t numBytes)
Definition: serial.cpp:184
SerialStream(std::shared_ptr< Data > data, const uint8_t &header=create::util::STREAM_HEADER, bool install_signal_handler=true)
uint64_t corruptPackets
Definition: serial.h:78


libcreate
Author(s): Jacob Perron
autogenerated on Sat May 8 2021 03:02:37