-
Notifications
You must be signed in to change notification settings - Fork 35
/
camdriver.c
223 lines (202 loc) · 7.24 KB
/
camdriver.c
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Johan Kanflo (github.com/kanflo)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <arducam.h>
#include <unistd.h>
#include <string.h>
#include <http_upload.h>
#include "timeutils.h"
#include "camdriver.h"
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
#define BUF_SIZE (200)
static char buffer[BUF_SIZE];
bool arducam_setup(void)
{
uint8_t vid, pid, temp;
arducam(smOV2640);
// Check if the ArduCAM SPI bus is OK
arducam_write_reg(ARDUCHIP_TEST1, 0x55);
temp = arducam_read_reg(ARDUCHIP_TEST1);
if(temp != 0x55) {
printf("SPI interface error (got 0x%02x)!\n", temp);
return false;
}
// Change MCU mode
// arducam_write_reg(ARDUCHIP_MODE, 0x00);
// Check if the camera module type is OV2640
arducam_i2c_read(OV2640_CHIPID_HIGH, &vid);
arducam_i2c_read(OV2640_CHIPID_LOW, &pid);
if((vid != 0x26) || (pid != 0x42)) {
printf("Error: cannot find OV2640 module (got 0x%02x, 0x%02x)\n", vid, pid);
return false;
} else {
printf("OV2640 detected\n");
}
printf("Setting JPEG\n");
arducam_set_format(fmtJPEG);
printf("Init\n");
arducam_init(); // Note to self. Must call set_format before init.
printf("Setting size\n");
arducam_set_jpeg_size(sz640x480);
// Allow for auto exposure loop to adapt to after resolution change
printf("Autoexposure working...\n");
delay_ms(1000);
printf("Done...\n");
return true;
}
bool arducam_capture(void)
{
uint8_t temp;
uint32_t start_time = systime_ms();
// arducam_flush_fifo(); // TODO: These are the same
arducam_clear_fifo_flag(); // TODO: These are the same
printf("Start capture\n");
arducam_start_capture();
temp = arducam_read_reg(ARDUCHIP_TRIG);
if (!temp) {
printf("Failed to start capture!\n");
return false;
} else {
while (!(arducam_read_reg(ARDUCHIP_TRIG) & CAP_DONE_MASK)) {
delay_ms(1);
}
printf("Capture done after %ums\n", systime_ms()-start_time);
}
return true;
}
void arudcam_fifo_to_netcon(struct netconn *client)
{
uint8_t temp, temp_last = 0, buf_idx = 0;
uint32_t fifo_size, bytes_read, start_time = systime_ms();
fifo_size = (arducam_read_reg(0x44) << 16) | (arducam_read_reg(0x43) << 8) | (arducam_read_reg(0x42));
printf("%u bytes in fifo, according to camera\n", fifo_size);
bytes_read = 1;
temp = arducam_read_fifo();
// printf("%02x ", temp);
buffer[buf_idx++] = temp;
// Read JPEG data from FIFO
while((temp != 0xd9) | (temp_last != 0xff)) {
temp_last = temp;
temp = arducam_read_fifo();
buffer[buf_idx++] = temp;
if (client && buf_idx == BUF_SIZE) {
if (ERR_OK != netconn_write(client, buffer, buf_idx, NETCONN_COPY)) {
printf("netconn_write failed\n");
return;
}
buf_idx = 0;
}
bytes_read++;
if (bytes_read > fifo_size) {
printf("Fifo error\n");
break;
}
}
if (client && buf_idx > 0) {
if (ERR_OK != netconn_write(client, buffer, buf_idx, NETCONN_COPY)) {
printf("netconn_write failed\n");
}
}
printf("Done, read %u bytes in %ums\n", bytes_read, systime_ms()-start_time);
}
void arudcam_fifo_to_devnull()
{
uint8_t temp, temp_last = 0, buf_idx = 0;
uint32_t fifo_size, bytes_read, start_time = systime_ms();
fifo_size = (arducam_read_reg(0x44) << 16) | (arducam_read_reg(0x43) << 8) | (arducam_read_reg(0x42));
printf("%u bytes in fifo, according to camera\n", fifo_size);
bytes_read = 1;
temp = arducam_read_fifo();
// printf("%02x ", temp);
buffer[buf_idx++] = temp;
// Read JPEG data from FIFO
while((temp != 0xd9) | (temp_last != 0xff)) {
temp_last = temp;
temp = arducam_read_fifo();
bytes_read++;
if (bytes_read > fifo_size) {
printf("Fifo error\n");
break;
}
}
printf("Done, read %u bytes in %ums\n", bytes_read, systime_ms()-start_time);
}
void arudcam_upload_fifo(char *host, uint16_t port)
{
uint8_t temp, temp_last = 0, buf_idx = 0;
uint32_t http_res, fifo_size, bytes_read, start_time = systime_ms();
fifo_size = (arducam_read_reg(0x44) << 16) | (arducam_read_reg(0x43) << 8) | (arducam_read_reg(0x42));
printf("%u bytes in fifo, according to camera\n", fifo_size);
do {
printf("Connecting to server...\n");
if (!upload_connect(host, port)) {
printf("Failed to connect to %s:%d\n", host, port);
break;
}
printf(" Connected\n");
if (!upload_begin("/", "image.jpg", fifo_size)) {
printf("Failed to upload\n");
break;
}
printf("Uploading...\n");
bytes_read = 1;
temp = arducam_read_fifo();
buffer[buf_idx++] = temp;
// Read JPEG data from FIFO
while((temp != 0xd9) | (temp_last != 0xff)) {
temp_last = temp;
temp = arducam_read_fifo();
buffer[buf_idx++] = temp;
if (buf_idx == BUF_SIZE) {
if (!upload_data(buffer, buf_idx)) {
printf("Data upload failed\n");
break;
}
buf_idx = 0;
}
bytes_read++;
if (bytes_read > fifo_size) {
printf("Fifo error\n");
break;
}
}
if (buf_idx > 0) {
(void) upload_data(buffer, buf_idx);
}
if (bytes_read < fifo_size) {
printf("Padding upload data with %d bytes\n", fifo_size - bytes_read);
memset(buffer, 0, BUF_SIZE);
while (bytes_read < fifo_size) {
uint32_t diff = fifo_size - bytes_read;
(void) upload_data(buffer, MIN(diff, BUF_SIZE));
bytes_read += MIN(diff, BUF_SIZE);
}
}
printf("Finishing upload\n");
http_res = upload_finish();
printf("Closing\n");
upload_close();
printf("Done, read %u bytes in %ums, server responded with %d\n", bytes_read, systime_ms()-start_time, http_res);
} while(0);
}