File size: 1,576 Bytes
caf1218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <pangolin/pangolin.h>

#include <pangolin/utils/posix/condition_variable.h>
#include <pangolin/utils/posix/shared_memory_buffer.h>
#include <pangolin/utils/timer.h>

#include <cmath>
#include <memory>

// This sample acts as a soft camera. It writes a pattern of GRAY8 pixels to the
// shared memory space. It can be seen in Pangolin's SimpleVideo sample using
// the shmem:[size=640x480]//example video URI.

using namespace pangolin;

unsigned char generate_value(double t)

{
  // 10s sinusoid
  const double d = std::sin(t * 10.0 / M_PI) * 128.0 + 128.0;
  return static_cast<unsigned char>(d);
}

int main(/*int argc, char *argv[]*/)

{
  std::string shmem_name = "/example";

  std::shared_ptr<SharedMemoryBufferInterface> shmem_buffer =
    create_named_shared_memory_buffer(shmem_name, 640 * 480);
  if (!shmem_buffer) {
    perror("Unable to create shared memory buffer");
    exit(1);
  }

  std::string cond_name = shmem_name + "_cond";
  std::shared_ptr<ConditionVariableInterface> buffer_full =
    create_named_condition_variable(cond_name);

  // Sit in a loop and write gray values based on some timing pattern.
  while (true) {
    shmem_buffer->lock();
    unsigned char *ptr = shmem_buffer->ptr();
    unsigned char value = generate_value(std::chrono::system_clock::now().time_since_epoch().count());

    for (int i = 0; i < 640*480; ++i) {
      ptr[i] = value;
    }

    shmem_buffer->unlock();
    buffer_full->signal();

    std::this_thread::sleep_for(std::chrono::milliseconds(10));
  }
}