summaryrefslogtreecommitdiffhomepage
path: root/video-ps3eye/wrapper.cxx
blob: b7f58185e09dfda84681be8e836351f81006e020 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "shm-layout.hpp"
#include "shm.hpp"

#include "ps3eye-driver/ps3eye.hpp"

#include <cstdlib>
#include <atomic>

#ifdef __clang__
#   pragma clang diagnostic ignored "-Watomic-implicit-seq-cst"
#endif

#ifdef __GNUG__
#   pragma GCC diagnostic ignored "-Wcast-qual"
#   pragma GCC diagnostic ignored "-Wformat-security"
#   pragma GCC diagnostic ignored "-Wformat-nonliteral"
#endif

template<int N, typename... xs>
[[noreturn]]
static void error(volatile ps3eye::shm_out& out, const char (&error)[N], const xs&... args)
{
    snprintf((char*)out.error_string, sizeof(ps3eye::shm_out::error_string), error, args...);
    std::quick_exit(2);
}

static void update_settings(ps3eye::camera& camera, const volatile ps3eye::shm_in& in)
{
    //camera.set_framerate(in.framerate);
    camera.set_auto_gain(in.auto_gain);
    camera.set_gain(in.gain);
    camera.set_exposure(in.exposure);
    camera.set_test_pattern_status(in.test_pattern);
}

static ps3eye::resolution get_mode(ps3eye::shm_in::mode res)
{
    switch (res)
    {
    default:
    case ps3eye::shm_in::mode::qvga:
        return ps3eye::res_QVGA;
    case ps3eye::shm_in::mode::vga:
        return ps3eye::res_VGA;
    }
}

int main(int argc, char** argv)
{
    (void)argc; (void)argv;
    shm_wrapper mem_("ps3eye-driver-shm", nullptr, sizeof(ps3eye::shm));
    volatile auto& ptr_ = *(ps3eye::shm*)mem_.ptr();
    volatile auto& in = ptr_.in;
    volatile auto& out = ptr_.out;
    int num_channels = in.channels;

    auto cameras = ps3eye::list_devices();

    out.status_ = ps3eye::shm_out::status::starting;

    if (cameras.empty())
        error(out, "no camera found");

    auto& camera = cameras[0];
    camera->set_debug(true);
    auto* frame = (uint8_t*)out.data_640x480;
    decltype(out.timecode) timecode = 0;

    auto fmt = num_channels == 1 ? ps3eye::format::Gray : ps3eye::format::BGR;

    {
        int framerate = in.framerate;
        if (framerate <= 0)
            framerate = 60;

        if (!camera->init(get_mode(in.resolution), framerate, fmt))
            error(out, "camera init failed: %s", camera->error_string());

        update_settings(*camera, in);

        if (!camera->start())
            error(out, "can't start camera: %s", camera->error_string());
    }

    out.timecode = 0;
    in.do_exit = false;
    std::atomic_thread_fence(std::memory_order_seq_cst);

    for (;;)
    {
        {
            auto cookie = in.settings_updated;
            if (cookie != out.settings_updated_ack)
            {
                camera->stop();
                update_settings(*camera, in);
                int framerate = in.framerate;
                if (framerate <= 0)
                    framerate = 60;
                if (!camera->init(get_mode(in.resolution), framerate, fmt))
                    error(out, "camera init failed: %s", camera->error_string());
                if (!camera->start())
                    error(out, "can't start camera: %s", camera->error_string());
                out.settings_updated_ack = cookie;
            }
        }

        if (!camera->get_frame(frame))
            continue;

        out.timecode = ++timecode;

        if (in.do_exit)
            break;

        std::atomic_thread_fence(std::memory_order_seq_cst);
    }

    return 0;
}