ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
calibrate.c
Go to the documentation of this file.
1 
26 #include <stdint.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <math.h>
30 
31 #include "user_config.h"
32 
33 #include "matrix.h"
34 
35 #ifdef DISPLAY
36 
40 #ifndef XPT2046
41 #error XPT2046 is not defined
42 #endif
43 
44 #include "xpt2046.h"
45 #include "display/ili9341.h"
46 
48 mat_t tft_calX;
49 mat_t tft_calY;
50 
52 int tft_is_calibrated = 0;
53 
54 
62 {
63  if(!tft_is_calibrated)
64  {
66  return(0);
67  }
68  return(1);
69 
70 }
71 
81 {
82  int i;
83  uint16_t w,h,X1,X2,Y1,Y2;
84  mat_t MatAI;
85  mat_t MatX = MatAlloc(5,1);
86  mat_t MatY = MatAlloc(5,1);
87  mat_t MatA = MatAlloc(5,3);
88 
89  w = win->w;
90  h = win->h;
91 
92  if(tft_is_calibrated)
93  {
94  MatFree(tft_calX);
95  MatFree(tft_calY);
96  tft_is_calibrated = 0;
97  }
98 
99  tft_fillWin(win, win->bg);
100  if(win->rotation & 1)
101  tft_set_font(win, 2);
102  else
103  tft_set_font(win, 1);
104  tft_printf("Please Calibrate");
105 
106 
107  MatX.data[0][0] = w / 4;
108  MatY.data[0][0] = h / 4;
109 
110  MatX.data[1][0] = w * 3 / 4;
111  MatY.data[1][0] = h / 4;
112 
113  MatX.data[2][0] = w / 4;
114  MatY.data[2][0] = h * 3 / 4;
115 
116  MatX.data[3][0] = w * 3 / 4;
117  MatY.data[3][0] = h * 3 / 4;
118 
119  MatX.data[4][0] = w / 2;
120  MatY.data[4][0] = h / 2;
121 
122 #if MATDEBUG & 2
123  printf("X\n");
124  MatPrint(MatX);
125  printf("Y\n");
126  MatPrint(MatY);
127 #endif
128 
129  for(i=0;i<5;++i)
130  {
131  X1 = MatX.data[i][0];
132  Y1 = MatY.data[i][0];
133  tft_fillCircle (win , X1,Y1, 5 , ILI9341_WHITE);
134  tft_set_textpos(win, 0,0);
135  if(win->rotation & 1)
136  tft_set_font(win, 1);
137  else
138  tft_set_font(win, 0);
139 
140  tft_printf(win,"touch point %3d,%3d", (int)X1, (int)Y1);
141  tft_cleareol(win);
142 
143  // This is the only XPT2046 specific bit of code in the file
144  while( XPT2046_key((uint16_t *)&X2, (uint16_t *)&Y2) == 0 )
145  optimistic_yield(1000);
146 
147  MatA.data[i][0] = (float)X2;
148  MatA.data[i][1] = (float)Y2;
149  MatA.data[i][2] = 1.0;
150  // reset pixel
151  tft_fillCircle (win , X1,Y1, 5 , win->bg);
152  tft_drawPixel(win, X1, Y1, win->bg);
153  }
154 
155 
158  MatAI = PseudoInvert(MatA);
159 
160  // Calibration values
161  tft_calX = MatMul(MatAI,MatX);
162  tft_calY = MatMul(MatAI,MatY);
163 
164 #if MATDEBUG & 2
165  printf("A\n");
166  MatPrint(MatA);
167 
168  printf("Invert\n");
169  MatPrint(MatAI);
170 
171  printf("tft_calX\n");
172  MatPrint(tft_calX);
173 
174  printf("tft_calY\n");
175  MatPrint(tft_calY);
176 #endif
177 
178  MatFree(MatA);
179  MatFree(MatX);
180  MatFree(MatY);
181  MatFree(MatAI);
182 
183  return( (tft_is_calibrated = 1) );
184 }
185 
193 MEMSPACE
194 int tft_touch_map(window *win, int16_t *X, int16_t *Y)
195 {
196  int16_t X2,Y2;
197  float XF,YF;
198 
200  if(!tft_check_calibrated(win))
201  {
202  return(0);
203  }
204 
205  XF = (float)*X;
206  YF = (float)*Y;
207 #if MATDEBUG & 2
208  printf("tft_touch_map: raw: X:%.0f,Y:%.0f\n", (double)XF, (double)YF);
209 #endif
210  X2 = (uint16_t)(tft_calX.data[0][0] * XF + tft_calX.data[1][0] * YF + tft_calX.data[2][0]);
211  Y2 = (uint16_t)(tft_calY.data[0][0] * XF + tft_calY.data[1][0] * YF + tft_calY.data[2][0]);
212 #if MATDEBUG & 2
213  printf("tft_touch_map: cal: X:%3d,Y:%3d\n", (int)X2, (int)Y2);
214 #endif
215 
216  // force result to fix in window limits
217  tft_clip_xy(win,X,Y);
218 
219  *X = X2;
220  *Y = Y2;
221  return(1);
222 }
223 
224 
231 MEMSPACE
232 int tft_map_test(window *win, int points)
233 {
234  int i;
235  int16_t X1,Y1;
236 
237  tft_fillWin(win, win->bg);
238  if(win->rotation & 1)
239  tft_set_font(win, 1);
240  else
241  tft_set_font(win, 0);
242  tft_printf(win,"press %d points", points);
243 
245  if(!tft_check_calibrated(win))
246  {
247  return(0);
248  }
249 
250  for(i=0;i<points;++i)
251  {
252  while( XPT2046_key((uint16_t *)&X1, (uint16_t *)&Y1) == 0 )
253  optimistic_yield(1000);
254 
255  tft_touch_map(win,&X1,&Y1);
256  tft_fillCircle (win , X1,Y1, 5 , ILI9341_WHITE);
257  }
258  return(1);
259 }
260 
266 MEMSPACE
267 int tft_touch_xy_raw(window *win, uint16_t *X, uint16_t *Y)
268 {
269  int T;
270  T = XPT2046_xy_raw(X, Y);
271  tft_touch_map(win,X,Y);
272  return(T);
273 }
274 
280 MEMSPACE
281 int tft_touch_xy(window *win, uint16_t *X, uint16_t *Y)
282 {
283  int T;
284  T = XPT2046_xy_filtered(X, Y);
285  tft_touch_map(win,X,Y);
286  return(T);
287 }
288 
289 
295 MEMSPACE
296 int tft_touch_key(window *win, uint16_t *X, uint16_t *Y)
297 {
298 
299  int T;
300  T = XPT2046_key(X,Y);
301  tft_touch_map(win,X,Y);
302  return(T);
303 }
304 #endif //DISPLAY
int16_t w
Definition: ili9341.h:40
MEMSPACE int tft_touch_map(window *win, int16_t *X, int16_t *Y)
float ** data
Definition: matrix.h:28
MEMSPACE void tft_set_textpos(window *win, int16_t x, int16_t y)
Set current window text pointer in characters (per current rotation) - overall font bounding box...
Definition: ili9341.c:1254
int16_t h
Definition: ili9341.h:41
unsigned short uint16_t
Definition: send.c:18
Master include file for project Includes all project includes and defines here.
uint16_t bg
Definition: ili9341.h:45
MEMSPACE void MatPrint(mat_t matrix)
Print a matrix.
Definition: matrix.c:187
Cordic_T X
Main Cordic routine - used for basic trig and vector rotations We use fixed point numbers...
Definition: cordic.c:102
uint8_t rotation
Definition: ili9341.h:46
#define ILI9341_WHITE
Cordic_T Y
Definition: cordic.c:102
MEMSPACE void tft_set_font(window *win, uint16_t index)
Set current font size (per current rotation)
Definition: ili9341.c:1267
MEMSPACE int tft_touch_xy_raw(window *win, uint16_t *X, uint16_t *Y)
void optimistic_yield(uint32_t interval_us)
Definition: user_task.c:102
MEMSPACE mat_t MatAlloc(int rows, int cols)
Allocate a matrix.
Definition: matrix.c:48
MEMSPACE int tft_check_calibrated(window *win)
int XPT2046_xy_raw(uint16_t *X, uint16_t *Y)
Definition: matrix.h:27
MEMSPACE void tft_clip_xy(window *win, int16_t *X, int16_t *Y)
Clip X,Y to fix inside specifiied window.
Definition: ili9341.c:1114
MEMSPACE int tft_map_test(window *win, int points)
matrix functions
void tft_drawPixel(window *win, int16_t x, int16_t y, int16_t color)
Pixel functions
Definition: ili9341.c:717
MEMSPACE int XPT2046_xy_filtered(uint16_t *X, uint16_t *Y)
MEMSPACE int XPT2046_key(uint16_t *X, uint16_t *Y)
#define MEMSPACE
Definition: cpu.h:25
MEMSPACE int tft_touch_xy(window *win, uint16_t *X, uint16_t *Y)
void tft_fillWin(window *win, uint16_t color)
Fill functions
Definition: ili9341.c:334
MEMSPACE void MatFree(mat_t matF)
Free a matrix.
Definition: matrix.c:116
MEMSPACE int printf(const char *format,...)
MEMSPACE mat_t MatMul(mat_t MatA, mat_t MatB)
Multiply two matrix.
Definition: matrix.c:490
Definition: ili9341.h:34
MEMSPACE void tft_cleareol(window *win)
Character and String functions
Definition: ili9341.c:1601
MEMSPACE void tft_fillCircle(window *win, int16_t x0, int16_t y0, int16_t r, uint16_t color)
Fill circle.
ili9341 driver inspired by Adafruit ili9341 code All code in this file has been rewritten by Mike Gor...
MEMSPACE int tft_printf(window *win, const char *fmt,...)
tft_printf function
Definition: tft_printf.c:52
MEMSPACE int tft_touch_key(window *win, uint16_t *X, uint16_t *Y)
MEMSPACE int tft_touch_calibrate(window *win)
MEMSPACE mat_t PseudoInvert(mat_t MatA)
Calculate Pseudo Matrix Inverse Used for least square fitting of non square matrix with excess soluti...
Definition: matrix.c:456