// File: /workspace/github_c_makefile_star2_O2/OnionIoT[P]lv_example/lv_drivers/indev/XPT7603.c
// Function: xpt7603_read
bool xpt7603_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    static int16_t last_x = 0;
    static int16_t last_y = 0;
    uint8_t buf;

    int16_t x = 0;
    int16_t y = 0;

    // uint8_t irq = LV_DRV_INDEV_IRQ_READ;
    uint8_t irq = (uint8_t)om_read_gpio(GPIO_IRQ_NUMBER);
    // printf("touch irq = %d\n", irq);

    if(irq == 0) {
        // LV_DRV_INDEV_SPI_CS(0);

        // LV_DRV_INDEV_SPI_XCHG_BYTE(CMD_X_READ);         /*Start x read*/

        // buf = LV_DRV_INDEV_SPI_XCHG_BYTE(0);           /*Read x MSB*/
        // x = buf << 8;
        // buf = LV_DRV_INDEV_SPI_XCHG_BYTE(CMD_Y_READ);  /*Until x LSB converted y command can be sent*/
        // x += buf;

        // buf =  LV_DRV_INDEV_SPI_XCHG_BYTE(0);   /*Read y MSB*/
        // y = buf << 8;

        // buf =  LV_DRV_INDEV_SPI_XCHG_BYTE(0);   /*Read y LSB*/
        // y += buf;
        
        x = (int16_t)(om_i2c_read(XPT7603_READ_X) >> 4);
        
        y = (int16_t)(om_i2c_read(XPT7603_READ_Y) >> 4);
        // printf("read\tx = 0x%04x", x);
        // printf("\ty = 0x%04x\n", y);

        /*Normalize Data*/
        // x = x >> 4;
        // y = y >> 4;
        // printf("val\tx = %d", x);
        // printf("\ty = %d\n", y);
        xpt7603_corr(&x, &y);
        xpt7603_avg(&x, &y);

        last_x = x;
        last_y = y;
		data->state = LV_INDEV_STATE_PR;
		
// 		printf("coord\tx = %d", x);
//         printf("\ty = %d\n", y);

        // LV_DRV_INDEV_SPI_CS(1);
    } else {
        x = last_x;
        y = last_y;
        avg_last = 0;
		data->state = LV_INDEV_STATE_REL;
    }

    data->point.x = x;
    data->point.y = y;

    return false;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
movq %rsi, %rbx
pushq $0xe
popq %rdi
callq 0x3ad9a
testb %al, %al
je 0x3ae55
movzwl 0x34dc8(%rip), %ecx     # 0x6fc08
movzwl 0x34dc3(%rip), %eax     # 0x6fc0a
movb $0x0, 0x34dbe(%rip)     # 0x6fc0c
xorl %edx, %edx
jmp 0x3af5c
movl $0xc8, %edi
callq 0x3acf4
movzwl %ax, %r14d
movl %r14d, %ebp
shrl $0x4, %ebp
movl $0xd8, %edi
callq 0x3acf4
movzwl %ax, %eax
movl %eax, %ecx
shrl $0x4, %ecx
movl $0xffffff38, %edx       # imm = 0xFFFFFF38
addl %edx, %ecx
xorl %esi, %esi
movw $0xc90, %di             # imm = 0xC90
cmpw %di, %ax
cmovbl %esi, %ecx
addl %edx, %ebp
cmpw %di, %r14w
cmovbl %esi, %ebp
movswl %cx, %eax
imull $0x140, %eax, %eax      # imm = 0x140
movl $0xe10, %edi            # imm = 0xE10
xorl %edx, %edx
divl %edi
movl %eax, %ecx
movswl %bp, %eax
imull $0xf0, %eax, %eax
xorl %edx, %edx
divl %edi
movl $0x140, %edi            # imm = 0x140
subl %ecx, %edi
leaq 0x34d48(%rip), %rcx     # 0x6fc0e
leaq 0x34d49(%rip), %rdx     # 0x6fc16
cmpq $-0x3, %rsi
je 0x3aef2
leal 0x2(%rsi), %r8d
movzwl (%rcx,%r8,2), %r9d
movw %r9w, 0x6(%rcx,%rsi,2)
movzwl (%rdx,%r8,2), %r8d
movw %r8w, 0x6(%rdx,%rsi,2)
decq %rsi
jmp 0x3aecd
movl $0xf0, %esi
subl %eax, %esi
movw %di, 0x34d0e(%rip)      # 0x6fc0e
movw %si, 0x34d0f(%rip)      # 0x6fc16
movb 0x34cff(%rip), %al      # 0x6fc0c
cmpb $0x3, %al
ja 0x3af19
incb %al
movb %al, 0x34cf3(%rip)      # 0x6fc0c
movzbl %al, %edi
leal (%rdi,%rdi), %r8d
xorl %r9d, %r9d
xorl %eax, %eax
xorl %esi, %esi
cmpq %r9, %r8
je 0x3af42
movswl (%r9,%rcx), %r10d
addl %r10d, %eax
movswl (%r9,%rdx), %r10d
addl %r10d, %esi
addq $0x2, %r9
jmp 0x3af27
cltd
idivl %edi
movl %eax, %ecx
movl %esi, %eax
cltd
idivl %edi
movw %cx, 0x34cb5(%rip)      # 0x6fc08
movw %ax, 0x34cb0(%rip)      # 0x6fc0a
movb $0x1, %dl
movb %dl, 0xe(%rbx)
movw %cx, (%rbx)
movw %ax, 0x2(%rbx)
xorl %eax, %eax
popq %rbx
popq %r14
popq %rbp
retq
nopl (%rax)