Renesas / Mbed OS GR-PEACH_Audio_Playback_Sample

Dependencies:   R_BSP TLV320_RBSP USBHost_custom

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers key.cpp Source File

key.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.h"
00029 #include "key_cmd.h"
00030 #include "system.h"
00031 
00032 /*--- Macro definition of key thread ---*/
00033 #define PROC_CYCLE_SW       (10u)   /* The process cycle of SW module */
00034 #define PROC_CYCLE_TFT      (50u)   /* The process cycle of TFT module */
00035 #define PROC_CYCLE_CMD      (2u)    /* The process cycle of command-line module */
00036 #define PROC_CYCLE_REFRESH  (50u)   /* Refresh cycle of counter */
00037 #define UNIT_TIME_MS        (2u)
00038 
00039 #define PROC_CNT_SW         (PROC_CYCLE_SW / UNIT_TIME_MS)      /* Counter for 10ms period */
00040 #define PROC_CNT_TFT        (PROC_CYCLE_TFT / UNIT_TIME_MS)     /* Counter for 50ms period */
00041 #define PROC_CNT_CMD        (PROC_CYCLE_CMD / UNIT_TIME_MS)     /* Counter for 2ms period */
00042 #define PROC_CNT_REFRESH    (PROC_CYCLE_REFRESH / UNIT_TIME_MS) /* Counter for 50ms period */
00043 
00044 /*--- Macro definition of SW module ---*/
00045 #define SW0_ACTIVE_LEVEL    (0)
00046 #define SW0_DECISION_TIME   (50u)   /* Time until the decision of the input status. */
00047 #define SW0_DECISION_CNT    (SW0_DECISION_TIME / PROC_CYCLE_SW) /* Counter for 50ms period */
00048 
00049 /*--- User defined types ---*/
00050 /* Control data of SW module */
00051 typedef struct {
00052     uint32_t        sampling_count; /* Sampling count for decision of input. */
00053     bool            current_status; /* Current input status. true=push, false=release. */
00054 } sw_ctrl_t;
00055 
00056 /* Control data of TFT module */
00057 typedef struct {
00058     uint32_t        dummy;
00059 } tft_ctrl_t;
00060 
00061 /* Control data of key thread */
00062 typedef struct {
00063     sw_ctrl_t       sw_data;
00064     tft_ctrl_t      tft_data;
00065     cmd_ctrl_t      cmd_data;
00066 } key_ctrl_t;
00067 
00068 static void sw_init_proc(sw_ctrl_t * const p_ctrl);
00069 static SYS_KeyCode sw_main_proc(sw_ctrl_t * const p_ctrl);
00070 static void tft_init_proc(tft_ctrl_t * const p_ctrl);
00071 static SYS_KeyCode tft_main_proc(tft_ctrl_t * const p_ctrl);
00072 
00073 void key_thread(void const *argument)
00074 {
00075     static key_ctrl_t   key_data;
00076     SYS_KeyCode         key_ev;
00077     SYS_KeyCode         tmp_ev;
00078     uint32_t            cnt = 0u;
00079 
00080     UNUSED_ARG(argument);
00081     
00082     /* Initializes the control data of key thread. */
00083     sw_init_proc(&key_data.sw_data);
00084     tft_init_proc(&key_data.tft_data);
00085     cmd_init_proc(&key_data.cmd_data);
00086     while(1) {
00087         key_ev = SYS_KEYCODE_NON;
00088         /* Is it a timing of the SW module processing? */
00089         if((cnt % PROC_CNT_SW) == 0u) {
00090             /* Executes main process of SW module. */
00091             tmp_ev = sw_main_proc(&key_data.sw_data);
00092             if(tmp_ev != SYS_KEYCODE_NON) {
00093                 key_ev = tmp_ev;
00094             }
00095         }
00096         /* Is it a timing of TFT module processing? */
00097         if((cnt % PROC_CNT_TFT) == 0u) {
00098             /* Executes main process of TFT module. */
00099             tmp_ev = tft_main_proc(&key_data.tft_data);
00100             if(tmp_ev != SYS_KEYCODE_NON) {
00101                 if(key_ev == SYS_KEYCODE_NON) {
00102                     /* There is no input from other modules. */
00103                     key_ev = tmp_ev;
00104                 }
00105             }
00106         }
00107         /* Is it a timing of command-line module processing? */
00108         if((cnt % PROC_CNT_CMD) == 0u) {
00109             /* Executes main process of command-line module. */
00110             tmp_ev = cmd_main_proc(&key_data.cmd_data);
00111             if(tmp_ev != SYS_KEYCODE_NON) {
00112                 if(key_ev == SYS_KEYCODE_NON) {
00113                     /* There is no input from other modules. */
00114                     key_ev = tmp_ev;
00115                 }
00116             }
00117         }
00118         /* Is it a refresh timing of the counter? */
00119         if(cnt >= PROC_CNT_REFRESH) {
00120             cnt = 0u;
00121         }
00122         /* When the event occurs, this mail is sent to main thread. */
00123         if(key_ev != SYS_KEYCODE_NON) {
00124             (void) sys_notify_key_input(key_ev);
00125         }
00126         Thread::wait(UNIT_TIME_MS);
00127         cnt++;
00128     }
00129 }
00130 
00131 /** Initialises SW module
00132  *
00133  *  @param p_ctrl Pointer to the control data of SW module.
00134  */
00135 static void sw_init_proc(sw_ctrl_t * const p_ctrl)
00136 {
00137     if (p_ctrl != NULL) {
00138         p_ctrl->sampling_count = 0u;
00139         p_ctrl->current_status = false;
00140     }
00141 }
00142 
00143 /** Executes the main processing of SW module
00144  *
00145  *  @param p_ctrl Pointer to the control data of SW module.
00146  *
00147  *  @returns 
00148  *    Key code.
00149  */
00150 static SYS_KeyCode sw_main_proc(sw_ctrl_t * const p_ctrl)
00151 {
00152     SYS_KeyCode         key_ev = SYS_KEYCODE_NON;
00153     int32_t             pin_level;
00154     static DigitalIn    sw0(P6_0);
00155 
00156     if (p_ctrl != NULL) {
00157         pin_level = sw0.read();
00158         if (pin_level == SW0_ACTIVE_LEVEL) {
00159             /* SW0 is pushed. */
00160             if (p_ctrl->sampling_count < SW0_DECISION_CNT) {
00161                 p_ctrl->sampling_count++;
00162                 if (p_ctrl->sampling_count == SW0_DECISION_CNT) {
00163                     key_ev = SYS_KEYCODE_PLAYPAUSE;
00164                 }
00165             }
00166             p_ctrl->current_status = true;
00167         } else {
00168             /* SW0 is released. */
00169             p_ctrl->sampling_count = 0u;
00170             p_ctrl->current_status = false;
00171         }
00172     }
00173     return key_ev;
00174 }
00175 
00176 /** Initialises TFT module
00177  *
00178  *  @param p_ctrl Pointer to the control data of TFT module.
00179  */
00180 static void tft_init_proc(tft_ctrl_t * const p_ctrl)
00181 {
00182     if (p_ctrl != NULL) {
00183         /* DO NOTHING */
00184     }
00185 }
00186 
00187 /** Executes the main processing of TFT module
00188  *
00189  *  @param p_ctrl Pointer to the control data of TFT module.
00190  *
00191  *  @returns 
00192  *    Key code.
00193  */
00194 static SYS_KeyCode tft_main_proc(tft_ctrl_t * const p_ctrl)
00195 {
00196     SYS_KeyCode     key_ev = SYS_KEYCODE_NON;
00197 
00198     if (p_ctrl != NULL) {
00199         /* DO NOTHING */
00200     }
00201     return key_ev;
00202 }