Bluetooth JPEG camera library with test program build over the the original library for LS-Y201 LinkSprite JPEG Camera

Dependents:   JPEGCameraTest JPEGCamera_SIM808_STM32F401RE JPEGCamera_SIM808_MPU9150_STM32F401RE

Committer:
thesane
Date:
Mon Oct 13 20:54:24 2014 +0000
Revision:
0:4df5706ea8d9
modify JPEG camera to send image over serial port/bluetooth module instead of writing it to USB drive

Who changed what in which revision?

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