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 timespec util::calc_img_timestamp(const timeval & buffer_time, const time_t & epoch_time_shift_us)
24 {
25  timespec img_timestamp;
26 
27  int64_t buffer_time_us = (buffer_time.tv_sec * 1000000) + buffer_time.tv_usec;
28  buffer_time_us += epoch_time_shift_us;
29 
30  img_timestamp.tv_sec = (buffer_time_us / 1000000);
31  img_timestamp.tv_nsec = (buffer_time_us % 1000000) * 1000;
32 
33  return img_timestamp;
34 }
35 
36 int util::xioctl(int fd, int request, void * arg)
37 {
38  int r;
39  do
40  {
41  r = ioctl(fd, request, arg);
42  continue;
43  } while (-1 == r && EINTR == errno);
44 
45  return r;
46 }
47 
48 unsigned char util::CLIPVALUE(const int & val)
49 {
50  // Old method (if)
51  /* val = val < 0 ? 0 : val; */
52  /* return val > 255 ? 255 : val; */
53 
54  try {
55  // New method array
58  } catch (std::out_of_range const &) {
59  // fall back to old method
60  unsigned char clipped_val = val < 0 ? 0 : static_cast<unsigned char>(val);
61  return val > 255 ? 255 : clipped_val;
62  }
63 }
usb_cam::util::calc_img_timestamp
timespec calc_img_timestamp(const timeval &buffer_time, const time_t &epoch_time_shift_us)
Calculate image timestamp from buffer time and epoch time shift. In this, the buffer time is first co...
Definition: util.cpp:23
usb_cam
Definition: camera_driver.h:35
usb_cam::constants::uchar_clipping_table
static const std::vector< unsigned char > uchar_clipping_table
Definition: types.h:29
usb_cam::util::get_epoch_time_shift_us
time_t get_epoch_time_shift_us()
Get epoch time shift.
Definition: util.cpp:5
usb_cam::util::xioctl
int xioctl(int fd, int request, void *arg)
Definition: util.cpp:36
usb_cam::util::CLIPVALUE
unsigned char CLIPVALUE(const int &val)
Definition: util.cpp:48
usb_cam::constants::clipping_table_offset
static const int clipping_table_offset
Definition: types.h:70
util.h


usb_cam
Author(s): Benjamin Pitzer
autogenerated on Sun Sep 3 2023 02:44:54