Main MRPT website > C++ reference for 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 }
mrpt::comms::CInterfaceFTDI::Seek
uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning)
This virtual method does nothing in this class.
Definition: CInterfaceFTDI_common.cpp:61
mrpt::comms::CInterfaceFTDI::getTotalBytesCount
uint64_t getTotalBytesCount() const
This virtual method does nothing in this class.
Definition: CInterfaceFTDI_common.cpp:66
mrpt::containers::circular_buffer::pop_many
void pop_many(T *out_array, size_t count)
Pop a number of elements into a user-provided array.
Definition: circular_buffer.h:101
int64_t
__int64 int64_t
Definition: rptypes.h:49
mrpt::comms::CInterfaceFTDI::Read
size_t Read(void *Buffer, size_t Count)
Introduces a pure virtual method responsible for reading from the stream.
Definition: CInterfaceFTDI_common.cpp:18
mrpt::comms
Serial and networking devices and utilities.
Definition: CClientTCPSocket.h:21
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
uint8_t
unsigned char uint8_t
Definition: rptypes.h:41
mrpt::comms::CInterfaceFTDI::ReadBufferImmediate
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,...
Definition: CInterfaceFTDI_common.cpp:68
mrpt::containers::circular_buffer::size
size_t size() const
Return the number of elements available for read ("pop") in the buffer (this is NOT the maximum size ...
Definition: circular_buffer.h:146
mrpt::comms::CInterfaceFTDI::getPosition
uint64_t getPosition() const
This virtual method does nothing in this class.
Definition: CInterfaceFTDI_common.cpp:67
CInterfaceFTDI.h
uint64_t
unsigned __int64 uint64_t
Definition: rptypes.h:50
mrpt::comms::CInterfaceFTDI::m_readBuffer
mrpt::containers::circular_buffer< uint8_t > m_readBuffer
Used in Read.
Definition: CInterfaceFTDI.h:176
mrpt::containers::circular_buffer::push_many
void push_many(T *array_elements, size_t count)
Insert an array of elements in the buffer.
Definition: circular_buffer.h:68
mrpt::containers::circular_buffer::available
size_t available() const
The maximum number of elements that can be written ("push") without rising an overflow error.
Definition: circular_buffer.h:161
mrpt::comms::CInterfaceFTDI::Write
size_t Write(const void *Buffer, size_t Count)
Introduces a pure virtual method responsible for writing to the stream.
Definition: CInterfaceFTDI_common.cpp:54
min
#define min(a, b)
Definition: rplidar_driver.cpp:42
mrpt::comms::CInterfaceFTDI::ftdi_read
void ftdi_read(void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytesRead)
Definition: CInterfaceFTDI_WIN.cpp:301
comms-precomp.h
mrpt::comms::CInterfaceFTDI::ftdi_write
void ftdi_write(const void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytes)
Definition: CInterfaceFTDI_WIN.cpp:313



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST