Skip to main content

Storage

Storage

SD, USB as storage, using the Mbed OS file class.


Example

Save the photo in the storage, then read the saved photo and send it to USB0.


#include <Arduino.h>
#include <Camera.h>
#include <RTC.h>
#include <DisplayApp.h>
#include "SdUsbConnect.h"
    
#define IMAGE_HW 640
#define IMAGE_VW 480
    
Camera camera(IMAGE_HW, IMAGE_VW); // CMOS Camera
DisplayApp display_app;
SdUsbConnect storage("storage");
RTC rtc; // for time stamp
    
void setup() {
    Serial.begin(9600);
    pinMode(PIN_LED_RED, OUTPUT);
    pinMode(PIN_LED_GREEN, OUTPUT);
    pinMode(PIN_LED_YELLOW, OUTPUT);
    
    Serial.println("Test starts");
    
    // RTC
    rtc.begin();
    rtc.setDateTime(2017, 10, 14, 15, 40, 0);
    
    // Camera
    camera.begin();
    
    // SD & USB
    Serial.print("Finding strage..");
    storage.wait_connect();
    Serial.println("done");
    
}
    
void loop() {
    FILE * wp = fopen("/storage/lychee_camera.jpg", "w");
    if (wp != NULL) {
        digitalWrite(PIN_LED_YELLOW, HIGH);
        size_t size = camera.createJpeg();
        fwrite(camera.getJpegAdr(), sizeof(char), (int) size, wp);
        fclose(wp);
        digitalWrite(PIN_LED_YELLOW, LOW);
    } else {
        Serial.print("Not found jpg");
        digitalWrite(PIN_LED_RED, HIGH);
        delay(1);
    }
    
    FILE * fp = fopen("/storage/lychee_camera.jpg", "r");
    if (fp != NULL) {
        digitalWrite(PIN_LED_GREEN, HIGH);
        fseek(fp, 0, SEEK_END);
        uint32_t jpegSize = ftell(fp);
        fseek(fp, 0, SEEK_SET);
        uint8_t * buffer = (uint8_t*) malloc(jpegSize);
        for (uint32_t i = 0; i 	< jpegSize; i++) {
            buffer[i] = (uint8_t) fgetc(fp);
        }
        display_app.SendJpeg(buffer, jpegSize);
        fclose(fp);
        digitalWrite(PIN_LED_GREEN, LOW);
    } else {
        Serial.print("Not found jpg");
        digitalWrite(PIN_LED_RED, HIGH);
        delay(1);
    }
}
}