1 /* 2 * Author: Jon Trulson <jtrulson (at) ics.com> 3 * Copyright (c) 2015 Intel Corporation. 4 * 5 * Thanks to Seeed Studio for a working arduino sketch 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining 8 * a copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sublicense, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be 16 * included in all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 */ 26 #pragma once 27 28 #include <string> 29 #include <iostream> 30 31 #include <stdint.h> 32 #include <stdlib.h> 33 #include <unistd.h> 34 #include <string.h> 35 #include <fcntl.h> 36 #include <errno.h> 37 #include <termios.h> 38 #include <sys/time.h> 39 #include <sys/select.h> 40 #include <sys/types.h> 41 #include <sys/stat.h> 42 43 #include <mraa/uart.h> 44 45 #define GROVESCAM_DEFAULT_UART 0 46 47 #define GROVESCAM_DEFAULT_CAMERA_ADDR 0 48 49 namespace upm { 50 /** 51 * @brief Grove Serial Camera library 52 * @defgroup grovescam libupm-grovescam 53 * @ingroup seeed uart other 54 */ 55 56 /** 57 * @library grovescam 58 * @sensor grovescam 59 * @comname Grove Serial Camera 60 * @type other 61 * @man seeed 62 * @con uart 63 * @web http://www.seeedstudio.com/wiki/Grove_-_Serial_Camera_Kit 64 * 65 * @brief API for the Grove Serial Camera 66 * 67 * The driver was tested with the Grove Serial Camera. There is 68 * no protocol documentation currently available, so this module 69 * was developed based completely on the Seeed Studio* Arduino* 70 * sketch. 71 * 72 * It is connected via a UART at 115,200 baud. 73 * 74 * @image html grovescam.jpg 75 * @snippet grovescam.cxx Interesting 76 */ 77 78 class GROVESCAM { 79 public: 80 81 static const unsigned int MAX_PKT_LEN = 128; 82 83 typedef enum { 84 FORMAT_VGA = 7, // 640x480 85 FORMAT_CIF = 5, // 352288 86 FORMAT_OCIF = 3 // ??? (maybe they meant QCIF?) 87 } PIC_FORMATS_T; 88 89 /** 90 * Grove Serial Camera constructor 91 * 92 * @param uart Default UART to use (0 or 1) 93 * @param camAddr 3-bit address identifier of the camera; default is 0 94 */ 95 GROVESCAM(int uart, uint8_t camAddr=GROVESCAM_DEFAULT_CAMERA_ADDR); 96 97 /** 98 * GROVESCAM destructor 99 */ 100 ~GROVESCAM(); 101 102 /** 103 * Checks to see if there is data available for reading 104 * 105 * @param millis Number of milliseconds to wait; 0 means no waiting. 106 * @return True if there is data available for reading 107 */ 108 bool dataAvailable(unsigned int millis); 109 110 /** 111 * Reads any available data into a user-supplied buffer. Note: the 112 * call blocks until data is available to be read. Use 113 * dataAvailable() to determine whether there is data available 114 * beforehand, to avoid blocking. 115 * 116 * @param buffer Buffer to hold the data read 117 * @param len Length of the buffer 118 * @return Number of bytes read 119 */ 120 int readData(uint8_t *buffer, int len); 121 122 /** 123 * Writes the data in the buffer to the device 124 * 125 * @param buffer Buffer to hold the data read 126 * @param len Length of the buffer 127 * @return Number of bytes written 128 */ 129 int writeData(uint8_t *buffer, int len); 130 131 /** 132 * Sets up proper tty I/O modes and the baud rate. For this device, the default 133 * baud rate is 9,600 (B9600). 134 * 135 * @param baud Desired baud rate 136 * @return True if successful 137 */ 138 bool setupTty(speed_t baud=B115200); 139 140 /** 141 * Reads serial input and discards until no more characters are available 142 * 143 */ 144 void drainInput(); 145 146 /** 147 * Initializes the camera 148 * 149 */ 150 bool init(); 151 152 /** 153 * Tells the camera to prepare for a capture 154 * 155 * @param fmt One of the PIC_FORMATS_T values 156 */ 157 bool preCapture(PIC_FORMATS_T fmt=FORMAT_VGA); 158 159 /** 160 * Starts the capture 161 * 162 * @return True if successful 163 */ 164 bool doCapture(); 165 166 /** 167 * Stores the captured image in a file 168 * 169 * @param fname Name of the file to write 170 * @return True if successful 171 */ 172 bool storeImage(const char *fname); 173 174 /** 175 * Returns the picture length. Note: this is only valid after 176 * doCapture() has run successfully. 177 * 178 * @return Image length 179 */ 180 int getImageSize() { return m_picTotalLen; }; 181 182 protected: 183 int ttyFd() { return m_ttyFd; }; 184 185 private: 186 mraa_uart_context m_uart; 187 int m_ttyFd; 188 189 uint8_t m_camAddr; 190 int m_picTotalLen; 191 }; 192 } 193 194 195