7 years, 12 months ago.

2MP LS-Y201 is not work with mbed. why?

hello everyone, i have a problem with using another version of LS-Y201 jpeg camera. i bought the 2MP LS-Y201, similar with usual LS-Y201 jpeg camera, but it has a higher resolution. i have worked with usual LS-Y201 camera, by using the codes from Shiniciro Nakamura and Hiroshi Yamaguchi, and it worked. but, strangely, after i changed the usual camera into 2MP LS-Y201 camera, and of course i did some changes, follows its datasheet, and it did not work. the result that i always got was "reset fail". i have tried both codes, Shiniciro and Hiroshi. is there any idea to solve my problem?

thank you

This is the datasheet https://s3.amazonaws.com/linksprite/camera/JPEG_camera_uartinterface_TTL/JPEG_Camera_2M_Manual.pdf

These are the main.cpp and its library

main.cpp

#include "mbed.h"
#include "JPEGCamera.h"

Serial pc(USBTX, USBRX);        //set serial comm w/ pc
JPEGCamera camera(p13, p14);    //set camera pinout
LocalFileSystem local("local"); //set local system memory
Timer timer;                    //set time counter

//////////-----Camera Function-----//////////
void CamFunc() {
    timer.start();
    camera.setPictureSize(JPEGCamera::SIZE640x480);

    for (int i = 0; i < 2; i++) {
        if (camera.isReady()) {
            char filename[32];
            sprintf(filename, "/local/pict%03d.jpg", i);
            printf("Picture: %s ", filename);
            if (camera.takePicture(filename)) {
                while (camera.isProcessing()) {
                    camera.processPicture();
                }
            } else {
                printf("take picture failed\n");
            }
        } else {
            printf("camera is not ready\n");
        }
    }
    printf("time = %f\n", timer.read());
}

//////////-----Main Program-----//////////
int main() {
    char comm;
    if(pc.readable()){
        comm=pc.getc();
        if(comm=='a'){    
        CamFunc();
        }
    }
}

JPEGcamera.cpp

/* Arduino JPEGCamera Library
 * Copyright 2010 SparkFun Electronic
 * Written by Ryan Owens
 * Modified by arms22
 * Ported to mbed by yamaguch
 */

#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[5] = {0x56, 0x00, 0x54, 0x01, (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(char *filename) {
    if (state == READY) {
        fp = fopen(filename, "wb");
        if (fp != 0) {
            if (takePicture()) {
                imageSize = getImageSize();
                address = 0;
                state = PROCESSING;
            } else {
                fclose(fp);
                printf("takePicture(%s) failed", filename);
                state = ERROR;
            }
        } else {
            printf("fopen() failed");
            state = ERROR;
        }
    }
    return state != ERROR;
}

bool JPEGCamera::processPicture() {
    if (state == PROCESSING) {
        if (address < imageSize) {
            char data[1024];
            int size = readData(data, min(sizeof(data), imageSize - address), address);
            int ret = fwrite(data, size, 1, fp);
            if (ret > 0)
                address += size;
            if (ret == 0 || address >= imageSize) {
                stopPictures();
                fclose(fp);
                wait(0.1); // ????
                state = ret > 0 ? READY : ERROR;
            }
        }
    }

    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() {
    char buf[5] = {0x56, 0x00, 0x36, 0x01, 0x00};
    int ret = sendReceive(buf, sizeof buf, 5);

    return ret == 4 && buf[0] == 0x76;
}

bool JPEGCamera::stopPictures() {
    char buf[5] = {0x56, 0x00, 0x36, 0x01, 0x03};
    int ret = sendReceive(buf, sizeof buf, 5);

    return ret == 5 && 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;
}

1 Answer

6 years, 5 months ago.

i have the same problem do you resolve?