Kenji Arai / Keypad

Dependents:   Keypad_input_OS2 Keypad_input

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Keypad.h Source File

Keypad.h

00001 /*
00002  * Mbed Library
00003  *  example:
00004  *      Akizuki AE-KIT45-KEYPAD4X3
00005  *      http://akizukidenshi.com/catalog/g/gK-12229/
00006  *
00007  * Copyright (c) 2017,'20 Kenji Arai / JH1PJL
00008  *  http://www7b.biglobe.ne.jp/~kenjia/
00009  *  https://os.mbed.com/users/kenjiArai/
00010  *      Created:    September 27th, 2017
00011  *      Revised:    April     13th, 2020
00012  */
00013 // ---- Merged below and added 4x5=20, 5x5=25 library --------------------------
00014 
00015 /*
00016  * 4x4 Fork: Henri Clarke
00017  *          May 17th, 2018
00018  *      https://os.mbed.com/users/microHenri/code/Keypad/
00019  */
00020 
00021 /*
00022  *  Performance
00023  *      every 2ms runs XuS for key detection(5x5=25 keys)
00024  *          on Nucleo-F446RE ->X=10uS, CPU occupancy is around 0.5%
00025  *          on Nucleo-L152RE ->X=50uS, CPU occupancy is around 2.5%
00026  *          on FRDM-K64F     ->X=28uS, CPU occupancy is around 1.4%
00027  */
00028 
00029 #ifndef KEYPAD_H
00030 #define KEYPAD_H
00031 
00032 #include "mbed.h"
00033 
00034 #define  CHNG_CNT       4
00035 
00036 //------------- ASCII CODE ---------------
00037 #define NUL     0x00
00038 #define SOH     0x01
00039 #define STX     0x02
00040 #define ETX     0x03
00041 #define EOT     0x04
00042 #define ENQ     0x05
00043 #define ACK     0x06
00044 #define BEL     0x07
00045 #define BS      0x08
00046 #define HT      0x09
00047 #define LF      0x0a
00048 #define VT      0x0b
00049 #define FF      0x0c
00050 #define CR      0x0d
00051 #define SD      0x0e
00052 #define SI      0x0f
00053 #define DLE     0x10
00054 #define DC1     0x11
00055 #define DC2     0x12
00056 #define DC3     0x13
00057 #define DC4     0x14
00058 #define NAK     0x15
00059 #define SYN     0x16
00060 #define ETB     0x17
00061 #define CAN     0x18
00062 #define EM      0x19
00063 #define SUB     0x1a
00064 #define ESC     0x1b
00065 #define FS      0x1c
00066 #define GS      0x1d
00067 #define RS      0x1e
00068 #define US      0x1f
00069 #define SPC     0x20
00070 
00071 /**
00072  * @code
00073  * #include "mbed.h"
00074  * #include "Keypad.h"
00075  *
00076  * //       output port  X  Y  Z
00077  * //       Input A      *  0  #
00078  * //       Input B      7  8  9
00079  * //       Input C      4  5  6
00080  * //       Input D      1  2  3
00081  * //          X    Y   Z   A   B   C   D   OUT(XYZ), IN(ABCD)
00082  * Keypad key(D10, D9, D8, D7, D6, D5, D4);
00083  *
00084  * // define key number at main routine
00085  * char *const key_table = "?*7410852#963";  // key_table[0]=? is not used!
00086  *
00087  * int main() {
00088  *     uint32_t key_num;
00089  *     while(true) {
00090  *         while ((key_num = key.read()) != 0){
00091  *             printf("%c\r\n", *(key_table + key_num));
00092  *         }
00093  *         wait(1.0);
00094  *     }
00095  * }
00096  * @endcode
00097  */
00098 
00099 class Keypad 
00100 {
00101 public:
00102     /** 4x3 keypad interface:
00103      *  @param key input port           A,B,C,D
00104      *  @param key output(scan) port    X,Y,Z
00105      */
00106     Keypad(PinName kx, PinName ky, PinName kz,
00107            PinName ka, PinName kb, PinName kc, PinName kd);
00108 
00109     /** 4x4 keypad interface:
00110      *  @param key input port           A,B,C,D
00111      *  @param key output(scan) port    X,Y,Z,W
00112      */
00113     Keypad(PinName kx, PinName ky, PinName kz, PinName kw,
00114            PinName ka, PinName kb, PinName kc, PinName kd);
00115 
00116     /** 5x4 keypad interface:
00117      *  @param key input port           A,B,C,D,E
00118      *  @param key output(scan) port    X,Y,Z,W
00119      */
00120     Keypad(PinName kx, PinName ky, PinName kz, PinName kw,
00121            PinName ka, PinName kb, PinName kc, PinName kd, PinName ke);
00122 
00123     /** 5x5 keypad interface:
00124      *  @param key input port           A,B,C,D,E
00125      *  @param key output(scan) port    X,Y,Z,W,V
00126      */
00127     Keypad(PinName kx, PinName ky, PinName kz, PinName kw, PinName kv,
00128            PinName ka, PinName kb, PinName kc, PinName kd, PinName ke);
00129 
00130     /** Read key data into buffer
00131      *  @param none
00132      *  @return key number by ASCII code
00133      */
00134     uint8_t read(void);
00135 
00136     /** Read key ON/OFF state
00137      *  @param none
00138      *  @return ON(true) or OFF(false)
00139      */
00140     bool read_state(uint8_t key_num);
00141 
00142 protected:
00143 #define BF_SIZE     32
00144 #define NUM         5
00145 
00146     DigitalIn     *k_in[NUM];
00147     DigitalInOut  *k_out[NUM];
00148     Ticker        tk;
00149 
00150     // key control
00151     enum State {OFF_state, OFF_to_ON_transient, ON_state, ON_to_OFF_transient};
00152     volatile State  key_state[NUM][NUM];
00153     volatile int8_t key_transent_cntr[NUM][NUM];
00154     void key_scan(void);
00155     void initialize(void);
00156 
00157     // mode control
00158     uint8_t key_mode;
00159     uint8_t key_in_num;
00160     uint8_t key_out_num;
00161 
00162     // buffer control
00163     uint8_t read_addr;
00164     uint8_t write_addr;
00165     uint8_t buf[BF_SIZE];
00166     void bf_put(char dat);
00167     int8_t bf_get(void);
00168 
00169 };
00170 
00171 #endif // KEYPAD_H