public static int setCalibration(CAL_POINT[] lcd, CAL_POINT[] tp)
        {
            CalibrationMatrix.div = ((tp[0].x - tp[2].x) * (tp[1].y - tp[2].y)) -
                            ((tp[1].x - tp[2].x) * (tp[0].y - tp[2].y));

            if (CalibrationMatrix.div == 0)
            {
                return 0;
            }

            CalibrationMatrix.a = ((lcd[0].x - lcd[2].x) * (tp[1].y - tp[2].y)) -
                          ((lcd[1].x - lcd[2].x) * (tp[0].y - tp[2].y));

            CalibrationMatrix.b = ((tp[0].x - tp[2].x) * (lcd[1].x - lcd[2].x)) -
                          ((lcd[0].x - lcd[2].x) * (tp[1].x - tp[2].x));

            CalibrationMatrix.c = (tp[2].x * lcd[1].x - tp[1].x * lcd[2].x) * tp[0].y +
                          (tp[0].x * lcd[2].x - tp[2].x * lcd[0].x) * tp[1].y +
                          (tp[1].x * lcd[0].x - tp[0].x * lcd[1].x) * tp[2].y;

            CalibrationMatrix.d = ((lcd[0].y - lcd[2].y) * (tp[1].y - tp[2].y)) -
                          ((lcd[1].y - lcd[2].y) * (tp[0].y - tp[2].y));

            CalibrationMatrix.e = ((tp[0].x - tp[2].x) * (lcd[1].y - lcd[2].y)) -
                          ((lcd[0].y - lcd[2].y) * (tp[1].x - tp[2].x));

            CalibrationMatrix.f = (tp[2].x * lcd[1].y - tp[1].x * lcd[2].y) * tp[0].y +
                          (tp[0].x * lcd[2].y - tp[2].x * lcd[0].y) * tp[1].y +
                          (tp[1].x * lcd[0].y - tp[0].x * lcd[1].y) * tp[2].y;

            CalibrationMatrix.LastUpdated = DateTime.Now;
            return 1;
        }
        private async void CalibrateTouch()
        {
            //5 point calibration
            CAL_POINT[] touchPoints = new CAL_POINT[5];
            touchPoints[0] = new CAL_POINT();
            touchPoints[1] = new CAL_POINT();
            touchPoints[2] = new CAL_POINT();
            touchPoints[3] = new CAL_POINT();
            touchPoints[4] = new CAL_POINT();

            CAL_POINT[] screenPoints = new CAL_POINT[5];
            screenPoints[0] = new CAL_POINT();
            screenPoints[1] = new CAL_POINT();
            screenPoints[2] = new CAL_POINT();
            screenPoints[3] = new CAL_POINT();
            screenPoints[4] = new CAL_POINT();

            ILI9341.fillRect(display1, 0, 0, 240, 320, 0x0000);

            ILI9341.LineDrawH(display1, 30, 30, 1, 0xFFFF);
            ILI9341.LineDrawV(display1, 30, 30, 1, 0xFFFF);
            while (TSC2046.pressure < 5) { TSC2046.CheckTouch(); }//wait for pen pressure
            screenPoints[0].x = 30;
            screenPoints[0].y = 30;
            touchPoints[0].x = TSC2046.tp_x;
            touchPoints[0].y = TSC2046.tp_y;
            while (TSC2046.pressure > 1) { TSC2046.CheckTouch(); } // wait for release of pen


            ILI9341.LineDrawH(display1, 30, 300, 1, 0xFFFF);
            ILI9341.LineDrawV(display1, 30, 300, 1, 0xFFFF);
            while (TSC2046.pressure < 5) { TSC2046.CheckTouch(); }//wait for pen pressure
            screenPoints[1].x = 300;
            screenPoints[1].y = 30;
            touchPoints[1].x = TSC2046.tp_x;
            touchPoints[1].y = TSC2046.tp_y;
            while (TSC2046.pressure > 1) { TSC2046.CheckTouch(); }// wait for release of pen

            ILI9341.LineDrawH(display1, 120, 160, 1, 0xFFFF);
            ILI9341.LineDrawV(display1, 120, 160, 1, 0xFFFF);
            while (TSC2046.pressure < 5) { TSC2046.CheckTouch(); }//wait for pen pressure
            screenPoints[2].x = 160;
            screenPoints[2].y = 120;
            touchPoints[2].x = TSC2046.tp_x;
            touchPoints[2].y = TSC2046.tp_y;
            while (TSC2046.pressure > 1) { TSC2046.CheckTouch(); }// wait for release of pen


            ILI9341.LineDrawH(display1, 210, 30, 1, 0xFFFF);
            ILI9341.LineDrawV(display1, 210, 30, 1, 0xFFFF);
            while (TSC2046.pressure < 5) { TSC2046.CheckTouch(); }//wait for pen pressure
            screenPoints[3].x = 30;
            screenPoints[3].y = 210;
            touchPoints[3].x = TSC2046.tp_x;
            touchPoints[3].y = TSC2046.tp_y;
            while (TSC2046.pressure > 1) { TSC2046.CheckTouch(); }// wait for release of pen


            ILI9341.LineDrawH(display1, 210, 300, 1, 0xFFFF);
            ILI9341.LineDrawV(display1, 210, 300, 1, 0xFFFF);
            while (TSC2046.pressure < 5) { TSC2046.CheckTouch(); }//wait for pen pressure
            screenPoints[4].x = 300;
            screenPoints[4].y = 210;
            touchPoints[4].x = TSC2046.tp_x;
            touchPoints[4].y = TSC2046.tp_y;
            while (TSC2046.pressure > 1) { TSC2046.CheckTouch(); }// wait for release of pen

            TSC2046.setCalibration(screenPoints, touchPoints);
            if (await TSC2046.CalibrationMatrix.SaveCalData(CalibrationFilename))
            {
                //Success
            }
            else
            {
                //Handle error
            }
            ILI9341.Flush(display1);
        }