keypad kutuphanesi, Interruptsiz

Dependents:   rtos_uygulamasi guvenlik_sistemi keypad_kullanimi lcd_kullanimi_keypad ... more

Files at this revision

API Documentation at this revision

Comitter:
marvas
Date:
Mon May 06 10:27:01 2019 +0000
Commit message:
keypad kutuphanesi

Changed in this revision

Keypad.cpp Show annotated file Show diff for this revision Revisions of this file
Keypad.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Keypad.cpp	Mon May 06 10:27:01 2019 +0000
@@ -0,0 +1,124 @@
+#include "Keypad.h"
+
+Keypad::Keypad(
+    PinName kq, PinName kx, PinName ky, PinName kz,
+    PinName ka, PinName kb, PinName kc, PinName kd
+)
+{
+    k_in[0]  = new DigitalIn (ka, PullUp);
+    k_in[1]  = new DigitalIn (kb, PullUp);
+    k_in[2]  = new DigitalIn (kc, PullUp);
+    k_in[3]  = new DigitalIn (kd, PullUp);
+    
+    k_out[0] = new DigitalOut (kq);
+    k_out[1] = new DigitalOut (kx);
+    k_out[2] = new DigitalOut (ky);
+    k_out[3] = new DigitalOut (kz);
+    
+    *k_out[0] = 1;
+    *k_out[1] = 1;
+    *k_out[2] = 1;
+    *k_out[3] = 1;
+    
+    uint8_t j,i;
+    for (j = 0; j < 4; j++){
+        for(i = 0; i < 4; i++){
+            key_transent_cntr[j][i] = CHNG_CNT;
+            key_state[j][i] = Off_state;
+        }
+    }
+    for(i = 0; i < BF_SIZE; i++){
+        buf[i] = 0;
+    }
+    read_addr = 0;
+    write_addr = 0;
+    tk.attach_us(callback(this, &Keypad::key_scan), 2000);     // 2mS
+}
+
+void Keypad::key_scan(void)
+{
+    //my_led = 1;
+    for (int32_t j = 0; j < 4; j++){
+        *k_out[0] = 1;   *k_out[1] = 1;  *k_out[2] = 1; *k_out[3] = 1;
+        *k_out[j] = 0;
+        wait_us(1);
+        for (int32_t i = 0; i < 4; i++){
+            switch (key_state[j][i]){
+                case Off_state:
+                    //my_led = 1;
+                    if (*k_in[i] == 0){ // key on
+                        key_state[j][i] = Off_to_on_transient;
+                        key_transent_cntr[j][i] = CHNG_CNT;
+                    }
+                    break;
+                case Off_to_on_transient:
+                    //my_led = 1;
+                    if (*k_in[i] == 0){ // key on
+                        if (--key_transent_cntr[j][i] < -CHNG_CNT){
+                            //my_led = 1;
+                            bf_put(j * 4 + i + 1); // save data into buffer
+                            key_state[j][i] = On_state;
+                        }
+                    } else {    // key off
+                        if (++key_transent_cntr[j][i] > CHNG_CNT){
+                            key_state[j][i] = Off_state;
+                        }
+                    }
+                    break;
+                case On_state:
+                    //my_led = 1;
+                    if (*k_in[i] == 1){ // key off
+                        key_state[j][i] = On_to_off_transient;
+                        key_transent_cntr[j][i] = -CHNG_CNT;
+                    }
+                    break;
+                case On_to_off_transient:
+                    //my_led = 1;
+                    if (*k_in[i] == 0){ // key on
+                        if (--key_transent_cntr[j][i] < -CHNG_CNT){
+                            key_state[j][i] = On_state;
+                        }
+                    } else {    // key off
+                        if (++key_transent_cntr[j][i] > CHNG_CNT){
+                            key_state[j][i] = Off_state;
+                        }
+                    }
+                    break;
+                default:    // just in case
+                    key_state[j][i] = Off_state;
+                    break;
+            }
+            //my_led = 0;
+        }
+    }
+    *k_out[0] = 1;   *k_out[1] = 1;  *k_out[2] = 1; *k_out[3] = 1;
+    //my_led = 0;
+}
+
+uint8_t Keypad::read(void)
+{
+    return bf_get();
+}
+
+void Keypad::bf_put(char dt)
+{
+    uint8_t next = write_addr + 1;
+    if (next == BF_SIZE) {
+        next = 0;
+    }
+    buf[write_addr] = dt;
+    write_addr = next;
+}
+
+int8_t Keypad::bf_get (void){
+    if (read_addr == write_addr){
+        return 0;
+    }
+    uint8_t dt = buf[read_addr];
+    ++read_addr;
+    if (read_addr == BF_SIZE){
+        read_addr = 0;
+    }
+    return dt;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Keypad.h	Mon May 06 10:27:01 2019 +0000
@@ -0,0 +1,70 @@
+#ifndef KEYPAD_H
+#define KEYPAD_H
+
+#include "mbed.h"
+
+#define  CHNG_CNT       4
+
+ /*
+  @code
+  #include "mbed.h"
+  #include "Keypad.h"
+ 
+Keypad key(D10, D9, D8, D7, D6, D5, D4, D3);
+ 
+ char key_table[] = { 
+                      '1', '2', '3', 'A',
+                      '4', '5', '6', 'B',
+                      '7', '8', '9', 'C',
+                      '*', '0', '#', 'D'
+                    };
+  int main() {
+      uint32_t key_num;
+      while(true) {
+         while ((key_num = key.read()) != 0){
+             printf("%c\r\n", *(key_table + key_num-1));
+         }
+         wait(1.0);  
+     }
+ }
+ * @endcode
+ */
+
+class Keypad {
+public:
+    /** 4x4 keypad interface:
+     *  @param key input port           A,B,C,D
+     *  @param key output(scan) port    Q,X,Y,Z
+     */
+    Keypad(PinName qx, PinName kx, PinName ky, PinName kz,
+           PinName ka, PinName kb, PinName kc, PinName kd);
+                    
+    /** Read key data into buffer
+     *  @param none
+     *  @return key number by ASCII code
+     */
+    uint8_t read(void);
+
+protected:
+    DigitalIn   *k_in[4];
+    DigitalOut  *k_out[4];
+    Ticker      tk;
+
+    // key control
+    enum State {Off_state, Off_to_on_transient, On_state, On_to_off_transient};
+    volatile State  key_state[4][4];
+    volatile int8_t key_transent_cntr[4][4];
+    void key_scan(void);
+    
+    // buffer control
+    #define BF_SIZE 20
+    uint8_t         read_addr;
+    uint8_t         write_addr;
+    uint8_t         buf[BF_SIZE];
+    void bf_put (char dat);
+    int8_t bf_get (void);
+
+};
+
+#endif // KEYPAD_H
+