Browse Source

keypad logic

main
Hendrik Langer 7 years ago
parent
commit
39e48453af
  1. 71
      src/keyboard.cpp
  2. 25
      src/keyboard.h
  3. 37
      src/main.cpp

71
src/keyboard.cpp

@ -0,0 +1,71 @@
#include "Arduino.h"
#include "keyboard.h"
Keyboard::Keyboard() {
}
Keyboard::init() {
this->thresholds = {40, 0, 0, 40, 40, 40, 40, 40, 20, 20};
this->touchDetected = {false, false, false, false, false, false, false, false, false, false};
touchAttachInterrupt(T0, gotTouch0, threshold[0]);
// touchAttachInterrupt(T1, gotTouch1, threshold[1]); // GPIO_00 not usable on dev board
// touchAttachInterrupt(T2, gotTouch2, threshold[2]); // GPIO_02 not usable on dev board
touchAttachInterrupt(T3, gotTouch3, threshold[3]);
touchAttachInterrupt(T4, gotTouch4, threshold[4]);
touchAttachInterrupt(T5, gotTouch5, threshold[5]);
touchAttachInterrupt(T6, gotTouch6, threshold[6]);
touchAttachInterrupt(T7, gotTouch7, threshold[7]);
touchAttachInterrupt(T8, gotTouch8, threshold[8]);
touchAttachInterrupt(T9, gotTouch9, threshold[9]);
}
Keyboard::gotTouch0() {
delay(10);
if (touchRead(T0) < threshold[0])
this->touchDetected[0] = true;
}
Keyboard::gotTouch1() {
delay(10);
if (touchRead(T1) < threshold[1])
this->touchDetected[1] = true;
}
Keyboard::gotTouch2() {
delay(10);
if (touchRead(T2) < threshold[2])
this->touchDetected[2] = true;
}
Keyboard::gotTouch3() {
delay(10);
if (touchRead(T3) < threshold[3])
this->touchDetected[3] = true;
}
Keyboard::gotTouch4() {
delay(10);
if (touchRead(T4) < threshold[4])
this->touchDetected[4] = true;
}
Keyboard::gotTouch5() {
delay(10);
if (touchRead(T5) < threshold[5])
this->touchDetected[5] = true;
}
Keyboard::gotTouch6() {
delay(10);
if (touchRead(T6) < threshold[6])
this->touchDetected[6] = true;
}
Keyboard::gotTouch7() {
delay(10);
if (touchRead(T7) < threshold[7])
this->touchDetected[7] = true;
}
Keyboard::gotTouch8() {
delay(10);
if (touchRead(T8) < threshold[8])
this->touchDetected[8] = true;
}
Keyboard::gotTouch9() {
delay(10);
if (touchRead(T9) < threshold[9])
this->touchDetected[9] = true;
}

25
src/keyboard.h

@ -0,0 +1,25 @@
#ifndef _KEYBOARD_H
#define _KEYBOARD_H
#include "Arduino.h"
class Keyboard {
public:
Keyboard();
void init();
bool touchDetected[10];
private:
uint8_t thresholds[10];
void gotTouch0();
void gotTouch1();
void gotTouch2();
void gotTouch3();
void gotTouch4();
void gotTouch5();
void gotTouch6();
void gotTouch7();
void gotTouch8();
void gotTouch9();
};
#endif /* _KEYBOARD_H */

37
src/main.cpp

@ -7,33 +7,38 @@
*/
#include "Arduino.h"
#include "keyboard.h"
#define LED_PIN 21
Keyboard keyboard;
void setup()
{
// initialize LED digital pin as an output.
pinMode(LED_PIN, OUTPUT);
keyboard.init();
}
void loop()
{
// turn the LED on (HIGH is the voltage level)
if ( touchRead(T0) < 50
// || touchRead(T1) < 50 // GPIO 00
// || touchRead(T2) < 50 // GPIO 02
|| touchRead(T3) < 50
|| touchRead(T4) < 50
|| touchRead(T5) < 50
|| touchRead(T6) < 50
|| touchRead(T7) < 50
|| touchRead(T8) < 20
|| touchRead(T9) < 20) {
digitalWrite(LED_PIN, HIGH);
} else {
// turn the LED off by making the voltage LOW
digitalWrite(LED_PIN, LOW);
bool touched = false;
for(int i; i<=9; i++) {
if (keyboard.touchDetected[i] == true) {
keyboard.touchDetected[i] = false;
touched = true;
}
}
if (touched == true) {
// turn the LED on (HIGH is the voltage level)
digitalWrite(LED_PIN, HIGH);
} else {
// turn the LED off by making the voltage LOW
digitalWrite(LED_PIN, LOW);
}
// wait for a second
delay(1000);
delay(50);
}

Loading…
Cancel
Save