touch.c 18.9 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8 9 10
 *
 * Change Logs:
 * Date           Author       Notes
 * 2017-12-30    Sundm75       first version
 */
 
11 12
#include <rtthread.h>
#include <rtdevice.h>
13 14 15 16 17 18 19 20
#include <stdbool.h>
#include <drivers/spi.h>
#include "ls1c.h"
#include "ls1c_gpio.h"  
#include "ls1c_spi.h"
#include "drv_spi.h"
#include "touch.h"

21
#ifdef XPT2046_USING_TOUCH
22

23 24 25 26 27 28
#include <rtgui/calibration.h>
#include <rtgui/event.h>
#include <rtgui/kbddef.h>
#include <rtgui/rtgui_server.h>
#include <rtgui/rtgui_system.h>

29 30 31

//竖屏幕 不需要  _ILI_HORIZONTAL_DIRECTION_
//横屏幕 需要  _ILI_HORIZONTAL_DIRECTION_
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

//#define _ILI_HORIZONTAL_DIRECTION_

#if defined(_ILI_HORIZONTAL_DIRECTION_)
#define X_WIDTH 272
#define Y_WIDTH 480
#else
#define X_WIDTH 480
#define Y_WIDTH 272
#endif
/*
TOUCH INT: 84
*/
#define IS_TOUCH_UP()     gpio_get(TOUCH_INT_PIN)

47
#define led_gpio 52    // led1指示 
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

#define DUMMY                 0x00

/*
7  6 - 4  3      2     1-0
s  A2-A0 MODE SER/DFR PD1-PD0
*/
/* bit[1:0] power-down */
#define POWER_MODE0     (0) /* Power-Down Between Conversions. When */
                            /* each conversion is finished, the converter */
                            /* enters a low-power mode. At the start of the */
                            /* next conversion, the device instantly powers up */
                            /* to full power. There is no need for additional */
                            /* delays to ensure full operation, and the very first */
                            /* conversion is valid. The Y? switch is on when in */
                            /* power-down.*/
#define POWER_MODE1     (1) /* Reference is off and ADC is on. */
#define POWER_MODE2     (2) /* Reference is on and ADC is off. */
#define POWER_MODE3     (3) /* Device is always powered. Reference is on and */
                            /* ADC is on. */
/* bit[2] SER/DFR */
#define DIFFERENTIAL    (0<<2)
#define SINGLE_ENDED    (1<<2)
/* bit[3] mode */
#define MODE_12BIT      (0<<3)
#define MODE_8BIT       (1<<3)
/* bit[6:4] differential mode */
#define MEASURE_X       (((1<<2) | (0<<1) | (1<<0))<<4)
#define MEASURE_Y       (((0<<2) | (0<<1) | (1<<0))<<4)
#define MEASURE_Z1      (((0<<2) | (1<<1) | (1<<0))<<4)
#define MEASURE_Z2      (((1<<2) | (0<<1) | (0<<0))<<4)
/* bit[7] start */
#define START           (1<<7)

/* X Y change. */
#define TOUCH_MSR_X     (START | MEASURE_X | MODE_12BIT | DIFFERENTIAL | POWER_MODE0)
#define TOUCH_MSR_Y     (START | MEASURE_Y | MODE_12BIT | DIFFERENTIAL | POWER_MODE0)


87
/* 以下定义XPT2046 的触摸屏位置*/
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
#if defined(_ILI_HORIZONTAL_DIRECTION_)
#define MIN_X_DEFAULT   2047
#define MAX_X_DEFAULT   47
#define MIN_Y_DEFAULT   102 
#define MAX_Y_DEFAULT   1939 
#else
#define MIN_X_DEFAULT   47
#define MAX_X_DEFAULT   2047
#define MIN_Y_DEFAULT   1939 
#define MAX_Y_DEFAULT   102
#endif



#define SAMP_CNT 8                              //the adc array size
#define SAMP_CNT_DIV2 4                         //the middle of the adc array
#define SH   10                                 // Valve value


107
/*宏定义 */
108 109 110 111 112 113 114 115
#define TOUCH_SPI_X                 SPI1 
#define TOUCH_INT_PIN               84 
#define TOUCH_CS_PIN                49 
#define TOUCH_SCK_PIN               46
#define TOUCH_MISO_PIN              47
#define TOUCH_MOSI_PIN              48     


116
/*创建结构体将需要用到的东西进行打包*/
117 118
struct rtgui_touch_device 
{
119
    struct rt_device parent;                    /* 用于注册设备*/
120

121
    rt_uint16_t x, y;                           /* 记录读取到的位置值  */ 
122

123 124
    rt_bool_t calibrating;                      /* 触摸校准标志 */ 
    rt_touch_calibration_func_t calibration_func;/* 触摸函数 函数指针 */       
125

126 127
    rt_uint16_t min_x, max_x;                   /* 校准后 X 方向最小 最大值 */ 
    rt_uint16_t min_y, max_y;                   /* 校准后 Y 方向最小 最大值 */
128

129 130
    struct rt_spi_device * spi_device;          /* SPI 设备 用于通信 */ 
    struct rt_event event;                       /* 事件同步,用于“笔中断” */ 
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
};
static struct rtgui_touch_device *touch = RT_NULL;

static rt_err_t  touch_send_then_recv(struct rt_spi_device *device,
                               const void           *send_buf,
                               rt_size_t             send_length,
                               void                 *recv_buf,
                               rt_size_t             recv_length)
{
    rt_err_t result;
    struct rt_spi_message message;
    rt_uint8_t dummy[128] ;

    rt_memset(dummy, DUMMY, sizeof(dummy));

    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(device->bus != RT_NULL);

    result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
    if (result == RT_EOK)
    {
        if (device->bus->owner != device)
        {
            /* not the same owner as current, re-configure SPI bus */
            result = device->bus->ops->configure(device, &device->config);
            if (result == RT_EOK)
            {
                /* set SPI bus owner */
                device->bus->owner = device;
            }
            else
            {
                /* configure SPI bus failed */
                result = -RT_EIO;
                goto __exit;
            }
        }

        /* send data */
        message.send_buf   = send_buf;
        message.recv_buf   = RT_NULL;
        message.length     = send_length;
        message.cs_take    = 1;
        message.cs_release = 0;
        message.next       = RT_NULL;

        result = device->bus->ops->xfer(device, &message);
        if (result == 0)
        {
            result = -RT_EIO;
            goto __exit;
        }

        /* recv data */
        message.send_buf   = dummy;
        message.recv_buf   = recv_buf;
        message.length     = recv_length;
        message.cs_take    = 0;
        message.cs_release = 1;
        message.next       = RT_NULL;

        result = device->bus->ops->xfer(device, &message);
        if (result == 0)
        {
            result = -RT_EIO;
            goto __exit;
        }

        result = RT_EOK;
    }
    else
    {
        return -RT_EIO;
    }

__exit:
    rt_mutex_release(&(device->bus->lock));

    return result;
}


static void rtgui_touch_calculate(void)
{
    if (touch != RT_NULL)
    {
        /* read touch */
        {
            rt_uint8_t i, j, k, min;
220
            rt_uint16_t temp;
221 222 223 224 225 226
            rt_uint16_t tmpxy[2][SAMP_CNT];
            rt_uint8_t send_buffer[1];
            rt_uint8_t recv_buffer[2];
            for(i=0; i<SAMP_CNT; i++)
            {
                send_buffer[0] = TOUCH_MSR_X;
227
          touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
228 229 230 231 232 233 234 235 236 237 238
                rt_kprintf("touch x: %d ",(recv_buffer[0]*256|recv_buffer[1])>>4);
#if defined(_ILI_HORIZONTAL_DIRECTION_)
                tmpxy[1][i]  = (recv_buffer[0]<<8)|recv_buffer[1] ;
                tmpxy[1][i] >>= 4;
#else
                tmpxy[0][i]  = (recv_buffer[0]<<8)|recv_buffer[1] ;
                tmpxy[0][i] >>=4;

#endif
                send_buffer[0] = TOUCH_MSR_Y;
                touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
239
          rt_kprintf("touch y: %d \n",(recv_buffer[0]*256|recv_buffer[1])>>4);
240 241 242 243 244 245 246 247 248

#if defined(_ILI_HORIZONTAL_DIRECTION_)
                tmpxy[0][i]  = (recv_buffer[0]<<8)|recv_buffer[1] ;
                tmpxy[0][i] >>= 4;
#else
                tmpxy[1][i]  = (recv_buffer[0]<<8)|recv_buffer[1] ;
                tmpxy[1][i] >>= 4;
#endif
            }
249
            /*再次打开触摸中断*/
250
            send_buffer[0] = 1 << 7;
251
          touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
252 253
                touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
            /* calculate average */
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
            {
                rt_uint32_t total_x = 0;
                rt_uint32_t total_y = 0;
                for(k=0; k<2; k++)
                { 
                    // sorting the ADC value
                    for(i=0; i<SAMP_CNT-1; i++)
                    {
                        min=i;
                        for (j=i+1; j<SAMP_CNT; j++)
                        {
                            if (tmpxy[k][min] > tmpxy[k][j]) 
                                min=j;
                        }
                        temp = tmpxy[k][i];
                        tmpxy[k][i] = tmpxy[k][min];
                        tmpxy[k][min] = temp;
                    }
                    //check value for Valve value
                    if((tmpxy[k][SAMP_CNT_DIV2+1]-tmpxy[k][SAMP_CNT_DIV2-2]) > SH)
                    {
                        return;
                    }
                }
                total_x=tmpxy[0][SAMP_CNT_DIV2-2]+tmpxy[0][SAMP_CNT_DIV2-1]+tmpxy[0][SAMP_CNT_DIV2]+tmpxy[0][SAMP_CNT_DIV2+1];
                total_y=tmpxy[1][SAMP_CNT_DIV2-2]+tmpxy[1][SAMP_CNT_DIV2-1]+tmpxy[1][SAMP_CNT_DIV2]+tmpxy[1][SAMP_CNT_DIV2+1];
                //calculate average value
                touch->x=total_x>>2;
                touch->y=total_y>>2;
283 284 285 286 287
                rt_kprintf("touch->x:%d touch->y:%d\r\n", touch->x, touch->y);
           } /* calculate average */
        } /* read touch */

        /* if it's not in calibration status  */
288
        /*触摸值缩放*/
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
        if (touch->calibrating != RT_TRUE)
        {
            if (touch->max_x > touch->min_x)
            {
                touch->x = (touch->x - touch->min_x) * X_WIDTH/(touch->max_x - touch->min_x);
            }
            else
            {
                touch->x = (touch->min_x - touch->x) * X_WIDTH/(touch->min_x - touch->max_x);
            }

            if (touch->max_y > touch->min_y)
            {
                touch->y = (touch->y - touch->min_y) * Y_WIDTH /(touch->max_y - touch->min_y);
            }
            else
            {
                touch->y = (touch->min_y - touch->y) * Y_WIDTH /(touch->min_y - touch->max_y);
            }
        }
    }
}
#include "ls1c_regs.h"
312
#define TOUCH_INT_EN                __REG32(LS1C_INT4_EN)
313 314
rt_inline void touch_int_cmd(rt_bool_t NewState)
{
315 316 317 318 319 320 321 322 323 324
    if(NewState == RT_TRUE)
        {
            //TOUCH_INT_EN |= (1<<(TOUCH_INT_PIN-64));
            reg_set_one_bit(LS1C_INT4_EN, 1<<(TOUCH_INT_PIN-64));
        }
    else
        {
            //TOUCH_INT_EN &=(~ (1<<(TOUCH_INT_PIN-64)));
            reg_clr_one_bit(LS1C_INT4_EN, 1<<(TOUCH_INT_PIN-64));
        }
325 326 327 328 329 330 331

}

void ls1c_touch_irqhandler(void) /* TouchScreen */
{
  if(gpio_get(TOUCH_INT_PIN)==0)
  {
332 333 334 335 336 337 338
    /* 触摸屏按下后操作 */  
    if (gpio_level_low == gpio_get(led_gpio))
        gpio_set(led_gpio, gpio_level_high); 
    else
        gpio_set(led_gpio, gpio_level_low); 
    touch_int_cmd(RT_FALSE);
    rt_event_send(&touch->event, 1);
339 340 341
  }
}

342
/*管脚初始化,配置中断打开SPI1 CS0 设备*/
343
rt_inline void touch_init(void)
344 345
{     
    unsigned int touch_int_gpio = TOUCH_INT_PIN;     // 触摸屏中断
346 347
    int touch_irq = LS1C_GPIO_TO_IRQ(touch_int_gpio);  
  
348
    // 初始化按键中断  
349 350 351 352 353
    gpio_set_irq_type(touch_int_gpio, IRQ_TYPE_EDGE_FALLING);  
    rt_hw_interrupt_install(touch_irq, ls1c_touch_irqhandler, RT_NULL, "touch");  
    rt_hw_interrupt_umask(touch_irq);  
    gpio_init(touch_int_gpio, gpio_mode_input);  
  
354
    // 初始化led  
355 356 357 358 359 360 361 362 363
    gpio_init(led_gpio, gpio_mode_output); 
    gpio_set(led_gpio, gpio_level_high);   
}


/* RT-Thread Device Interface */
static rt_err_t rtgui_touch_init (rt_device_t dev)
{
    rt_uint8_t send;
364
    rt_uint8_t recv_buffer[2];
365
    struct rtgui_touch_device * touch_device = (struct rtgui_touch_device *)dev;
366
    
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
    touch_init();
    rt_kprintf("touch_init ...\n");
    send = START | DIFFERENTIAL | POWER_MODE0;
    touch_send_then_recv(touch->spi_device, &send, 1, recv_buffer, 2);

    return RT_EOK;
}

static rt_err_t rtgui_touch_control (rt_device_t dev, int cmd, void *args)
{
    switch (cmd)
    {
    case RT_TOUCH_CALIBRATION:
        touch->calibrating = RT_TRUE;
        touch->calibration_func = (rt_touch_calibration_func_t)args;
        break;

    case RT_TOUCH_NORMAL:
        touch->calibrating = RT_FALSE;
        break;

    case RT_TOUCH_CALIBRATION_DATA:
    {
        struct calibration_data* data;
        data = (struct calibration_data*) args;

        //update
        touch->min_x = data->min_x;
        touch->max_x = data->max_x;
        touch->min_y = data->min_y;
        touch->max_y = data->max_y;
    }
    break;
    }

    return RT_EOK;
}

void _set_mouse_position(rt_uint32_t X, rt_uint32_t Y)
{}
static void touch_thread_entry(void *parameter)
{
    rt_bool_t touch_down = RT_FALSE;
    rt_uint32_t event_value;
    struct rtgui_event_mouse emouse;
    static struct _touch_previous
    {
        rt_uint32_t x;
        rt_uint32_t y;
    } touch_previous;

    RTGUI_EVENT_MOUSE_BUTTON_INIT(&emouse);
    emouse.wid = RT_NULL;

    while(1)
    {
423
        /* 接收到触摸中断事件 */
424 425 426 427 428 429 430 431 432 433 434
        if(rt_event_recv(&touch->event,
                         1,
                         RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
                         100,
                         &event_value)
                == RT_EOK)
        {
            while(1)
            {
                if (IS_TOUCH_UP())
                {
435
                    /* 触摸笔抬起 */
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
                    /* touch up */
                    emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_UP);

                    /* use old value */
                    emouse.x = touch->x;
                    emouse.y = touch->y;

                    if(touch_down != RT_TRUE) 
                    {                    
                        touch_int_cmd(RT_TRUE);
                        break;
                    }    

                    if ((touch->calibrating == RT_TRUE) && (touch->calibration_func != RT_NULL))
                    {
451
                        /* 触摸校准处理 */
452 453
                        /* callback function */
                        touch->calibration_func(emouse.x, emouse.y);
454
                                            
455 456 457
                    }
                    else
                    {
458
                        /* 向ui发送触摸坐标 */
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
                        rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
                    }
                    rt_kprintf("touch up: (%d, %d)\n", emouse.x, emouse.y);

                    /* clean */
                    touch_previous.x = touch_previous.y = 0;
                    touch_down = RT_FALSE;

                    touch_int_cmd(RT_TRUE);
                    break;
                } /* touch up */
                else /* touch down or move */
                {
                    if(touch_down == RT_FALSE)
                    {
                        rt_thread_delay(RT_TICK_PER_SECOND / 10);
                    }
                    else
                    {
                        rt_thread_delay(5);
                    }

                    if(IS_TOUCH_UP()) continue;

                    /* calculation */
                    rtgui_touch_calculate();
                    
                    /* send mouse event */
                    emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
                    emouse.parent.sender = RT_NULL;

                    emouse.x = touch->x;
                    emouse.y = touch->y;
                    _set_mouse_position(emouse.x, emouse.y);
493
                    /* 光标跟随 */
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
                    /* init mouse button */
                    emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_DOWN);

                    /* send event to server */
                    if (touch->calibrating != RT_TRUE)
                    {
#define previous_keep      8
                        /* filter. */
                        if((touch_previous.x > touch->x + previous_keep)
                            || (touch_previous.x < touch->x - previous_keep)
                            || (touch_previous.y > touch->y + previous_keep)
                            || (touch_previous.y < touch->y - previous_keep))
                        {
                            touch_previous.x = touch->x;
                            touch_previous.y = touch->y;
509
                            /* 向ui发送触摸坐标 */
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
                            rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
                            if(touch_down == RT_FALSE)
                            {
                                touch_down = RT_TRUE;
                                rt_kprintf("touch down: (%d, %d)\n", emouse.x, emouse.y);
                            }
                            else
                            {
                                rt_kprintf("touch motion: (%d, %d)\n", emouse.x, emouse.y);
                            }
                        }
                    }
                    else
                    {
                        touch_down = RT_TRUE;
                    }
                } /* touch down or move */
            } /* read touch */
        } /* event recv */
    } /* thread while(1) */
}


rt_err_t rtgui_touch_hw_init(const char * spi_device_name)
{
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
         rt_uint32_t arg[2]; 
    struct rt_device * spi_device;
        struct rt_thread * touch_thread;
    rt_err_t err;

    rt_kprintf("spi1 cs0 start...\n");
    spi_device = rt_device_find("spi10");
    if(spi_device ==  RT_NULL)
    {
        rt_kprintf("Did not find spi1, exit thread....\n");
        return;
    }
    err = rt_device_open(spi_device, RT_DEVICE_OFLAG_RDWR);
    if(err != RT_EOK)
    {
        rt_kprintf("Open spi1 failed %08X, exit thread....\n", err);
        return;
    }
553 554 555 556 557 558 559 560 561 562
      
      /* config spi */
      {
          struct rt_spi_configuration cfg;
          cfg.data_width = 8;
          cfg.mode = RT_SPI_MODE_0; 
       cfg.max_hz  = 200 * 1000; /* 200K */
        rt_spi_configure((struct rt_spi_device *)spi_device, &cfg);
      }

563
    touch = (struct rtgui_touch_device*)rt_malloc (sizeof(struct rtgui_touch_device));
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
    if (touch == RT_NULL) return RT_ENOMEM; /* no memory yet */

    /* clear device structure */
    rt_memset(&(touch->parent), 0, sizeof(struct rt_device));

    rt_event_init(&touch->event, "touch", RT_IPC_FLAG_FIFO);

    touch->spi_device = (struct rt_spi_device *)spi_device;
    touch->calibrating = false;

    touch->min_x = MIN_X_DEFAULT;
    touch->max_x = MAX_X_DEFAULT;
    touch->min_y = MIN_Y_DEFAULT; 
    touch->max_y = MAX_Y_DEFAULT;

    /* init device structure */
    touch->parent.type = RT_Device_Class_Miscellaneous;
    touch->parent.init = rtgui_touch_init;
    touch->parent.control = rtgui_touch_control;
    touch->parent.user_data = RT_NULL;

    /* register touch device to RT-Thread */
    rt_device_register(&(touch->parent), "touch", RT_DEVICE_FLAG_RDWR);
587
    
588 589 590 591 592 593 594 595 596

    touch_thread = rt_thread_create("touch_thread",
                                    touch_thread_entry, RT_NULL,
                                    4096, RTGUI_SVR_THREAD_PRIORITY-1, 1);
    if (touch_thread != RT_NULL) rt_thread_startup(touch_thread);

     rt_device_init((rt_device_t)touch);
   return RT_EOK;
}
597 598

#endif