-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cpp
More file actions
200 lines (177 loc) · 4.48 KB
/
app.cpp
File metadata and controls
200 lines (177 loc) · 4.48 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include <cstdio>
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
#include <sstream>
#include <raspicam/raspicam.h>
#include <raspicam/raspicam_cv.h>
// Static variables for actions
bool invert_change = false;
bool normal_change = false;
bool zoom_change = false;
// Events
bool event_button_up = false;
bool event_button_down = false;
bool event_button_invert = false;
// Static variables for debouncing
uint8_t last_button_up = 1;
uint8_t last_button_down = 1;
uint8_t last_button_invert = 1;
// Capture and display variables
float width = 1280.0f;
float height = 960.0f;
float zoom = 1.0f;
bool invert = false;
int x = floor((((width / zoom) * (zoom / 2.0)) - ((width / zoom) / 2.0)));
int y = floor((((height / zoom) * (zoom / 2.0))- ((height / zoom) / 2.0)));
int w = floor((width / zoom));
int h = floor((height / zoom));
// efine RPi GPIO pins
#define PIN_UP "/sys/class/gpio/gpio4/value"
#define PIN_DOWN "/sys/class/gpio/gpio23/value"
#define PIN_INVERT "/sys/class/gpio/gpio24/value"
uint8_t getval_gpio(const char * pin)
{
std::string val;
uint8_t returnval = 0;
std::ifstream getvalgpio(pin);
getvalgpio >> val ; //read gpio value
if (val != "0")
returnval = 1;
getvalgpio.close(); //close the value file
return returnval;
}
void debounce()
{
uint8_t button_up = getval_gpio(PIN_UP);
if (button_up != last_button_up)
{
last_button_up = button_up;
if (button_up == 0)
{
event_button_up = true;
}
}
uint8_t button_down = getval_gpio(PIN_DOWN);
if (button_down != last_button_down)
{
last_button_down = button_down;
if (button_down == 0)
{
event_button_down = true;
}
}
uint8_t button_invert = getval_gpio(PIN_INVERT);
if (button_invert != last_button_invert)
{
last_button_invert = button_invert;
if (button_invert == 0)
{
event_button_invert = true;
}
}
}
void handle_events()
{
if (event_button_up)
{
event_button_up = false;
if ((int)zoom < 8)
{
zoom = (float)((int)zoom + 1);
zoom_change = true;
}
}
if (event_button_down)
{
event_button_down = false;
if ((int)zoom > 1)
{
zoom = (float)((int)zoom - 1);
zoom_change = true;
}
}
if (event_button_invert)
{
event_button_invert = false;
if (invert)
{
invert = false;
normal_change = true;
}
else
{
invert = true;
invert_change = true;
}
}
}
int main()
{
raspicam::RaspiCam_Cv camera;
cv::Mat image;
camera.set( CV_CAP_PROP_FORMAT, CV_8UC3 );
camera.set(CV_CAP_PROP_WHITE_BALANCE_RED_V, 0);
camera.set(CV_CAP_PROP_FRAME_WIDTH, 1440);
camera.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);
if (!camera.open())
{
std::cout << "Error opening the camera" << std::endl;
return -1;
}
std::cout << "Camera is opened" << std::endl;
cvNamedWindow("Name", CV_WINDOW_NORMAL);
cvSetWindowProperty("Name", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
while (1)
{
//-- INVERT --------------------------------------
if (invert_change)
{
std::cout << "Invert ON" << std::endl;
camera.set(CV_CAP_PROP_MODE, raspicam::RASPICAM_IMAGE_EFFECT_NEGATIVE);
invert_change = false;
}
if (normal_change)
{
std::cout << "Invert OFF" << std::endl;
camera.set(CV_CAP_PROP_MODE, raspicam::RASPICAM_IMAGE_EFFECT_NONE);
normal_change = false;
}
if (zoom_change)
{
std::cout << "Zoom: " << zoom << std::endl;
x = floor((((width / zoom) * (zoom / 2.0)) - ((width / zoom) / 2.0)));
y = floor((((height / zoom) * (zoom / 2.0))- ((height / zoom) / 2.0)));
w = floor((width / zoom));
h = floor((height / zoom));
zoom_change = false;
}
//-- CAPTURE --------------------------------------------------------------------------
camera.grab();
camera.retrieve(image);
//-- ZOOM -----------------------------------------------------------------------------
cv::Rect new_size(x, y, w, h);
cv::Mat tmp = image(new_size);
cv::resize(tmp, tmp, cv::Size(width, height), 0, 0, CV_INTER_LINEAR);
//-- DISPLAY -----------------------------------------------------------------------------
cv::imshow("Name", tmp);
//-- USER INPUT -------------------------------------------------------
auto key = cv::waitKey(1);
if (key == 'q')
break;
else if (key == 'a')
event_button_down = true;
else if (key == 'z')
event_button_up = true;
else if (key == 'x')
event_button_invert = true;
debounce();
handle_events();
}
return 0;
}