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-2017, 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 /*-------------------------------------------------------------
19  Read
20 -------------------------------------------------------------*/
21 size_t CInterfaceFTDI::Read(void* Buffer, size_t Count)
22 {
23  if (!Count) return 0;
24 
25  // Employ a circular_buffer to speed-up lots of small readings:
26  if (m_readBuffer.size() >= Count)
27  {
28  // It's enough with the data in the buffer:
29  m_readBuffer.pop_many(reinterpret_cast<uint8_t*>(Buffer), Count);
30  return Count;
31  }
32  else
33  {
34  // More data must be read:
35  uint8_t buf[4000];
36 
37  unsigned long nActualRead = 0;
38  unsigned long to_read = std::min(m_readBuffer.available(), sizeof(buf));
39 
40  ftdi_read(
41  buf, to_read,
42  &nActualRead); // ftdi_read(Buffer,(unsigned long)Count, &ret );
43 
44  // Save data into the circular buffer:
45  m_readBuffer.push_many(buf, nActualRead);
46 
47  // Read the required amount of bytes:
48  size_t nActualReturn = std::min(m_readBuffer.size(), Count);
49 
51  reinterpret_cast<uint8_t*>(Buffer), nActualReturn);
52 
53  return nActualReturn;
54  }
55 }
56 
57 /*-------------------------------------------------------------
58  Write
59 -------------------------------------------------------------*/
60 size_t CInterfaceFTDI::Write(const void* Buffer, size_t Count)
61 {
62  unsigned long ret = 0;
63  ftdi_write(Buffer, (unsigned long)Count, &ret);
64  return (size_t)ret;
65 }
66 
67 /*-------------------------------------------------------------
68  Seek
69 -------------------------------------------------------------*/
70 uint64_t CInterfaceFTDI::Seek(uint64_t Offset, CStream::TSeekOrigin Origin)
71 {
72  MRPT_UNUSED_PARAM(Offset);
73  MRPT_UNUSED_PARAM(Origin);
74  return 0;
75 }
76 
77 /*-------------------------------------------------------------
78  getTotalBytesCount
79 -------------------------------------------------------------*/
81 /*-------------------------------------------------------------
82  getPosition
83 -------------------------------------------------------------*/
85 /*-------------------------------------------------------------
86  ReadBufferImmediate
87 -------------------------------------------------------------*/
88 size_t CInterfaceFTDI::ReadBufferImmediate(void* Buffer, size_t Count)
89 {
90  unsigned long nRead;
91  ftdi_read(Buffer, Count, &nRead);
92  return nRead;
93 }
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)
size_t Write(const void *Buffer, size_t Count)
Introduces a pure virtual method responsible for writing to the stream.
size_t available() const
The maximum number of elements that can be written ("push") without rising an overflow error...
size_t size() const
Return the number of elements available for read ("pop") in the buffer (this is NOT the maximum size ...
uint64_t getTotalBytesCount()
This virtual method does nothing in this class.
void pop_many(T *out_array, size_t count)
Pop a number of elements into a user-provided array.
unsigned char uint8_t
Definition: rptypes.h:41
mrpt::utils::circular_buffer< uint8_t > m_readBuffer
Used in Read.
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
size_t Read(void *Buffer, size_t Count)
Introduces a pure virtual method responsible for reading from the stream.
uint64_t getPosition()
This virtual method does nothing in this class.
unsigned __int64 uint64_t
Definition: rptypes.h:50
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void ftdi_read(void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytesRead)
void ftdi_write(const void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytes)
Serial and networking devices and utilities.
void push_many(T *array_elements, size_t count)
Insert an array of elements in the buffer.
uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning)
This virtual method does nothing in this class.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019