-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCamera.cpp
executable file
·136 lines (112 loc) · 2.87 KB
/
Camera.cpp
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
#include <Adafruit_ST7735.h> // Hardware-specific library
#include "Camera.h"
extern Adafruit_ST7735 lcd;
// Class Constructor
Camera::Camera(byte _vSync, byte _hRef, byte _pClk,
byte _d0, byte _d1, byte _d2, byte _d3,
byte _d4, byte _d5, byte _d6, byte _d7,
byte _reset) {
// Saving incoming
vSync = _vSync;
hRef = _hRef;
pClk = _pClk;
d0 = _d0;
d1 = _d1;
d2 = _d2;
d3 = _d3;
d4 = _d4;
d5 = _d5;
d6 = _d6;
d7 = _d7;
reset = _reset;
// Setup all GPIO pins as inputs
pinMode(vSync, INPUT);
pinMode(hRef, INPUT);
pinMode(pClk, INPUT);
pinMode(d0, INPUT);
pinMode(d1, INPUT);
pinMode(d2, INPUT);
pinMode(d3, INPUT);
pinMode(d4, INPUT);
pinMode(d5, INPUT);
pinMode(d6, INPUT);
pinMode(d7, INPUT);
// Initialize reset signal
pinMode(reset, OUTPUT);
// Put OV7670 into reset
digitalWrite(reset, LOW);
}
// Control the camera reset signal.
// Low is reset; High is run
void Camera::setCameraReset(bool b) {
digitalWrite(reset, b);
}
// Read a byte of the pixel data
inline byte Camera::readPixel(void) {
byte b = 0;
if (digitalReadFast(d7)) {
b |= 0x80;
}
if (digitalReadFast(d6)) {
b |= 0x40;
}
if (digitalReadFast(d5)) {
b |= 0x20;
}
if (digitalReadFast(d4)) {
b |= 0x10;
}
if (digitalReadFast(d3)) {
b |= 0x08;
}
if (digitalReadFast(d2)) {
b |= 0x04;
}
if (digitalReadFast(d1)) {
b |= 0x02;
}
if (digitalReadFast(d0)) {
b |= 0x01;
}
return b;
}
inline bool Camera::checkVSync(void) {
return digitalReadFast(vSync);
}
inline bool Camera::checkHRef(void) {
return digitalReadFast(hRef);
}
inline bool Camera::checkPClk(void) {
return digitalReadFast(pClk);
}
// Acquire and display RGB565 image from the camera
void Camera::acquireAndDisplayRGBImage(void) {
byte hByte;
int i = 0;
while (checkVSync()); // Wait for the old frame to end
while (! checkVSync()); // Wait for a new frame to start
while (checkVSync()) {
while (checkVSync() && ! checkHRef()); // Wait for a line to start
if (! checkVSync()) {
break; // Line didn't start, but frame ended
}
while (checkHRef()) { // Wait for a line to end
while (! checkPClk()); // Wait for clock to go high
// Read the high byte of the RGB565 pixel's value
hByte = readPixel();
while (checkPClk()); // Wait for clock to go back low
while (! checkPClk()); // Wait for clock to go high
// Read the low byte of the RGB565 pixel's value,
// combine it with the high byte and store it
image[i++] = (hByte << 8) | readPixel();
while (checkPClk()); // Wait for clock to go back low
}
}
Serial.println(i);
// Now display the QQVGA image
for (int y = 0; y < 120; y++) {
for (int x = 0; x < 160; x++) {
lcd.drawPixel(x, y, image[y * 160 + x]);
}
}
}