#include "pad.h"
#include "adc.h"
#include "uart.h"
#include <STDIO.H>
#include "usb_req_class.h"

BYTE SendCnt;

BYTE HPadCode = 0x00;
BYTE LPadCode = 0x00;
BYTE LTCode   = 0x00;
BYTE RTCode   = 0x00;
BYTE HLXCode  = 0xf0;
BYTE LLXCode  = 0xf0;
BYTE HLYCode  = 0xf0;
BYTE LLYCode  = 0xf0;
BYTE HRXCode  = 0xf0;
BYTE LRXCode  = 0xf0;
BYTE HRYCode  = 0xf0;
BYTE LRYCode  = 0xf0;

char xdata p[30];

BYTE code PadCode_Idle[12] = {0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff};
BYTE xdata PadCode_Down[12];

BYTE xdata keyPadData[20] = {0x00, 0x14,
                             0x00, // 第一组按键  R  L   SE   ST   RI   LE   DO   UP
                             0x00, // 第二组按键  Y  X   B    A    NOP  ME   RB   LB
                             0x00, // LT
                             0x00, // RT
                             0x00, // LX   双字
                             0xff, // LX2
                             0x00, // LY
                             0xff, // LY2
                             0xff, // RX
                             0xff, // RX2
                             0x00, // RY
                             0x00, // RY2
                             0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

u16 joyConvert(BYTE chanel)
{
    u16 retValue          = 0;
    u16 retValue1         = 0;
    u16 retValue2         = 0;
    u16 retValueAveragU16 = 0;
    u16 retValueAverag    = 0;

    _nop_();
    retValue = Get_ADC12bitResult(chanel);
    _nop_();
    retValue1 = Get_ADC12bitResult(chanel);
    _nop_();
    retValue2 = Get_ADC12bitResult(chanel);

    retValueAveragU16 = (retValue + retValue1 + retValue2) / 3;

    if (retValueAveragU16 > 4000) {
        retValueAverag = retValueAveragU16 - 2048;
    } else if (retValueAveragU16 < 1600) {
        retValueAverag = retValueAveragU16 - 2048;
    } else {
        retValueAverag = 0;
    }

#ifdef DEBUG
    P6 = retValueAverag;
    sprintf(p, "the joy is  %d", retValueAverag);
    UartSendStr(p);
#endif // DEBUG

    return retValueAverag;
}

/// @brief 配置发送的手柄数据
/// @param
void SendPadData(void)
{
    uint i = 0;

    LRXCode = joyConvert(1) >> 4;

    LRYCode = joyConvert(3) >> 4;

    PadCode_Down[9]  = LRXCode;
    PadCode_Down[11] = LRYCode;

    keyPadData[11] = PadCode_Down[9];
    keyPadData[13] = PadCode_Down[11];

    if (!padIdle) {

        PadCode_Down[0]  = HPadCode;
        PadCode_Down[1]  = LPadCode;
        PadCode_Down[2]  = LTCode;
        PadCode_Down[3]  = RTCode;
        PadCode_Down[4]  = HLXCode;
        PadCode_Down[5]  = LLXCode;
        PadCode_Down[6]  = HLYCode;
        PadCode_Down[7]  = LLYCode;
        PadCode_Down[8]  = HRXCode;
        PadCode_Down[9]  = LRXCode;
        PadCode_Down[10] = HRYCode;
        PadCode_Down[11] = LRYCode;

        for (i = 0; i < 12; i++) {
            keyPadData[i + 2] = PadCode_Down[i];
        }
    } else {
        if (SendCnt < 3) {
            SendCnt++;
        } else {
            SendCnt  = 0;
            HPadCode = PadCode_Idle[0];
            LPadCode = PadCode_Idle[1];
            LTCode   = PadCode_Idle[2];
            RTCode   = PadCode_Idle[3];
            HLXCode  = PadCode_Idle[4];
            LLXCode  = PadCode_Idle[5];
            HLYCode  = PadCode_Idle[6];
            LLYCode  = PadCode_Idle[7];
            HRXCode  = PadCode_Idle[8];
            // LRXCode  = PadCode_Idle[9];
            HRYCode = PadCode_Idle[10];
            // LRYCode  = PadCode_Idle[11];

            for (i = 0; i < 8; i++) {
                keyPadData[i + 2] = PadCode_Idle[i];
            }
        }
    }
}