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

Files at this revision

API Documentation at this revision

Comitter:
thesane
Date:
Mon Oct 13 20:54:24 2014 +0000
Commit message:
modify JPEG camera to send image over serial port/bluetooth module instead of writing it to USB drive

Changed in this revision

JPEGCamera.cpp Show annotated file Show diff for this revision Revisions of this file
JPEGCamera.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 4df5706ea8d9 JPEGCamera.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/JPEGCamera.cpp	Mon Oct 13 20:54:24 2014 +0000
@@ -0,0 +1,136 @@
+/* Arduino JPEGCamera Library
+ * Copyright 2010 SparkFun Electronic
+ * Written by Ryan Owens
+ * Modified by arms22
+ * Ported to mbed by yamaguch
+ * Modified by thesane
+ */
+
+#include "JPEGCamera.h"
+
+#define min(x, y) ((x) < (y)) ? (x) : (y)
+
+const int RESPONSE_TIMEOUT = 500;
+const int DATA_TIMEOUT = 1000;
+
+JPEGCamera::JPEGCamera(PinName tx, PinName rx) : Serial(tx, rx) {
+    baud(38400);
+    state = READY;
+}
+
+bool JPEGCamera::setPictureSize(JPEGCamera::PictureSize size, bool doReset) {
+    char buf[9] = {0x56, 0x00, 0x31, 0x05, 0x04, 0x01, 0x00, 0x19, (char) size};
+    int ret = sendReceive(buf, sizeof buf, 5);
+
+    if (ret == 5 && buf[0] == 0x76) {
+        if (doReset)
+            reset();
+        return true;
+    } else
+        return false;
+}
+
+bool JPEGCamera::isReady() {
+    return state == READY;
+}
+
+bool JPEGCamera::isProcessing() {
+    return state == PROCESSING;
+}
+
+bool JPEGCamera::takePicture() {
+    if (state == READY) {
+            if (takePicture_int()) {
+                imageSize = getImageSize();
+                address = 0;
+                state = PROCESSING;
+            } else {
+                state = ERROR;
+            }
+        
+    }
+    return state != ERROR;
+}
+
+bool JPEGCamera::processPicture(Serial &control) {
+    if (state == PROCESSING) {
+        if (address < imageSize) {
+            char data[1024];
+            int size = readData(data, min(sizeof(data), imageSize - address), address);
+            for(int i = 0;i<size;i++)
+                control.putc(data[i]);
+            address += size;
+            if (address >= imageSize) {
+                stopPictures();
+                wait(0.1); 
+                state = READY;
+            }
+        }
+    }
+
+    return state == PROCESSING || state == READY;
+}
+
+bool JPEGCamera::reset() {
+    char buf[4] = {0x56, 0x00, 0x26, 0x00};
+    int ret = sendReceive(buf, sizeof buf, 4);
+    if (ret == 4 && buf[0] == 0x76) {
+        wait(4.0);
+        state = READY;
+    } else {
+        state = ERROR;
+    }
+    return state == READY;
+}
+
+bool JPEGCamera::takePicture_int() {
+    char buf[5] = {0x56, 0x00, 0x36, 0x01, 0x00};
+    int ret = sendReceive(buf, sizeof buf, 5);
+
+    return ret == 5 && buf[0] == 0x76;
+}
+
+bool JPEGCamera::stopPictures() {
+    char buf[5] = {0x56, 0x00, 0x36, 0x01, 0x03};
+    int ret = sendReceive(buf, sizeof buf, 5);
+
+    return ret == 4 && buf[0] == 0x76;
+}
+
+int JPEGCamera::getImageSize() {
+    char buf[9] = {0x56, 0x00, 0x34, 0x01, 0x00};
+    int ret = sendReceive(buf, sizeof buf, 9);
+
+    //The size is in the last 2 characters of the response.
+    return (ret == 9 && buf[0] == 0x76) ? (buf[7] << 8 | buf[8]) : 0;
+}
+
+int JPEGCamera::readData(char *dataBuf, int size, int address) {
+    char buf[16] = {0x56, 0x00, 0x32, 0x0C, 0x00, 0x0A, 0x00, 0x00,
+                    address >> 8, address & 255, 0x00, 0x00, size >> 8, size & 255, 0x00, 0x0A
+                   };
+    int ret = sendReceive(buf, sizeof buf, 5);
+
+    return (ret == 5 && buf[0] == 0x76) ? receive(dataBuf, size, DATA_TIMEOUT) : 0;
+}
+
+int JPEGCamera::sendReceive(char *buf, int sendSize, int receiveSize) {
+    while (readable()) getc();
+
+    for (int i = 0; i < sendSize; i++) putc(buf[i]);
+
+    return receive(buf, receiveSize, RESPONSE_TIMEOUT);
+}
+
+int JPEGCamera::receive(char *buf, int size, int timeout) {
+    timer.start();
+    timer.reset();
+
+    int i = 0;
+    while (i < size && timer.read_ms() < timeout) {
+        if (readable())
+            buf[i++] = getc();
+    }
+
+    return i;
+}
\ No newline at end of file
diff -r 000000000000 -r 4df5706ea8d9 JPEGCamera.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/JPEGCamera.h	Mon Oct 13 20:54:24 2014 +0000
@@ -0,0 +1,123 @@
+/* Arduino JPEGCamera Library
+ * Copyright 2010 SparkFun Electronics
+ * Written by Ryan Owens
+ * Modified by arms22
+ * Ported to mbed by yamaguch
+ * Modified by thesane
+ */
+
+#ifndef JPEG_CAMERA_H
+#define JPEG_CAMERA_H
+
+#include "mbed.h"
+
+/**
+ * Interface for LinkSprite JPEG Camera module LS-Y201
+ */
+class JPEGCamera : public Serial {
+public:
+    /***/
+    enum PictureSize {
+        SIZE160x120 = 0x22,
+        SIZE320x240 = 0x11,
+        SIZE640x480 = 0x00,
+    };
+
+    /**
+     * Create JPEG Camera
+     *
+     * @param tx tx pin
+     * @param rx rx pin
+     */
+    JPEGCamera(PinName tx, PinName rx);
+
+    /**
+     * Set picture size
+     *
+     * @param size picture size (available sizes are SIZE160x120, SIZE320x240, SIZE640x480)
+     * @param doReset flag to perform reset operation after changing size
+     *
+     * @returns true if succeeded, false otherwise
+     */
+    bool setPictureSize(JPEGCamera::PictureSize size, bool doReset = true);
+
+    /**
+     * Return whether camera is ready or not
+     *
+     * @returns true if ready, false otherwise
+     */
+    bool isReady();
+
+    /**
+     * Return whether camera is processing the taken picture or not
+     *
+     * @returns true if the camera is in processing, false otherwise
+     */
+    bool isProcessing();
+
+    /**
+     * Take a picture
+     *
+     * @param filename filename to store the picture data
+     * @returns true if succeeded, false otherwise
+     */
+    bool takePicture();
+    /**
+     * Process picture (writing the file)
+     *
+     * @param address of serial port used to transmit the image
+     * @returns true if no error in processing, false otherwise
+     */
+    bool processPicture(Serial &control);
+
+    /**
+     * Perform reset oepration (it takes 4 seconds)
+     *
+     * @returns true if succeeded, false otherwise
+     */
+    bool reset();
+
+    /**
+     * Send a picture command to the camera module
+     *
+     *  @returns true if succeeded, false otherwise
+     */
+    bool takePicture_int(void);
+
+    /**
+     * Send a stop pictures command to the camera module
+     *
+     *  @returns true if succeeded, false otherwise
+     */
+    bool stopPictures(void);
+
+    /**
+     * Get the picture image size
+     *
+     * @returns the actual image size in bytes
+     */
+    int getImageSize();
+
+    /**
+     * Read the picture data to the buffer
+     *
+     * @param dataBuf data buffer address to store the received data
+     * @param size data size to read
+     * @param address the address of the picture data to read
+     *
+     * @returns the size of the data read
+     */
+    int readData(char *dataBuf, int size, int address);
+
+//private:
+    Timer timer;
+    FILE *fp;
+    int imageSize;
+    int address;
+    enum State {UNKNOWN, READY, PROCESSING, ERROR = -1} state;
+    
+    int sendReceive(char *buf, int sendSize, int receiveSize);
+    int receive(char *buf, int size, int timeout);
+};
+
+#endif