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