Cornelius Bezuidenhout / ResistiveTouchController
Revision:
1:51bde7ae4a65
Parent:
0:c04a78051c81
Child:
2:a4ebeb3ba49c
diff -r c04a78051c81 -r 51bde7ae4a65 ResistiveTouchController.cpp
--- a/ResistiveTouchController.cpp	Sat Oct 08 17:10:42 2016 +0000
+++ b/ResistiveTouchController.cpp	Sat Oct 08 19:22:11 2016 +0200
@@ -0,0 +1,122 @@
+#include "ResistiveTouchController.hpp"
+
+ResistiveTouchController::ResistiveTouchController(PinName ur, PinName lr, PinName s, PinName ul, PinName ll, int num_of_samples = 5, int settle = 20) :
+  _UR(ur),
+  _LR(lr),
+  _UL(ul),
+  _LL(ll)
+{
+  _UR.output();
+  _UR.mode(PullNone);
+
+  _LR.output();
+  _LR.mode(PullNone);
+
+  _UL.output();
+  _UL.mode(PullNone);
+
+  _LL.output();
+  _LL.mode(PullNone);
+
+  _S = s;
+
+  _samples = num_of_samples;
+  _settle = settle;
+
+  //-- Change these values if needed for desired screen
+  A = 1660.540541f;
+  B = 20.5005005f;
+  C = -334.0556557f;
+  D = 0.0f;
+  E = 1517.037037f;
+  F = -246.5185185;
+}
+
+int ResistiveTouchController::TouchDetected()
+{
+  DigitalIn S(_S, PullUp);
+  ConfigPins(DETECT_MODE);
+
+  return ( !S.read() );
+}
+
+void ResistiveTouchController::Measure()
+{
+    ConfigPins(X_MODE);
+    _xt = MeasureS();
+
+    ConfigPins(Y_MODE);
+    _yt = MeasureS();
+}
+
+void ResistiveTouchController::RawX()
+{
+  return _xt;
+}
+
+void ResistiveTouchController::RawY()
+{
+  return _yt;
+}
+
+void ResistiveTouchController::X()
+{
+  return (A*_xt+B*_yt+C);
+}
+
+void ResistiveTouchController::Y()
+{
+  return (D*_xt+E*_yt+F);
+}
+
+void ResistiveTouchController::Calibrate(float t[3][2], float d[3][2])
+{
+  A = (X_D1*(Y_T2-Y_T3)+X_D2*(Y_T3-Y_T1)+X_D3*(Y_T1-Y_T2))/(X_T1*(Y_T2-Y_T3)+X_T2*(Y_T3-Y_T1)+X_T3*(Y_T1-Y_T2));
+  B = (A*(X_T3-X_T2)+X_D2-X_D3)/(Y_T2-Y_T3);
+  C = X_D3-A*X_T3-B*Y_T3;
+  D = (Y_D1*(Y_T2-Y_T3)+Y_D2*(Y_T3-Y_T1)+Y_D3*(Y_T1-Y_T2))/(X_T1*(Y_T2-Y_T3)+X_T2*(Y_T3-Y_T1)+X_T3*(Y_T1-Y_T2));
+  E = (D*(X_T3-X_T2)+Y_D2-Y_D3)/(Y_T2-Y_T3);
+  F = Y_D3-D*X_T3-E*Y_T3;
+}
+
+void ResistiveTouchController::SetSettleTime(int settle)
+{
+  _settle = settle;
+}
+
+void ResistiveTouchController::ConfigPins(char mode)
+{
+  switch (mode)
+  {
+    case DETECT_MODE:
+      _UR.write(0);
+      _UL.write(0);
+      _LR.write(0);
+      _LL.write(0);
+      break;
+    case X_MODE:
+      _UR.write(1);
+      _UL.write(0);
+      _LR.write(1);
+      _LL.write(0);
+      break;
+    case Y_MODE:
+      _UR.write(1);
+      _UL.write(1);
+      _LR.write(0);
+      _LL.write(0);
+      break;
+  }
+
+  Delay(_settle);
+}
+
+void ResistiveTouchController::Delay(float time)
+{
+  _delayTimer.start();
+
+  while( _delayTimer.read() < time );
+
+  _delayTimer.stop();
+  _delayTimer.reset();
+}