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