I made Digital Camera using Arducam & WIZwiki-W7500
Dependencies: Arduino Arducam_OV5642 SDFileSystem Arducam_UTFT_SPI WIZnetInterface_Ricky mbed
main.cpp@1:3a14a4c84db2, 2015-10-29 (annotated)
- Committer:
- justinkim
- Date:
- Thu Oct 29 06:58:08 2015 +0000
- Revision:
- 1:3a14a4c84db2
- Parent:
- 0:e01f64037748
- Child:
- 2:3a12c5e8b030
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
justinkim | 0:e01f64037748 | 1 | #include "mbed.h" |
justinkim | 0:e01f64037748 | 2 | |
justinkim | 0:e01f64037748 | 3 | #include "UTFT_SPI.h" |
justinkim | 0:e01f64037748 | 4 | #include "OV5642.h" |
justinkim | 0:e01f64037748 | 5 | #include "OV5642_regs.h" |
justinkim | 0:e01f64037748 | 6 | #include "SDFileSystem.h" |
justinkim | 0:e01f64037748 | 7 | #include "Arduino.h" |
justinkim | 0:e01f64037748 | 8 | |
justinkim | 0:e01f64037748 | 9 | void setup(); |
justinkim | 0:e01f64037748 | 10 | void loop(); |
justinkim | 0:e01f64037748 | 11 | void GrabImage(char* str); |
justinkim | 0:e01f64037748 | 12 | void dispBitmap(); |
justinkim | 0:e01f64037748 | 13 | void Playback(); |
justinkim | 0:e01f64037748 | 14 | void dispBitmap(FILE* fname); |
justinkim | 0:e01f64037748 | 15 | /* itoa: convert n to characters in s */ |
justinkim | 0:e01f64037748 | 16 | void itoa( unsigned long long int value, char *str) |
justinkim | 0:e01f64037748 | 17 | { |
justinkim | 0:e01f64037748 | 18 | int i,j; |
justinkim | 0:e01f64037748 | 19 | char temp[30]; |
justinkim | 0:e01f64037748 | 20 | for(i=0; value > 0; i++){ |
justinkim | 0:e01f64037748 | 21 | str[i] = value%10+'0'; |
justinkim | 0:e01f64037748 | 22 | value=value/10; |
justinkim | 0:e01f64037748 | 23 | } |
justinkim | 0:e01f64037748 | 24 | for(j=0;i>=0;j++,i--){ |
justinkim | 0:e01f64037748 | 25 | temp[j]=str[i-1]; |
justinkim | 0:e01f64037748 | 26 | } |
justinkim | 0:e01f64037748 | 27 | for(i=0;i<j;i++){ |
justinkim | 0:e01f64037748 | 28 | str[i]=temp[i]; |
justinkim | 0:e01f64037748 | 29 | } |
justinkim | 0:e01f64037748 | 30 | } |
justinkim | 0:e01f64037748 | 31 | |
justinkim | 0:e01f64037748 | 32 | #define BMPIMAGEOFFSET 66 |
justinkim | 0:e01f64037748 | 33 | static FILE *outFile; |
justinkim | 0:e01f64037748 | 34 | static FILE *inFile; |
justinkim | 0:e01f64037748 | 35 | |
justinkim | 0:e01f64037748 | 36 | const char bmp_header[BMPIMAGEOFFSET] = |
justinkim | 0:e01f64037748 | 37 | { |
justinkim | 0:e01f64037748 | 38 | 0x42, 0x4D, 0x36, 0x58, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x28, 0x00, |
justinkim | 0:e01f64037748 | 39 | 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x01, 0x00, 0x10, 0x00, 0x03, 0x00, |
justinkim | 0:e01f64037748 | 40 | 0x00, 0x00, 0x00, 0x58, 0x02, 0x00, 0xC4, 0x0E, 0x00, 0x00, 0xC4, 0x0E, 0x00, 0x00, 0x00, 0x00, |
justinkim | 0:e01f64037748 | 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x00, 0x00, 0xE0, 0x07, 0x00, 0x00, 0x1F, 0x00, |
justinkim | 0:e01f64037748 | 42 | 0x00, 0x00 |
justinkim | 0:e01f64037748 | 43 | }; |
justinkim | 0:e01f64037748 | 44 | |
justinkim | 0:e01f64037748 | 45 | ArduCAM myCAM(D11, D12, D13, D10, D14, D15); |
justinkim | 1:3a14a4c84db2 | 46 | ArduLCD myGLCD(D11, D12, D13, D10); |
justinkim | 0:e01f64037748 | 47 | SDFileSystem SD(PB_3, PB_2, PB_1, PB_0, "sd"); |
justinkim | 0:e01f64037748 | 48 | Serial pc(USBTX, USBRX); |
justinkim | 0:e01f64037748 | 49 | bool isShowFlag = true; |
justinkim | 0:e01f64037748 | 50 | |
justinkim | 0:e01f64037748 | 51 | int main() |
justinkim | 0:e01f64037748 | 52 | { |
justinkim | 1:3a14a4c84db2 | 53 | *(volatile uint32_t *)(0x41001014) = 0x0060100; //clock 48MHz |
justinkim | 0:e01f64037748 | 54 | |
justinkim | 0:e01f64037748 | 55 | setup(); |
justinkim | 0:e01f64037748 | 56 | |
justinkim | 0:e01f64037748 | 57 | while(1) |
justinkim | 0:e01f64037748 | 58 | { |
justinkim | 0:e01f64037748 | 59 | loop(); |
justinkim | 0:e01f64037748 | 60 | } |
justinkim | 0:e01f64037748 | 61 | } |
justinkim | 0:e01f64037748 | 62 | |
justinkim | 0:e01f64037748 | 63 | void setup() |
justinkim | 0:e01f64037748 | 64 | { |
justinkim | 0:e01f64037748 | 65 | uint8_t vid,pid; |
justinkim | 0:e01f64037748 | 66 | uint8_t temp; |
justinkim | 0:e01f64037748 | 67 | |
justinkim | 0:e01f64037748 | 68 | pc.baud(115200); |
justinkim | 0:e01f64037748 | 69 | pc.printf("ArduCAM Start!\r\n"); |
justinkim | 1:3a14a4c84db2 | 70 | |
justinkim | 1:3a14a4c84db2 | 71 | uint8_t temp1,temp2; |
justinkim | 1:3a14a4c84db2 | 72 | myCAM.write_reg(ARDUCHIP_TEST1, 0x55); //Write to test1 register by 0x55 |
justinkim | 1:3a14a4c84db2 | 73 | myCAM.write_reg(ARDUCHIP_TEST2, 0xAA); //Write to test1 register by 0xaa |
justinkim | 1:3a14a4c84db2 | 74 | wait_ms(1000); |
justinkim | 1:3a14a4c84db2 | 75 | temp1 = myCAM.read_reg(ARDUCHIP_TEST1); //Read from test1 register |
justinkim | 1:3a14a4c84db2 | 76 | temp2 = myCAM.read_reg(ARDUCHIP_TEST2); //Read from test1 register |
justinkim | 1:3a14a4c84db2 | 77 | pc.printf("temp1 : %d\r\n",temp1); |
justinkim | 1:3a14a4c84db2 | 78 | pc.printf("temp2 : %d\r\n",temp2); |
justinkim | 1:3a14a4c84db2 | 79 | wait_ms(1000); |
justinkim | 1:3a14a4c84db2 | 80 | |
justinkim | 0:e01f64037748 | 81 | myCAM.write_reg(ARDUCHIP_TEST1, 0x55); |
justinkim | 0:e01f64037748 | 82 | temp = myCAM.read_reg(ARDUCHIP_TEST1); |
justinkim | 1:3a14a4c84db2 | 83 | |
justinkim | 0:e01f64037748 | 84 | if(temp != 0x55) |
justinkim | 0:e01f64037748 | 85 | { |
justinkim | 0:e01f64037748 | 86 | pc.printf("SPI interface Error!\r\n"); |
justinkim | 0:e01f64037748 | 87 | while(1); |
justinkim | 0:e01f64037748 | 88 | } |
justinkim | 0:e01f64037748 | 89 | |
justinkim | 0:e01f64037748 | 90 | //Change MCU mode |
justinkim | 0:e01f64037748 | 91 | myCAM.set_mode(MCU2LCD_MODE); |
justinkim | 0:e01f64037748 | 92 | |
justinkim | 0:e01f64037748 | 93 | //Initialize the LCD Module |
justinkim | 1:3a14a4c84db2 | 94 | myGLCD.InitLCD(); |
justinkim | 0:e01f64037748 | 95 | |
justinkim | 0:e01f64037748 | 96 | //Check if the camera module type is OV5642 |
justinkim | 0:e01f64037748 | 97 | myCAM.rdSensorReg16_8(OV5642_CHIPID_HIGH, &vid); |
justinkim | 0:e01f64037748 | 98 | myCAM.rdSensorReg16_8(OV5642_CHIPID_LOW, &pid); |
justinkim | 0:e01f64037748 | 99 | if((vid != 0x56) || (pid != 0x42)) |
justinkim | 0:e01f64037748 | 100 | pc.printf("Can't find OV5642 module!\r\n"); |
justinkim | 0:e01f64037748 | 101 | else |
justinkim | 0:e01f64037748 | 102 | pc.printf("OV5642 detected\r\n"); |
justinkim | 0:e01f64037748 | 103 | |
justinkim | 0:e01f64037748 | 104 | myCAM.set_format(BMP); |
justinkim | 0:e01f64037748 | 105 | myCAM.InitCAM(); |
justinkim | 0:e01f64037748 | 106 | } |
justinkim | 0:e01f64037748 | 107 | |
justinkim | 0:e01f64037748 | 108 | void loop() |
justinkim | 0:e01f64037748 | 109 | { |
justinkim | 0:e01f64037748 | 110 | char str[8]; |
justinkim | 0:e01f64037748 | 111 | unsigned long previous_time = 0; |
justinkim | 0:e01f64037748 | 112 | static int k = 0; |
justinkim | 0:e01f64037748 | 113 | myCAM.set_mode(CAM2LCD_MODE); //Switch to CAM |
justinkim | 0:e01f64037748 | 114 | |
justinkim | 0:e01f64037748 | 115 | while(1) |
justinkim | 0:e01f64037748 | 116 | { |
justinkim | 0:e01f64037748 | 117 | |
justinkim | 0:e01f64037748 | 118 | if(!myCAM.get_bit(ARDUCHIP_TRIG,VSYNC_MASK)) //New Frame is coming |
justinkim | 0:e01f64037748 | 119 | { |
justinkim | 0:e01f64037748 | 120 | myCAM.set_mode(MCU2LCD_MODE); //Switch to MCU |
justinkim | 1:3a14a4c84db2 | 121 | myGLCD.resetXY(); |
justinkim | 0:e01f64037748 | 122 | myCAM.set_mode(CAM2LCD_MODE); //Switch to CAM |
justinkim | 0:e01f64037748 | 123 | while(!myCAM.get_bit(ARDUCHIP_TRIG,VSYNC_MASK)); //Wait for VSYNC is gone |
justinkim | 1:3a14a4c84db2 | 124 | pc.printf("VSYNC\r\n"); |
justinkim | 0:e01f64037748 | 125 | } |
justinkim | 0:e01f64037748 | 126 | else if(myCAM.get_bit(ARDUCHIP_TRIG,SHUTTER_MASK)) |
justinkim | 1:3a14a4c84db2 | 127 | { |
justinkim | 1:3a14a4c84db2 | 128 | pc.printf("SHUTTER\r\n"); |
justinkim | 0:e01f64037748 | 129 | previous_time = millis(); |
justinkim | 0:e01f64037748 | 130 | while(myCAM.get_bit(ARDUCHIP_TRIG,SHUTTER_MASK)) |
justinkim | 0:e01f64037748 | 131 | { |
justinkim | 0:e01f64037748 | 132 | if((millis() - previous_time) > 1500) |
justinkim | 0:e01f64037748 | 133 | { |
justinkim | 0:e01f64037748 | 134 | Playback(); |
justinkim | 1:3a14a4c84db2 | 135 | pc.printf("playback\r\n"); |
justinkim | 0:e01f64037748 | 136 | } |
justinkim | 0:e01f64037748 | 137 | } |
justinkim | 0:e01f64037748 | 138 | if((millis() - previous_time) < 1500) |
justinkim | 0:e01f64037748 | 139 | { |
justinkim | 0:e01f64037748 | 140 | k = k + 1; |
justinkim | 0:e01f64037748 | 141 | itoa(k, str); |
justinkim | 0:e01f64037748 | 142 | strcat(str,".bmp"); //Generate file name |
justinkim | 0:e01f64037748 | 143 | myCAM.set_mode(MCU2LCD_MODE); //Switch to MCU, freeze the screen |
justinkim | 0:e01f64037748 | 144 | GrabImage(str); |
justinkim | 1:3a14a4c84db2 | 145 | pc.printf("GrabImage\r\n"); |
justinkim | 0:e01f64037748 | 146 | } |
justinkim | 0:e01f64037748 | 147 | } |
justinkim | 0:e01f64037748 | 148 | } |
justinkim | 0:e01f64037748 | 149 | } |
justinkim | 0:e01f64037748 | 150 | |
justinkim | 0:e01f64037748 | 151 | |
justinkim | 0:e01f64037748 | 152 | void GrabImage(char* str) |
justinkim | 0:e01f64037748 | 153 | { |
justinkim | 0:e01f64037748 | 154 | char VH,VL; |
justinkim | 0:e01f64037748 | 155 | uint8_t buf[256]; |
justinkim | 0:e01f64037748 | 156 | static int k = 0; |
justinkim | 0:e01f64037748 | 157 | int i,j = 0; |
justinkim | 0:e01f64037748 | 158 | outFile = fopen("preview.jpg", "w"); |
justinkim | 0:e01f64037748 | 159 | if (!outFile) |
justinkim | 0:e01f64037748 | 160 | { |
justinkim | 0:e01f64037748 | 161 | pc.printf("Open File Error\r\n"); |
justinkim | 0:e01f64037748 | 162 | return; |
justinkim | 0:e01f64037748 | 163 | } |
justinkim | 0:e01f64037748 | 164 | |
justinkim | 0:e01f64037748 | 165 | //Switch to FIFO Mode |
justinkim | 0:e01f64037748 | 166 | myCAM.write_reg(ARDUCHIP_TIM, MODE_MASK); |
justinkim | 0:e01f64037748 | 167 | //Flush the FIFO |
justinkim | 0:e01f64037748 | 168 | myCAM.flush_fifo(); |
justinkim | 0:e01f64037748 | 169 | //Start capture |
justinkim | 0:e01f64037748 | 170 | myCAM.start_capture(); |
justinkim | 0:e01f64037748 | 171 | pc.printf("Start Capture\r\n"); |
justinkim | 0:e01f64037748 | 172 | |
justinkim | 0:e01f64037748 | 173 | //Polling the capture done flag |
justinkim | 0:e01f64037748 | 174 | while(!myCAM.get_bit(ARDUCHIP_TRIG,CAP_DONE_MASK)); |
justinkim | 0:e01f64037748 | 175 | pc.printf("Capture Done!\r\n"); |
justinkim | 0:e01f64037748 | 176 | |
justinkim | 0:e01f64037748 | 177 | k = 0; |
justinkim | 0:e01f64037748 | 178 | //Write the BMP header |
justinkim | 0:e01f64037748 | 179 | for( i = 0; i < BMPIMAGEOFFSET; i++) |
justinkim | 0:e01f64037748 | 180 | { |
justinkim | 0:e01f64037748 | 181 | char ch = pgm_read_byte(&bmp_header[i]); |
justinkim | 0:e01f64037748 | 182 | buf[k++] = ch; |
justinkim | 0:e01f64037748 | 183 | } |
justinkim | 0:e01f64037748 | 184 | fwrite(buf,256,1,outFile); |
justinkim | 0:e01f64037748 | 185 | //Read the first dummy byte |
justinkim | 0:e01f64037748 | 186 | //myCAM.read_fifo(); |
justinkim | 0:e01f64037748 | 187 | |
justinkim | 0:e01f64037748 | 188 | k = 0; |
justinkim | 0:e01f64037748 | 189 | //Read 320x240x2 byte from FIFO |
justinkim | 0:e01f64037748 | 190 | //Save as RGB565 bmp format |
justinkim | 0:e01f64037748 | 191 | for(i = 0; i < 240; i++) |
justinkim | 0:e01f64037748 | 192 | for(j = 0; j < 320; j++) |
justinkim | 0:e01f64037748 | 193 | { |
justinkim | 0:e01f64037748 | 194 | VH = myCAM.read_fifo(); |
justinkim | 0:e01f64037748 | 195 | VL = myCAM.read_fifo(); |
justinkim | 0:e01f64037748 | 196 | buf[k++] = VL; |
justinkim | 0:e01f64037748 | 197 | buf[k++] = VH; |
justinkim | 0:e01f64037748 | 198 | //Write image data to bufer if not full |
justinkim | 0:e01f64037748 | 199 | if(k >= 256) |
justinkim | 0:e01f64037748 | 200 | { |
justinkim | 0:e01f64037748 | 201 | //Write 256 bytes image data to file from buffer |
justinkim | 0:e01f64037748 | 202 | fwrite(buf,256,1,outFile); |
justinkim | 0:e01f64037748 | 203 | k = 0; |
justinkim | 0:e01f64037748 | 204 | } |
justinkim | 0:e01f64037748 | 205 | } |
justinkim | 0:e01f64037748 | 206 | //Close the file |
justinkim | 0:e01f64037748 | 207 | fclose(outFile); |
justinkim | 0:e01f64037748 | 208 | |
justinkim | 0:e01f64037748 | 209 | //Clear the capture done flag |
justinkim | 0:e01f64037748 | 210 | myCAM.clear_fifo_flag(); |
justinkim | 0:e01f64037748 | 211 | |
justinkim | 0:e01f64037748 | 212 | //Switch to LCD Mode |
justinkim | 0:e01f64037748 | 213 | myCAM.write_reg(ARDUCHIP_TIM, 0); |
justinkim | 0:e01f64037748 | 214 | return; |
justinkim | 0:e01f64037748 | 215 | } |
justinkim | 0:e01f64037748 | 216 | |
justinkim | 0:e01f64037748 | 217 | void Playback() |
justinkim | 0:e01f64037748 | 218 | { |
justinkim | 0:e01f64037748 | 219 | char str[8]; |
justinkim | 0:e01f64037748 | 220 | int k = 0; |
justinkim | 0:e01f64037748 | 221 | myCAM.set_mode(MCU2LCD_MODE); //Switch to MCU |
justinkim | 1:3a14a4c84db2 | 222 | myGLCD.InitLCD(PORTRAIT); |
justinkim | 0:e01f64037748 | 223 | |
justinkim | 0:e01f64037748 | 224 | while(1) |
justinkim | 0:e01f64037748 | 225 | { |
justinkim | 0:e01f64037748 | 226 | k = k + 1; |
justinkim | 0:e01f64037748 | 227 | itoa(k, str); |
justinkim | 0:e01f64037748 | 228 | strcat(str,".bmp"); |
justinkim | 0:e01f64037748 | 229 | inFile = fopen("preview","r"); |
justinkim | 0:e01f64037748 | 230 | if (! inFile) |
justinkim | 0:e01f64037748 | 231 | return; |
justinkim | 1:3a14a4c84db2 | 232 | myGLCD.clrScr(); |
justinkim | 1:3a14a4c84db2 | 233 | myGLCD.resetXY(); |
justinkim | 0:e01f64037748 | 234 | dispBitmap(inFile); |
justinkim | 0:e01f64037748 | 235 | fclose(inFile); |
justinkim | 0:e01f64037748 | 236 | wait_ms(2000); |
justinkim | 0:e01f64037748 | 237 | } |
justinkim | 0:e01f64037748 | 238 | } |
justinkim | 0:e01f64037748 | 239 | |
justinkim | 0:e01f64037748 | 240 | //Only support RGB565 bmp format |
justinkim | 0:e01f64037748 | 241 | void dispBitmap(FILE* fname) |
justinkim | 0:e01f64037748 | 242 | { |
justinkim | 0:e01f64037748 | 243 | uint8_t buf[256]; |
justinkim | 0:e01f64037748 | 244 | char VH,VL; |
justinkim | 0:e01f64037748 | 245 | int i,j = 0; |
justinkim | 0:e01f64037748 | 246 | for(i = 0 ;i < BMPIMAGEOFFSET; i++) |
justinkim | 0:e01f64037748 | 247 | fread(buf,256,1,fname); |
justinkim | 0:e01f64037748 | 248 | for(i = 0; i < 320; i++) |
justinkim | 0:e01f64037748 | 249 | for(j = 0; j < 240; j++) |
justinkim | 0:e01f64037748 | 250 | { |
justinkim | 0:e01f64037748 | 251 | VL = fread(buf,256,1,fname); |
justinkim | 0:e01f64037748 | 252 | //Serial.write(VL); |
justinkim | 0:e01f64037748 | 253 | VH = fread(buf,256,1,fname); |
justinkim | 0:e01f64037748 | 254 | //Serial.write(VH); |
justinkim | 1:3a14a4c84db2 | 255 | myGLCD.LCD_Write_DATA(VH,VL); |
justinkim | 0:e01f64037748 | 256 | } |
justinkim | 1:3a14a4c84db2 | 257 | myGLCD.clrXY(); |
justinkim | 0:e01f64037748 | 258 | } |