 
 void loop(void)
 {
-  int pen_x;
-  int pen_y;
+  switch (state)
+  {
+    case 0:
+      if (ts.touched())
+      {
+        TS_Point p = ts.getPoint();
+        uint16_t pen_x = p.x;
+        uint16_t pen_y = p.y;
 
-  if (touch_read(&pen_x, &pen_y))
-  {
-    if (pen_x < minx) minx = pen_x;
-    if (pen_y < miny) miny = pen_y;
-    if (pen_x > maxx) maxx = pen_x;
-    if (pen_y > maxy) maxy = pen_y;
-  }
-
-  if (minx != last_minx || miny != last_miny || maxx != last_maxx || maxy != last_maxy)
-  {
-    Serial.printf("// touchscreen calibration data\n");
-    Serial.printf("#define TS_MINX %d\n", minx);
-    Serial.printf("#define TS_MINY %d\n", miny);
-    Serial.printf("#define TS_MAXX %d\n", maxx);
-    Serial.printf("#define TS_MAXY %d\n\n", maxy);
-
-    last_minx = minx;
-    last_miny = miny;
-    last_maxx = maxx;
-    last_maxy = maxy;
+        undraw_target(cal_index);
+        calibrate[cal_index].raw_x = (int) (calibrate[cal_index].raw_x * (count-1)/count + pen_x/count);
+        calibrate[cal_index].raw_y = (int) (calibrate[cal_index].raw_y * (count-1)/count + pen_y/count);
+        state = 1;
+      }
+      break;
+    case 1:
+      if (! ts.touched())
+      {
+        if (++cal_index >= NumCalPoints)
+        {
+          Serial.printf("count=%d\n", count);
+          for (int ndx = 0; ndx < NumCalPoints; ndx += 1)
+          {
+            Serial.printf("%02d: x=%3d, y=%3d, raw_x=%4d, raw_y=%4d\n",
+                          ndx, calibrate[ndx].x, calibrate[ndx].y,
+                          calibrate[ndx].raw_x, calibrate[ndx].raw_y); 
+          }
+          cal_index = 0;
+          ++count;
+        }
+        state = 0;
+        draw_target(cal_index);
+      }
+      break;
   }
 }
+

  Serial.printf("%s %s.%s\n", ProgramName, Version, MinorVersion); 

  // initialize the pins, mode and level
  pinMode(T_CS, OUTPUT);
  digitalWrite(T_CS, HIGH);
  pinMode(T_IRQ, INPUT_PULLUP);

  // initiaize some state
  PenDown = false;

  // kick off the SPI system
  SPI.begin();

////////////////////////////////////////////////////////////////////////////////

#include <SPI.h>
#include <Adafruit_ILI9341.h>
#include <XPT2046_Touchscreen.h>

const char *ProgramName = "Calibrate";
const char *Version = "1.2";
const char *MinorVersion = "0";

// TFT display chip-select and data/control pins
#define TFT_RST     8
#define TFT_DC      9
#define TFT_CS      10

// pins for touchscreen and IRQ line
#define TS_CS       4
#define TS_IRQ      3

#define SCREEN_BG   ILI9341_BLACK
#define CROSS_FG    ILI9341_RED

// variables
typedef struct cal_data
{
  uint16_t x;
  uint16_t y;
  uint16_t raw_x;
  uint16_t raw_y;
} CalData;

#define NumCalPoints  9
CalData calibrate[NumCalPoints];

int cal_index = 0;    // index into calibrate[]

int state;
int count;

// Declare the display interface
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);

// declare the touchscreen interface
XPT2046_Touchscreen ts(TS_CS);

//----------------------------------------
// Interrupt - pen went down (or up).
//
// Maintain the correct PenDown state.
//----------------------------------------

#if 0
void touch_isr(void)
{
  PenDown = (digitalRead(TS_IRQ) == 0);
  Serial.printf("%d:PenDown=%s\n", millis(), (PenDown) ? "true" : "false");
}
#endif

//-----------------------------------------------
// Initialize PixelVFO.
