MRPT  1.9.9
CInterfaceFTDI_common.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://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 
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...
virtual size_t ReadBufferImmediate(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer, and returns the amound of bytes actually read...
#define min(a, b)
void ftdi_write(const void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytes)
uint64_t getPosition() const
This virtual method does nothing in this class.
size_t Write(const void *Buffer, size_t Count)
Introduces a pure virtual method responsible for writing to the stream.
mrpt::containers::circular_buffer< uint8_t > m_readBuffer
Used in Read.
unsigned char uint8_t
Definition: rptypes.h:41
uint64_t getTotalBytesCount() const
This virtual method does nothing in this class.
__int64 int64_t
Definition: rptypes.h:49
size_t size() const
Return the number of elements available for read ("pop") in the buffer (this is NOT the maximum size ...
size_t Read(void *Buffer, size_t Count)
Introduces a pure virtual method responsible for reading from the stream.
unsigned __int64 uint64_t
Definition: rptypes.h:50
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)
uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning)
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 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020