Sample web camera program for GR-PEACH

Dependencies:   GR-PEACH_video GraphicsFramework HttpServer_snapshot_mbed-os LWIPBP3595Interface_STA_for_mbed-os R_BSP RomRamBlockDevice mbed-rpc

Committer:
tuthai
Date:
Mon Aug 07 06:43:34 2017 +0000
Revision:
2:82e09e910229
Parent:
0:4edfb8401555
Update to mbed-os 5.4.7

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tuthai 0:4edfb8401555 1 /*
tuthai 0:4edfb8401555 2 Permission is hereby granted, free of charge, to any person obtaining a copy
tuthai 0:4edfb8401555 3 of this software and associated documentation files (the "Software"), to deal
tuthai 0:4edfb8401555 4 in the Software without restriction, including without limitation the rights
tuthai 0:4edfb8401555 5 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
tuthai 0:4edfb8401555 6 copies of the Software, and to permit persons to whom the Software is
tuthai 0:4edfb8401555 7 furnished to do so, subject to the following conditions:
tuthai 0:4edfb8401555 8
tuthai 0:4edfb8401555 9 The above copyright notice and this permission notice shall be included in
tuthai 0:4edfb8401555 10 all copies or substantial portions of the Software.
tuthai 0:4edfb8401555 11
tuthai 0:4edfb8401555 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
tuthai 0:4edfb8401555 13 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
tuthai 0:4edfb8401555 14 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
tuthai 0:4edfb8401555 15 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
tuthai 0:4edfb8401555 16 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
tuthai 0:4edfb8401555 17 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
tuthai 0:4edfb8401555 18 THE SOFTWARE.
tuthai 0:4edfb8401555 19 */
tuthai 0:4edfb8401555 20
tuthai 0:4edfb8401555 21 #include "mbed.h"
tuthai 0:4edfb8401555 22 #include "i2c_setting.h"
tuthai 0:4edfb8401555 23
tuthai 0:4edfb8401555 24 #define REG_REQ_BUF_SIZE (7)
tuthai 0:4edfb8401555 25 #define DATA_MAX_SIZE (32)
tuthai 0:4edfb8401555 26 #define ARG_MAX_NUM (DATA_MAX_SIZE + 3) // Reqest, I2C addr, len, data1, data2, data3, ...
tuthai 0:4edfb8401555 27 #define ARG_MAX_SIZE (2) // upper bit + lower bit
tuthai 0:4edfb8401555 28 #define NULL_SIZE (1)
tuthai 0:4edfb8401555 29 #define CODE_NULL (0x00)
tuthai 0:4edfb8401555 30 #define NUM_STR_TO_HEX (0x30)
tuthai 0:4edfb8401555 31 #define BIG_STR_TO_HEX (0x37)
tuthai 0:4edfb8401555 32 #define SMA_STR_TO_HEX (0x57)
tuthai 0:4edfb8401555 33 #define MASK_HEX10 (0x10)
tuthai 0:4edfb8401555 34
tuthai 0:4edfb8401555 35 #define OFS_REQ (0)
tuthai 0:4edfb8401555 36 #define OFS_I2C_ADDR (1)
tuthai 0:4edfb8401555 37 #define OFS_DATA_SIZE (2)
tuthai 0:4edfb8401555 38 #define OFS_DATA (3)
tuthai 0:4edfb8401555 39
tuthai 0:4edfb8401555 40 #define STR_WR "Wr:"
tuthai 0:4edfb8401555 41 #define STR_RD "Rd:"
tuthai 0:4edfb8401555 42 #define STR_WR_NO_P "WrNoP:"
tuthai 0:4edfb8401555 43 #define STR_RD_NO_P "RdNoP:"
tuthai 0:4edfb8401555 44
tuthai 0:4edfb8401555 45 #define REQ_NONE (0)
tuthai 0:4edfb8401555 46 #define REQ_WR (1)
tuthai 0:4edfb8401555 47 #define REQ_RD (2)
tuthai 0:4edfb8401555 48 #define REQ_WR_NO_P (3)
tuthai 0:4edfb8401555 49 #define REQ_RD_NO_P (4)
tuthai 0:4edfb8401555 50
tuthai 0:4edfb8401555 51 I2C i2c(I2C_SDA, I2C_SCL);
tuthai 0:4edfb8401555 52
tuthai 0:4edfb8401555 53 static char hex_to_char_tbl[] = {
tuthai 0:4edfb8401555 54 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
tuthai 0:4edfb8401555 55 'A', 'B', 'C', 'D', 'E', 'F'
tuthai 0:4edfb8401555 56 };
tuthai 0:4edfb8401555 57
tuthai 0:4edfb8401555 58 static int str_to_hex(char * psrcbuf, char * pdestbuf, int cnt) {
tuthai 0:4edfb8401555 59 int retval = false;
tuthai 0:4edfb8401555 60 int32_t tmp_hex;
tuthai 0:4edfb8401555 61
tuthai 0:4edfb8401555 62 if ((psrcbuf != NULL) && (pdestbuf != NULL)) {
tuthai 0:4edfb8401555 63 retval = true;
tuthai 0:4edfb8401555 64 if ((((int32_t)*psrcbuf) >= '0') && (((int32_t)*psrcbuf) <= '9')) {
tuthai 0:4edfb8401555 65 tmp_hex = NUM_STR_TO_HEX;
tuthai 0:4edfb8401555 66 } else if ((((int32_t)*psrcbuf) >= 'A') && (((int32_t)*psrcbuf) <= 'F')) {
tuthai 0:4edfb8401555 67 tmp_hex = BIG_STR_TO_HEX;
tuthai 0:4edfb8401555 68 } else if ((((int32_t)*psrcbuf) >= 'a') && (((int32_t)*psrcbuf) <= 'f')) {
tuthai 0:4edfb8401555 69 tmp_hex = SMA_STR_TO_HEX;
tuthai 0:4edfb8401555 70 } else {
tuthai 0:4edfb8401555 71 retval = false;
tuthai 0:4edfb8401555 72 }
tuthai 0:4edfb8401555 73 if (retval == true) {
tuthai 0:4edfb8401555 74 *pdestbuf += ((int32_t)*psrcbuf) - tmp_hex;
tuthai 0:4edfb8401555 75 if (cnt == 0) {
tuthai 0:4edfb8401555 76 *pdestbuf *= MASK_HEX10;
tuthai 0:4edfb8401555 77 }
tuthai 0:4edfb8401555 78 }
tuthai 0:4edfb8401555 79 }
tuthai 0:4edfb8401555 80
tuthai 0:4edfb8401555 81 return retval;
tuthai 0:4edfb8401555 82 }
tuthai 0:4edfb8401555 83
tuthai 0:4edfb8401555 84 static void char_to_16char(char * pdestbuf, char * psrcbuf, int length) {
tuthai 0:4edfb8401555 85 if ((pdestbuf != NULL) && (psrcbuf != NULL)) {
tuthai 0:4edfb8401555 86 while (1) {
tuthai 0:4edfb8401555 87 *pdestbuf = hex_to_char_tbl[((int32_t)*psrcbuf) / MASK_HEX10];
tuthai 0:4edfb8401555 88 pdestbuf++;
tuthai 0:4edfb8401555 89 *pdestbuf = hex_to_char_tbl[((int32_t)*psrcbuf) % MASK_HEX10];
tuthai 0:4edfb8401555 90 pdestbuf++;
tuthai 0:4edfb8401555 91
tuthai 0:4edfb8401555 92 psrcbuf++;
tuthai 0:4edfb8401555 93 length--;
tuthai 0:4edfb8401555 94 if (length != 0) {
tuthai 0:4edfb8401555 95 *pdestbuf = ',';
tuthai 0:4edfb8401555 96 pdestbuf++;
tuthai 0:4edfb8401555 97 } else {
tuthai 0:4edfb8401555 98 break;
tuthai 0:4edfb8401555 99 }
tuthai 0:4edfb8401555 100 }
tuthai 0:4edfb8401555 101 *pdestbuf = CODE_NULL;
tuthai 0:4edfb8401555 102 }
tuthai 0:4edfb8401555 103 }
tuthai 0:4edfb8401555 104
tuthai 0:4edfb8401555 105 static int analysis_cmd(char * buf, char * p_reg_arg_buf) {
tuthai 0:4edfb8401555 106 int arg_cnt = 0;
tuthai 0:4edfb8401555 107 int byte_cnt = 0;
tuthai 0:4edfb8401555 108 int retval;
tuthai 0:4edfb8401555 109 char * psrcbuf = buf;
tuthai 0:4edfb8401555 110 int ret;
tuthai 0:4edfb8401555 111
tuthai 0:4edfb8401555 112 if (strncmp(psrcbuf, STR_WR, sizeof(STR_WR) - 1) == 0) {
tuthai 0:4edfb8401555 113 ret = REQ_WR;
tuthai 0:4edfb8401555 114 psrcbuf += sizeof(STR_WR) - 1;
tuthai 0:4edfb8401555 115 } else if (strncmp(psrcbuf, STR_RD, sizeof(STR_RD) - 1) == 0) {
tuthai 0:4edfb8401555 116 ret = REQ_RD;
tuthai 0:4edfb8401555 117 psrcbuf += sizeof(STR_RD) - 1;
tuthai 0:4edfb8401555 118 } else if (strncmp(psrcbuf, STR_WR_NO_P, sizeof(STR_WR_NO_P) - 1) == 0) {
tuthai 0:4edfb8401555 119 ret = REQ_WR_NO_P;
tuthai 0:4edfb8401555 120 psrcbuf += sizeof(STR_WR_NO_P) - 1;
tuthai 0:4edfb8401555 121 } else if (strncmp(psrcbuf, STR_RD_NO_P, sizeof(STR_RD_NO_P) - 1) == 0) {
tuthai 0:4edfb8401555 122 ret = REQ_RD_NO_P;
tuthai 0:4edfb8401555 123 psrcbuf += sizeof(STR_RD_NO_P) - 1;
tuthai 0:4edfb8401555 124 } else {
tuthai 0:4edfb8401555 125 ret = REQ_NONE;
tuthai 0:4edfb8401555 126 }
tuthai 0:4edfb8401555 127
tuthai 0:4edfb8401555 128 if (ret != REQ_NONE) {
tuthai 0:4edfb8401555 129 /* get argument(Reqest, I2C addr, len, data1, data2, data3, ...) */
tuthai 0:4edfb8401555 130 p_reg_arg_buf[arg_cnt] = ret;
tuthai 0:4edfb8401555 131 arg_cnt++;
tuthai 0:4edfb8401555 132 byte_cnt = 0;
tuthai 0:4edfb8401555 133 while (((int32_t)*psrcbuf) != CODE_NULL) {
tuthai 0:4edfb8401555 134 retval = str_to_hex(psrcbuf, &p_reg_arg_buf[arg_cnt], byte_cnt);
tuthai 0:4edfb8401555 135 if (retval != false) {
tuthai 0:4edfb8401555 136 byte_cnt++;
tuthai 0:4edfb8401555 137 if (byte_cnt >= ARG_MAX_SIZE) {
tuthai 0:4edfb8401555 138 if ((arg_cnt + 1) >= ARG_MAX_NUM) {
tuthai 0:4edfb8401555 139 ret = REQ_NONE;
tuthai 0:4edfb8401555 140 break;
tuthai 0:4edfb8401555 141 } else {
tuthai 0:4edfb8401555 142 arg_cnt++;
tuthai 0:4edfb8401555 143 byte_cnt = 0;
tuthai 0:4edfb8401555 144 }
tuthai 0:4edfb8401555 145 }
tuthai 0:4edfb8401555 146 }
tuthai 0:4edfb8401555 147 psrcbuf++;
tuthai 0:4edfb8401555 148 }
tuthai 0:4edfb8401555 149 }
tuthai 0:4edfb8401555 150
tuthai 0:4edfb8401555 151 return ret;
tuthai 0:4edfb8401555 152 }
tuthai 0:4edfb8401555 153
tuthai 0:4edfb8401555 154 static void execute_cmd(char * buf, char * p_reg_arg_buf) {
tuthai 0:4edfb8401555 155 int ret;
tuthai 0:4edfb8401555 156 size_t len;
tuthai 0:4edfb8401555 157 int stop = 0;
tuthai 0:4edfb8401555 158
tuthai 0:4edfb8401555 159 /* check request */
tuthai 0:4edfb8401555 160 if ((p_reg_arg_buf[OFS_REQ] == REQ_WR_NO_P) || (p_reg_arg_buf[OFS_REQ] == REQ_RD_NO_P)) {
tuthai 0:4edfb8401555 161 stop = 1;
tuthai 0:4edfb8401555 162 }
tuthai 0:4edfb8401555 163
tuthai 0:4edfb8401555 164 switch (p_reg_arg_buf[OFS_REQ]) {
tuthai 0:4edfb8401555 165 case REQ_WR:
tuthai 0:4edfb8401555 166 case REQ_WR_NO_P:
tuthai 0:4edfb8401555 167 ret = i2c.write(p_reg_arg_buf[OFS_I2C_ADDR], &p_reg_arg_buf[OFS_DATA], p_reg_arg_buf[OFS_DATA_SIZE], stop);
tuthai 0:4edfb8401555 168 if (ret == 0) {
tuthai 0:4edfb8401555 169 sprintf(buf, "OK");
tuthai 0:4edfb8401555 170 }
tuthai 0:4edfb8401555 171 break;
tuthai 0:4edfb8401555 172 case REQ_RD:
tuthai 0:4edfb8401555 173 case REQ_RD_NO_P:
tuthai 0:4edfb8401555 174 ret = i2c.read(p_reg_arg_buf[OFS_I2C_ADDR], &p_reg_arg_buf[OFS_DATA], p_reg_arg_buf[OFS_DATA_SIZE], stop);
tuthai 0:4edfb8401555 175 if (ret == 0) {
tuthai 0:4edfb8401555 176 sprintf(buf, "OK ");
tuthai 0:4edfb8401555 177 len = strlen(buf);
tuthai 0:4edfb8401555 178 char_to_16char(&buf[len], &p_reg_arg_buf[OFS_DATA], p_reg_arg_buf[OFS_DATA_SIZE]);
tuthai 0:4edfb8401555 179 }
tuthai 0:4edfb8401555 180 break;
tuthai 0:4edfb8401555 181 default:
tuthai 0:4edfb8401555 182 case REQ_NONE:
tuthai 0:4edfb8401555 183 ret = -1;
tuthai 0:4edfb8401555 184 break;
tuthai 0:4edfb8401555 185 }
tuthai 0:4edfb8401555 186 if (ret != 0) {
tuthai 0:4edfb8401555 187 sprintf(buf, "NG");
tuthai 0:4edfb8401555 188 }
tuthai 0:4edfb8401555 189 }
tuthai 0:4edfb8401555 190
tuthai 0:4edfb8401555 191 bool i2c_setting_exe(char * buf) {
tuthai 0:4edfb8401555 192 int reg_arg_cnt;
tuthai 0:4edfb8401555 193 char reg_arg_buf[ARG_MAX_NUM] = {0};
tuthai 0:4edfb8401555 194
tuthai 0:4edfb8401555 195 /* analysis command */
tuthai 0:4edfb8401555 196 reg_arg_cnt = analysis_cmd(buf, reg_arg_buf);
tuthai 0:4edfb8401555 197 if (reg_arg_cnt != REQ_NONE) {
tuthai 0:4edfb8401555 198 /* check length */
tuthai 0:4edfb8401555 199 if (reg_arg_buf[OFS_DATA_SIZE] >= DATA_MAX_SIZE) {
tuthai 0:4edfb8401555 200 reg_arg_buf[OFS_DATA_SIZE] = DATA_MAX_SIZE;
tuthai 0:4edfb8401555 201 }
tuthai 0:4edfb8401555 202 /* execute command */
tuthai 0:4edfb8401555 203 execute_cmd(buf, reg_arg_buf);
tuthai 0:4edfb8401555 204 return true;
tuthai 0:4edfb8401555 205 }
tuthai 0:4edfb8401555 206
tuthai 0:4edfb8401555 207 return false;
tuthai 0:4edfb8401555 208 }
tuthai 0:4edfb8401555 209
tuthai 0:4edfb8401555 210 #if(0) /* Please enable this line when performing the setting from the Terminal side. */
tuthai 0:4edfb8401555 211 Serial terminal(USBTX, USBRX);
tuthai 0:4edfb8401555 212 static char recv_term_buffer[I2C_SETTING_STR_BUF_SIZE];
tuthai 0:4edfb8401555 213
tuthai 0:4edfb8401555 214 void SetI2CfromTerm(void const *argument) {
tuthai 0:4edfb8401555 215 int32_t term_buf_offset = 0;
tuthai 0:4edfb8401555 216 char recv_data;
tuthai 0:4edfb8401555 217
tuthai 0:4edfb8401555 218 while (1) {
tuthai 0:4edfb8401555 219 recv_data = terminal.getc();
tuthai 0:4edfb8401555 220 /* echo back */
tuthai 0:4edfb8401555 221 printf("%c", recv_data);
tuthai 0:4edfb8401555 222 switch ((int32_t)recv_data) {
tuthai 0:4edfb8401555 223 case 0x0A :
tuthai 0:4edfb8401555 224 recv_term_buffer[term_buf_offset] = CODE_NULL;
tuthai 0:4edfb8401555 225 term_buf_offset = 0;
tuthai 0:4edfb8401555 226 /* command analysis and execute */
tuthai 0:4edfb8401555 227 if (i2c_setting_exe(recv_term_buffer) != false) {
tuthai 0:4edfb8401555 228 terminal.puts(recv_term_buffer);
tuthai 0:4edfb8401555 229 }
tuthai 0:4edfb8401555 230 break;
tuthai 0:4edfb8401555 231 case 0x0D :
tuthai 0:4edfb8401555 232 /* Do Nothing */
tuthai 0:4edfb8401555 233 break;
tuthai 0:4edfb8401555 234 default :
tuthai 0:4edfb8401555 235 /* check data_buffer size */
tuthai 0:4edfb8401555 236 if (term_buf_offset < I2C_SETTING_STR_BUF_SIZE) {
tuthai 0:4edfb8401555 237 recv_term_buffer[term_buf_offset] = recv_data;
tuthai 0:4edfb8401555 238 term_buf_offset++;
tuthai 0:4edfb8401555 239 }
tuthai 0:4edfb8401555 240 break;
tuthai 0:4edfb8401555 241 }
tuthai 0:4edfb8401555 242 }
tuthai 0:4edfb8401555 243 }
tuthai 0:4edfb8401555 244 #endif
tuthai 0:4edfb8401555 245