Skip to content
Browse files

Assign own functions called when key pressed

  • Loading branch information...
1 parent cf82508 commit 18c6c9cd3a421806168a88adadd5de9c87f58f6d @netmaniac committed Mar 28, 2011
Showing with 91 additions and 2 deletions.
  1. +56 −0 Examples/Handlers/Handlers.pde
  2. +18 −1 NettigoKeypad.cpp
  3. +17 −1 NettigoKeypad.h
View
56 Examples/Handlers/Handlers.pde
@@ -0,0 +1,56 @@
+/*
+Example how to read selected key from Nettigo Keypad.
+
+Uses:
+ - Arduino
+ - LCD Kit from Nettigo (but could be any LCD display compatible with LiquidCrystal library)
+ - Nettigo Keypad
+
+ Connect:
+ LCD:
+ 1 - GND
+ 2 - +5V
+ 3 - 10k pot output
+ 4 - D2 on Arduino
+ 5 - GND
+ 6 - D3 on Arduino
+ 7 - 10 - not connected
+ 11 - 14 - D4-D7 on Arduino
+ 15 - +5V (backlight)
+ 16 - GND
+
+ Nettigo Keypad:
+ AD1:
+ 1 (marked with square pad) - +5V
+ 2 - A0 on Arduino
+ 3 - GND
+*/
+#include <NettigoKeypad.h>
+
+#include <LiquidCrystal.h>
+
+LiquidCrystal lcd(2,3,4,5,6,7);
+
+NG_Keypad keypad;
+
+char* keys[] = { "NONE", "SELECT", "LEFT", "DOWN", "UP", "RIGHT" };
+
+void up_was_pressed() {
+ lcd.print("it goes UP ");
+}
+
+void setup(){
+ lcd.begin(16,2);
+ lcd.print("Selected key:");
+ keypad.register_handler(NG_Keypad::UP, up_was_pressed);
+}
+
+void loop(){
+ int rd;
+ rd = analogRead(0);
+ lcd.setCursor(0,1);
+ keypad.check_handlers(rd);
+ lcd.print( keys[ keypad.key_pressed(rd) ] );
+ lcd.print(" ");
+ delay(100);
+}
View
19 NettigoKeypad.cpp
@@ -5,7 +5,9 @@
#include "WProgram.h"
#include "NettigoKeypad.h"
-NG_Keypad::NG_Keypad(void) {};
+NG_Keypad::NG_Keypad(void) {
+ for (int i=0; i++; i< NG_KEYPAD_SIZE) { _functions[i] = NULL; }
+};
int NG_Keypad::key_pressed(int rd) {
if (rd < 250) return RIGHT;
@@ -16,3 +18,18 @@ int NG_Keypad::key_pressed(int rd) {
return NONE;
};
+void NG_Keypad::check_handlers(int rd) {
+ if (_functions[key_pressed(rd)] != NULL)
+ {
+ _functions[key_pressed(rd)]();
+ };
+ return;
+}
+
+
+int NG_Keypad::register_handler( int key, void (*userF)(void) ) {
+ if(key >= NG_KEYPAD_SIZE)
+ return -1;
+ _functions[key] = userF;
+};
+
View
18 NettigoKeypad.h
@@ -2,6 +2,8 @@
//Published on MIT licence
// (c) 2011 Nettigo
+#define NG_KEYPAD_SIZE 6
+
class NG_Keypad
{
public:
@@ -12,9 +14,23 @@ class NG_Keypad
static const int UP = 4;
static const int RIGHT = 5;
+
+
NG_Keypad(void);
- //which key was pressed?
+ //which key was pressed? Takes analog input value and returns constant
+ //describing which key was pressed.
int key_pressed(int rd);
+
+ //Register handler to be called when key pressed
+ // takes two args
+ // key - which key should be used
+ // userF - which function should be called when key pressed
+ int register_handler( int key, void (*userF)(void) );
+
+ //Check for handlers to be called
+ void check_handlers(int rd);
+ private:
+ void (*_functions[NG_KEYPAD_SIZE])(void);
};

0 comments on commit 18c6c9c

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