The "GR-PEACH_Audio_Playback_7InchLCD_Sample" is a sample code that can provides high-resolution audio playback of FLAC format files. It also allows the user to audio-playback control functions such as play, pause, and stop by manipulating key switches.

Dependencies:   GR-PEACH_video R_BSP TLV320_RBSP USBHost_custom

Fork of GR-PEACH_Audio_Playback_Sample by Renesas

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers key_cmd.cpp Source File

key_cmd.cpp

00001 /*******************************************************************************
00002 * DISCLAIMER
00003 * This software is supplied by Renesas Electronics Corporation and is only
00004 * intended for use with Renesas products. No other uses are authorized. This
00005 * software is owned by Renesas Electronics Corporation and is protected under
00006 * all applicable laws, including copyright laws.
00007 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
00008 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
00009 * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
00010 * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
00011 * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
00012 * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
00013 * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
00014 * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
00015 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
00016 * Renesas reserves the right, without notice, to make changes to this software
00017 * and to discontinue the availability of this software. By using this software,
00018 * you agree to the additional terms and conditions found by accessing the
00019 * following link:
00020 * http://www.renesas.com/disclaimer*
00021 * Copyright (C) 2015 Renesas Electronics Corporation. All rights reserved.
00022 *******************************************************************************/
00023 
00024 #include "mbed.h"
00025 #include "rtos.h"
00026 #include "misratypes.h"
00027 
00028 #include "key_cmd.h"
00029 #include "system.h"
00030 #include "display.h"
00031 
00032 /*--- Macro definition ---*/
00033 #define CHR_BS              '\b'        /* 0x08: BACKSPACE */
00034 #define CHR_LF              '\n'        /* 0x0A: LINE FEED */
00035 #define CHR_CR              '\r'        /* 0x0D: CARRIAGE RETURN */
00036 #define CHR_SPACE           ' '         /* 0x20: SPACE */
00037 #define PRINT_CHR_MIN       CHR_SPACE   /* Minimum numeric value of printable character. */
00038 #define PRINT_CHR_MAX       '~'         /* Maximum numeric value of printable character. */
00039 
00040 /* Command Name */
00041 #define CMD_STOP            "STOP"      /* Stop */
00042 #define CMD_PLAYPAUSE       "PLAYPAUSE" /* Play / Pause */
00043 #define CMD_NEXT            "NEXT"      /* Next track */
00044 #define CMD_PREV            "PREV"      /* Previous track */
00045 #define CMD_PLAYINFO        "PLAYINFO"  /* Play info */
00046 #define CMD_REPEAT          "REPEAT"    /* Repeat */
00047 #define CMD_HELP            "HELP"      /* Help */
00048 
00049 #define VALID_CMD_NUM       (7u)
00050 
00051 #define MAX_CNT_OF_ARG      (1u)
00052 
00053 #define MSG_UNKNOWN_CMD     "command not found"
00054 
00055 /*--- User defined types ---*/
00056 typedef struct {
00057     char_t          argv[MAX_CNT_OF_ARG][CMD_INPUT_MAX_LEN];
00058     uint32_t        argc;
00059 } split_str_t;
00060 
00061 static Serial pc_in(USBTX, USBRX);
00062 
00063 static void clear_input_string(cmd_ctrl_t * const p);
00064 static bool read_data(cmd_ctrl_t * const p);
00065 static bool split_input_string(split_str_t * const p,
00066                         const char_t * const p_inp_str, const uint32_t inp_len);
00067 static SYS_KeyCode parse_input_string(const split_str_t * const p);
00068 
00069 void cmd_init_proc(cmd_ctrl_t * const p_ctrl)
00070 {
00071     if (p_ctrl != NULL) {
00072         pc_in.baud(DSP_PC_COM_BAUDRATE);
00073         clear_input_string(p_ctrl);
00074     }
00075 }
00076 
00077 SYS_KeyCode cmd_main_proc(cmd_ctrl_t * const p_ctrl)
00078 {
00079     SYS_KeyCode     key_ev = SYS_KEYCODE_NON;
00080     split_str_t     split;
00081     bool            result;
00082     
00083     if (p_ctrl != NULL) {
00084         result = read_data(p_ctrl);
00085         if (result == true) {
00086             /* Decided the input character string from command-line. */
00087             /* Splits the input character string in argument. */
00088             result = split_input_string(&split, p_ctrl->inp_str, p_ctrl->inp_len);
00089             if (result == true) {
00090                 key_ev = parse_input_string(&split);
00091                 if (key_ev == SYS_KEYCODE_NON) {
00092                     /* The input character string is unknown command. */
00093                     (void) dsp_notify_print_string(MSG_UNKNOWN_CMD);
00094                 }
00095             }
00096             clear_input_string(p_ctrl);
00097         }
00098     }
00099     return key_ev;
00100 }
00101 
00102 /** Clears the data of the input character string
00103  *
00104  *  @param p Pointer to the control data of command-line module.
00105  */
00106 static void clear_input_string(cmd_ctrl_t * const p)
00107 {
00108     if (p != NULL) {
00109         p->inp_str[0] = '\0';
00110         p->inp_len = 0u;
00111     }
00112 }
00113 
00114 /** Reads the input character string from command-line
00115  *
00116  *  @param p Pointer to the control data of command-line module.
00117  *
00118  *  @returns 
00119  *    Results of process. true is success. false is failure.
00120  */
00121 static bool read_data(cmd_ctrl_t * const p)
00122 {
00123     bool            ret = false;
00124     int32_t         c;
00125 
00126     if (p != NULL) {
00127         if (pc_in.readable() == true) {
00128             c = pc_in.getc();
00129             if ((PRINT_CHR_MIN <= c) && (c <= PRINT_CHR_MAX)) {
00130                 /* The character of "c" variable can print.  */
00131                 /* Checks the length except the null terminal character. */
00132                 if (p->inp_len < ((sizeof(p->inp_str)/sizeof(p->inp_str[0])) - 1u)) {
00133                     p->inp_str[p->inp_len] = (char_t)c;
00134                     p->inp_len++;
00135                     p->inp_str[p->inp_len] = '\0';
00136                 } else {
00137                     /* Because buffer is full, "c" variable is canceled. */
00138                 }
00139             } else {
00140                 /* The character of "c" variable can not print.  */
00141                 if ((c == CHR_CR) || (c == CHR_LF) || (c == '\0')) {
00142                     /* Detected the end of the input from command-line. */
00143                     ret = true;
00144                 } else if (c == CHR_BS) {
00145                     /* Deletes one character. */
00146                     if (p->inp_len > 0u) {
00147                         p->inp_len--;
00148                         p->inp_str[p->inp_len] = '\0';
00149                     }
00150                 } else {
00151                     /* DO NOTHING */
00152                 }
00153             }
00154             /* Sends the input character string to display thread. */
00155             (void) dsp_notify_input_string(p->inp_str, ret);
00156         }
00157     }
00158     return ret;
00159 }
00160 
00161 /** Splits the input character string in argument
00162  *
00163  *  @param p Pointer to the structure to store the argument data.
00164  *  @param p_inp_str Pointer to the input character string.
00165  *  @param inp_len Length of the input character string. 
00166  *                 (Excepting the null terminal character.)
00167  *
00168  *  @returns 
00169  *    Results of process. true is success. false is failure.
00170  */
00171 static bool split_input_string(split_str_t * const p,
00172                         const char_t * const p_inp_str, const uint32_t inp_len)
00173 {
00174     bool            ret = false;
00175     uint32_t        arg_cnt;
00176     uint32_t        chr_cnt;
00177     uint32_t        i;
00178     
00179     if ((p != NULL) && (p_inp_str != NULL) && (inp_len > 0u)) {
00180         arg_cnt = 0u;
00181         chr_cnt = 0u;
00182         /* Checks the input character string. */
00183         /* (Including the null terminal character.) */
00184         for (i = 0u; i < (inp_len + 1u); i++) {
00185             /* Deletes the white space. */
00186             if (((int32_t)p_inp_str[i] == CHR_SPACE) || 
00187                 ((int32_t)p_inp_str[i] == '\0')) {
00188                 if (chr_cnt > 0u) {
00189                     /* Detects the white space after the string. */
00190                     arg_cnt++;
00191                     chr_cnt = 0u;
00192                 }
00193             } else {
00194                 /* Checks the length except the null terminal character. */
00195                 if (chr_cnt < (sizeof(p->argv[0]) - 1u)) {
00196                     if (arg_cnt < (sizeof(p->argv)/sizeof(p->argv[0]))) {
00197                         p->argv[arg_cnt][chr_cnt] = p_inp_str[i];
00198                         p->argv[arg_cnt][chr_cnt + 1u] = '\0';
00199                     }
00200                     chr_cnt++;
00201                 }
00202             }
00203         }
00204         if ((arg_cnt > 0u) && 
00205             (arg_cnt <= (sizeof(p->argv)/sizeof(p->argv[0])))) {
00206             p->argc = arg_cnt;
00207             ret = true;
00208         } else {
00209             p->argc = 0u;
00210         }
00211     }
00212     return ret;
00213 }
00214 
00215 /** Parses the input character string using the argument data
00216  *
00217  *  @param p Pointer to the structure of the argument data.
00218  *
00219  *  @returns 
00220  *    Key code.
00221  */
00222 static SYS_KeyCode parse_input_string(const split_str_t * const p)
00223 {
00224     SYS_KeyCode         key_ret = SYS_KEYCODE_NON;
00225     const char_t        *p_str;
00226     uint32_t            max_len;
00227     uint32_t            i;
00228     struct {
00229         const char_t    *p_cmd;
00230         SYS_KeyCode     key_ev;
00231     } static const cmd_list[VALID_CMD_NUM] = {
00232         {   CMD_STOP,       SYS_KEYCODE_STOP        },
00233         {   CMD_PLAYPAUSE,  SYS_KEYCODE_PLAYPAUSE   },
00234         {   CMD_NEXT,       SYS_KEYCODE_NEXT        },
00235         {   CMD_PREV,       SYS_KEYCODE_PREV        },
00236         {   CMD_PLAYINFO,   SYS_KEYCODE_PLAYINFO    },
00237         {   CMD_REPEAT,     SYS_KEYCODE_REPEAT      },
00238         {   CMD_HELP,       SYS_KEYCODE_HELP        }
00239     };
00240 
00241     if (p != NULL) {
00242         if (p->argc == MAX_CNT_OF_ARG) {
00243             p_str = p->argv[0];
00244             max_len = sizeof(p->argv[0])/sizeof(p->argv[0][0]);
00245             for (i = 0u; (i < VALID_CMD_NUM) && (key_ret == SYS_KEYCODE_NON); i++) {
00246                 if (strncasecmp(cmd_list[i].p_cmd, p_str, max_len) == 0) {
00247                     key_ret = cmd_list[i].key_ev;
00248                 }
00249             }
00250         }
00251     }
00252     return key_ret;
00253 }