// File: /workspace/github_c_makefile_star2_O1/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 0x3ea2b
testb %al, %al
je 0x3eaee
movzwl 0x3312f(%rip), %esi     # 0x71c08
movzwl 0x3312a(%rip), %eax     # 0x71c0a
movb $0x0, 0x33125(%rip)     # 0x71c0c
xorl %ecx, %ecx
jmp 0x3ec14
movl $0xc8, %edi
callq 0x3e97c
movzwl %ax, %r14d
movl %r14d, %ebp
shrl $0x4, %ebp
movl $0xd8, %edi
callq 0x3e97c
movzwl %ax, %ecx
movl %ecx, %edx
shrl $0x4, %edx
movl $0xffffff38, %esi       # imm = 0xFFFFFF38
addl %esi, %edx
xorl %eax, %eax
movl $0xc90, %edi            # imm = 0xC90
cmpl %edi, %ecx
cmovbl %eax, %edx
addl %esi, %ebp
cmpl %edi, %r14d
cmovbl %eax, %ebp
movswl %dx, %ecx
shll $0x6, %ecx
leal (%rcx,%rcx,4), %ecx
movl $0x91a2b3c5, %edx       # imm = 0x91A2B3C5
imulq %rdx, %rcx
shrq $0x2b, %rcx
movswl %bp, %esi
imull $0xf0, %esi, %esi
imulq %rdx, %rsi
shrq $0x2b, %rsi
movl $0x140, %edx            # imm = 0x140
subl %ecx, %edx
movl $0xf0, %ecx
subl %esi, %ecx
movzwl 0x330a7(%rip), %esi     # 0x71c12
movl 0x3309d(%rip), %edi     # 0x71c0e
movl %edi, 0x33099(%rip)     # 0x71c10
movw %si, 0x33096(%rip)      # 0x71c14
movzwl 0x33095(%rip), %esi     # 0x71c1a
movl 0x3308b(%rip), %edi     # 0x71c16
movl %edi, 0x33087(%rip)     # 0x71c18
movw %si, 0x33084(%rip)      # 0x71c1c
movw %dx, 0x3306f(%rip)      # 0x71c0e
movw %cx, 0x33070(%rip)      # 0x71c16
movb 0x33060(%rip), %cl      # 0x71c0c
cmpb $0x3, %cl
ja 0x3ebb9
incb %cl
movb %cl, 0x33053(%rip)      # 0x71c0c
movzbl 0x3304c(%rip), %edi     # 0x71c0c
movl $0x0, %ecx
testq %rdi, %rdi
je 0x3ebfa
leal (%rdi,%rdi), %edx
xorl %esi, %esi
leaq 0x33038(%rip), %r8      # 0x71c0e
leaq 0x33039(%rip), %r9      # 0x71c16
xorl %ecx, %ecx
xorl %eax, %eax
movswl (%rsi,%r8), %r10d
addl %r10d, %eax
movswl (%rsi,%r9), %r10d
addl %r10d, %ecx
addq $0x2, %rsi
cmpq %rsi, %rdx
jne 0x3ebe1
cltd
idivl %edi
movl %eax, %esi
movl %ecx, %eax
cltd
idivl %edi
movw %si, 0x32ffd(%rip)      # 0x71c08
movw %ax, 0x32ff8(%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
nopl (%rax)