ERIS CORE
touch.cpp
Go to the documentation of this file.
1 #include "touch.h"
2 #include "HSI.h"
3 #include "Eris.h"
4 
5 #define Z_THRESHOLD 600
6 #define Z_THRESHOLD_INT 75
7 #define MSEC_THRESHOLD 50
8 #define SPI_SETTING SPISettings(2000000, MSBFIRST, SPI_MODE0)
9 
10 bool Touch::begin(){
11  Serial.println("Touch:begin");
12  SPI1.setMISO(SDI);
13  SPI1.setMOSI(SDO);
14  SPI1.setSCK(SCLK);
15  SPI1.begin();
16  pinMode(csPin, OUTPUT);
17  digitalWrite(csPin, HIGH);
18  tirqPin = 255; //255 = not used
19  return true;
20 }
21 
22 static int16_t besttwoavg( int16_t x , int16_t y , int16_t z ) {
23  int16_t da, db, dc;
24  int16_t reta = 0;
25  if ( x > y ) da = x - y; else da = y - x;
26  if ( x > z ) db = x - z; else db = z - x;
27  if ( z > y ) dc = z - y; else dc = y - z;
28  if ( da <= db && da <= dc ) reta = (x + y) >> 1;
29  else if ( db <= da && db <= dc ) reta = (x + z) >> 1;
30  else reta = (y + z) >> 1; // else if ( dc <= da && dc <= db ) reta = (x + y) >> 1;
31  return (reta);
32 }
33 
34 void FLASHMEM Touch::update()
35 {
36  //Serial.println("Touch:update");
37  int16_t data[6];
38 
39  if (!isrWake) return;
40  uint32_t now = millis();
41  if (now - msraw < MSEC_THRESHOLD) return;
42 
43  SPI1.beginTransaction(SPI_SETTING);
44  digitalWrite(csPin, LOW);
45  SPI1.transfer(0xB1 /* Z1 */);
46  int16_t z1 = SPI1.transfer16(0xC1 /* Z2 */) >> 3;
47  int z = z1 + 4095;
48  int16_t z2 = SPI1.transfer16(0x91 /* X */) >> 3;
49  z -= z2;
50  if (z >= Z_THRESHOLD) {
51  SPI1.transfer16(0x91 /* X */); // dummy X measure, 1st is always noisy
52  data[0] = SPI1.transfer16(0xD1 /* Y */) >> 3;
53  data[1] = SPI1.transfer16(0x91 /* X */) >> 3; // make 3 x-y measurements
54  data[2] = SPI1.transfer16(0xD1 /* Y */) >> 3;
55  data[3] = SPI1.transfer16(0x91 /* X */) >> 3;
56  }
57  else data[0] = data[1] = data[2] = data[3] = 0; // Compiler warns these values may be used unset on early exit.
58  data[4] = SPI1.transfer16(0xD0 /* Y */) >> 3; // Last Y touch power down
59  data[5] = SPI1.transfer16(0) >> 3;
60  digitalWrite(csPin, HIGH);
61  SPI1.endTransaction();
62  //Serial.printf("z=%d :: z1=%d, z2=%d ", z, z1, z2);
63  if (z < 0) z = 0;
64  if (z < Z_THRESHOLD) { // if ( !touched ) {
65  // Serial.println();
66  zraw = 0;
67  if (z < Z_THRESHOLD_INT) { // if ( !touched ) {
68  if (255 != tirqPin) isrWake = false;
69  }
70  return;
71  }
72  zraw = z;
73 
74  // Average pair with least distance between each measured x then y
75  //Serial.printf(" z1=%d,z2=%d ", z1, z2);
76  //Serial.printf("p=%d, %d,%d %d,%d %d,%d", zraw,
77  //data[0], data[1], data[2], data[3], data[4], data[5]);
78  int16_t x = besttwoavg( data[0], data[2], data[4] );
79  int16_t y = besttwoavg( data[1], data[3], data[5] );
80 
81  //Serial.printf(" %d,%d", x, y);
82  //Serial.println();
83  if (z >= Z_THRESHOLD) {
84  msraw = now; // good read completed, set wait
85  switch (rotation) {
86  case 0:
87  xraw = 4095 - y;
88  yraw = x;
89  break;
90  case 1:
91  xraw = x;
92  yraw = y;
93  break;
94  case 2:
95  xraw = y;
96  yraw = 4095 - x;
97  break;
98  default: // 3
99  xraw = 4095 - x;
100  yraw = 4095 - y;
101  }
102  }
103 }
104 
106 {
107  //Serial.println("Touch:touched");
108  return (zraw >= Z_THRESHOLD);
109 }
110 
111 TS_Point Touch::getPoint()
112 {
113  float fx,fy;
114  int16_t x,y;
115  //Serial.println("Touch:getPoint");
116  //update();
117 
118  //dynamic calibration is supported by monitoring the raw touch limits
119  //which are used for the screen space translation calculations
120  if (xraw < _raw_minx) _raw_minx = xraw;
121  if (xraw > _raw_maxx) _raw_maxx = xraw;
122  if (yraw < _raw_miny) _raw_miny = yraw;
123  if (yraw > _raw_maxy) _raw_maxy = yraw;
124 
125  //normalize the raw touch readings to percentage of full scale (0 to 1)
126  fx = (xraw - _raw_minx)/(1.0 * (_raw_maxx - _raw_minx));
127  fy = (yraw - _raw_miny)/(1.0 * (_raw_maxy - _raw_miny));
128  //convert normalized values to screen space
129  x = (int16_t)(fx * SCREEN_WIDTH);
130  y = (int16_t)(fy * SCREEN_HEIGHT);
131  (x < 0)?x=0:x=x;
132  (y < 0)?y=0:y=y;
133  return TS_Point(SCREEN_WIDTH-x, SCREEN_HEIGHT-y, zraw);
134 }
bool begin()
Definition: touch.cpp:10
void update()
Definition: touch.cpp:34
uint8_t rotation
Definition: Touch.h:41
uint32_t msraw
Definition: Touch.h:42
uint16_t _raw_maxy
Definition: Touch.h:46
uint16_t _raw_maxx
Definition: Touch.h:45
uint8_t csPin
Definition: Touch.h:41
uint16_t _raw_miny
Definition: Touch.h:44
int16_t yraw
Definition: Touch.h:19
uint8_t tirqPin
Definition: Touch.h:41
bool touched()
Definition: touch.cpp:105
uint16_t _raw_minx
Definition: Touch.h:43
TS_Point getPoint()
Definition: touch.cpp:111
int16_t zraw
Definition: Touch.h:19
int16_t xraw
Definition: Touch.h:19
static int16_t besttwoavg(int16_t x, int16_t y, int16_t z)
Definition: touch.cpp:22