为什么这个'white'变量一定要定义使用它的运算前面(和图片相同的位置我下面原文件标注的232行),不能定义在函数的开头(和图片相同的位置我下面标注的189行)?下面是原文件
void xpt2046_calibration(void)
{ const char* lcd_name = "lcd";
const char* touch_name = "xpt0";
const uint32_t black = 0x0000;
**189__**
rt_device_t lcd = rt_device_find(lcd_name);
if (lcd == RT_NULL)
{
LOG_E(LOG_TAG" cannot find lcd device named %s\n", lcd_name);
return;
}
if (rt_device_open(lcd, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
{
LOG_E(LOG_TAG" cannot open lcd device named %s\n", lcd_name);
return;
}
rt_xpt2046_t touch = (rt_xpt2046_t)rt_device_find(touch_name);
if (touch == RT_NULL)
{
LOG_E(LOG_TAG" cannot find touch device named %s\n", touch_name);
return;
}
if (rt_device_open(touch, RT_DEVICE_FLAG_INT_RX) != RT_EOK)
{
LOG_E(LOG_TAG" cannot open touch device named %s\n", touch_name);
return;
}
struct rt_device_graphic_info lcd_info;
rt_device_control(lcd, RTGRAPHIC_CTRL_GET_INFO, &lcd_info);
for (rt_uint32_t y = 0; y < lcd_info.height; ++y)
{
rt_graphix_ops(lcd)->draw_hline((const char *)(&black), 0, lcd_info.width, y);
}
rt_uint32_t cross_size = (lcd_info.width > lcd_info.height ? lcd_info.height : lcd_info.width) / 10;
rt_uint32_t x0 = cross_size;
rt_uint32_t y0 = cross_size;
rt_uint32_t x1 = lcd_info.width - cross_size;
rt_uint32_t y1 = cross_size;
rt_uint32_t x2 = lcd_info.width - cross_size;
rt_uint32_t y2 = lcd_info.height - cross_size;
rt_uint32_t x3 = cross_size;
rt_uint32_t y3 = lcd_info.height - cross_size;
// Upper left cross
** 232 const rt_uint32_t white = 0xFFFFFFFF;__**
rt_graphix_ops(lcd)->draw_hline((const char *)(&white), 0, x0+cross_size, y0);
rt_graphix_ops(lcd)->draw_vline((const char *)(&white), x0, 0, y0+cross_size);
lcd_show_string(50,50,16,"Touch screen calibration");
touch->min_raw_x = 0;
touch->min_raw_y = 0;
touch->max_raw_x = 4096;
touch->max_raw_y = 4096;
touch->parent.info.range_x = 4096;
touch->parent.info.range_y = 4096;
rt_uint16_t x_raw[4];
rt_uint16_t y_raw[4];
rt_uint8_t raw_idx = 0;
rt_memset(&x_raw, 0, sizeof(rt_uint32_t)*4);
rt_memset(&y_raw, 0, sizeof(rt_uint32_t)*4);
while (1)
{ const rt_uint32_t white = 0xFFFFFFFF;
struct rt_touch_data read_data;
rt_memset(&read_data, 0, sizeof(struct rt_touch_data));
if (rt_device_read(touch, 0, &read_data, 1) == 1)
{
x_raw[raw_idx] = read_data.x_coordinate;
y_raw[raw_idx++] = read_data.y_coordinate;
LOG_I(LOG_TAG" %d point capture", raw_idx-1);
for (rt_uint32_t y = 0; y < lcd_info.height; ++y)
{
rt_graphix_ops(lcd)->draw_hline((const char *)(&black), 0, lcd_info.width, y);
}
rt_thread_mdelay(1000);
if (raw_idx >= 4)
{
break;
}
switch(raw_idx)
{
case 1:
// Upper right cross
rt_graphix_ops(lcd)->draw_hline((const char *)(&white), x1-cross_size, lcd_info.width, y1);
rt_graphix_ops(lcd)->draw_vline((const char *)(&white), x1, 0, y1+cross_size);
rt_thread_mdelay(1000);
break;
case 2:
// lower right cross
rt_graphix_ops(lcd)->draw_hline((const char *)(&white), x2-cross_size, lcd_info.width, y2);
rt_graphix_ops(lcd)->draw_vline((const char *)(&white), x2, y2-cross_size, lcd_info.height);
rt_thread_mdelay(1000);
break;
case 3:
// lower left cross
rt_graphix_ops(lcd)->draw_hline((const char *)(&white), 0, x3+cross_size, y3);
rt_graphix_ops(lcd)->draw_vline((const char *)(&white), x3, y3-cross_size, lcd_info.height);
rt_thread_mdelay(1000);
break;
default:
break;
}
}
rt_thread_mdelay(10);
}
rt_uint32_t min_x = (x_raw[0]+x_raw[3])/2;
rt_uint32_t max_x = (x_raw[1]+x_raw[2])/2;
rt_uint32_t min_y = (y_raw[0]+y_raw[1])/2;
rt_uint32_t max_y = (y_raw[2]+y_raw[3])/2;
rt_uint32_t x_raw_cnt_per_pixel = (max_x-min_x) / (x1-x0);
rt_uint32_t y_raw_cnt_per_pixel = (max_y-min_y) / (y2-y1);
min_x -= cross_size * x_raw_cnt_per_pixel;
max_x += cross_size * x_raw_cnt_per_pixel;
min_y -= cross_size * y_raw_cnt_per_pixel;
max_y += cross_size * y_raw_cnt_per_pixel;
touch->min_raw_x = min_x;
touch->min_raw_y = min_y;
touch->max_raw_x = max_x;
touch->max_raw_y = max_y;
touch->parent.info.range_x = lcd_info.width;
touch->parent.info.range_y = lcd_info.height;
LOG_I(LOG_TAG" Calibration result, min_x:%d\n, min_y:%d\n, max_x:%d\n, max_y:%d\n", min_x, min_y, max_x, max_y);
rt_thread_mdelay(1000);
rt_device_close(lcd);
rt_device_close(touch);
}