A controller for a 5 wire resistive touch screen
Diff: TouchController.cpp
- Revision:
- 2:d0930962a3bb
- Parent:
- 1:ed8485a78c6a
- Child:
- 3:710534c5a240
--- a/TouchController.cpp Thu Sep 15 15:38:12 2016 +0200 +++ b/TouchController.cpp Fri Sep 16 04:36:15 2016 +0200 @@ -19,9 +19,9 @@ _LL.mode(PullNone); _S = s; - _samples = num_of_samples; + n_samples = num_of_samples; - _settle = settle; + settle = 100; A = 1660.540541f; B = 20.5005005f; @@ -33,37 +33,36 @@ int TouchController::TouchDetected() { - DigitalInOut S(_S, PullUp); - ConfigPins(DETECT_MODE); - Thread::wait(_settle); + DigitalIn S(_S, PullUp); + ConfigPins(DETECT); + Thread::wait(settle); - return S.read(); + return (S.read() == 1 ? 0 : 1); } float TouchController::RawX() { - ConfigPins(X_MODE); - Thread::wait(_settle); + ConfigPins(X); + Thread::wait(settle); return MeasureS(); } float TouchController::RawY() { - ConfigPins(Y_MODE); - Thread::wait(_settle); - + ConfigPins(Y); + Thread::wait(settle); return MeasureS(); } float TouchController::X() { - ConfigPins(X_MODE); - Thread::wait(_settle); + ConfigPins(X); + Thread::wait(settle); float x_t = MeasureS(); - ConfigPins(Y_MODE); - Thread::wait(_settle); + ConfigPins(Y); + Thread::wait(settle); float y_t = MeasureS(); return (A*x_t+B*y_t+C); @@ -71,12 +70,12 @@ float TouchController::Y() { - ConfigPins(X_MODE); - Thread::wait(_settle); + ConfigPins(X); + Thread::wait(settle); float x_t = MeasureS(); - ConfigPins(Y_MODE); - Thread::wait(_settle); + ConfigPins(Y); + Thread::wait(settle); float y_t = MeasureS(); return (D*x_t+E*y_t+F); @@ -85,30 +84,30 @@ void TouchController::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; + 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 TouchDetected::ConfigPins(char mode) { switch (mode) { - case DETECT_MODE: + case DETECT: _UR.write(0); _UL.write(0); _LR.write(0); _LL.write(0); break; - case X_MODE: + case X: _UR.write(1); _UL.write(0); _LR.write(1); _LL.write(0); break; - case Y_MODE: + case Y: _UR.write(1); _UL.write(1); _LR.write(0); @@ -121,12 +120,12 @@ { AnalogIn S(_S); - float measured_val = 0.0f; + float measured_val = 0.0f; - for( i = 0; i < _samples ; i++) - { - measured_val += S.read(); - } + for( int i = 0; i < n_samples; i++) + { + measured_val += S.read(); + } - return measured_val/_samples; + return ( measured_val/n_samples); }