画像を2枚撮影

Committer:
yamaguch
Date:
Tue Nov 15 07:56:40 2011 +0000
Revision:
0:974923b4db2d
Child:
1:27417b6a951d
initial creation, ported from an Arduino library by arms22

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yamaguch 0:974923b4db2d 1 /* Arduino JPEGCamera Library
yamaguch 0:974923b4db2d 2 * Copyright 2010 SparkFun Electronic
yamaguch 0:974923b4db2d 3 * Written by Ryan Owens
yamaguch 0:974923b4db2d 4 * Modified by arms22
yamaguch 0:974923b4db2d 5 * Ported to mbed by yamaguch
yamaguch 0:974923b4db2d 6 */
yamaguch 0:974923b4db2d 7
yamaguch 0:974923b4db2d 8 #include "JPEGCamera.h"
yamaguch 0:974923b4db2d 9
yamaguch 0:974923b4db2d 10 #define min(x, y) ((x) < (y)) ? (x) : (y)
yamaguch 0:974923b4db2d 11
yamaguch 0:974923b4db2d 12 const int RESPONSE_TIMEOUT = 500;
yamaguch 0:974923b4db2d 13 const int DATA_TIMEOUT = 1000;
yamaguch 0:974923b4db2d 14
yamaguch 0:974923b4db2d 15 JPEGCamera::JPEGCamera(PinName tx, PinName rx) : Serial(tx, rx) {
yamaguch 0:974923b4db2d 16 baud(38400);
yamaguch 0:974923b4db2d 17 state = READY;
yamaguch 0:974923b4db2d 18 }
yamaguch 0:974923b4db2d 19
yamaguch 0:974923b4db2d 20 bool JPEGCamera::setPictureSize(JPEGCamera::PictureSize size, bool doReset) {
yamaguch 0:974923b4db2d 21 char buf[9] = {0x56, 0x00, 0x31, 0x05, 0x04, 0x01, 0x00, 0x19, (char) size};
yamaguch 0:974923b4db2d 22 int ret = sendReceive(buf, sizeof buf, 5);
yamaguch 0:974923b4db2d 23
yamaguch 0:974923b4db2d 24 if (ret == 5 && buf[0] == 0x76) {
yamaguch 0:974923b4db2d 25 if (doReset)
yamaguch 0:974923b4db2d 26 reset();
yamaguch 0:974923b4db2d 27 return true;
yamaguch 0:974923b4db2d 28 } else
yamaguch 0:974923b4db2d 29 return false;
yamaguch 0:974923b4db2d 30 }
yamaguch 0:974923b4db2d 31
yamaguch 0:974923b4db2d 32 bool JPEGCamera::isReady() {
yamaguch 0:974923b4db2d 33 return state == READY;
yamaguch 0:974923b4db2d 34 }
yamaguch 0:974923b4db2d 35
yamaguch 0:974923b4db2d 36 bool JPEGCamera::isProcessing() {
yamaguch 0:974923b4db2d 37 return state == PROCESSING;
yamaguch 0:974923b4db2d 38 }
yamaguch 0:974923b4db2d 39
yamaguch 0:974923b4db2d 40 bool JPEGCamera::takePicture(char *filename) {
yamaguch 0:974923b4db2d 41 if (state == READY) {
yamaguch 0:974923b4db2d 42 fp = fopen(filename, "wb");
yamaguch 0:974923b4db2d 43 if (fp != 0) {
yamaguch 0:974923b4db2d 44 if (takePicture()) {
yamaguch 0:974923b4db2d 45 imageSize = getImageSize();
yamaguch 0:974923b4db2d 46 address = 0;
yamaguch 0:974923b4db2d 47 state = PROCESSING;
yamaguch 0:974923b4db2d 48 } else {
yamaguch 0:974923b4db2d 49 fclose(fp);
yamaguch 0:974923b4db2d 50 printf("takePicture(%s) failed", filename);
yamaguch 0:974923b4db2d 51 state = ERROR;
yamaguch 0:974923b4db2d 52 }
yamaguch 0:974923b4db2d 53 } else {
yamaguch 0:974923b4db2d 54 printf("fopen() failed");
yamaguch 0:974923b4db2d 55 state = ERROR;
yamaguch 0:974923b4db2d 56 }
yamaguch 0:974923b4db2d 57 }
yamaguch 0:974923b4db2d 58 return state != ERROR;
yamaguch 0:974923b4db2d 59 }
yamaguch 0:974923b4db2d 60
yamaguch 0:974923b4db2d 61 bool JPEGCamera::processPicture() {
yamaguch 0:974923b4db2d 62 if (state == PROCESSING) {
yamaguch 0:974923b4db2d 63 if (address < imageSize) {
yamaguch 0:974923b4db2d 64 char data[1024];
yamaguch 0:974923b4db2d 65 int size = readData(data, min(sizeof(data), imageSize - address), address);
yamaguch 0:974923b4db2d 66 int ret = fwrite(data, size, 1, fp);
yamaguch 0:974923b4db2d 67 if (ret > 0)
yamaguch 0:974923b4db2d 68 address += size;
yamaguch 0:974923b4db2d 69 if (ret == 0 || address >= imageSize) {
yamaguch 0:974923b4db2d 70 stopPictures();
yamaguch 0:974923b4db2d 71 fclose(fp);
yamaguch 0:974923b4db2d 72 wait(0.1); // ????
yamaguch 0:974923b4db2d 73 state = ret > 0 ? READY : ERROR;
yamaguch 0:974923b4db2d 74 }
yamaguch 0:974923b4db2d 75 }
yamaguch 0:974923b4db2d 76 }
yamaguch 0:974923b4db2d 77
yamaguch 0:974923b4db2d 78 return state == PROCESSING || state == READY;
yamaguch 0:974923b4db2d 79 }
yamaguch 0:974923b4db2d 80
yamaguch 0:974923b4db2d 81 bool JPEGCamera::reset() {
yamaguch 0:974923b4db2d 82 char buf[4] = {0x56, 0x00, 0x26, 0x00};
yamaguch 0:974923b4db2d 83 int ret = sendReceive(buf, sizeof buf, 4);
yamaguch 0:974923b4db2d 84 if (ret == 4 && buf[0] == 0x76) {
yamaguch 0:974923b4db2d 85 wait(4.0);
yamaguch 0:974923b4db2d 86 state = READY;
yamaguch 0:974923b4db2d 87 } else {
yamaguch 0:974923b4db2d 88 state = ERROR;
yamaguch 0:974923b4db2d 89 }
yamaguch 0:974923b4db2d 90 return state == READY;
yamaguch 0:974923b4db2d 91 }
yamaguch 0:974923b4db2d 92
yamaguch 0:974923b4db2d 93 bool JPEGCamera::takePicture() {
yamaguch 0:974923b4db2d 94 char buf[5] = {0x56, 0x00, 0x36, 0x01, 0x00};
yamaguch 0:974923b4db2d 95 int ret = sendReceive(buf, sizeof buf, 5);
yamaguch 0:974923b4db2d 96
yamaguch 0:974923b4db2d 97 return ret == 5 && buf[0] == 0x76;
yamaguch 0:974923b4db2d 98 }
yamaguch 0:974923b4db2d 99
yamaguch 0:974923b4db2d 100 bool JPEGCamera::stopPictures() {
yamaguch 0:974923b4db2d 101 char buf[5] = {0x56, 0x00, 0x36, 0x01, 0x03};
yamaguch 0:974923b4db2d 102 int ret = sendReceive(buf, sizeof buf, 5);
yamaguch 0:974923b4db2d 103
yamaguch 0:974923b4db2d 104 return ret == 4 && buf[0] == 0x76;
yamaguch 0:974923b4db2d 105 }
yamaguch 0:974923b4db2d 106
yamaguch 0:974923b4db2d 107 int JPEGCamera::getImageSize() {
yamaguch 0:974923b4db2d 108 char buf[9] = {0x56, 0x00, 0x34, 0x01, 0x00};
yamaguch 0:974923b4db2d 109 int ret = sendReceive(buf, sizeof buf, 9);
yamaguch 0:974923b4db2d 110
yamaguch 0:974923b4db2d 111 //The size is in the last 2 characters of the response.
yamaguch 0:974923b4db2d 112 return (ret == 9 && buf[0] == 0x76) ? (buf[7] << 8 | buf[8]) : 0;
yamaguch 0:974923b4db2d 113 }
yamaguch 0:974923b4db2d 114
yamaguch 0:974923b4db2d 115 int JPEGCamera::readData(char *dataBuf, int size, int address) {
yamaguch 0:974923b4db2d 116 char buf[16] = {0x56, 0x00, 0x32, 0x0C, 0x00, 0x0A, 0x00, 0x00,
yamaguch 0:974923b4db2d 117 address >> 8, address & 255, 0x00, 0x00, size >> 8, size & 255, 0x00, 0x0A
yamaguch 0:974923b4db2d 118 };
yamaguch 0:974923b4db2d 119 int ret = sendReceive(buf, sizeof buf, 5);
yamaguch 0:974923b4db2d 120
yamaguch 0:974923b4db2d 121 return (ret == 5 && buf[0] == 0x76) ? receive(dataBuf, size, DATA_TIMEOUT) : 0;
yamaguch 0:974923b4db2d 122 }
yamaguch 0:974923b4db2d 123
yamaguch 0:974923b4db2d 124 int JPEGCamera::sendReceive(char *buf, int sendSize, int receiveSize) {
yamaguch 0:974923b4db2d 125 while (readable()) getc();
yamaguch 0:974923b4db2d 126
yamaguch 0:974923b4db2d 127 for (int i = 0; i < sendSize; i++) putc(buf[i]);
yamaguch 0:974923b4db2d 128
yamaguch 0:974923b4db2d 129 return receive(buf, receiveSize, RESPONSE_TIMEOUT);
yamaguch 0:974923b4db2d 130 }
yamaguch 0:974923b4db2d 131
yamaguch 0:974923b4db2d 132 int JPEGCamera::receive(char *buf, int size, int timeout) {
yamaguch 0:974923b4db2d 133 timer.start();
yamaguch 0:974923b4db2d 134 timer.reset();
yamaguch 0:974923b4db2d 135
yamaguch 0:974923b4db2d 136 int i = 0;
yamaguch 0:974923b4db2d 137 while (i < size && timer.read_ms() < timeout) {
yamaguch 0:974923b4db2d 138 if (readable())
yamaguch 0:974923b4db2d 139 buf[i++] = getc();
yamaguch 0:974923b4db2d 140 }
yamaguch 0:974923b4db2d 141
yamaguch 0:974923b4db2d 142 return i;
yamaguch 0:974923b4db2d 143 }
yamaguch 0:974923b4db2d 144
yamaguch 0:974923b4db2d 145 #if 0
yamaguch 0:974923b4db2d 146 void takePicture(char *filename) {
yamaguch 0:974923b4db2d 147 FILE *fp = fopen(filename, "wb");
yamaguch 0:974923b4db2d 148 if (fp != 0) {
yamaguch 0:974923b4db2d 149 if (camera.takePicture()) {
yamaguch 0:974923b4db2d 150 leds[3] = 1;
yamaguch 0:974923b4db2d 151 led.setColor(BLUE);
yamaguch 0:974923b4db2d 152 led = 1;
yamaguch 0:974923b4db2d 153 int imageSize = camera.getImageSize();
yamaguch 0:974923b4db2d 154 for (int size, address = 0; address < imageSize; address += size) {
yamaguch 0:974923b4db2d 155 char data[1024];
yamaguch 0:974923b4db2d 156 size = camera.readData(data, sizeof(data), address);
yamaguch 0:974923b4db2d 157 fwrite(data, size, 1, fp);
yamaguch 0:974923b4db2d 158 leds[3] = !leds[3];
yamaguch 0:974923b4db2d 159 led = !led;
yamaguch 0:974923b4db2d 160 }
yamaguch 0:974923b4db2d 161 camera.stopPictures();
yamaguch 0:974923b4db2d 162 wait(0.1);
yamaguch 0:974923b4db2d 163 fclose(fp);
yamaguch 0:974923b4db2d 164 leds[3] = 0;
yamaguch 0:974923b4db2d 165 led = 0;
yamaguch 0:974923b4db2d 166 } else
yamaguch 0:974923b4db2d 167 printf("takePicture(%s) failed", filename);
yamaguch 0:974923b4db2d 168 } else
yamaguch 0:974923b4db2d 169 printf("fopen() failed");
yamaguch 0:974923b4db2d 170 }
yamaguch 0:974923b4db2d 171 #endif