diff -r 000000000000 -r b74b0e4902c3 main/calibration.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/calibration.c Sat Oct 20 13:23:15 2018 +0200 @@ -0,0 +1,142 @@ +/** + * @file calibration.c + * @brief TFT screen calibration. + */ + +#include "config.h" + + +static const char *TAG = "calibration"; + + +extern uint16_t tp_xleft; +extern uint16_t tp_xright; +extern uint16_t tp_ytop; +extern uint16_t tp_ybottom; + + +// Distance of the testpoints from the corders. +#define CAL_DISTANCE 15 + + +int TS_set_calibration(int xleft, int xright, int ytop, int ybottom) +{ + if ((xleft > xright) || (ybottom > ytop)) { + ESP_LOGE(TAG, "Touch Calibration error X=%d..%d Y=%d..%d", xleft, xright, ybottom, ytop); + return 1; + } + + tp_xleft = xleft; + tp_xright = xright; + tp_ytop = ytop; + tp_ybottom = ybottom; + ESP_LOGI(TAG, "Touch Calibration X=%d..%d Y=%d..%d", xleft, xright, ybottom, ytop); + + return 0; +} + + + +void Calibration_Init(void) +{ + _fg = TFT_ORANGE; + TFT_setFont(DEJAVU24_FONT, NULL); + TFT_print("Scherm calibratie", CENTER, 60); + TFT_setFont(DEJAVU18_FONT, NULL); + TFT_print("Druk op de oplichtende stippen", CENTER, 90); +} + + + +/* + * Run in a blocking loop + */ +void Calibration_Loop(void) +{ + int tx, ty, pass, cx[8], cy[8], xl = 0, xh = 0, yl = 0, yh = 0; + int offset; + + pass = 0; + ESP_LOGI(TAG, "Touchscreen calibration"); + + while (1) { + switch (pass) { + case 0: + case 4: TFT_fillCircle(320 - CAL_DISTANCE, 240 - CAL_DISTANCE, 5, TFT_BLACK); + TFT_fillCircle(CAL_DISTANCE, CAL_DISTANCE, 5, TFT_YELLOW); + xl = 2800; + xh = 3800; + yl = 2800; + yh = 3800; + break; + case 1: + case 5: TFT_fillCircle(CAL_DISTANCE, CAL_DISTANCE, 5, TFT_BLACK); + TFT_fillCircle(320 - CAL_DISTANCE, CAL_DISTANCE, 5, TFT_YELLOW); + xl = 2800; + xh = 3800; + yl = 100; + yh = 1000; + break; + case 2: + case 6: TFT_fillCircle(320 - CAL_DISTANCE, CAL_DISTANCE, 5, TFT_BLACK); + TFT_fillCircle(CAL_DISTANCE, 240 - CAL_DISTANCE, 5, TFT_YELLOW); + xl = 100; + xh = 1000; + yl = 2800; + yh = 3800; + break; + case 3: + case 7: TFT_fillCircle(CAL_DISTANCE, 240 - CAL_DISTANCE, 5, TFT_BLACK); + TFT_fillCircle(320 - CAL_DISTANCE, 240 - CAL_DISTANCE, 5, TFT_YELLOW); + xl = 100; + xh = 1000; + yl = 100; + yh = 1000; + break; + } + if (TFT_read_touch(&tx, &ty, 1)) { // Raw read touch. + printf(" Calibrarion loop %d %d %d\n", pass, tx, ty); + if ((tx >= xl) && (tx <= xh) && (ty >= yl) && (ty <= yh)) { + cx[pass] = tx; + cy[pass] = ty; + pass++; + if (pass == 8) { + break; + } + } else { + SoundPlay(SOUND_Warn); + } + WaitTouchReleased(); + } + vTaskDelay(100 / portTICK_PERIOD_MS); + } + + TFT_fillScreen(TFT_BLACK); + TFT_setFont(DEJAVU18_FONT, NULL); + TFT_print("Scherm calibratie is klaar.", CENTER, 60); + /* + * Calculate the new calibration values. + * The testdots were 10 pixels of the corners, + * so take that in account. + */ + offset = (CAL_DISTANCE * 3800) / 240; + config.ts_xleft = ((cx[2] + cx[3] + cx[6] + cx[7]) / 4) - offset; + config.ts_xright = ((cx[0] + cx[1] + cx[4] + cx[5]) / 4) + offset; + offset = (CAL_DISTANCE * 3800) / 320; + config.ts_ytop = ((cy[0] + cy[2] + cy[4] + cy[6]) / 4) + offset; + config.ts_ybottom = ((cy[1] + cy[3] + cy[5] + cy[7]) / 4) - offset; + TS_set_calibration(config.ts_xleft, config.ts_xright, config.ts_ytop, config.ts_ybottom); + write_config(); + + Buttons_Add(130, 200, 60, 40, "Ok", 0); + Buttons_Show(); + while (1) { + vTaskDelay(20 / portTICK_PERIOD_MS); + if (Buttons_Scan() == 0) { + return; + break; + } + } +} + +