티스토리 뷰

OS/micro:bit

micro:bit - Camera (LS-Y201) image writing to SD

파란크리스마스 2018. 6. 17. 13:02
728x90

출처

Camera (LS-Y201)

micro:bit를 이용해서 카메라로 입력받은 이미지 데이터를 SD 메모리에 저장해보았습니다.

소스

//#include "MicroBit.h"
//#include "MicroBitSerial.h"
#include "Camera_LS_Y201.h"
#include "SDFileSystem.h"

#define DEBMSG      printf
#define NEWLINE()   printf("\r\n")
#define FILENAME    "/sd/IMG_%04d.jpg"

SDFileSystem sd(P0_21, P0_22, P0_23, P0_16, "sd"); // mosi, miso, sclk, cs
Camera_LS_Y201 cam1(P0_2, P0_3);
//MicroBitSerial serial(USBTX, USBRX);

typedef struct work {
    FILE *fp;
} work_t;
 
work_t work;
 
/**
 * Callback function for readJpegFileContent.
 *
 * @param buf A pointer to a buffer.
 * @param siz A size of the buffer.
 */
void callback_func(int done, int total, uint8_t *buf, size_t siz) {
    fwrite(buf, siz, 1, work.fp);
 
    static int n = 0;
    int tmp = done * 100 / total;
    if (n != tmp) {
        n = tmp;
        DEBMSG("Writing...: %3d%%", n);
        NEWLINE();
    }
}
 
/**
 * Capture.
 *
 * @param cam A pointer to a camera object.
 * @param filename The file name.
 *
 * @return Return 0 if it succeed.
 */
int capture(Camera_LS_Y201 *cam, char *filename) {
    /*
     * Take a picture.
     */
    if (cam->takePicture() != 0) {
        return -1;
    }
    DEBMSG("Captured.");
    NEWLINE();
 
    /*
     * Open file.
     */
    work.fp = fopen(filename, "wb");
    if (work.fp == NULL) {
        return -2;
    }
 
    /*
     * Read the content.
     */
    DEBMSG("%s", filename);
    NEWLINE();
    if (cam->readJpegFileContent(callback_func) != 0) {
        fclose(work.fp);
        return -3;
    }
    fclose(work.fp);
 
    /*
     * Stop taking pictures.
     */
    cam->stopTakingPictures();
 
    return 0;
}

int main() {
	  DEBMSG("Camera module");
	  NEWLINE();
	  DEBMSG("Resetting...");
	  NEWLINE();
	  wait(1);

	  if (cam1.reset() == 0) {
	      DEBMSG("Reset OK.");
	      NEWLINE();
	  } else {
	      DEBMSG("Reset fail.");
	      NEWLINE();
	      error("Reset fail.");
	  }
	  wait(1);

	  int cnt = 0;
	  while (1) {
	      char fname[64];
	      snprintf(fname, sizeof(fname) - 1, FILENAME, cnt);
	      int r = capture(&cam1, fname);
	      if (r == 0) {
	          DEBMSG("[%04d]:OK.", cnt);
	          NEWLINE();
	      } else {
	          DEBMSG("[%04d]:NG. (code=%d)", cnt, r);
	          NEWLINE();
	          error("Failure.");
	      }
	      cnt++;
	  }
}

HEX 파일

pulse-combined.zip

실행

댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함