First release

Dependencies:   EthernetInterface HTTPServer TextLCD mbed-rpc mbed-rtos mbed

Sample code of section 5 in Oct 2014 issue of the Interface Magazine, published by CQ publishing in Japan. CQ出版社インターフェース誌 2014年10月号5章に掲載のサンプルコードです.

LPC1768にトラ技OV7670モジュールとサーボを接続したうえで,リモート操作可能なネットワーク・カメラにしています.このコードのうちカメラ制御部には,Sadaei Osakabe氏のコードを流用させていただいています.

また,次のHTMLファイルをダウンロードして,LPC1768のフラッシュメモリに置いてください.ネットワーク経由で,このHTMLにアクセスをします(ブラウザで開く). /media/uploads/smorioka/netcam.htm

Committer:
smorioka
Date:
Tue Aug 26 16:49:26 2014 +0000
Revision:
0:993f719c9352
Sample code of section 5 in Oct 2014 issue of the Interface Magazine, published by CQ publishing in Japan.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
smorioka 0:993f719c9352 1 /*
smorioka 0:993f719c9352 2 * Copyright (c) 2014, Sumio Morioka
smorioka 0:993f719c9352 3 * All rights reserved.
smorioka 0:993f719c9352 4 *
smorioka 0:993f719c9352 5 * This source code was originally written by Dr.Sumio Morioka for use in the Oct 2014 issue of
smorioka 0:993f719c9352 6 * "the Interface magazine", published by CQ publishing Co.Ltd in Japan (http://www.cqpub.co.jp).
smorioka 0:993f719c9352 7 * The author has no responsibility on any results caused by using this code.
smorioka 0:993f719c9352 8 *
smorioka 0:993f719c9352 9 * - Distribution date of this code: Aug 26, 2014
smorioka 0:993f719c9352 10 * - Author: Dr.Sumio Morioka (http://www002.upp.so-net.ne.jp/morioka)
smorioka 0:993f719c9352 11 *
smorioka 0:993f719c9352 12 *
smorioka 0:993f719c9352 13 * IMPORTANT NOTICE:
smorioka 0:993f719c9352 14 * Redistribution and use in source and binary forms, with or without
smorioka 0:993f719c9352 15 * modification, are permitted provided that the following conditions are met:
smorioka 0:993f719c9352 16 * * Redistributions of source code must retain the above copyright
smorioka 0:993f719c9352 17 * notice, this list of conditions and the following disclaimer.
smorioka 0:993f719c9352 18 * * Redistributions in binary form must reproduce the above copyright
smorioka 0:993f719c9352 19 * notice, this list of conditions and the following disclaimer in the
smorioka 0:993f719c9352 20 * documentation and/or other materials provided with the distribution.
smorioka 0:993f719c9352 21 * * Neither the name of the copyright holder nor the
smorioka 0:993f719c9352 22 * names of its contributors may be used to endorse or promote products
smorioka 0:993f719c9352 23 * derived from this software without specific prior written permission.
smorioka 0:993f719c9352 24 *
smorioka 0:993f719c9352 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
smorioka 0:993f719c9352 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
smorioka 0:993f719c9352 27 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
smorioka 0:993f719c9352 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
smorioka 0:993f719c9352 29 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
smorioka 0:993f719c9352 30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
smorioka 0:993f719c9352 31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
smorioka 0:993f719c9352 32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
smorioka 0:993f719c9352 33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
smorioka 0:993f719c9352 34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
smorioka 0:993f719c9352 35 */
smorioka 0:993f719c9352 36
smorioka 0:993f719c9352 37 #include "mbed.h"
smorioka 0:993f719c9352 38 #include "mbed_rpc.h"
smorioka 0:993f719c9352 39
smorioka 0:993f719c9352 40 #include "TextLCD.h"
smorioka 0:993f719c9352 41 #include "LocalFileSystem.h"
smorioka 0:993f719c9352 42
smorioka 0:993f719c9352 43 #include "EthernetInterface.h"
smorioka 0:993f719c9352 44 #include "HTTPServer.h"
smorioka 0:993f719c9352 45 #include "FsHandler.h"
smorioka 0:993f719c9352 46 #include "RpcHandler.h"
smorioka 0:993f719c9352 47
smorioka 0:993f719c9352 48 #include "ov7670.h"
smorioka 0:993f719c9352 49
smorioka 0:993f719c9352 50 TextLCD lcd(p24, p26, p27, p28, p29, p30);
smorioka 0:993f719c9352 51
smorioka 0:993f719c9352 52
smorioka 0:993f719c9352 53 //#define USE_RPCOUT
smorioka 0:993f719c9352 54 #undef USE_RPCOUT
smorioka 0:993f719c9352 55
smorioka 0:993f719c9352 56 #ifndef USE_RPCOUT
smorioka 0:993f719c9352 57 DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
smorioka 0:993f719c9352 58 PwmOut sv0(p21), sv1(p22), sv2(p23);
smorioka 0:993f719c9352 59 #else
smorioka 0:993f719c9352 60 RpcDigitalOut led1(LED1, "led1");
smorioka 0:993f719c9352 61 RpcDigitalOut led2(LED2, "led2");
smorioka 0:993f719c9352 62 RpcDigitalOut led3(LED3, "led3");
smorioka 0:993f719c9352 63 RpcDigitalOut led4(LED4, "led4");
smorioka 0:993f719c9352 64
smorioka 0:993f719c9352 65 RpcPwmOut sv0(p21, "sv0");
smorioka 0:993f719c9352 66 RpcPwmOut sv1(p22, "sv1");
smorioka 0:993f719c9352 67 RpcPwmOut sv2(p23, "sv2");
smorioka 0:993f719c9352 68 #endif
smorioka 0:993f719c9352 69
smorioka 0:993f719c9352 70
smorioka 0:993f719c9352 71 LocalFileSystem local("webfs");
smorioka 0:993f719c9352 72
smorioka 0:993f719c9352 73 OV7670 camera(
smorioka 0:993f719c9352 74 p9,p10, // SDA,SCL(I2C / SCCB)
smorioka 0:993f719c9352 75 p5,p6,p7, // VSYNC,HREF,WEN(FIFO)
smorioka 0:993f719c9352 76 p20,p19,p18,p17,p16,p15,p14,p13, // D7-D0
smorioka 0:993f719c9352 77 p8,p11,p12); // RRST,OE,RCLK
smorioka 0:993f719c9352 78
smorioka 0:993f719c9352 79 //Timer tmr;
smorioka 0:993f719c9352 80
smorioka 0:993f719c9352 81 #define QQVGA
smorioka 0:993f719c9352 82 //#define QVGA
smorioka 0:993f719c9352 83 //#define VGA34
smorioka 0:993f719c9352 84
smorioka 0:993f719c9352 85 #ifdef QQVGA
smorioka 0:993f719c9352 86 #define SIZEX 160
smorioka 0:993f719c9352 87 #define SIZEY 120
smorioka 0:993f719c9352 88 #endif
smorioka 0:993f719c9352 89
smorioka 0:993f719c9352 90 #ifdef QVGA
smorioka 0:993f719c9352 91 #define SIZEX 320
smorioka 0:993f719c9352 92 #define SIZEY 240
smorioka 0:993f719c9352 93 #endif
smorioka 0:993f719c9352 94
smorioka 0:993f719c9352 95 #ifdef VGA34
smorioka 0:993f719c9352 96 #define SIZEX 480
smorioka 0:993f719c9352 97 #define SIZEY 360
smorioka 0:993f719c9352 98 #endif
smorioka 0:993f719c9352 99
smorioka 0:993f719c9352 100
smorioka 0:993f719c9352 101 //void cam_cap(void);
smorioka 0:993f719c9352 102 void cam_cap(Arguments* input, Reply* output);
smorioka 0:993f719c9352 103 RPCFunction rpcFunc(&cam_cap, "cam_cap");
smorioka 0:993f719c9352 104
smorioka 0:993f719c9352 105 #ifndef USE_RPCOUT
smorioka 0:993f719c9352 106 float sv0_rpc;
smorioka 0:993f719c9352 107 float sv1_rpc;
smorioka 0:993f719c9352 108 float sv2_rpc;
smorioka 0:993f719c9352 109
smorioka 0:993f719c9352 110 RPCVariable<float> rpc_sv0_val(&sv0_rpc, "sv0");
smorioka 0:993f719c9352 111 RPCVariable<float> rpc_sv1_val(&sv1_rpc, "sv1");
smorioka 0:993f719c9352 112 RPCVariable<float> rpc_sv2_val(&sv2_rpc, "sv2");
smorioka 0:993f719c9352 113 #endif
smorioka 0:993f719c9352 114
smorioka 0:993f719c9352 115 int cap_flag;
smorioka 0:993f719c9352 116 RPCVariable<int> rpc_cap_flag(&cap_flag, "cap_flag");
smorioka 0:993f719c9352 117
smorioka 0:993f719c9352 118
smorioka 0:993f719c9352 119 int memfree(void)
smorioka 0:993f719c9352 120 {
smorioka 0:993f719c9352 121 int ret = 1;
smorioka 0:993f719c9352 122 while (1) {
smorioka 0:993f719c9352 123 char *p = (char *)malloc(ret);
smorioka 0:993f719c9352 124 if (p == NULL)
smorioka 0:993f719c9352 125 break;
smorioka 0:993f719c9352 126 free(p);
smorioka 0:993f719c9352 127 ret++;
smorioka 0:993f719c9352 128 }
smorioka 0:993f719c9352 129 return (ret);
smorioka 0:993f719c9352 130 }
smorioka 0:993f719c9352 131
smorioka 0:993f719c9352 132
smorioka 0:993f719c9352 133 float deg_to_pulsewidth(float deg)
smorioka 0:993f719c9352 134 {
smorioka 0:993f719c9352 135 // limit range
smorioka 0:993f719c9352 136 if (deg < -90.0f)
smorioka 0:993f719c9352 137 deg = -90.0f;
smorioka 0:993f719c9352 138 else if (deg > 90.0f)
smorioka 0:993f719c9352 139 deg = 90.0f;
smorioka 0:993f719c9352 140
smorioka 0:993f719c9352 141 // return result
smorioka 0:993f719c9352 142 return (0.00145f + 0.001f * (deg / 90.0f));
smorioka 0:993f719c9352 143 }
smorioka 0:993f719c9352 144
smorioka 0:993f719c9352 145 int main()
smorioka 0:993f719c9352 146 {
smorioka 0:993f719c9352 147 float sv0_val, sv1_val, sv2_val;
smorioka 0:993f719c9352 148
smorioka 0:993f719c9352 149 #ifndef USE_RPCOUT
smorioka 0:993f719c9352 150 led1 = 0;
smorioka 0:993f719c9352 151 led2 = 0;
smorioka 0:993f719c9352 152 led3 = 0;
smorioka 0:993f719c9352 153 led4 = 0;
smorioka 0:993f719c9352 154 #else
smorioka 0:993f719c9352 155 led1.write(0);
smorioka 0:993f719c9352 156 led2.write(0);
smorioka 0:993f719c9352 157 led3.write(0);
smorioka 0:993f719c9352 158 led4.write(0);
smorioka 0:993f719c9352 159 #endif
smorioka 0:993f719c9352 160
smorioka 0:993f719c9352 161 #ifdef USE_RPCOUT
smorioka 0:993f719c9352 162 sv0_val = 0.00045f; // unit: sec
smorioka 0:993f719c9352 163 sv1_val = 0.00145f; // unit: sec
smorioka 0:993f719c9352 164 sv2_val = 0.00245f; // unit: sec
smorioka 0:993f719c9352 165
smorioka 0:993f719c9352 166 sv0.period(0.02f); // unit: sec
smorioka 0:993f719c9352 167 sv0.pulsewidth(sv0_val);
smorioka 0:993f719c9352 168
smorioka 0:993f719c9352 169 sv1.period(0.02f); // unit: sec
smorioka 0:993f719c9352 170 sv1.pulsewidth(sv1_val);
smorioka 0:993f719c9352 171
smorioka 0:993f719c9352 172 sv2.period(0.02f); // unit: sec
smorioka 0:993f719c9352 173 sv2.pulsewidth(sv2_val);
smorioka 0:993f719c9352 174
smorioka 0:993f719c9352 175 #else
smorioka 0:993f719c9352 176 sv0_val = 0.0f; // unit: deg
smorioka 0:993f719c9352 177 sv1_val = 0.0f; // unit: deg
smorioka 0:993f719c9352 178 sv2_val = 0.0f; // unit: deg
smorioka 0:993f719c9352 179
smorioka 0:993f719c9352 180 sv0.period(0.02f); // unit: sec
smorioka 0:993f719c9352 181 sv0.pulsewidth(deg_to_pulsewidth(sv0_val));
smorioka 0:993f719c9352 182
smorioka 0:993f719c9352 183 sv1.period(0.02f); // unit: sec
smorioka 0:993f719c9352 184 sv1.pulsewidth(deg_to_pulsewidth(sv1_val));
smorioka 0:993f719c9352 185
smorioka 0:993f719c9352 186 sv2.period(0.02f); // unit: sec
smorioka 0:993f719c9352 187 sv2.pulsewidth(deg_to_pulsewidth(sv2_val));
smorioka 0:993f719c9352 188
smorioka 0:993f719c9352 189 sv0_rpc = sv0_val;
smorioka 0:993f719c9352 190 sv1_rpc = sv1_val;
smorioka 0:993f719c9352 191 sv2_rpc = sv2_val;
smorioka 0:993f719c9352 192 #endif
smorioka 0:993f719c9352 193
smorioka 0:993f719c9352 194 cap_flag = 0;
smorioka 0:993f719c9352 195
smorioka 0:993f719c9352 196 ////////////////////////////////////////////////////////////////////////////
smorioka 0:993f719c9352 197 camera.WriteReg(0x12, 0x80); // com7; reset
smorioka 0:993f719c9352 198 wait_ms(200);
smorioka 0:993f719c9352 199
smorioka 0:993f719c9352 200 camera.InitDefaultReg();
smorioka 0:993f719c9352 201
smorioka 0:993f719c9352 202 // negate vsync
smorioka 0:993f719c9352 203 camera.WriteReg(0x15, 0x02); // com10; negative vsync
smorioka 0:993f719c9352 204
smorioka 0:993f719c9352 205 #ifdef QQVGA
smorioka 0:993f719c9352 206 camera.InitQQVGA();
smorioka 0:993f719c9352 207 #endif
smorioka 0:993f719c9352 208 #ifdef QVGA
smorioka 0:993f719c9352 209 camera.InitQVGA();
smorioka 0:993f719c9352 210 #endif
smorioka 0:993f719c9352 211 #ifdef VGA34
smorioka 0:993f719c9352 212 camera.InitVGA_3_4();
smorioka 0:993f719c9352 213 #endif
smorioka 0:993f719c9352 214
smorioka 0:993f719c9352 215 // data format
smorioka 0:993f719c9352 216 camera.WriteReg(0x12, 0x04 + 0); // com7 RGB (bit1...test pattern)
smorioka 0:993f719c9352 217 camera.WriteReg(0x40, 0xD0); // com15 RGB565
smorioka 0:993f719c9352 218 camera.WriteReg(0x8c, 0x00); // RGB444
smorioka 0:993f719c9352 219
smorioka 0:993f719c9352 220 wait_ms(300);
smorioka 0:993f719c9352 221
smorioka 0:993f719c9352 222 //{
smorioka 0:993f719c9352 223 //FILE *f = fopen("/webfs/REG.TXT", "wt");
smorioka 0:993f719c9352 224 //for (int i = 0; i < 256; i++) {
smorioka 0:993f719c9352 225 // int val = camera.ReadReg(i);
smorioka 0:993f719c9352 226 // fprintf(f, "%02X ", val);
smorioka 0:993f719c9352 227 // if ((i % 16) == 15)
smorioka 0:993f719c9352 228 // fprintf(f, "\n");
smorioka 0:993f719c9352 229 //}
smorioka 0:993f719c9352 230 //fclose(f);
smorioka 0:993f719c9352 231 //}
smorioka 0:993f719c9352 232
smorioka 0:993f719c9352 233 //////////////////////////////////////////////////
smorioka 0:993f719c9352 234
smorioka 0:993f719c9352 235 // network
smorioka 0:993f719c9352 236 EthernetInterface eth; // locate here
smorioka 0:993f719c9352 237
smorioka 0:993f719c9352 238 // //Static IP
smorioka 0:993f719c9352 239 // char *ip = "192.168.0.20";
smorioka 0:993f719c9352 240 // char *mask = "255.255.255.0";
smorioka 0:993f719c9352 241 // char *gateway = "192.168.0.1";
smorioka 0:993f719c9352 242 // eth.init(ip, mask, gateway);
smorioka 0:993f719c9352 243 //DHCP
smorioka 0:993f719c9352 244 eth.init();
smorioka 0:993f719c9352 245
smorioka 0:993f719c9352 246 eth.connect();
smorioka 0:993f719c9352 247
smorioka 0:993f719c9352 248 lcd.locate(0, 0);
smorioka 0:993f719c9352 249 lcd.printf("IP %s", eth.getIPAddress());
smorioka 0:993f719c9352 250
smorioka 0:993f719c9352 251 // rcp
smorioka 0:993f719c9352 252 #ifdef USE_RPCOUT
smorioka 0:993f719c9352 253 // RPC::add_rpc_class<RpcDigitalIn>(); // read
smorioka 0:993f719c9352 254 RPC::add_rpc_class<RpcDigitalOut>(); // read,write
smorioka 0:993f719c9352 255 // RPC::add_rpc_class<RpcDigitalInOut>(); // read,write,input,output
smorioka 0:993f719c9352 256 RPC::add_rpc_class<RpcPwmOut>(); // read,write,period,period_ms,pulsewidth,pulsewidth_ms
smorioka 0:993f719c9352 257 // RPC::add_rpc_class<RpcAnalogIn>(); // read,read_u16
smorioka 0:993f719c9352 258 // RPC::add_rpc_class<RpcAnalogOut>(); // read,write,write_u16
smorioka 0:993f719c9352 259 // RPC::add_rpc_class<RpcSPI>(); // format,frequency,write
smorioka 0:993f719c9352 260 // RPC::add_rpc_class<RpcSerial>(); // baud,readable,writeable(SPELL?),putc,getc,puts
smorioka 0:993f719c9352 261 // RPC::add_rpc_class<RpcTimer>(); // start,stop,reset,read,read_ms,read_us
smorioka 0:993f719c9352 262 #endif
smorioka 0:993f719c9352 263
smorioka 0:993f719c9352 264 // http
smorioka 0:993f719c9352 265 HTTPServer svr; // locate here
smorioka 0:993f719c9352 266 HTTPFsRequestHandler::mount("/webfs/", "/");
smorioka 0:993f719c9352 267 svr.addHandler<HTTPFsRequestHandler>("/");
smorioka 0:993f719c9352 268 svr.addHandler<HTTPRpcRequestHandler>("/rpc");
smorioka 0:993f719c9352 269
smorioka 0:993f719c9352 270 svr.start(80, &eth);
smorioka 0:993f719c9352 271
smorioka 0:993f719c9352 272 // lcd.locate(0, 1);
smorioka 0:993f719c9352 273 // lcd.printf("mem %d", memfree());
smorioka 0:993f719c9352 274
smorioka 0:993f719c9352 275 #ifndef USE_RPCOUT
smorioka 0:993f719c9352 276 led1 = 1;
smorioka 0:993f719c9352 277 #else
smorioka 0:993f719c9352 278 led1.write(1);
smorioka 0:993f719c9352 279 #endif
smorioka 0:993f719c9352 280
smorioka 0:993f719c9352 281 while (1) {
smorioka 0:993f719c9352 282 svr.poll();
smorioka 0:993f719c9352 283
smorioka 0:993f719c9352 284 #ifndef USE_RPCOUT
smorioka 0:993f719c9352 285 if (sv0_val != sv0_rpc) {
smorioka 0:993f719c9352 286 sv0_val = sv0_rpc;
smorioka 0:993f719c9352 287 // limit
smorioka 0:993f719c9352 288 if (sv0_val < -90.0f)
smorioka 0:993f719c9352 289 sv0_val = -90.0f;
smorioka 0:993f719c9352 290 else if (sv0_val > 90.0f)
smorioka 0:993f719c9352 291 sv0_val = 90.0f;
smorioka 0:993f719c9352 292
smorioka 0:993f719c9352 293 sv0_rpc = sv0_val;
smorioka 0:993f719c9352 294 sv0.pulsewidth(deg_to_pulsewidth(sv0_val));
smorioka 0:993f719c9352 295
smorioka 0:993f719c9352 296 lcd.locate(0, 1);
smorioka 0:993f719c9352 297 lcd.printf("0:%f ", sv0_val);
smorioka 0:993f719c9352 298 }
smorioka 0:993f719c9352 299
smorioka 0:993f719c9352 300 if (sv1_val != sv1_rpc) {
smorioka 0:993f719c9352 301 sv1_val = sv1_rpc;
smorioka 0:993f719c9352 302 // limit
smorioka 0:993f719c9352 303 if (sv1_val < -90.0f)
smorioka 0:993f719c9352 304 sv1_val = -90.0f;
smorioka 0:993f719c9352 305 else if (sv1_val > 90.0f)
smorioka 0:993f719c9352 306 sv1_val = 90.0f;
smorioka 0:993f719c9352 307
smorioka 0:993f719c9352 308 sv1_rpc = sv1_val;
smorioka 0:993f719c9352 309 sv1.pulsewidth(deg_to_pulsewidth(sv1_val));
smorioka 0:993f719c9352 310
smorioka 0:993f719c9352 311 lcd.locate(0, 1);
smorioka 0:993f719c9352 312 lcd.printf("1:%f ", sv1_val);
smorioka 0:993f719c9352 313 }
smorioka 0:993f719c9352 314
smorioka 0:993f719c9352 315 if (sv2_val != sv2_rpc) {
smorioka 0:993f719c9352 316 sv2_val = sv2_rpc;
smorioka 0:993f719c9352 317 // limit
smorioka 0:993f719c9352 318 if (sv2_val < -90.0f)
smorioka 0:993f719c9352 319 sv2_val = -90.0f;
smorioka 0:993f719c9352 320 else if (sv2_val > 90.0f)
smorioka 0:993f719c9352 321 sv2_val = 90.0f;
smorioka 0:993f719c9352 322
smorioka 0:993f719c9352 323 sv2_rpc = sv2_val;
smorioka 0:993f719c9352 324 sv2.pulsewidth(deg_to_pulsewidth(sv2_val));
smorioka 0:993f719c9352 325
smorioka 0:993f719c9352 326 lcd.locate(0, 1);
smorioka 0:993f719c9352 327 lcd.printf("2:%f ", sv2_val);
smorioka 0:993f719c9352 328 }
smorioka 0:993f719c9352 329 #endif
smorioka 0:993f719c9352 330
smorioka 0:993f719c9352 331 wait_ms(10);
smorioka 0:993f719c9352 332 }
smorioka 0:993f719c9352 333 }
smorioka 0:993f719c9352 334
smorioka 0:993f719c9352 335
smorioka 0:993f719c9352 336 //void cam_capture(char *input, char *output) // compile error
smorioka 0:993f719c9352 337 void cam_cap(Arguments* input, Reply* output)
smorioka 0:993f719c9352 338 //void cam_cap(void)
smorioka 0:993f719c9352 339 {
smorioka 0:993f719c9352 340 FILE *fp_bmp;
smorioka 0:993f719c9352 341 unsigned int d1, d2;
smorioka 0:993f719c9352 342 unsigned char sort[3];
smorioka 0:993f719c9352 343
smorioka 0:993f719c9352 344 #ifndef USE_RPCOUT
smorioka 0:993f719c9352 345 led2 = 1;
smorioka 0:993f719c9352 346 #else
smorioka 0:993f719c9352 347 led2.write(1);
smorioka 0:993f719c9352 348 #endif
smorioka 0:993f719c9352 349
smorioka 0:993f719c9352 350 cap_flag = 0;
smorioka 0:993f719c9352 351
smorioka 0:993f719c9352 352 fp_bmp = fopen("/webfs/cam.bmp", "wb");
smorioka 0:993f719c9352 353
smorioka 0:993f719c9352 354 /////////////////////////
smorioka 0:993f719c9352 355 // file header
smorioka 0:993f719c9352 356 /////////////////////////
smorioka 0:993f719c9352 357 fprintf(fp_bmp, "BM");
smorioka 0:993f719c9352 358 int val = 14 + 40 + SIZEX * SIZEY * 3; // file size
smorioka 0:993f719c9352 359 fprintf(fp_bmp, "%c%c%c%c", val % 0x100, val / 0x100, val / 0x10000, val / 0x1000000);
smorioka 0:993f719c9352 360 fprintf(fp_bmp, "%c%c%c%c%c%c%c%c", 0, 0, 0, 0, 0x36, 0, 0, 0);
smorioka 0:993f719c9352 361
smorioka 0:993f719c9352 362 /////////////////////////
smorioka 0:993f719c9352 363 // information header
smorioka 0:993f719c9352 364 /////////////////////////
smorioka 0:993f719c9352 365 fprintf(fp_bmp, "%c%c%c%c", 0x28, 0, 0, 0); // header size
smorioka 0:993f719c9352 366 fprintf(fp_bmp, "%c%c%c%c", SIZEX % 0x100, SIZEX / 0x100, SIZEX / 0x10000, SIZEX / 0x1000000);
smorioka 0:993f719c9352 367 fprintf(fp_bmp, "%c%c%c%c", SIZEY % 0x100, SIZEY / 0x100, SIZEY / 0x10000, SIZEY / 0x1000000);
smorioka 0:993f719c9352 368 fprintf(fp_bmp, "%c%c", 1, 0); // # of plane
smorioka 0:993f719c9352 369 fprintf(fp_bmp, "%c%c", 24, 0); // bit count
smorioka 0:993f719c9352 370 fprintf(fp_bmp, "%c%c%c%c", 0, 0, 0, 0); // compression
smorioka 0:993f719c9352 371 val = SIZEX * SIZEY * 3; // data size
smorioka 0:993f719c9352 372 fprintf(fp_bmp, "%c%c%c%c", val % 0x100, val / 0x100, val / 0x10000, val / 0x1000000);
smorioka 0:993f719c9352 373 fprintf(fp_bmp, "%c%c%c%c", 0, 0, 0, 0);
smorioka 0:993f719c9352 374 fprintf(fp_bmp, "%c%c%c%c", 0, 0, 0, 0);
smorioka 0:993f719c9352 375 fprintf(fp_bmp, "%c%c%c%c", 0, 0, 0, 0);
smorioka 0:993f719c9352 376 fprintf(fp_bmp, "%c%c%c%c", 0, 0, 0, 0);
smorioka 0:993f719c9352 377
smorioka 0:993f719c9352 378 camera.CaptureNext(); // sample start!
smorioka 0:993f719c9352 379
smorioka 0:993f719c9352 380 while(camera.CaptureDone() == false)
smorioka 0:993f719c9352 381 ;
smorioka 0:993f719c9352 382
smorioka 0:993f719c9352 383 camera.ReadStart(); // reset pointer
smorioka 0:993f719c9352 384
smorioka 0:993f719c9352 385 #ifndef USE_RPCOUT
smorioka 0:993f719c9352 386 led3 = 1;
smorioka 0:993f719c9352 387 #else
smorioka 0:993f719c9352 388 led3.write(1);
smorioka 0:993f719c9352 389 #endif
smorioka 0:993f719c9352 390
smorioka 0:993f719c9352 391 for (int y = 0;y < SIZEY;y++) {
smorioka 0:993f719c9352 392 for (int x = 0;x < SIZEX;x++) {
smorioka 0:993f719c9352 393 d1 = camera.ReadOneByte() ; // upper nibble is XXX , lower nibble is B
smorioka 0:993f719c9352 394 d2 = camera.ReadOneByte() ; // upper nibble is G , lower nibble is R
smorioka 0:993f719c9352 395
smorioka 0:993f719c9352 396 // RGB565
smorioka 0:993f719c9352 397 sort[0] = ((d1 & 0xF8) >> 3) << 3; // R
smorioka 0:993f719c9352 398 sort[1] = ( ((d1 & 0x07) << 3) + ((d2 & 0xE0) >> 5) ) << 2; // G
smorioka 0:993f719c9352 399 sort[2] = (d2 & 0x1F) << 3; // B
smorioka 0:993f719c9352 400
smorioka 0:993f719c9352 401 fprintf(fp_bmp, "%c%c%c", sort[2], sort[1], sort[0]); // B,G,R
smorioka 0:993f719c9352 402 }
smorioka 0:993f719c9352 403 }
smorioka 0:993f719c9352 404
smorioka 0:993f719c9352 405 camera.ReadStop();
smorioka 0:993f719c9352 406 fclose(fp_bmp);
smorioka 0:993f719c9352 407
smorioka 0:993f719c9352 408 #ifndef USE_RPCOUT
smorioka 0:993f719c9352 409 led2 = 0;
smorioka 0:993f719c9352 410 led3 = 0;
smorioka 0:993f719c9352 411 led4 = 0;
smorioka 0:993f719c9352 412 #else
smorioka 0:993f719c9352 413 led2.write(0);
smorioka 0:993f719c9352 414 led3.write(0);
smorioka 0:993f719c9352 415 led4.write(0);
smorioka 0:993f719c9352 416 #endif
smorioka 0:993f719c9352 417
smorioka 0:993f719c9352 418 cap_flag = 1;
smorioka 0:993f719c9352 419 }
smorioka 0:993f719c9352 420
smorioka 0:993f719c9352 421 // end of file