code for taking image and saving to SD card
Dependencies: SDFileSystem mbed
Diff: main.cpp
- Revision:
- 0:c88af26224b8
- Child:
- 1:43abfc6827c6
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Mar 03 22:24:57 2015 +0000 @@ -0,0 +1,186 @@ +/* +Porting example Arduino code for camera module. +http://learn.linksprite.com/jpeg-camera/tutorial-of-using-linksprite-2mp-uart-jpeg-camera-with-arduino/ + +*/ + +#include "mbed.h" +#include "SDFileSystem.h" + +//int csPin = 4; + +Serial mySerial(D0, D1); // Nucleo D0 is rx and D1 is tx for camera + +byte ZERO = 0x00; +byte incomingbyte; +long int j=0,k=0,count=0,i=0x0000; +uint8_t MH,ML; +boolean EndFlag=0; +File myFile; + +void SendResetCmd() +{ + mySerial.printf(0x56); + mySerial.printf(ZERO); + mySerial.printf(0x26); + mySerial.printf(ZERO); +} + +/*************************************/ +/* Set ImageSize : +/* <1> 0x22 : 160*120 +/* <2> 0x11 : 320*240 +/* <3> 0x00 : 640*480 +/* <4> 0x1D : 800*600 +/* <5> 0x1C : 1024*768 +/* <6> 0x1B : 1280*960 +/* <7> 0x21 : 1600*1200 +/************************************/ +void SetImageSizeCmd(byte Size) +{ + mySerial.printf(0x56); + mySerial.printf(ZERO); + mySerial.printf(0x54); + mySerial.printf(0x01); + mySerial.printf(Size); +} + +/*************************************/ +/* Set BaudRate : +/* <1> 0xAE : 9600 +/* <2> 0x2A : 38400 +/* <3> 0x1C : 57600 +/* <4> 0x0D : 115200 +/* <5> 0xAE : 128000 +/* <6> 0x56 : 256000 +/*************************************/ +void SetBaudRateCmd(byte baudrate) +{ + mySerial.printf(0x56); + mySerial.printf(ZERO); + mySerial.printf(0x24); + mySerial.printf(0x03); + mySerial.printf(0x01); + mySerial.printf(baudrate); +} + +void SendTakePhotoCmd() +{ + mySerial.printf(0x56); + mySerial.printf(ZERO); + mySerial.printf(0x36); + mySerial.printf(0x01); + mySerial.printf(ZERO); +} + +void SendReadDataCmd() +{ + MH=i/0x100; + ML=i%0x100; + mySerial.printf(0x56); + mySerial.printf(ZERO); + mySerial.printf(0x32); + mySerial.printf(0x0c); + mySerial.printf(ZERO); + mySerial.printf(0x0a); + mySerial.printf(ZERO); + mySerial.printf(ZERO); + mySerial.printf(MH); + mySerial.printf(ML); + mySerial.printf(ZERO); + mySerial.printf(ZERO); + mySerial.printf(ZERO); + mySerial.printf(0x20); + mySerial.printf(ZERO); + mySerial.printf(0x0a); + i+=0x20; +} + +void StopTakePhotoCmd() +{ + mySerial.printf(0x56); + mySerial.printf(ZERO); + mySerial.printf(0x36); + mySerial.printf(0x01); + mySerial.printf(0x03); +} + +void setup() +{ +//pinMode(csPin, OUTPUT); +//Serial.begin(115200); +//while (!Serial) { +//; // wait for serial port to connect. Needed for Leonardo only +//} + +//Serial.print("Initializing SD card..."); +// On the Ethernet Shield, CS is pin 4. It's set as an output by default. +// Note that even if it's not used as the CS pin, the hardware SS pin +// (10 on most Arduino boards, 53 on the Mega) must be left as an output +// or the SD library functions will not work. +//if (!SD.begin(4)) { +//Serial.println("initialization failed!"); +//return; +} +//Serial.println("initialization done."); +//Serial.println("please waiting ...."); +//} + +void loop() +{ + byte a[32]; + int ii; + mySerial.begin(115200); + delay(200); + SendResetCmd();//Wait 2-3 second to send take picture command + delay(2000); + SetBaudRateCmd(0x2A); + delay(100); + mySerial.begin(38400); + delay(100); + SetImageSizeCmd(0x21); + delay(100); + SendTakePhotoCmd(); + delay(3000); + while(mySerial.available()>0) + { + incomingbyte=mySerial.read(); + } + myFile = SD.open("pic.jpg", FILE_WRITE); //<strong><span style="color: #ff0000;">The file name should not be too long</span></strong> + while(!EndFlag) + { + j=0; + k=0; + count=0; + SendReadDataCmd(); + delay(5); + while(mySerial.available()>0) + { + incomingbyte=mySerial.read(); + k++; + delayMicroseconds(100); + if((k>5)&&(j<32)&&(!EndFlag)) + { + a[j]=incomingbyte; + if((a[j-1]==0xFF)&&(a[j]==0xD9)) //tell if the picture is finished + EndFlag=1; + j++; + count++; + } + } + /* + for(j=0;j<count;j++) + { + if(a[j]<0x10) Serial.print("0"); + Serial.print(a[j],HEX); // observe the image through serial port + Serial.print(" "); + } +*/ + for(ii=0; ii<count; ii++) + myFile.write(a[ii]); + //Serial.println(); + } + myFile.close(); + //Serial.print("Finished writing data to file"); + while(1); +}