-
Notifications
You must be signed in to change notification settings - Fork 1
/
Final_main_code
417 lines (364 loc) · 12.8 KB
/
Final_main_code
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// #include "address_map_arm.h"
#include "stdio.h"
#include "time.h"
#include "string.h"
#define KEY_BASE 0xFF200050
#define VIDEO_IN_BASE 0xFF203060
#define FPGA_ONCHIP_BASE 0xC8000000
#define TEXT 0xC9000000
#define SW_BASE 0xFF200040
#define ROW 240
#define COL 320
/* This program demonstrates the use of the D5M camera with the DE1-SoC Board
* It performs the following:
* 1. Capture one frame of video when any key is pressed.
* 2. Display the captured frame when any key is pressed.
*/
/*
* Global Variables
*/
///////////////////////////////////////////////////////////////////////////////
///////////////////////NIOS flags and buffer///////////////////////////////////
volatile int * KEY_ptr = (int *) KEY_BASE;
volatile int * Video_In_DMA_ptr = (int *) VIDEO_IN_BASE;
volatile short * Video_Mem_ptr = (short *) FPGA_ONCHIP_BASE;
volatile short * TXT = (short *) TEXT;
volatile int * SW_ptr = (int *) SW_BASE;
///////////////////////////////////////////////////////////////////////////////
///////////////////////Conley's global/////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//////////////////////Kai's global/////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
int counter = 0;
///////////////////////////////////////////////////////////////////////////////
//////////////////////Gongtao's global/////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
char *text_ptr;
///////////////////////////////////////////////////////////////////////////////
///////////////////////Conley's functions//////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//swap two pixels
void Mswap (int a, int b, int c, int d ){
short temp = *(Video_Mem_ptr + (a << 9) + b);
*(Video_Mem_ptr + (a << 9) + b) = *(Video_Mem_ptr + (c << 9) + d);
*(Video_Mem_ptr + (c << 9) + d) = temp;
}
void rotate180 (){
*(Video_In_DMA_ptr + 3) = 0x0; // Disable the video to capture one frame
while(*KEY_ptr != 0 );
//while(*KEY_ptr == 0 ); //wait for KEY press to end this cycle
//while(*KEY_ptr != 0 );
while(*KEY_ptr == 0 ); //wait for KEY press to end this cycle
printf("Hello");
int x,y;
for (y = 0; y < 240/2; y++) {
for (x = 0; x < 320; x++) {
Mswap(y,x,ROW-1-y,COL-1-x);
}
}
}
void mirror (){
*(Video_In_DMA_ptr + 3) = 0x0; // Disable the video to capture one frame
int x,y;
while(*KEY_ptr != 0 );
//while(*KEY_ptr == 0 ); //wait for KEY press to end this cycle
//while(*KEY_ptr != 0 );
while(*KEY_ptr == 0 ); //wait for KEY press to end this cycle
for (y = 0; y < 240; y++) {
for (x = 0; x < 320/2;x++) {
Mswap(y,x,y,COL-1-x);
}
}
}
void diff(){
short temp =0;
short temp2 =0;
short arr[ROW][COL];
*(Video_In_DMA_ptr + 3) = 0x0; // Disable the video to capture one frame, picture 1
int x,y;
for (y = 0; y < 240; y++) {
for (x = 0; x < 320; x++) {
short ww = *(Video_Mem_ptr + ((y) << 9) + (x));
arr[y][x] = ww;
}
}
while(*KEY_ptr != 0 );
while(*KEY_ptr == 0 ); //wait for KEY press to end this cycle
*(Video_In_DMA_ptr + 3) = 0x4; // Enable the video for picture 2
while(*KEY_ptr != 0 );
while(*KEY_ptr == 0 ); //wait for KEY press to end this cycle
*(Video_In_DMA_ptr + 3) = 0x0; // Disable the video to capture one frame, picture 2
//compare loop
for (y = 0; y < 240; y++) {
for (x = 0; x < 320; x++) {
temp = *(Video_Mem_ptr + ((y) << 9) + (x));
if(arr[y][x]-temp>50){
temp = 63488; //all red
}
*(Video_Mem_ptr + ((y) << 9) + (x)) =temp;
}
}
while(*KEY_ptr == 0 );
}
///////////////////////////////////////////////////////////////////////////////
//////////////////////Kai's functions//////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
char* combine(char* a, char* b){ // combine two String
char src[100], dest[100];
strcpy(src, a);
strcpy(dest,b);
strcat(dest,src);
return dest;
}
void EdgeDetect(){
int x,y;
*(Video_In_DMA_ptr + 3) = 0x0; // Disable the video to capture one frame
for (y = 0; y < 240; y++) {
for (x = 0; x < 320; x++) {
short temp2 = *(Video_Mem_ptr + (y << 9) + x);
short diff1 = temp2 - *(Video_Mem_ptr + ((y+1) << 9) + x);
//short diff2 = temp2 - *(Video_Mem_ptr + ((y-1) << 9) + x);
short diff3 = temp2 - *(Video_Mem_ptr + (y << 9) + (x+1));
//short diff4 = temp2 - *(Video_Mem_ptr + (y << 9) + (x-1));
if(diff1 > 4000 ){
temp2 = 63488;
}
else if(diff3 > 2800 ){
temp2 = 63488;
}
*(Video_Mem_ptr + (y << 9) + x) = temp2;
}
}
}
void Counter()
{
char *C;
text_ptr = "Counter: ";
int offset= (7<<1)+ 1;
*(Video_In_DMA_ptr + 3) = 0x0; // Disable the video to capture one frame
sprintf(C, "%d", counter);
text_ptr = combine((char*)C , (char*)text_ptr);
printf("%s, %d", text_ptr, counter);
while ( *(text_ptr) )
{
*(TXT + offset) = *(text_ptr); // write to the character buffer
++text_ptr;
++offset;
}
}
int invert(){
int x,y;
short arr[ROW][COL];
*(Video_In_DMA_ptr + 3) = 0x0; // Disable the video to capture one frame
for (y = 0; y < 240; y++) {
for (x = 0; x < 320; x++) {
short temp2 = *(Video_Mem_ptr + (y << 9) + x);
arr[y][x] = temp2;
if(temp2 > 8210){
temp2 = 0;
}
else if(temp2 < 8210){
temp2 = 0xFFFF;
}
*(Video_Mem_ptr + (y << 9) + x) = temp2;
}
}
while(*KEY_ptr != 0 );
while(*KEY_ptr == 0 ); //wait for KEY press to end this cycle
for (y = 0; y < 240; y++) {
for (x = 0; x < 320; x++) {
short ttt = arr[y][x];
if(ttt < 8210){
ttt = 0;
}
else if(ttt > 8210){
ttt = 0xFFFF;
}
*(Video_Mem_ptr + (y << 9) + x) = ttt;
}
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////
//////////////////////Gongtao's functions//////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void Welcome(){
char* welcome = "pick a switch an press KEY";
char* t = "SW1 -> timestamp";
char* c = "SW2 -> image count";
char* ro = "SW3 -> rotate";
char* mir = "SW4 -> mirror";
char* inv = "SW5 -> invert";
char* ed = "SW6 -> edge detection";
char* di = "SW7 -> difference detection";
int offset;
offset = ( 1 << 7) + 1;
while ( *(welcome) )
{
*(TXT + offset) = *(welcome); // write to the character buffer
//printf("%s",*(TXT + offset));
++welcome;
++offset;
}
offset = ( 2 << 7) + 1;
while ( *(t) )
{
*(TXT + offset) = *(t); // write to the character buffer
//printf("%s",*(TXT + offset));
++t;
++offset;
}
offset = ( 3 << 7) + 1;
while ( *(c) )
{
*(TXT + offset) = *(c); // write to the character buffer
//printf("%s",*(TXT + offset));
++c;
++offset;
}
offset = ( 4 << 7) + 1;
while ( *(ro) )
{
*(TXT + offset) = *(ro); // write to the character buffer
//printf("%s",*(TXT + offset));
++ro;
++offset;
}
offset = ( 5 << 7) + 1;
while ( *(mir) )
{
*(TXT + offset) = *(mir); // write to the character buffer
//printf("%s",*(TXT + offset));
++mir;
++offset;
}
offset = ( 6 << 7) + 1;
while ( *(inv) )
{
*(TXT + offset) = *(inv); // write to the character buffer
//printf("%s",*(TXT + offset));
++inv;
++offset;
}
offset = ( 7 << 7) + 1;
while ( *(ed) )
{
*(TXT + offset) = *(ed); // write to the character buffer
//printf("%s",*(TXT + offset));
++ed;
++offset;
}
offset = ( 8 << 7) + 1;
while ( *(di) )
{
*(TXT + offset) = *(di); // write to the character buffer
//printf("%s",*(TXT + offset));
++di;
++offset;
}
}
int timeStamp(){ // write current time to the screen
int offset = (25 << 7) + 1; // place of the text
// time stampt use
struct tm *mt;
time_t mtt;
char * text_ptr;
setenv("TZ", "EST5EDT4", 1);
tzset();
mtt = time(NULL);
mt = localtime(&mtt);
text_ptr = ctime(&mtt);
//strftime(text_ptr, 20, "%Z %H%M", mt);
printf("%s\n", text_ptr);
*(Video_In_DMA_ptr + 3) = 0x0; // Disable the video to capture one frame
while ( *(text_ptr) )
{
*(TXT + offset) = *(text_ptr); // write to the character buffer
printf("%s",*(TXT + offset));
++text_ptr;
++offset;
}
return 0;
}
/*
* main code below, image() and main()
*/
int image(){
//TODO,fill in the blanks
*(Video_In_DMA_ptr + 3) = 0x4; // Enable the video every time
while(*KEY_ptr != 0 ); // catch long KEY holding, wait for release
while (1)
{
if (*KEY_ptr != 0) // check if any KEY was pressed
{
if(*SW_ptr == 0){
Welcome();
while(*KEY_ptr != 0 );
while(*KEY_ptr == 0 ); //wait for KEY press to end this cycle
return 0;
}
counter++;
if(*SW_ptr == 2 ){ // SW1, time stamp
timeStamp(); //writting time to the screen
while(*KEY_ptr != 0 );
while(*KEY_ptr == 0 ); //wait for KEY press to end this cycle
return 0;
}
else if(*SW_ptr == 4){ // SW2, image count
//****** code here
Counter();
while(*KEY_ptr != 0 );
while(*KEY_ptr == 0 ); //wait for KEY press to end this cycle
return 0;
}
else if(*SW_ptr == 8){ // SW3, rotate 180
rotate180();
while(*KEY_ptr != 0 );
while(*KEY_ptr == 0 ); //wait for KEY press to end this cycle
return 0;
}
else if(*SW_ptr == 16){ // SW4, mirror
mirror();
while(*KEY_ptr != 0 );
while(*KEY_ptr == 0 ); //wait for KEY press to end this cycle
return 0;
}
else if(*SW_ptr == 32){ // SW5, invert
//****** code here
invert();
while(*KEY_ptr != 0 );
while(*KEY_ptr == 0 ); //wait for KEY press to end this cycle
return 0;
}
else if(*SW_ptr == 64){ // SW6, edge detect
EdgeDetect();
while(*KEY_ptr != 0 );
while(*KEY_ptr == 0 ); //wait for KEY press to end this cycle
return 0;
}
else if(*SW_ptr == 128){ // SW7, detect difference
diff();
while(*KEY_ptr != 0 );
while(*KEY_ptr == 0 ); //wait for KEY press to end this cycle
return 0;
}
}
}
return 0;
}
int main(void){ //Da main code
//Welcome();
/*
* HERE comes da main code
*/
int y;
text_ptr = " ";
while(1){
image();
for(y = 0 ; y<(80*60); y++){
*(TXT + y) = *(text_ptr); // write to the character buffer
}
}
return 0; //shouldn't reach here
}