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
|
#include "legato.h"
#include "interfaces.h"
#include "camera.h"
#include <time.h>
COMPONENT_INIT {
Camera cam = {
.fd = openCameraFd(),
.serialNum = 0x0,
.bufferLen = 0,
.frameptr = 0
};
bool success = takePicture(&cam);
LE_INFO("Number of bytes in buffer: %d", NUM_ARRAY_MEMBERS(cam.buff));
LE_INFO("Did it work: %d", success);
}
// Used to wait for the serial port
void delay (unsigned int msecs) {
unsigned int secs = msecs / 1000;
unsigned int retTime = time(0) + secs;
while (time(0) < retTime);
}
// Returns a file descriptor that can be used to
// talk to the camera
int openCameraFd () {
int fd = le_tty_Open(SERIAL_PATH, O_RDWR);
le_tty_SetBaudRate(fd, CAM_BAUD_RATE);
return fd;
}
// Send a command to the camera serial port
void sendCommand (Camera *cam, byte cmd, byte args[]) {
uint8_t init = VC0706_PREFIX;
write(cam->fd, &init, 1); // send the cmd prefix
write(cam->fd, &(cam->serialNum), 1); // send the serial number
write(cam->fd, &cmd, 1); // send the cmd
for(int i = 0; i < NUM_ARRAY_MEMBERS(args); i++) { // send each arg
write(cam->fd, &args[i], 1);
}
}
// Run a command
bool runCommand (Camera *cam, byte cmd, byte args[], int respLen) {
sendCommand(cam, cmd, args);
if (readResponse(cam, respLen, TIMEOUT) != respLen)
return false;
if (verifyResponse(cam, cmd))
return false;
else return true;
}
// Reads from the camera and returns how many bytes it read
int readResponse (Camera *cam, int nBytes, int timeout) {
int counter = 0;
int bufferLen = 0;
// read while below timeout and while the buffer
// is still smaller than expected
while(timeout >= counter && cam->bufferLen <= nBytes) {
ssize_t bytesRead = read(cam->fd, &(cam->buff) + cam->bufferLen, 1); // read one byte at a time
bufferLen++;
// bytesRead will be 0 or -1 if no data was received
if (bytesRead <= 0) {
delay(1);
counter++;
continue;
}
counter = 0;
}
return cam->bufferLen;
}
bool verifyResponse (Camera *cam, byte cmd) {
// If any of these are not equal than
// the command failed
return
!(cam->buff[0] != VC0706_RESP_PREFIX ||
cam->buff[1] != cam->serialNum ||
cam->buff[2] != cmd ||
cam->buff[3] != 0x0);
}
bool cameraFrameBuffCtrl (Camera *cam, byte cmd) {
byte args[] = { 0x1, cmd };
return runCommand(cam, cmd, args, 5);
}
bool takePicture (Camera *cam) {
return cameraFrameBuffCtrl(cam, VC0706_STOPCURRENTFRAME);
}
bool reset (Camera *cam) {
byte args[] = { 0x0 };
return runCommand(cam, VC0706_RESET, args, 5);
}
size_t bufferToFile (Camera *cam, char *path) {
FILE *photo = fopen(path, "w");
if (photo != NULL) {
LE_INFO("File pointer for %s is valid", path);
size_t bytesWritten = fwrite(&(cam->buff), sizeof(cam->buff[0]), cam->bufferLen, photo);
fclose(photo);
return bytesWritten;
}
else {
LE_INFO("File pointer for %s is invalid", path);
return -1;
}
}
// TODO implement these
bool TVon (Camera *cam);
bool TVOff (Camera *cam);
byte *readPicture (Camera *cam);
bool resumeVideo (Camera *cam);
uint32_t frameLength (Camera *cam);
char *getVersion (Camera *cam);
byte available (Camera *cam);
byte getDownsize (Camera *cam);
bool setDownsize(Camera *cam, int newSize);
int getImageSize (Camera *cam);
bool setImageSize (Camera *cam, int size);
bool getMotionDetect (Camera *cam);
byte getMotionStatus(Camera *cam, byte x);
bool motionDetected (Camera *cam);
bool setMotionDetect (Camera *cam, bool flag);
bool setMotionStatus (Camera *cam, byte x, byte d1, byte d2);
byte getCompression (Camera *cam);
bool setCompression(Camera *cam, byte c);
bool getPTZ(Camera *cam, uint16_t *w, uint16_t *h, uint16_t *wz, uint16_t *hz, uint16_t *pan, uint16_t *tilt);
bool setPTZ(Camera *cam, uint16_t wz, uint16_t hz, uint16_t pan, uint16_t tilt);
|