MRPT  2.0.4
CInterfaceFTDI_common.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "comms-precomp.h" // Precompiled headers
11 
13 #include <algorithm> // min()
14 
15 using namespace mrpt;
16 using namespace mrpt::comms;
17 
18 size_t CInterfaceFTDI::Read(void* Buffer, size_t Count)
19 {
20  if (!Count) return 0;
21 
22  // Employ a circular_buffer to speed-up lots of small readings:
23  if (m_readBuffer.size() >= Count)
24  {
25  // It's enough with the data in the buffer:
26  m_readBuffer.pop_many(reinterpret_cast<uint8_t*>(Buffer), Count);
27  return Count;
28  }
29  else
30  {
31  // More data must be read:
32  uint8_t buf[4000];
33 
34  unsigned long nActualRead = 0;
35  unsigned long to_read = std::min(m_readBuffer.available(), sizeof(buf));
36 
37  ftdi_read(
38  buf, to_read,
39  &nActualRead); // ftdi_read(Buffer,(unsigned long)Count, &ret );
40 
41  // Save data into the circular buffer:
42  m_readBuffer.push_many(buf, nActualRead);
43 
44  // Read the required amount of bytes:
45  size_t nActualReturn = std::min(m_readBuffer.size(), Count);
46 
48  reinterpret_cast<uint8_t*>(Buffer), nActualReturn);
49 
50  return nActualReturn;
51  }
52 }
53 
54 size_t CInterfaceFTDI::Write(const void* Buffer, size_t Count)
55 {
56  unsigned long ret = 0;
57  ftdi_write(Buffer, (unsigned long)Count, &ret);
58  return (size_t)ret;
59 }
60 
61 uint64_t CInterfaceFTDI::Seek(int64_t Offset, CStream::TSeekOrigin Origin)
62 {
63  return 0;
64 }
65 
66 uint64_t CInterfaceFTDI::getTotalBytesCount() const { return 0; }
67 uint64_t CInterfaceFTDI::getPosition() const { return 0; }
68 size_t CInterfaceFTDI::ReadBufferImmediate(void* Buffer, size_t Count)
69 {
70  unsigned long nRead;
71  ftdi_read(Buffer, Count, &nRead);
72  return nRead;
73 }
size_t available() const
The maximum number of elements that can be written ("push") without rising an overflow error...
void ftdi_write(const void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytes)
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
mrpt::containers::circular_buffer< uint8_t > m_readBuffer
Used in Read.
uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
This virtual method does nothing in this class.
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
uint64_t getPosition() const override
This virtual method does nothing in this class.
size_t size() const
Return the number of elements available for read ("pop") in the buffer (this is NOT the maximum size ...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void pop_many(T *out_array, size_t count)
Pop a number of elements into a user-provided array.
void ftdi_read(void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytesRead)
size_t ReadBufferImmediate(void *Buffer, size_t Count) override
Reads a block of bytes from the stream into Buffer, and returns the amound of bytes actually read...
uint64_t getTotalBytesCount() const override
This virtual method does nothing in this class.
void push_many(T *array_elements, size_t count)
Insert an array of elements in the buffer.
Serial and networking devices and utilities.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020