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