-
Notifications
You must be signed in to change notification settings - Fork 28
/
captureimage.c
277 lines (273 loc) · 4.85 KB
/
captureimage.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
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
#include <avr/io.h>
#include <avr/interrupt.h>
#include "config.h"
#include "TFT.h"
void capImgqqvga(uint8_t offsetx){
cli();
uint8_t w,ww,h;
uint8_t y=0;
w=160;
h=120;
DDRA=0xFF;
DDRC=0;
#ifdef MT9D111
while (PINE&32){}//wait for low
while (!(PINE&32)){}//wait for high
#else
while (!(PINE&32)){}//wait for high
while (PINE&32){}//wait for low
#endif
do{
tft_setXY(y,offsetx);
++y;
CS_LOW;
RS_HIGH;
RD_HIGH;
ww=w;
do{
WR_LOW;
while (PINE&16){}//wait for low
PORTA=PINC;
WR_HIGH;
#ifndef ov7670
while (!(PINE&16)){}//wait for high
#endif
WR_LOW;
while (PINE&16){}//wait for low
PORTA=PINC;
WR_HIGH;
#ifndef ov7670
while (!(PINE&16)){}//wait for high
#endif
}while (--ww);
}while (--h);
CS_HIGH;
sei();
}
static inline void serialWrB(uint8_t dat){
UDR0=dat;
while (!( UCSR0A & (1<<UDRE0))); //wait for byte to transmit
}
#ifdef MT9D111
void capImgOff(uint16_t off)
#else
void capImgOff(uint8_t off)
#endif
{
tft_setXY(0,0);
CS_LOW;
RS_HIGH;
RD_HIGH;
DDRA=0xFF;
DDRC=0;
uint16_t w,ww;
uint8_t h;
#ifdef MT9D111
w=1600;
h=96;
while (PINE&32){}//wait for low
while (!(PINE&32)){}//wait for high
#else
w=640;
h=240;
while (!(PINE&32)){}//wait for high
while (PINE&32){}//wait for low
#endif
if(off!=0){
while (off--){
ww=w;
while(ww--){
#ifdef MT9D111
while (!(PINE&16)){}//wait for high
while (PINE&16){}//wait for low
#else
while (PINE&16){}//wait for low
while (!(PINE&16)){}//wait for high
#endif
}
}
}
while (h--){
ww=w;
while(ww--){
WR_LOW;
while (PINE&16){}//wait for low
PORTA=PINC;
WR_HIGH;
while (!(PINE&16)){}//wait for high
}
}
}
static void transBuffer(void){
uint16_t w;
uint8_t h;
for (h=0;h<240;++h){
for (w=0;w<320;++w){
tft_setXY(h,w);
uint16_t res=tft_readRegister(0x22);
serialWrB(res>>8);
serialWrB(res&255);
}
}
}
void capImgPCqvga(void){
cli();
serialWrB('R');
serialWrB('D');
serialWrB('Y');
uint16_t w,ww;
uint8_t h;
w=640;
h=240;
tft_setXY(0,0);
CS_LOW;
RS_HIGH;
RD_HIGH;
DDRA=0xFF;
DDRC=0;
#ifdef MT9D111
while (PINE&32){}//wait for low
while (!(PINE&32)){}//wait for high
#else
while (!(PINE&32)){}//wait for high
while (PINE&32){}//wait for low
#endif
while (h--){
ww=w;
while (ww--){
WR_LOW;
while (PINE&16){}//wait for low
PORTA=PINC;
WR_HIGH;
while (!(PINE&16)){}//wait for high
}
}
transBuffer();
}
void capImgPC(void){//sends image data to serial
//this must be divided into multiple transfers due to lack of ram
cli();
serialWrB('R');
serialWrB('D');
serialWrB('Y');
#ifdef MT9D111
uint16_t o;
for(o=0;o<576;o+=96){
capImgOff(o);
transBuffer();
}
#else
capImgOff(0);
transBuffer();
capImgOff(240);
transBuffer();
#endif
sei();
}
#ifdef MT9D111
uint32_t capJpeg(void){
uint32_t numBytes;
while(1){
uint32_t count=0;
cli();
tft_setXY(0,0);
CS_LOW;
RS_HIGH;
RD_HIGH;
DDRA=0xFF;
DDRC=0;
while (PINE&32){}//wait for low
while (!(PINE&32)){}//wait for high
do{//keep reading until no valid jpeg data is output
while (PINE&16){
if(!(PINE&32))
break;
}//wait for low
WR_LOW;
PORTA=PINC;
WR_HIGH;
while (!(PINE&16)){
if(!(PINE&32))
break;
}//wait for high
}while (PINE&32);//Seems to get stuck here
sei();
//Return number of bytes read
wrReg(0xF0,2);
uint16_t stat=rdReg16(0x02);
if((stat&14)){
//Restart the process
char tmpD[16];
utoa(stat,tmpD,10);
tft_drawString(tmpD,224,320,2,RED);
tft_setOrientation(1);
}else{
wrReg16(0xF0,1);
wrReg16(0xC6,(1<<13)|(9<<8)|16);
numBytes=((uint32_t)rdReg16(0xC8))&0xFFFF;
wrReg16(0xC6,(1<<15)|(1<<13)|(9<<8)|15);
numBytes|=(((uint32_t)rdReg16(0xC8)&255)<<16);
char tmpD[32];
ultoa(numBytes,tmpD,16);
tft_fillRectangle(224,320,16,160,BLUE);
tft_drawString(tmpD,224,320,2,WHITE);
if(numBytes<=((uint32_t)320*240*2))
if(numBytes)
break;
else
tft_setOrientation(1);
else{
tft_setOrientation(1);
}
}
}
tft_setOrientation(1);
return numBytes;
}
#endif
void capImg(void){
cli();
uint8_t w,ww;
uint8_t h;
w=160;
h=240;
tft_setXY(0,0);
CS_LOW;
RS_HIGH;
RD_HIGH;
DDRA=0xFF;
#ifdef MT9D111
while (PINE&32){}//wait for low
while (!(PINE&32)){}//wait for high
#else
while (!(PINE&32)){}//wait for high
while (PINE&32){}//wait for low
#endif
while (h--){
ww=w;
while (ww--){
WR_LOW;
while (PINE&16){}//wait for low
PORTA=PINC;
WR_HIGH;
while (!(PINE&16)){}//wait for high
WR_LOW;
while (PINE&16){}//wait for low
PORTA=PINC;
WR_HIGH;
while (!(PINE&16)){}//wait for high
WR_LOW;
while (PINE&16){}//wait for low
PORTA=PINC;
WR_HIGH;
while (!(PINE&16)){}//wait for high
WR_LOW;
while (PINE&16){}//wait for low
PORTA=PINC;
WR_HIGH;
while (!(PINE&16)){}//wait for high
}
}
CS_HIGH;
sei();
}