![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Test Code for OV7670 Camera module. The images are sent over ethernet link.
Dependencies: EthernetInterface mbed-rtos mbed ov7670
You can find more information in this page: https://mbed.org/users/edodm85/notebook/ov7670-camera-and-ethernet-stream/
main.cpp@1:0e4d23df43ea, 2013-07-20 (annotated)
- Committer:
- edodm85
- Date:
- Sat Jul 20 16:36:31 2013 +0000
- Revision:
- 1:0e4d23df43ea
- Parent:
- 0:2208b72b1ba1
- Child:
- 2:d6b38a4512eb
updated "for" condition
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
edodm85 | 0:2208b72b1ba1 | 1 | /* |
edodm85 | 0:2208b72b1ba1 | 2 | * Author: Edoardo De Marchi |
edodm85 | 1:0e4d23df43ea | 3 | * Date: 20/07/13 |
edodm85 | 0:2208b72b1ba1 | 4 | * Notes: OV7670 camera with Ethernet Connection |
edodm85 | 0:2208b72b1ba1 | 5 | */ |
edodm85 | 0:2208b72b1ba1 | 6 | |
edodm85 | 0:2208b72b1ba1 | 7 | #include "main.h" |
edodm85 | 0:2208b72b1ba1 | 8 | |
edodm85 | 0:2208b72b1ba1 | 9 | #define VGA 307200 //640*480 |
edodm85 | 0:2208b72b1ba1 | 10 | #define QVGA 76800 //320*240 |
edodm85 | 0:2208b72b1ba1 | 11 | #define QQVGA 19200 //160*120 |
edodm85 | 0:2208b72b1ba1 | 12 | |
edodm85 | 0:2208b72b1ba1 | 13 | static char format = ' '; |
edodm85 | 0:2208b72b1ba1 | 14 | static int resolution = 0; |
edodm85 | 0:2208b72b1ba1 | 15 | |
edodm85 | 0:2208b72b1ba1 | 16 | |
edodm85 | 0:2208b72b1ba1 | 17 | int Init() |
edodm85 | 0:2208b72b1ba1 | 18 | { |
edodm85 | 0:2208b72b1ba1 | 19 | led1 = 0; |
edodm85 | 0:2208b72b1ba1 | 20 | led2 = 0; // mbed status |
edodm85 | 0:2208b72b1ba1 | 21 | led3 = 0; // Ethernet link enable/disable |
edodm85 | 1:0e4d23df43ea | 22 | led4 = 0; // Snap status |
edodm85 | 1:0e4d23df43ea | 23 | |
edodm85 | 0:2208b72b1ba1 | 24 | //ETHERNET |
edodm85 | 0:2208b72b1ba1 | 25 | eth.init(ip, mask, gateway); |
edodm85 | 0:2208b72b1ba1 | 26 | eth.connect(); |
edodm85 | 0:2208b72b1ba1 | 27 | server.bind(ECHO_SERVER_PORT); |
edodm85 | 0:2208b72b1ba1 | 28 | server.listen(1); |
edodm85 | 0:2208b72b1ba1 | 29 | printf("IP Address is %s\r\n", eth.getIPAddress()); |
edodm85 | 0:2208b72b1ba1 | 30 | |
edodm85 | 0:2208b72b1ba1 | 31 | //THREAD |
edodm85 | 0:2208b72b1ba1 | 32 | osThreadCreate(osThread(net_thread), NULL); |
edodm85 | 0:2208b72b1ba1 | 33 | |
edodm85 | 0:2208b72b1ba1 | 34 | return 0; |
edodm85 | 0:2208b72b1ba1 | 35 | } |
edodm85 | 0:2208b72b1ba1 | 36 | |
edodm85 | 0:2208b72b1ba1 | 37 | |
edodm85 | 0:2208b72b1ba1 | 38 | |
edodm85 | 0:2208b72b1ba1 | 39 | int main (void) |
edodm85 | 0:2208b72b1ba1 | 40 | { |
edodm85 | 0:2208b72b1ba1 | 41 | Init(); |
edodm85 | 0:2208b72b1ba1 | 42 | t.start(); |
edodm85 | 0:2208b72b1ba1 | 43 | |
edodm85 | 0:2208b72b1ba1 | 44 | while (true) |
edodm85 | 0:2208b72b1ba1 | 45 | { |
edodm85 | 0:2208b72b1ba1 | 46 | //mbed is alive? |
edodm85 | 0:2208b72b1ba1 | 47 | led2 = !led2; |
edodm85 | 0:2208b72b1ba1 | 48 | osDelay(500); |
edodm85 | 0:2208b72b1ba1 | 49 | } |
edodm85 | 0:2208b72b1ba1 | 50 | } |
edodm85 | 0:2208b72b1ba1 | 51 | |
edodm85 | 0:2208b72b1ba1 | 52 | |
edodm85 | 0:2208b72b1ba1 | 53 | void net_thread(void const *argument) |
edodm85 | 0:2208b72b1ba1 | 54 | { |
edodm85 | 0:2208b72b1ba1 | 55 | while (true) |
edodm85 | 0:2208b72b1ba1 | 56 | { |
edodm85 | 0:2208b72b1ba1 | 57 | led3 = 1; |
edodm85 | 1:0e4d23df43ea | 58 | //printf("\r\nWait for new connection...\r\n"); |
edodm85 | 0:2208b72b1ba1 | 59 | server.accept(client); |
edodm85 | 1:0e4d23df43ea | 60 | printf("Connection from: %s\r\n", client.get_address()); |
edodm85 | 0:2208b72b1ba1 | 61 | while (true) |
edodm85 | 0:2208b72b1ba1 | 62 | { |
edodm85 | 0:2208b72b1ba1 | 63 | led3 = 0; |
edodm85 | 0:2208b72b1ba1 | 64 | int n = client.receive(bufferRX, sizeof(bufferRX)); |
edodm85 | 0:2208b72b1ba1 | 65 | if (n <= 0) break; |
edodm85 | 0:2208b72b1ba1 | 66 | |
edodm85 | 0:2208b72b1ba1 | 67 | bufferRX[n]=0; // make terminater |
edodm85 | 0:2208b72b1ba1 | 68 | parse_cmd(); |
edodm85 | 0:2208b72b1ba1 | 69 | } |
edodm85 | 0:2208b72b1ba1 | 70 | client.close(); |
edodm85 | 0:2208b72b1ba1 | 71 | //printf("Connection close.\r\n"); |
edodm85 | 0:2208b72b1ba1 | 72 | } |
edodm85 | 0:2208b72b1ba1 | 73 | } |
edodm85 | 0:2208b72b1ba1 | 74 | |
edodm85 | 0:2208b72b1ba1 | 75 | |
edodm85 | 0:2208b72b1ba1 | 76 | void Send() |
edodm85 | 0:2208b72b1ba1 | 77 | { |
edodm85 | 0:2208b72b1ba1 | 78 | client.send_all(bufferTX, sizeof(bufferTX)); |
edodm85 | 0:2208b72b1ba1 | 79 | memset(bufferTX, 0, sizeof(bufferTX)); |
edodm85 | 0:2208b72b1ba1 | 80 | } |
edodm85 | 0:2208b72b1ba1 | 81 | |
edodm85 | 0:2208b72b1ba1 | 82 | |
edodm85 | 0:2208b72b1ba1 | 83 | void parse_cmd(){ |
edodm85 | 0:2208b72b1ba1 | 84 | new_send = false; |
edodm85 | 0:2208b72b1ba1 | 85 | |
edodm85 | 0:2208b72b1ba1 | 86 | if(strcmp("snap", bufferRX) == 0) |
edodm85 | 0:2208b72b1ba1 | 87 | { |
edodm85 | 0:2208b72b1ba1 | 88 | CameraSnap(); |
edodm85 | 0:2208b72b1ba1 | 89 | memset(bufferRX, 0, sizeof(bufferRX)); |
edodm85 | 0:2208b72b1ba1 | 90 | }else |
edodm85 | 0:2208b72b1ba1 | 91 | if(strcmp("init_bw_VGA", bufferRX) == 0) // Set up for 640*480 pixels RAW |
edodm85 | 0:2208b72b1ba1 | 92 | { |
edodm85 | 0:2208b72b1ba1 | 93 | format = 'b'; |
edodm85 | 0:2208b72b1ba1 | 94 | resolution = VGA; |
edodm85 | 0:2208b72b1ba1 | 95 | if(camera.Init('b', VGA) != 1) |
edodm85 | 0:2208b72b1ba1 | 96 | { |
edodm85 | 0:2208b72b1ba1 | 97 | printf("Init Fail\r\n"); |
edodm85 | 0:2208b72b1ba1 | 98 | } |
edodm85 | 0:2208b72b1ba1 | 99 | memset(bufferRX, 0, sizeof(bufferRX)); |
edodm85 | 0:2208b72b1ba1 | 100 | }else |
edodm85 | 0:2208b72b1ba1 | 101 | if(strcmp("init_yuv_QVGA", bufferRX) == 0) // Set up for 320*240 pixels YUV422 |
edodm85 | 0:2208b72b1ba1 | 102 | { |
edodm85 | 0:2208b72b1ba1 | 103 | format = 'y'; |
edodm85 | 0:2208b72b1ba1 | 104 | resolution = QVGA; |
edodm85 | 0:2208b72b1ba1 | 105 | if(camera.Init('b', QVGA) != 1) |
edodm85 | 0:2208b72b1ba1 | 106 | { |
edodm85 | 0:2208b72b1ba1 | 107 | printf("Init Fail\r\n"); |
edodm85 | 0:2208b72b1ba1 | 108 | } |
edodm85 | 0:2208b72b1ba1 | 109 | memset(bufferRX, 0, sizeof(bufferRX)); |
edodm85 | 0:2208b72b1ba1 | 110 | }else |
edodm85 | 0:2208b72b1ba1 | 111 | if(strcmp("init_rgb_QVGA", bufferRX) == 0) // Set up for 320*240 pixels RGB565 |
edodm85 | 0:2208b72b1ba1 | 112 | { |
edodm85 | 0:2208b72b1ba1 | 113 | format = 'r'; |
edodm85 | 0:2208b72b1ba1 | 114 | resolution = QVGA; |
edodm85 | 0:2208b72b1ba1 | 115 | if(camera.Init('r', QVGA) != 1) |
edodm85 | 0:2208b72b1ba1 | 116 | { |
edodm85 | 0:2208b72b1ba1 | 117 | printf("Init Fail\r\n"); |
edodm85 | 0:2208b72b1ba1 | 118 | } |
edodm85 | 0:2208b72b1ba1 | 119 | memset(bufferRX, 0, sizeof(bufferRX)); |
edodm85 | 0:2208b72b1ba1 | 120 | }else |
edodm85 | 0:2208b72b1ba1 | 121 | if(strcmp("init_bw_QVGA", bufferRX) == 0) // Set up for 320*240 pixels YUV (Only Y) |
edodm85 | 0:2208b72b1ba1 | 122 | { |
edodm85 | 0:2208b72b1ba1 | 123 | format = 'b'; |
edodm85 | 0:2208b72b1ba1 | 124 | resolution = QVGA; |
edodm85 | 0:2208b72b1ba1 | 125 | if(camera.Init('b', QVGA) != 1) |
edodm85 | 0:2208b72b1ba1 | 126 | { |
edodm85 | 0:2208b72b1ba1 | 127 | printf("Init Fail\r\n"); |
edodm85 | 0:2208b72b1ba1 | 128 | } |
edodm85 | 0:2208b72b1ba1 | 129 | memset(bufferRX, 0, sizeof(bufferRX)); |
edodm85 | 0:2208b72b1ba1 | 130 | }else |
edodm85 | 0:2208b72b1ba1 | 131 | if(strcmp("init_yuv_QQVGA", bufferRX) == 0) // Set up for 160*120 pixels YUV422 |
edodm85 | 0:2208b72b1ba1 | 132 | { |
edodm85 | 0:2208b72b1ba1 | 133 | format = 'y'; |
edodm85 | 0:2208b72b1ba1 | 134 | resolution = QQVGA; |
edodm85 | 0:2208b72b1ba1 | 135 | if(camera.Init('b', QQVGA) != 1) |
edodm85 | 0:2208b72b1ba1 | 136 | { |
edodm85 | 0:2208b72b1ba1 | 137 | printf("Init Fail\r\n"); |
edodm85 | 0:2208b72b1ba1 | 138 | } |
edodm85 | 0:2208b72b1ba1 | 139 | memset(bufferRX, 0, sizeof(bufferRX)); |
edodm85 | 0:2208b72b1ba1 | 140 | }else |
edodm85 | 0:2208b72b1ba1 | 141 | if(strcmp("init_rgb_QQVGA", bufferRX) == 0) // Set up for 160*120 pixels RGB565 |
edodm85 | 0:2208b72b1ba1 | 142 | { |
edodm85 | 0:2208b72b1ba1 | 143 | format = 'r'; |
edodm85 | 0:2208b72b1ba1 | 144 | resolution = QQVGA; |
edodm85 | 0:2208b72b1ba1 | 145 | if(camera.Init('r', QQVGA) != 1) |
edodm85 | 0:2208b72b1ba1 | 146 | { |
edodm85 | 0:2208b72b1ba1 | 147 | printf("Init Fail\r\n"); |
edodm85 | 0:2208b72b1ba1 | 148 | } |
edodm85 | 0:2208b72b1ba1 | 149 | memset(bufferRX, 0, sizeof(bufferRX)); |
edodm85 | 0:2208b72b1ba1 | 150 | }else |
edodm85 | 0:2208b72b1ba1 | 151 | if(strcmp("init_bw_QQVGA", bufferRX) == 0) // Set up for 160*120 pixels YUV (Only Y) |
edodm85 | 0:2208b72b1ba1 | 152 | { |
edodm85 | 0:2208b72b1ba1 | 153 | format = 'b'; |
edodm85 | 0:2208b72b1ba1 | 154 | resolution = QQVGA; |
edodm85 | 0:2208b72b1ba1 | 155 | if(camera.Init('b', QQVGA) != 1) |
edodm85 | 0:2208b72b1ba1 | 156 | { |
edodm85 | 0:2208b72b1ba1 | 157 | printf("Init Fail\r\n"); |
edodm85 | 0:2208b72b1ba1 | 158 | } |
edodm85 | 0:2208b72b1ba1 | 159 | memset(bufferRX, 0, sizeof(bufferRX)); |
edodm85 | 0:2208b72b1ba1 | 160 | }else |
edodm85 | 0:2208b72b1ba1 | 161 | if(strcmp("reset", bufferRX) == 0) |
edodm85 | 0:2208b72b1ba1 | 162 | { |
edodm85 | 0:2208b72b1ba1 | 163 | mbed_reset(); |
edodm85 | 0:2208b72b1ba1 | 164 | }else |
edodm85 | 0:2208b72b1ba1 | 165 | if(strcmp("time", bufferRX) == 0) |
edodm85 | 0:2208b72b1ba1 | 166 | { |
edodm85 | 0:2208b72b1ba1 | 167 | sprintf(bufferTX, "Tot time acq + send (mbed): %dms\r\n", t2-t1); |
edodm85 | 0:2208b72b1ba1 | 168 | client.send_all(bufferTX, sizeof(bufferTX)); |
edodm85 | 0:2208b72b1ba1 | 169 | memset(bufferTX, 0, sizeof(bufferTX)); |
edodm85 | 1:0e4d23df43ea | 170 | }else |
edodm85 | 1:0e4d23df43ea | 171 | if(strcmp("reg", bufferRX) == 0) |
edodm85 | 1:0e4d23df43ea | 172 | { |
edodm85 | 1:0e4d23df43ea | 173 | int n = 0; |
edodm85 | 1:0e4d23df43ea | 174 | int g = 0; |
edodm85 | 1:0e4d23df43ea | 175 | int c = 650; |
edodm85 | 1:0e4d23df43ea | 176 | for (int i=0;i<201;i++) |
edodm85 | 1:0e4d23df43ea | 177 | { |
edodm85 | 1:0e4d23df43ea | 178 | n = snprintf(bufferTX+g, c, "%02X", camera.ReadReg(i)); |
edodm85 | 1:0e4d23df43ea | 179 | g = g + n; |
edodm85 | 1:0e4d23df43ea | 180 | c = c - n - 1; |
edodm85 | 1:0e4d23df43ea | 181 | } |
edodm85 | 1:0e4d23df43ea | 182 | client.send_all(bufferTX, sizeof(bufferTX)); |
edodm85 | 1:0e4d23df43ea | 183 | memset(bufferTX, 0, sizeof(bufferTX)); |
edodm85 | 0:2208b72b1ba1 | 184 | } |
edodm85 | 0:2208b72b1ba1 | 185 | memset(bufferRX, 0, sizeof(bufferRX)); |
edodm85 | 0:2208b72b1ba1 | 186 | } |
edodm85 | 0:2208b72b1ba1 | 187 | |
edodm85 | 0:2208b72b1ba1 | 188 | |
edodm85 | 0:2208b72b1ba1 | 189 | void CameraSnap(){ |
edodm85 | 0:2208b72b1ba1 | 190 | led4 = 1; |
edodm85 | 0:2208b72b1ba1 | 191 | t1 = t.read_ms(); |
edodm85 | 0:2208b72b1ba1 | 192 | |
edodm85 | 0:2208b72b1ba1 | 193 | // Kick things off by capturing an image |
edodm85 | 0:2208b72b1ba1 | 194 | camera.CaptureNext(); |
edodm85 | 0:2208b72b1ba1 | 195 | while(camera.CaptureDone() == false); |
edodm85 | 0:2208b72b1ba1 | 196 | // Start reading in the image data from the camera hardware buffer |
edodm85 | 0:2208b72b1ba1 | 197 | camera.ReadStart(); |
edodm85 | 0:2208b72b1ba1 | 198 | |
edodm85 | 0:2208b72b1ba1 | 199 | |
edodm85 | 0:2208b72b1ba1 | 200 | int p = 0; |
edodm85 | 0:2208b72b1ba1 | 201 | if(format == 'b') // B&W |
edodm85 | 0:2208b72b1ba1 | 202 | { |
edodm85 | 1:0e4d23df43ea | 203 | for(int x = 0; x<resolution/sizeof(bufferTX); x++) |
edodm85 | 0:2208b72b1ba1 | 204 | { |
edodm85 | 1:0e4d23df43ea | 205 | for(int q = 0; q<sizeof(bufferTX); q++) |
edodm85 | 0:2208b72b1ba1 | 206 | { |
edodm85 | 0:2208b72b1ba1 | 207 | // Read in the first half of the image |
edodm85 | 0:2208b72b1ba1 | 208 | if(resolution != VGA) |
edodm85 | 0:2208b72b1ba1 | 209 | camera.ReadOnebyte(); |
edodm85 | 0:2208b72b1ba1 | 210 | // Read in the Second half of the image |
edodm85 | 0:2208b72b1ba1 | 211 | bufferTX[q] = camera.ReadOnebyte(); // Y only |
edodm85 | 0:2208b72b1ba1 | 212 | } |
edodm85 | 0:2208b72b1ba1 | 213 | Send(); |
edodm85 | 0:2208b72b1ba1 | 214 | } |
edodm85 | 0:2208b72b1ba1 | 215 | } |
edodm85 | 0:2208b72b1ba1 | 216 | |
edodm85 | 0:2208b72b1ba1 | 217 | if(format == 'y' || format == 'r') // Color |
edodm85 | 0:2208b72b1ba1 | 218 | { |
edodm85 | 1:0e4d23df43ea | 219 | for(int x = 0; x<(resolution/sizeof(bufferTX))*2; x++) |
edodm85 | 0:2208b72b1ba1 | 220 | { |
edodm85 | 1:0e4d23df43ea | 221 | for(int q = 0; q<sizeof(bufferTX)/2; q++) |
edodm85 | 0:2208b72b1ba1 | 222 | { |
edodm85 | 0:2208b72b1ba1 | 223 | p = q*2; |
edodm85 | 0:2208b72b1ba1 | 224 | // Read in the first half of the image |
edodm85 | 0:2208b72b1ba1 | 225 | bufferTX[p] = camera.ReadOnebyte(); |
edodm85 | 0:2208b72b1ba1 | 226 | // Read in the Second half of the image |
edodm85 | 0:2208b72b1ba1 | 227 | bufferTX[p+1] = camera.ReadOnebyte(); |
edodm85 | 0:2208b72b1ba1 | 228 | } |
edodm85 | 0:2208b72b1ba1 | 229 | Send(); |
edodm85 | 0:2208b72b1ba1 | 230 | } |
edodm85 | 0:2208b72b1ba1 | 231 | } |
edodm85 | 0:2208b72b1ba1 | 232 | camera.ReadStop(); |
edodm85 | 0:2208b72b1ba1 | 233 | t2 = t.read_ms(); |
edodm85 | 0:2208b72b1ba1 | 234 | |
edodm85 | 0:2208b72b1ba1 | 235 | // Immediately request the next image to be captured (takes around 45ms) |
edodm85 | 0:2208b72b1ba1 | 236 | camera.CaptureNext(); |
edodm85 | 0:2208b72b1ba1 | 237 | // Now wait for the image to finish being captured |
edodm85 | 0:2208b72b1ba1 | 238 | while (camera.CaptureDone() == false); |
edodm85 | 0:2208b72b1ba1 | 239 | |
edodm85 | 1:0e4d23df43ea | 240 | //printf("Snap_done\r\n"); |
edodm85 | 0:2208b72b1ba1 | 241 | led4 = 0; |
edodm85 | 0:2208b72b1ba1 | 242 | } |