Web Camera for mbed-os. When you use this program, we judge you have agreed to the following contents. https://developer.mbed.org/teams/Renesas/wiki/About-LICENSE

Dependencies:   HttpServer_snapshot_mbed-os LWIPBP3595Interface_STA_for_mbed-os RomRamBlockDevice mbed-rpc

Fork of GR-Boards_WebCamera by Renesas

このサンプルは 「GR-LYCHEE」ではじめる電子工作 で紹介しています。
出版時と内容が異ならないよう、各ライブラリはアップデートせずに使用してください。

このサンプルの最新バージョンは下記から入手できます。最新バージョンは本の内容と一部処理が異なります。
https://github.com/d-kato/GR-Boards_WebCamera

i2c_setting.cpp

Committer:
1050186
Date:
2015-10-23
Revision:
5:34d84609dd60
Child:
7:c45ecff1b44d

File content as of revision 5:34d84609dd60:

/*
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "mbed.h"
#include "i2c_setting.h"

I2C i2c(I2C_SDA, I2C_SCL);
Serial terminal(USBTX, USBRX);

static char recv_term_buffer[RECV_BUF_SIZE];
static char reg_req_buffer[REG_REQ_BUF_SIZE];
static char reg_arg_buf[ARG_MAX_NUM];
static char wr_data[DATA_MAX_SIZE];
static char rd_data[DATA_MAX_SIZE];
static char recv_data;
static int32_t term_buf_offset = 0;

static char hex_to_char_tbl[] = {
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'A', 'B', 'C', 'D', 'E', 'F'
};

static int str_to_hex(char * psrcbuf, char * pdestbuf, int cnt) {
    int retval = false;
    int32_t tmp_hex;

    if ((psrcbuf != NULL) && (pdestbuf != NULL)) {
        retval = true;
        if ((((int32_t)*psrcbuf) >= '0') && (((int32_t)*psrcbuf) <= '9')) {
            tmp_hex = NUM_STR_TO_HEX;
        } else if ((((int32_t)*psrcbuf) >= 'A') && (((int32_t)*psrcbuf) <= 'F')) {
            tmp_hex = BIG_STR_TO_HEX;
        } else if ((((int32_t)*psrcbuf) >= 'a') && (((int32_t)*psrcbuf) <= 'f')) {
            tmp_hex = SMA_STR_TO_HEX;
        } else {
            retval = false;
        }
        if (retval == true) {
            *pdestbuf += ((int32_t)*psrcbuf) - tmp_hex;
            if (cnt == 0) {
                *pdestbuf *= MASK_HEX10;
            }
        }
    }

    return retval;
}

static void char_to_16char(char * pdestbuf, char * psrcbuf, int length) {
    if ((pdestbuf != NULL) && (psrcbuf != NULL)) {
        while(length != 0) {
            *pdestbuf = hex_to_char_tbl[((int32_t)*psrcbuf) / MASK_HEX10];
            pdestbuf++;
            *pdestbuf = hex_to_char_tbl[((int32_t)*psrcbuf) % MASK_HEX10];
            pdestbuf++;

            psrcbuf++;
            length--;
        }
        *pdestbuf = CODE_NULL;
    }
}
static int receive_term(void) {
    int ret = false;

    recv_data = terminal.getc();
    /* echo back */
    printf("%c", recv_data);
    switch ((int32_t)recv_data) {
        case 0x0A :
            recv_term_buffer[term_buf_offset] = CODE_NULL;
            term_buf_offset = 0;
            ret = true;
        break;
        case 0x0D :
            /* Do Nothing */
        break;
        default :
            /* check data_buffer size  */
            if (term_buf_offset < RECV_BUF_SIZE) {
                recv_term_buffer[term_buf_offset] = recv_data;
                term_buf_offset++;
            }
        break;
    }

    return ret;

}

static int analysis_cmd(char * buf) {
    int  arg_cnt;
    int  byte_cnt;
    int  retval;
    char * psrcbuf;
    char * pdestbuf;

    arg_cnt = 0;
    byte_cnt = 0;
    /* get reg req */
    memset(&reg_req_buffer[0], 0, REG_REQ_BUF_SIZE);
    psrcbuf  = buf;
    pdestbuf = &reg_req_buffer[0];
    while (((int32_t)*psrcbuf) != ':') {
        if (byte_cnt >= REG_REQ_BUF_SIZE) {
            arg_cnt = DATA_ANALY_ERROR;
            goto err_out;
        } else {
            *pdestbuf = *psrcbuf;
            byte_cnt++;
            pdestbuf++;
            psrcbuf++;
        }
    }
    *pdestbuf = *psrcbuf;
    if ((strncmp(&reg_req_buffer[0], "Wr:", REG_REQ_BUF_SIZE) == 0) || (strncmp(&reg_req_buffer[0], "Rd:", REG_REQ_BUF_SIZE) == 0) ||
        (strncmp(&reg_req_buffer[0], "WrNoP:", REG_REQ_BUF_SIZE) == 0) || (strncmp(&reg_req_buffer[0], "RdNoP:", REG_REQ_BUF_SIZE) == 0)) {
        /* get argument(I2C addr, len, data1, data2, data3, ...) */
        byte_cnt = 0;
        memset(&reg_arg_buf[0], 0, sizeof(reg_arg_buf));
        psrcbuf++;
        while (((int32_t)*psrcbuf) != CODE_NULL) {
            retval = str_to_hex(psrcbuf, &reg_arg_buf[arg_cnt], byte_cnt);
            if (retval == true) {
                byte_cnt++;
                psrcbuf++;
                if (byte_cnt >= ARG_MAX_SIZE) {
                    if ((arg_cnt + 1) >= ARG_MAX_NUM) {
                        arg_cnt = DATA_ANALY_ERROR;
                        goto err_out;
                    } else {
                        arg_cnt++;
                        byte_cnt = 0;
                    }
                }
            } else {
                psrcbuf++;
            }
        }
    } else {
        arg_cnt = DATA_ANALY_ERROR;
        goto err_out;
    }
    
err_out:

    return arg_cnt;
}

static void execute_cmd(void) {
    char convert_data[(DATA_MAX_SIZE * 2) + NULL_SIZE];

    /* check length */
    if (reg_arg_buf[1] >= DATA_MAX_SIZE) {
        reg_arg_buf[1] = DATA_MAX_SIZE;
    }
    /* check request */
    if (strncmp(&reg_req_buffer[0], "Wr:", REG_REQ_BUF_SIZE) == 0) {
        /* write */
        memcpy(&wr_data[0], &reg_arg_buf[2], reg_arg_buf[1]);
        (void)i2c.write(reg_arg_buf[0], wr_data, reg_arg_buf[1], 0);    // write(I2C Addr, write data, len, restart flg(=0))
    } else if (strncmp(&reg_req_buffer[0], "Rd:", REG_REQ_BUF_SIZE) == 0) {
        /* read */
        memset(&rd_data[0], 0, reg_arg_buf[1]);
        (void)i2c.read(reg_arg_buf[0], rd_data, reg_arg_buf[1], 0);     // read(I2C Addr, read data, len, restart flg(=0))

        char_to_16char(&convert_data[0], &rd_data[0], reg_arg_buf[1]);
        DEBUGPRINT("    Read Data is %s\r\n", &convert_data[0]);
    } else if (strncmp(&reg_req_buffer[0], "WrNoP:", REG_REQ_BUF_SIZE) == 0) {
        /* write(non stop condition) */
        memcpy(&wr_data[0], &reg_arg_buf[2], reg_arg_buf[1]);
        (void)i2c.write(reg_arg_buf[0], wr_data, reg_arg_buf[1], 1);    // write(I2C Addr, write data, len, restart flg(=1))
    } else if (strncmp(&reg_req_buffer[0], "RdNoP:", REG_REQ_BUF_SIZE) == 0) {
        /* read(non stop condition) */
        memset(&rd_data[0], 0, reg_arg_buf[1]);
        (void)i2c.read(reg_arg_buf[0], rd_data, reg_arg_buf[1], 1);     // read(I2C Addr, read data, len, restart flg(=1))

        char_to_16char(&convert_data[0], &rd_data[0], reg_arg_buf[1]);
        DEBUGPRINT("    Read Data is %s\r\n", &convert_data[0]);
    }
}

void analy_and_exe(char * buf) {
    int reg_arg_cnt;

    /* analysis command */
    reg_arg_cnt = analysis_cmd(buf);
    if (reg_arg_cnt != DATA_ANALY_ERROR) {
        /* execute command */
        execute_cmd();
    }
}

void SetI2CfromTerm(void const *argument) {
    int ret;

    while (1) {
        ret = receive_term();
        if (ret == true) {
            /* command analysis and execute */
            analy_and_exe(&recv_term_buffer[0]);
        }
    }
}