forked from martinruenz/v360
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv360.hpp
More file actions
292 lines (240 loc) · 8.34 KB
/
Copy pathv360.hpp
File metadata and controls
292 lines (240 loc) · 8.34 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <Python.h>
#include <string>
#include <iostream>
#include <exception>
#ifdef unix
#include <stdlib.h>
#endif
namespace v360 {
class Variant{
public:
enum TYPE { INT, BOOL, DOUBLE, STRING, UNSET };
union DATA
{
int int_data;
double double_data;
bool bool_data;
void* raw_data = NULL;
};
Variant(){
type = UNSET;
}
Variant(bool value){
data.bool_data = value;
type = BOOL;
}
Variant(int value){
data.int_data = value;
type = INT;
}
Variant(double value){
data.double_data = value;
type = DOUBLE;
}
Variant(float value){
data.double_data = value;
type = DOUBLE;
}
Variant(std::string value){
data.raw_data = new std::string;
*(std::string*)data.raw_data = value;
type = STRING;
}
Variant(const char* value, size_t length = -1){
data.raw_data = new std::string(value, length != -1 ? length : strnlen(value, 100000000));
type = STRING;
}
~Variant(){
deleteRaw();
}
Variant(const Variant& other ){
*this = other;
}
Variant& operator=( const Variant& other ) {
type = other.getType();
if(type != STRING){
data = other.getData();
} else {
deleteRaw();
data.raw_data = new std::string;
*(std::string*)data.raw_data = *(std::string*)other.getData().raw_data;
}
return *this;
}
inline void deleteRaw() {
if(data.raw_data == NULL) return;
if(type == STRING){
delete (std::string*)data.raw_data;
return;
}
}
inline TYPE getType() const { return type; }
bool isSet() const { return type != UNSET; }
bool isBool() const { return type == BOOL; }
bool isInt() const { return type == INT; }
bool isDouble() const { return type == DOUBLE; }
bool isString() const { return type == STRING; }
bool toBool() const { return data.bool_data; }
int toInt() const { return data.int_data; }
double toDouble() const { return data.double_data; }
const std::string& toString() const { return *(std::string*)data.raw_data; }
std::ostream& outputToStream(std::ostream& ostr) const {
switch (type){
case DOUBLE:
return ostr << data.double_data;
case BOOL:
if (data.bool_data) return ostr << "true";
else return ostr << "false";
case INT:
return ostr << data.int_data;
case STRING:
return ostr << *(std::string*)data.raw_data;
case UNSET:
return ostr << "(unset)";
}
return ostr << "(unknown)";
}
private:
DATA data;
inline DATA getData() const { return data; }
TYPE type;
};
inline std::ostream& operator<<(std::ostream& lhs, Variant const& value){
return value.outputToStream(lhs);
}
class Remote {
private:
PyObject *pName, *pModule, *pDict, *pFunc, *pClass, *pInstance;
public:
Remote(){
#ifdef unix
setenv("PYTHONPATH",".",1);
#endif
Py_Initialize();
PyRun_SimpleString("import os, sys");
PyRun_SimpleString("sys.path.insert(0, os.path.abspath(\"..\"))");
PyRun_SimpleString("import v360");
pName = PyUnicode_FromString("v360");
pModule = PyImport_Import(pName);
if (pModule == NULL) {
PyErr_Print();
fprintf(stderr, "Failed to load \"v360\"\n");
throw std::exception(); // TODO
}
pDict = PyModule_GetDict(pModule); // borrowed
pClass = PyDict_GetItemString(pDict, "Remote"); // borrowed
pInstance = PyObject_CallObject(pClass, NULL);
}
~Remote(){
Py_DECREF(pModule);
Py_DECREF(pName);
Py_DECREF(pClass);
Py_DECREF(pDict);
Py_XDECREF(pInstance);
Py_Finalize();
}
inline Variant convertResult(PyObject* pValue){
Variant result;
if (pValue != NULL){
if(PyFloat_Check(pValue)) result = Variant(PyFloat_AsDouble(pValue));
if(PyBool_Check(pValue)) result = Variant((bool)(PyLong_AsLong(pValue) != 0));
if(PyLong_CheckExact(pValue)) result = Variant((int)PyLong_AsLong(pValue));
if(PyUnicode_Check(pValue)) {
PyObject* ascii = PyUnicode_AsASCIIString(pValue);
result = Variant(PyBytes_AsString(ascii), PyBytes_Size(ascii));
Py_DECREF(ascii);
}
Py_DECREF(pValue);
}else{
PyErr_Print();
}
return result;
}
inline bool returnBool(PyObject* pValue){
Variant result = convertResult(pValue);
if (!result.isBool()) return false;
return result.toBool();
}
#ifdef unix
inline bool turnOn(const std::string& bluetooth_mac, const std::string& interface = "", int repeat = 3){
return returnBool(PyObject_CallMethod(pInstance, (char*)"turnOn", (char*)"(ssi)", bluetooth_mac.c_str(), interface.c_str(), repeat));
}
#endif
inline void turnOff(){
Py_XDECREF(PyObject_CallMethod(pInstance, (char*)"turnOff", NULL));
}
inline bool connect(const std::string& pin = "0000", const std::string& token = ""){
if (token == "") return returnBool(PyObject_CallMethod(pInstance, (char*)"connect", (char*)"(s)", pin.c_str()));
return returnBool(PyObject_CallMethod(pInstance, (char*)"connect", (char*)"(ss)", pin.c_str(), token.c_str()));
}
inline void disconnect(){
Py_XDECREF(PyObject_CallMethod(pInstance, (char*)"disconnect", NULL));
}
inline void setVerbose(bool verbose){
PyObject_SetAttrString(pClass, (char*)"verbose", verbose ? Py_True : Py_False);
}
inline bool setVideoResolution(const std::string& resolution){
return returnBool(PyObject_CallMethod(pInstance, (char*)"setVideoResolution", (char*)"(s)", resolution.c_str()));
}
inline bool setVideoWhitebalance(const std::string& whitebalance){
return returnBool(PyObject_CallMethod(pInstance, (char*)"setVideoWhitebalance", (char*)"(s)", whitebalance.c_str()));
}
inline bool setVideoEffect(const std::string& effect){
return returnBool(PyObject_CallMethod(pInstance, (char*)"setVideoEffect", (char*)"(s)", effect.c_str()));
}
inline bool setMode(const std::string& mode){
return returnBool(PyObject_CallMethod(pInstance, (char*)"setMode", (char*)"(s)", mode.c_str()));
}
inline bool setAutoRotation(bool on){
return returnBool(PyObject_CallMethod(pInstance, (char*)"setAutoRotation", (char*)"(b)", on)); // TODO: Check (b), otherwise (i)
}
inline bool startVideo(){
return returnBool(PyObject_CallMethod(pInstance, (char*)"startVideo", NULL));
}
inline bool stopVideo(){
return returnBool(PyObject_CallMethod(pInstance, (char*)"stopVideo", NULL));
}
inline Variant getName(){
return convertResult(PyObject_CallMethod(pInstance, (char*)"getName", NULL));
}
inline Variant getBattery(){
return convertResult(PyObject_CallMethod(pInstance, (char*)"getBattery", NULL));
}
inline Variant getHdmiConnected(){
return convertResult(PyObject_CallMethod(pInstance, (char*)"getHdmiConnected", NULL));
}
inline Variant getPowerConnected(){
return convertResult(PyObject_CallMethod(pInstance, (char*)"getPowerConnected", NULL));
}
inline Variant getVideoResolution(){
return convertResult(PyObject_CallMethod(pInstance, (char*)"getVideoResolution", NULL));
}
inline Variant getVideoEffect(){
return convertResult(PyObject_CallMethod(pInstance, (char*)"getVideoEffect", NULL));
}
inline Variant getVideoWhitebalance(){
return convertResult(PyObject_CallMethod(pInstance, (char*)"getVideoWhitebalance", NULL));
}
inline Variant getVideoTimelapseInterval(){
return convertResult(PyObject_CallMethod(pInstance, (char*)"getVideoTimelapseInterval", NULL));
}
inline Variant getPhotoBurstRate(){
return convertResult(PyObject_CallMethod(pInstance, (char*)"getPhotoBurstRate", NULL));
}
inline Variant getPhotoEffect(){
return convertResult(PyObject_CallMethod(pInstance, (char*)"getPhotoEffect", NULL));
}
inline Variant getPhotoExposure(){
return convertResult(PyObject_CallMethod(pInstance, (char*)"getPhotoExposure", NULL));
}
inline Variant getPhotoResolution(){
return convertResult(PyObject_CallMethod(pInstance, (char*)"getPhotoResolution", NULL));
}
inline Variant getPhotoTimelapseInterval(){
return convertResult(PyObject_CallMethod(pInstance, (char*)"getPhotoTimelapseInterval", NULL));
}
inline Variant getPhotoWhitebalance(){
return convertResult(PyObject_CallMethod(pInstance, (char*)"getPhotoWhitebalance", NULL));
}
};
}