Add the (missing?) Keypad lib draft

Mon, 28 Jan 2019 21:51:45 +0100

author
David Douard <david.douard@logilab.fr>
date
Mon, 28 Jan 2019 21:51:45 +0100
changeset 24
b43536c064f6
parent 23
daf26b083899
child 25
5b1e0b384d31

Add the (missing?) Keypad lib

lib/keypad/Keypad.cpp file | annotate | diff | comparison | revisions
lib/keypad/Keypad.h file | annotate | diff | comparison | revisions
lib/keypad/library.json file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/keypad/Keypad.cpp	Mon Jan 28 21:51:45 2019 +0100
@@ -0,0 +1,88 @@
+#include "mbed.h"
+#include "Keypad.h"
+
+
+Keypad::Keypad(uint8_t nRows, DigitalIn rows[],
+	       uint8_t nColumns, DigitalOut cols[],
+	       int debounce_ms):
+  _rows(),
+  _columns(),
+  _debounce(debounce_ms),
+  _started(false),
+  _nrows(nRows),
+  _ncols(nColumns)
+{
+  printf("Setup Keypad: %dx%d\r\n", nColumns, nRows);
+  for(uint8_t c=0; c<nColumns; c++) {
+    _columns.push_back(cols[c]);
+    _columns[c].write(0);
+  }
+
+  for(uint8_t r=0; r<nRows; r++) {
+    _rows.push_back(rows[r]);
+    _rows[r].mode(PullDown);
+  }
+  _keys = new uint8_t[_nrows*_ncols];
+}
+
+Keypad::~Keypad()
+{
+  delete _keys;
+}
+
+void Keypad::run()
+{
+  uint8_t col=0;
+  uint8_t row;
+
+  for (row=0; row<(_ncols*_nrows); row++)
+    _keys[row] = 0;
+  while(_started) {
+    _columns[col].write(1);
+    Thread::wait(5);
+    for(row=0; row<_nrows; row++)
+      {
+	if (_rows[row].read())
+	  {
+	    if (_keys[row + _nrows*col] < 2)
+	      {
+		_keys[row + _nrows*col]++;
+		if ((_keys[row + _nrows*col] == 2) && _kp_callback)
+		  _kp_callback.call(row, col);
+	      }
+	  }
+	else
+	  {
+	    if (_keys[row + _nrows*col] > 0)
+	      {
+		_keys[row + _nrows*col]--;
+		if ((_keys[row + _nrows*col] == 0) && _kr_callback)
+		  _kr_callback.call(row, col);
+	      }
+	  }
+      }
+    _columns[col].write(0);
+    col = (col+1) % _ncols;
+    Thread::wait(5);
+  }
+}
+
+void Keypad::attach(const keyevent_callback_t& kp_callback,
+		      const keyevent_callback_t& kr_callback)
+{
+  _kp_callback = kp_callback;
+  _kr_callback = kr_callback;
+}
+
+void Keypad::start(void)
+{
+if (_started)
+  return;
+_started = true;
+_loop.start(this, &Keypad::run);
+}
+
+void Keypad::stop(void)
+{
+  _started = false;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/keypad/Keypad.h	Mon Jan 28 21:51:45 2019 +0100
@@ -0,0 +1,53 @@
+
+#ifndef KEYPAD_H
+#define KEYPAD_H
+
+#include "mbed.h"
+#include "rtos.h"
+#include <vector>
+
+
+
+typedef Callback<void(uint8_t, uint8_t)> keyevent_callback_t;
+
+class Keypad {
+public:
+  Keypad(uint8_t nRows, DigitalIn rows[],
+	 uint8_t nColumns, DigitalOut cols[],
+	 int debounce_ms=20);
+    
+  /** Destructor
+   */
+  ~Keypad();
+  
+  void attach(const keyevent_callback_t& kp_callback=NULL,
+	      const keyevent_callback_t& kr_callback=NULL);
+  
+  void start(void);
+  void stop(void);
+  
+  
+protected:
+  std::vector<DigitalIn> _rows;
+  std::vector<DigitalOut> _columns;
+  int              _debounce;  // miliseconds
+
+  void _kp();
+  void _kr();
+  void _keyPressed(int row);
+  void _keyReleased(int row);
+  void run();
+  
+private:
+  keyevent_callback_t _kp_callback;
+  keyevent_callback_t _kr_callback;
+  bool _started;
+  Thread _loop;
+  //tloop.start(&loop);
+  uint8_t _nrows;
+  uint8_t _ncols;
+  uint8_t *_keys;
+};
+
+
+#endif // KEYPAD_H
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/keypad/library.json	Mon Jan 28 21:51:45 2019 +0100
@@ -0,0 +1,36 @@
+{
+    "authors": {
+        "name": "HM Yoong",
+        "url": "https://developer.mbed.org/users/yoonghm/"
+    },
+    "description": "An interrupt-driven interface to 4x4 keypad.",
+    "examples": [
+        "https://developer.mbed.org/users/daryl2110/code/FYPFinalProgram/",
+        "https://developer.mbed.org/users/daryl2110/code/FYPFinalizeProgram/",
+        "https://developer.mbed.org/users/ficofer/code/KEYS/",
+        "https://developer.mbed.org/users/sherckuith/code/Proyect_Patric_electronic_door_MSC_Ok_ES/",
+        "https://developer.mbed.org/users/salondonog/code/Tarea6/",
+        "https://developer.mbed.org/users/avallejopo/code/Tarea6/",
+        "https://developer.mbed.org/users/amarincan/code/tarea6keypad/",
+        "https://developer.mbed.org/users/mandres7/code/Teclado/",
+        "https://developer.mbed.org/users/lcorralesc1/code/Tarea6_Teclado/",
+        "https://developer.mbed.org/users/yoonghm/code/_library_keypad/"
+    ],
+    "frameworks": "mbed",
+    "keywords": [
+        "interrupt",
+        "keypad"
+    ],
+    "name": "keypad",
+    "platforms": [
+        "freescalekinetis",
+        "nordicnrf51",
+        "nxplpc",
+        "ststm32",
+        "teensy"
+    ],
+    "repository": {
+        "type": "hg",
+        "url": "https://developer.mbed.org/users/yoonghm/code/keypad/"
+    }
+}
\ No newline at end of file

mercurial