Browse Source

learn how to code

main
Hendrik Langer 8 years ago
parent
commit
6c19e3a81b
  1. 88
      src/keyboard.cpp
  2. 16
      src/keyboard.h
  3. 3
      src/main.cpp

88
src/keyboard.cpp

@ -1,71 +1,89 @@
#include "Arduino.h" #include "Arduino.h"
#include "keyboard.h" #include "keyboard.h"
Keyboard::Keyboard() { using namespace std;
}
void Keyboard::init() { Keyboard::Keyboard() {
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]);
} }
void Keyboard::gotTouch0() { void gotTouch0() {
delay(10); delay(10);
if (touchRead(T0) < threshold[0]) if (touchRead(T0) < threshold[0])
this->touchDetected[0] = true; touchDetected[0] = true;
} }
void Keyboard::gotTouch1() { void gotTouch1() {
delay(10); delay(10);
if (touchRead(T1) < threshold[1]) if (touchRead(T1) < threshold[1])
this->touchDetected[1] = true; touchDetected[1] = true;
} }
void Keyboard::gotTouch2() { void gotTouch2() {
delay(10); delay(10);
if (touchRead(T2) < threshold[2]) if (touchRead(T2) < threshold[2])
this->touchDetected[2] = true; touchDetected[2] = true;
} }
void Keyboard::gotTouch3() { void gotTouch3() {
delay(10); delay(10);
if (touchRead(T3) < threshold[3]) if (touchRead(T3) < threshold[3])
this->touchDetected[3] = true; touchDetected[3] = true;
} }
void Keyboard::gotTouch4() { void gotTouch4() {
delay(10); delay(10);
if (touchRead(T4) < threshold[4]) if (touchRead(T4) < threshold[4])
this->touchDetected[4] = true; touchDetected[4] = true;
} }
void Keyboard::gotTouch5() { void gotTouch5() {
delay(10); delay(10);
if (touchRead(T5) < threshold[5]) if (touchRead(T5) < threshold[5])
this->touchDetected[5] = true; touchDetected[5] = true;
} }
void Keyboard::gotTouch6() { void gotTouch6() {
delay(10); delay(10);
if (touchRead(T6) < threshold[6]) if (touchRead(T6) < threshold[6])
this->touchDetected[6] = true; touchDetected[6] = true;
} }
void Keyboard::gotTouch7() { void gotTouch7() {
delay(10); delay(10);
if (touchRead(T7) < threshold[7]) if (touchRead(T7) < threshold[7])
this->touchDetected[7] = true; touchDetected[7] = true;
} }
void Keyboard::gotTouch8() { void gotTouch8() {
delay(10); delay(10);
if (touchRead(T8) < threshold[8]) if (touchRead(T8) < threshold[8])
this->touchDetected[8] = true; touchDetected[8] = true;
} }
void Keyboard::gotTouch9() { void gotTouch9() {
delay(10); delay(10);
if (touchRead(T9) < threshold[9]) if (touchRead(T9) < threshold[9])
this->touchDetected[9] = true; touchDetected[9] = true;
}
bool Keyboard::getTouchDetected(uint8_t pad) {
bool res = touchDetected[pad];
touchDetected[pad] = false;
return res;
} }
void Keyboard::init() {
threshold[0] = 40;
threshold[1] = 0;
threshold[2] = 0;
threshold[3] = 40;
threshold[4] = 40;
threshold[5] = 40;
threshold[6] = 40;
threshold[7] = 40;
threshold[8] = 20;
threshold[9] = 20;
for (int i=0; i<=9; i++) touchDetected[i] = 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]);
}

16
src/keyboard.h

@ -3,23 +3,15 @@
#include "Arduino.h" #include "Arduino.h"
static bool touchDetected[10];
static uint8_t threshold[10];
class Keyboard { class Keyboard {
public: public:
Keyboard(); Keyboard();
void init(); void init();
bool touchDetected[10]; bool getTouchDetected(uint8_t pad);
private: 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 */ #endif /* _KEYBOARD_H */

3
src/main.cpp

@ -24,8 +24,7 @@ void loop()
{ {
bool touched = false; bool touched = false;
for(int i; i<=9; i++) { for(int i; i<=9; i++) {
if (keyboard.touchDetected[i] == true) { if (keyboard.getTouchDetected(i) == true) {
keyboard.touchDetected[i] = false;
touched = true; touched = true;
} }
} }

Loading…
Cancel
Save