util.cpp
Go to the documentation of this file.
1 #include "usb_cam/util.h"
2 
3 using namespace usb_cam;
4 
6 {
7  struct timeval epoch_time;
8  struct timespec monotonic_time;
9 
10  gettimeofday(&epoch_time, NULL);
11  clock_gettime(CLOCK_MONOTONIC, &monotonic_time);
12 
13  const int64_t uptime_ms =
14  monotonic_time.tv_sec * 1000 + static_cast<int64_t>(
15  std::round(monotonic_time.tv_nsec / 1000000.0));
16  const int64_t epoch_ms =
17  epoch_time.tv_sec * 1000 + static_cast<int64_t>(
18  std::round(epoch_time.tv_usec / 1000.0));
19 
20  return static_cast<time_t>((epoch_ms - uptime_ms) / 1000);
21 }
22 
23 int util::xioctl(int fd, int request, void * arg)
24 {
25  int r;
26  do
27  {
28  r = ioctl(fd, request, arg);
29  continue;
30  } while (-1 == r && EINTR == errno);
31 
32  return r;
33 }
34 
35 unsigned char util::CLIPVALUE(const int & val)
36 {
37  // Old method (if)
38  /* val = val < 0 ? 0 : val; */
39  /* return val > 255 ? 255 : val; */
40 
41  try {
42  // New method array
45  } catch (std::out_of_range const &) {
46  // fall back to old method
47  unsigned char clipped_val = val < 0 ? 0 : static_cast<unsigned char>(val);
48  return val > 255 ? 255 : clipped_val;
49  }
50 }
51 
53 {
54  struct timeval epoch_time;
55  struct timespec monotonic_time;
56  gettimeofday(&epoch_time, NULL);
57  clock_gettime(CLOCK_MONOTONIC, &monotonic_time);
58  const int64_t uptime_ms =
59  monotonic_time.tv_sec * 1000 + static_cast<int64_t>(
60  std::round(monotonic_time.tv_nsec / 1000000.0));
61  const int64_t epoch_ms =
62  epoch_time.tv_sec * 1000 + static_cast<int64_t>(
63  std::round(epoch_time.tv_usec / 1000.0));
64  return static_cast<time_t>((epoch_ms - uptime_ms) / 1000);
65 }
time_t get_epoch_time_shift()
Get epoch time shift.
Definition: util.cpp:5
unsigned char CLIPVALUE(const int &val)
Definition: util.cpp:35
static const std::vector< unsigned char > uchar_clipping_table
Definition: types.h:29
static const int clipping_table_offset
Definition: types.h:70
time_t get_epoch_time_shift()
Get epoch time shift.
Definition: util.cpp:52
int xioctl(int fd, int request, void *arg)
Definition: util.cpp:23


usb_cam
Author(s): Benjamin Pitzer
autogenerated on Sat May 27 2023 02:53:05