lib/keypad/Keypad.cpp

Thu, 15 Oct 2020 01:11:03 +0200

author
David Douard <david.douard@sdfa3.org>
date
Thu, 15 Oct 2020 01:11:03 +0200
changeset 26
86f099bda525
parent 24
b43536c064f6
child 28
424d792fea4f
permissions
-rw-r--r--

Several stuff

#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;
}

mercurial