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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers i2c_setting.cpp Source File

i2c_setting.cpp

00001 /*
00002 Permission is hereby granted, free of charge, to any person obtaining a copy
00003 of this software and associated documentation files (the "Software"), to deal
00004 in the Software without restriction, including without limitation the rights
00005 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00006 copies of the Software, and to permit persons to whom the Software is
00007 furnished to do so, subject to the following conditions:
00008 
00009 The above copyright notice and this permission notice shall be included in
00010 all copies or substantial portions of the Software.
00011 
00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00013 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00014 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00015 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00016 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00018 THE SOFTWARE.
00019 */
00020 
00021 #include "mbed.h"
00022 #include "i2c_setting.h"
00023 
00024 #define REG_REQ_BUF_SIZE        (7)
00025 #define DATA_MAX_SIZE           (32)
00026 #define ARG_MAX_NUM             (DATA_MAX_SIZE + 3)      // Reqest, I2C addr, len, data1, data2, data3, ...
00027 #define ARG_MAX_SIZE            (2)                      // upper bit + lower bit
00028 #define NULL_SIZE               (1)
00029 #define CODE_NULL               (0x00)
00030 #define NUM_STR_TO_HEX          (0x30)
00031 #define BIG_STR_TO_HEX          (0x37)
00032 #define SMA_STR_TO_HEX          (0x57)
00033 #define MASK_HEX10              (0x10)
00034 
00035 #define OFS_REQ                 (0)
00036 #define OFS_I2C_ADDR            (1)
00037 #define OFS_DATA_SIZE           (2)
00038 #define OFS_DATA                (3)
00039 
00040 #define STR_WR                  "Wr:"
00041 #define STR_RD                  "Rd:"
00042 #define STR_WR_NO_P             "WrNoP:"
00043 #define STR_RD_NO_P             "RdNoP:"
00044 
00045 #define REQ_NONE                (0)
00046 #define REQ_WR                  (1)
00047 #define REQ_RD                  (2)
00048 #define REQ_WR_NO_P             (3)
00049 #define REQ_RD_NO_P             (4)
00050 
00051 I2C i2c(I2C_SDA, I2C_SCL);
00052 
00053 static char hex_to_char_tbl[] = {
00054     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
00055     'A', 'B', 'C', 'D', 'E', 'F'
00056 };
00057 
00058 static int str_to_hex(char * psrcbuf, char * pdestbuf, int cnt) {
00059     int retval = false;
00060     int32_t tmp_hex;
00061 
00062     if ((psrcbuf != NULL) && (pdestbuf != NULL)) {
00063         retval = true;
00064         if ((((int32_t)*psrcbuf) >= '0') && (((int32_t)*psrcbuf) <= '9')) {
00065             tmp_hex = NUM_STR_TO_HEX;
00066         } else if ((((int32_t)*psrcbuf) >= 'A') && (((int32_t)*psrcbuf) <= 'F')) {
00067             tmp_hex = BIG_STR_TO_HEX;
00068         } else if ((((int32_t)*psrcbuf) >= 'a') && (((int32_t)*psrcbuf) <= 'f')) {
00069             tmp_hex = SMA_STR_TO_HEX;
00070         } else {
00071             retval = false;
00072         }
00073         if (retval == true) {
00074             *pdestbuf += ((int32_t)*psrcbuf) - tmp_hex;
00075             if (cnt == 0) {
00076                 *pdestbuf *= MASK_HEX10;
00077             }
00078         }
00079     }
00080 
00081     return retval;
00082 }
00083 
00084 static void char_to_16char(char * pdestbuf, char * psrcbuf, int length) {
00085     if ((pdestbuf != NULL) && (psrcbuf != NULL)) {
00086         while (1) {
00087             *pdestbuf = hex_to_char_tbl[((int32_t)*psrcbuf) / MASK_HEX10];
00088             pdestbuf++;
00089             *pdestbuf = hex_to_char_tbl[((int32_t)*psrcbuf) % MASK_HEX10];
00090             pdestbuf++;
00091 
00092             psrcbuf++;
00093             length--;
00094             if (length != 0) {
00095                 *pdestbuf = ',';
00096                 pdestbuf++;
00097             } else {
00098                 break;
00099             }
00100         }
00101         *pdestbuf = CODE_NULL;
00102     }
00103 }
00104 
00105 static int analysis_cmd(char * buf, char * p_reg_arg_buf) {
00106     int  arg_cnt = 0;
00107     int  byte_cnt = 0;
00108     int  retval;
00109     char * psrcbuf = buf;
00110     int  ret;
00111 
00112     if (strncmp(psrcbuf, STR_WR, sizeof(STR_WR) - 1) == 0) {
00113         ret = REQ_WR;
00114         psrcbuf += sizeof(STR_WR) - 1;
00115     } else if (strncmp(psrcbuf, STR_RD, sizeof(STR_RD) - 1) == 0) {
00116         ret = REQ_RD;
00117         psrcbuf += sizeof(STR_RD) - 1;
00118     } else if (strncmp(psrcbuf, STR_WR_NO_P, sizeof(STR_WR_NO_P) - 1) == 0) {
00119         ret = REQ_WR_NO_P;
00120         psrcbuf += sizeof(STR_WR_NO_P) - 1;
00121     } else if (strncmp(psrcbuf, STR_RD_NO_P, sizeof(STR_RD_NO_P) - 1) == 0) {
00122         ret = REQ_RD_NO_P;
00123         psrcbuf += sizeof(STR_RD_NO_P) - 1;
00124     } else {
00125         ret = REQ_NONE;
00126     }
00127 
00128     if (ret != REQ_NONE) {
00129         /* get argument(Reqest, I2C addr, len, data1, data2, data3, ...) */
00130         p_reg_arg_buf[arg_cnt] = ret;
00131         arg_cnt++;
00132         byte_cnt = 0;
00133         while (((int32_t)*psrcbuf) != CODE_NULL) {
00134             retval = str_to_hex(psrcbuf, &p_reg_arg_buf[arg_cnt], byte_cnt);
00135             if (retval != false) {
00136                 byte_cnt++;
00137                 if (byte_cnt >= ARG_MAX_SIZE) {
00138                     if ((arg_cnt + 1) >= ARG_MAX_NUM) {
00139                         ret = REQ_NONE;
00140                         break;
00141                     } else {
00142                         arg_cnt++;
00143                         byte_cnt = 0;
00144                     }
00145                 }
00146             }
00147             psrcbuf++;
00148         }
00149     }
00150 
00151     return ret;
00152 }
00153 
00154 static void execute_cmd(char * buf, char * p_reg_arg_buf) {
00155     int ret;
00156     size_t len;
00157     int stop = 0;
00158 
00159     /* check request */
00160     if ((p_reg_arg_buf[OFS_REQ] == REQ_WR_NO_P) || (p_reg_arg_buf[OFS_REQ] == REQ_RD_NO_P)) {
00161         stop = 1;
00162     }
00163 
00164     switch (p_reg_arg_buf[OFS_REQ]) {
00165         case REQ_WR:
00166         case REQ_WR_NO_P:
00167             ret = i2c.write(p_reg_arg_buf[OFS_I2C_ADDR], &p_reg_arg_buf[OFS_DATA], p_reg_arg_buf[OFS_DATA_SIZE], stop);
00168             if (ret == 0) {
00169                 sprintf(buf, "OK");
00170             }
00171         break;
00172         case REQ_RD:
00173         case REQ_RD_NO_P:
00174             ret = i2c.read(p_reg_arg_buf[OFS_I2C_ADDR], &p_reg_arg_buf[OFS_DATA], p_reg_arg_buf[OFS_DATA_SIZE], stop);
00175             if (ret == 0) {
00176                 sprintf(buf, "OK ");
00177                 len = strlen(buf);
00178                 char_to_16char(&buf[len], &p_reg_arg_buf[OFS_DATA], p_reg_arg_buf[OFS_DATA_SIZE]);
00179             }
00180         break;
00181         default:
00182         case REQ_NONE:
00183             ret = -1;
00184         break;
00185     }
00186     if (ret != 0) {
00187         sprintf(buf, "NG");
00188     }
00189 }
00190 
00191 bool i2c_setting_exe(char * buf) {
00192     int reg_arg_cnt;
00193     char reg_arg_buf[ARG_MAX_NUM] = {0};
00194 
00195     /* analysis command */
00196     reg_arg_cnt = analysis_cmd(buf, reg_arg_buf);
00197     if (reg_arg_cnt != REQ_NONE) {
00198         /* check length */
00199         if (reg_arg_buf[OFS_DATA_SIZE] >= DATA_MAX_SIZE) {
00200             reg_arg_buf[OFS_DATA_SIZE] = DATA_MAX_SIZE;
00201         }
00202         /* execute command */
00203         execute_cmd(buf, reg_arg_buf);
00204         return true;
00205     }
00206 
00207     return false;
00208 }
00209 
00210 #if(0) /* Please enable this line when performing the setting from the Terminal side. */
00211 Serial terminal(USBTX, USBRX);
00212 static char recv_term_buffer[I2C_SETTING_STR_BUF_SIZE];
00213 
00214 void SetI2CfromTerm(void const *argument) {
00215     int32_t term_buf_offset = 0;
00216     char recv_data;
00217 
00218     while (1) {
00219         recv_data = terminal.getc();
00220         /* echo back */
00221         printf("%c", recv_data);
00222         switch ((int32_t)recv_data) {
00223             case 0x0A :
00224                 recv_term_buffer[term_buf_offset] = CODE_NULL;
00225                 term_buf_offset = 0;
00226                 /* command analysis and execute */
00227                 if (i2c_setting_exe(recv_term_buffer) != false) {
00228                     terminal.puts(recv_term_buffer);
00229                 }
00230             break;
00231             case 0x0D :
00232                 /* Do Nothing */
00233             break;
00234             default :
00235                 /* check data_buffer size  */
00236                 if (term_buf_offset < I2C_SETTING_STR_BUF_SIZE) {
00237                     recv_term_buffer[term_buf_offset] = recv_data;
00238                     term_buf_offset++;
00239                 }
00240             break;
00241         }
00242     }
00243 }
00244 #endif
00245