Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Assign own functions called when key pressed

  • Loading branch information...
commit 18c6c9cd3a421806168a88adadd5de9c87f58f6d 1 parent cf82508
Witold Rugowski authored
56 Examples/Handlers/Handlers.pde
... ... @@ -0,0 +1,56 @@
  1 +/*
  2 +Example how to read selected key from Nettigo Keypad.
  3 +
  4 +Uses:
  5 + - Arduino
  6 + - LCD Kit from Nettigo (but could be any LCD display compatible with LiquidCrystal library)
  7 + - Nettigo Keypad
  8 +
  9 + Connect:
  10 + LCD:
  11 + 1 - GND
  12 + 2 - +5V
  13 + 3 - 10k pot output
  14 + 4 - D2 on Arduino
  15 + 5 - GND
  16 + 6 - D3 on Arduino
  17 + 7 - 10 - not connected
  18 + 11 - 14 - D4-D7 on Arduino
  19 + 15 - +5V (backlight)
  20 + 16 - GND
  21 +
  22 + Nettigo Keypad:
  23 + AD1:
  24 + 1 (marked with square pad) - +5V
  25 + 2 - A0 on Arduino
  26 + 3 - GND
  27 +*/
  28 +#include <NettigoKeypad.h>
  29 +
  30 +#include <LiquidCrystal.h>
  31 +
  32 +LiquidCrystal lcd(2,3,4,5,6,7);
  33 +
  34 +NG_Keypad keypad;
  35 +
  36 +char* keys[] = { "NONE", "SELECT", "LEFT", "DOWN", "UP", "RIGHT" };
  37 +
  38 +void up_was_pressed() {
  39 + lcd.print("it goes UP ");
  40 +}
  41 +
  42 +void setup(){
  43 + lcd.begin(16,2);
  44 + lcd.print("Selected key:");
  45 + keypad.register_handler(NG_Keypad::UP, up_was_pressed);
  46 +}
  47 +
  48 +void loop(){
  49 + int rd;
  50 + rd = analogRead(0);
  51 + lcd.setCursor(0,1);
  52 + keypad.check_handlers(rd);
  53 + lcd.print( keys[ keypad.key_pressed(rd) ] );
  54 + lcd.print(" ");
  55 + delay(100);
  56 +}
19 NettigoKeypad.cpp
@@ -5,7 +5,9 @@
5 5 #include "WProgram.h"
6 6 #include "NettigoKeypad.h"
7 7
8   -NG_Keypad::NG_Keypad(void) {};
  8 +NG_Keypad::NG_Keypad(void) {
  9 + for (int i=0; i++; i< NG_KEYPAD_SIZE) { _functions[i] = NULL; }
  10 +};
9 11
10 12 int NG_Keypad::key_pressed(int rd) {
11 13 if (rd < 250) return RIGHT;
@@ -16,3 +18,18 @@ int NG_Keypad::key_pressed(int rd) {
16 18 return NONE;
17 19 };
18 20
  21 +void NG_Keypad::check_handlers(int rd) {
  22 + if (_functions[key_pressed(rd)] != NULL)
  23 + {
  24 + _functions[key_pressed(rd)]();
  25 + };
  26 + return;
  27 +}
  28 +
  29 +
  30 +int NG_Keypad::register_handler( int key, void (*userF)(void) ) {
  31 + if(key >= NG_KEYPAD_SIZE)
  32 + return -1;
  33 + _functions[key] = userF;
  34 +};
  35 +
18 NettigoKeypad.h
@@ -2,6 +2,8 @@
2 2 //Published on MIT licence
3 3 // (c) 2011 Nettigo
4 4
  5 +#define NG_KEYPAD_SIZE 6
  6 +
5 7 class NG_Keypad
6 8 {
7 9 public:
@@ -12,9 +14,23 @@ class NG_Keypad
12 14 static const int UP = 4;
13 15 static const int RIGHT = 5;
14 16
  17 +
  18 +
15 19 NG_Keypad(void);
16   - //which key was pressed?
  20 + //which key was pressed? Takes analog input value and returns constant
  21 + //describing which key was pressed.
17 22 int key_pressed(int rd);
  23 +
  24 + //Register handler to be called when key pressed
  25 + // takes two args
  26 + // key - which key should be used
  27 + // userF - which function should be called when key pressed
  28 + int register_handler( int key, void (*userF)(void) );
  29 +
  30 + //Check for handlers to be called
  31 + void check_handlers(int rd);
  32 + private:
  33 + void (*_functions[NG_KEYPAD_SIZE])(void);
18 34
19 35
20 36 };

0 comments on commit 18c6c9c

Please sign in to comment.
Something went wrong with that request. Please try again.