// File: /workspace/github_c_makefile_star2_O3/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
movl $0xe, %edi
callq 0x3eda3
testb %al, %al
je 0x3ee68
movzwl 0x32db5(%rip), %esi     # 0x71c08
movzwl 0x32db0(%rip), %eax     # 0x71c0a
movb $0x0, 0x32dab(%rip)     # 0x71c0c
xorl %ecx, %ecx
jmp 0x3ef7e
movl $0xc8, %edi
callq 0x3ecf4
movzwl %ax, %r14d
movl %r14d, %ebp
shrl $0x4, %ebp
movl $0xd8, %edi
callq 0x3ecf4
movzwl %ax, %eax
movl %eax, %edx
shrl $0x4, %edx
movl $0xffffff38, %esi       # imm = 0xFFFFFF38
addl %esi, %edx
xorl %ecx, %ecx
movl $0xc90, %edi            # imm = 0xC90
cmpl %edi, %eax
cmovbl %ecx, %edx
addl %esi, %ebp
cmpl %edi, %r14d
cmovbl %ecx, %ebp
movswl %dx, %eax
shll $0x6, %eax
leal (%rax,%rax,4), %eax
movl $0x91a2b3c5, %edx       # imm = 0x91A2B3C5
imulq %rdx, %rax
shrq $0x2b, %rax
movswl %bp, %esi
imull $0xf0, %esi, %esi
imulq %rdx, %rsi
shrq $0x2b, %rsi
movl $0x140, %edx            # imm = 0x140
subl %eax, %edx
movl $0xf0, %eax
subl %esi, %eax
movzwl 0x32d2d(%rip), %esi     # 0x71c12
movl 0x32d23(%rip), %edi     # 0x71c0e
movl %edi, 0x32d1f(%rip)     # 0x71c10
movw %si, 0x32d1c(%rip)      # 0x71c14
movzwl 0x32d1b(%rip), %esi     # 0x71c1a
movl 0x32d11(%rip), %edi     # 0x71c16
movl %edi, 0x32d0d(%rip)     # 0x71c18
movw %si, 0x32d0a(%rip)      # 0x71c1c
movw %dx, 0x32cf5(%rip)      # 0x71c0e
movw %ax, 0x32cf6(%rip)      # 0x71c16
movb 0x32ce6(%rip), %al      # 0x71c0c
cmpb $0x3, %al
ja 0x3ef32
incb %al
movb %al, 0x32cda(%rip)      # 0x71c0c
movzbl %al, %edi
leaq 0x32cd2(%rip), %rdx     # 0x71c0e
leaq 0x32cd3(%rip), %rsi     # 0x71c16
xorl %r8d, %r8d
movq %rdi, %r9
xorl %eax, %eax
movswl (%r8,%rdx), %r10d
addl %r10d, %eax
movswl (%r8,%rsi), %r10d
addl %r10d, %ecx
addq $0x2, %r8
decq %r9
jne 0x3ef4b
cltd
idivl %edi
movl %eax, %esi
movl %ecx, %eax
cltd
idivl %edi
movw %si, 0x32c93(%rip)      # 0x71c08
movw %ax, 0x32c8e(%rip)      # 0x71c0a
movb $0x1, %cl
movb %cl, 0xe(%rbx)
movw %si, (%rbx)
movw %ax, 0x2(%rbx)
xorl %eax, %eax
popq %rbx
popq %r14
popq %rbp
retq
nop