⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ps2.c

📁 PS2和USB兼容的鼠标源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
    globle_isr_disable();
    relase_ps2_pins();
    retval=ps2_send_sbyte(mdata);
    relase_ps2_pins();
    globle_isr_enable();
    return retval;
}

/********************************************************************************/
/* function : ps2_put_hostbyte()                                                */
/* created  : hw-chen                                                           */
/* descript : PS2发送数据给主机                                                 */
/********************************************************************************/
char ps2_put_hostbyte(unsigned char mdata)
{   char retval;
    globle_isr_disable();
    relase_ps2_pins();
    retval=ps2_put_sbyte(mdata);
    relase_ps2_pins();
    globle_isr_enable();
    return retval;
}
/********************************************************************************/
/* function : ps2_pro_new_command()                                             */
/* created  : hw-chen                                                           */
/* descript : PS2处理从主机接收到的新命令                                       */
/********************************************************************************/
char ps2_pro_new_command(char type)
{   switch(type)
    {
    case PS2_CMD_RST_SCALING:
         ps2_rst_scaling();
         break;
    case PS2_CMD_SET_SCALING_2_1:
         ps2_set_scaling();
         break;
    case PS2_CMD_SET_RESOLUTION:
         ps2_set_resolution_ack();
         break;
    case PS2_CMD_STATUS_REQUEST:
         ps2_status_request();
         break;
    case PS2_CMD_SET_STREAM_MODE:
         ps2_set_stream_mode();
         break;
    case PS2_CMD_READ_DATA:
         ps2_packet_mouse_mdata();
         break;
    case PS2_CMD_RST_WRAP_MODE:
         ps2_rst_wrap_mode();
         break;
    case PS2_CMD_SET_WRAP_MODE:
         ps2_set_wrap_mode();
         break;
    case PS2_CMD_REMOTE_MODE:
         ps2_set_remote_mode();
         break;
    case PS2_CMD_READ_DEV_TYPE:
         ps2_read_device_type();
         break;
    case PS2_CMD_SET_SAMPLE_RATE:
         ps2_set_sample_rate_ack();
         break;
    case PS2_CMD_ENABLE:
         ps2_enable();
         break;
    case PS2_CMD_DISABLE:
         ps2_disable();
         break;
    case PS2_CMD_DEFAULT:
         ps2_set_default_state();
         break;
    case PS2_CMD_RESEND:
         ps2_resend();
         break;
    case PS2_CMD_RESET:
         ps2_reset();
         break;
    default:
         return 0x0;
    }
    return 0x1;
}

/********************************************************************************/
/* function : ps2_app_resolution()                                              */
/* created  : hw-chen                                                           */
/* descript : 如果主机设置了分辨率,计算在分辨率下的计数器的值                   */
/*            大多数鼠标采用了400CPI,少数罗技高端鼠标采用了800CPI.400CPI就是说  */
/*            当鼠标每移动一英吋就可反馈400个不同的坐标,换句话说                */
/*            也就是采用400CPI的鼠标可以观察到你手部0.06毫米的微弱移动,就说明   */
/*            CPI越大,光电鼠标就越灵敏.                                         */
/*            我们用的ADNS5020有两种CPI可选,500和1000,初始化成为1000,用作800    */
/*            1 英寸= 2.5400 厘米=25MM,那么1000/25=40个点就是移动1MM时value=40  */
/*            1 英尺= 12 英寸 = 0.3048 米                                       */
/*            0x0 <===> 1count/mm                                               */
/*            0x1 <===> 2count/mm                                               */
/*            0x2 <===> 4count/mm                                               */
/*            0x3 <===> 8count/mm                                               */
/********************************************************************************/
char ps2_app_resolution(char value)
{   char res;
    res = ps2_parameter.resolution;
    res = RES_8MM-res;
    if(res)
    {   value>>=res;
    }
    return(value);
}

/********************************************************************************/
/* function : ps2_app_scaling()                                                 */
/* created  : hw-chen                                                           */
/* descript : 如果主机启动了SCALING操作                                         */
/********************************************************************************/
signed char ps2_app_scaling(char value)
{   if((ps2_parameter.scale==SC_1_1))
    {   return(value);
    }
    if((value==0x2))
    {   return 0x1;
    }
    if((value==-2))
    {   return -1;
    }
    if((value==0x1))
    {   return 0x1;
    }
    if((value==-1))
    {   return -1;
    }
    if((value==0x3)||(value==-3))
    {   return(value);
    }
    if((value==0x4))
    {   return 0x6;
    }
    if((value==-4))
    {	return -6;
    }
    if((value==0x5))
    {   return 0x9;
    }
    if((value==-5))
    {   return -9;
    }
    if((value>=64))
    {   return 127;
    }
    if((value<=-64))
    {   return -127;
    }
    else
    {   return(value<<1);
    }
}

/********************************************************************************/
/* function : ps2_3D_5D_mdata()                                                 */
/* created  : hw-chen                                                           */
/* descript : 组装标准的数据报                                                  */
/********************************************************************************/
void ps2_3D_5D_mdata(void)
{   unsigned char * ptr=ps2_xmit.ps2_buffer;
    mouse_status.y_count=-mouse_status.y_count;
    ptr[0] = (mouse_status.button_byte&0x7)|0x8|((mouse_status.x_count>>0x3)&0x10)|((mouse_status.y_count>>0x2)&0x20);
    ptr[1] = mouse_status.x_count;
    ptr[2] = mouse_status.y_count;
    ps2_xmit.ps2_xmit=ps2_xmit.ps2_length=0x3;//* send the buffer
    mouse_status.change_flag=0x0;
    mouse_status.x_count=0x0;
    mouse_status.y_count=0x0;
    mouse_status.z_count=0x0;
}

/********************************************************************************/
/* function : ps2_packet_mouse_mdata()                                          */
/* created  : hw-chen                                                           */
/* descript : 组装一个IBM格式的数据包给主机                                     */
/********************************************************************************/
void ps2_packet_mouse_mdata(void)
{   ps2_3D_5D_mdata();
}

/********************************************************************************/
/* function : ps2_check_zmouse()                                                */
/* created  : hw-chen                                                           */
/* descript : 要进入滚轮模式,主机应该发送如下的命令序列                         */
/*      1.set sample rate 200                                                   */
/*      2.set sample rate 100 或者 200                                          */
/*      3.set sample rate 80                                                    */
/*      这个函数必须在受到设置采样率时调用                                      */
/********************************************************************************/
unsigned char check_wheel_steps=0x0;
void ps2_check_3D_function(char rate)
{   if((check_wheel_steps==0x1)&&(rate==100))
    {   check_wheel_steps=0x2;          //* 3D <==> 200 100 80
    }
    else if((check_wheel_steps==0x2)&&(rate==80))
    {   ps2_parameter.zmouse=0x1;       //* 3D检查成功
        check_wheel_steps=0x0;
    }
    else
    {   check_wheel_steps=0x0;
    }
}
void ps2_check_5D_function(char rate)
{   if((check_wheel_steps==0x1)&&(rate==200))
    {   check_wheel_steps=0x2;          //* 3D <==> 200 200 80
    }
    else if((check_wheel_steps==0x2)&&(rate==80))
    {   ps2_parameter.zmouse=0x2;       //* 5D检查成功
        check_wheel_steps=0x0;
    }
    else
    {   check_wheel_steps=0x0;
    }
}
void ps2_check_zmouse(char rate)
{   if((rate==200)&&(check_wheel_steps==0x0))
    {   check_wheel_steps=0x1;          //* 功能检查开始
        return;
    }
    if((ps2_parameter.zmouse=0x0))      //* 检查3D功能鼠标
    {   ps2_check_3D_function(rate);
    }
    else if((ps2_parameter.zmouse=0x1))
    {   ps2_check_5D_function(rate);
    }
    else
    {   check_wheel_steps=0x0;          //* 复位检查序列
    }
}

/********************************************************************************/
/* function : ps2_pro_host_command()                                            */
/* created  : hw-chen                                                           */
/* descript : 处理主机发送过来的命令                                            */
/********************************************************************************/
void ps2_pro_host_command(char command)
{   char i;
    /****************************************************************************/
    /* WRAP模式,一般用于鼠标测试 */
    /****************************************************************************/
    if((ps2_parameter.wrap))
    {   ps2_last_valid_cmd=0x0;
        if((command!=PS2_CMD_RESET)&&(command!=PS2_CMD_RST_WRAP_MODE))
        {   ps2_put_hostbyte(command);      //* simply send what we received
            return;
        }
    }
    /****************************************************************************/
    /* 根据上一个命令来处理 */
    /****************************************************************************/
    switch (ps2_last_valid_cmd)
    {
    case PS2_CMD_SET_SAMPLE_RATE:                   //* last command byte  was set sample rate, so this must be
        if((ps2_set_sample_rate(command))==PS2_ACK) //* the rate itself. if the rate is valid,
        {   ps2_put_hostbyte(PS2_ACK);              //* ack it
            ps2_last_valid_cmd=0x0;                 //* reset ps2_last_valid_cmd
            ps2_error_flag=0x0;                     //* clear the error count
            ps2_check_zmouse(command);              //* check for z-mouse enabling
            return;                                 //* and return
        }
        break;

    case PS2_CMD_SET_RESOLUTION:
        if((ps2_set_resolution(command))==PS2_ACK)  //* last command was set resolution, so this must be
        {   ps2_put_hostbyte(PS2_ACK);              //* ack it
            ps2_last_valid_cmd=0x0;                 //* reset ps2_last_valid_cmd
            ps2_error_flag=0x0;                     //* clear the error count
            return;                                 //* and return
        }
        break;

    /****************************************************************************/
    /* 处理新接收到的数据,如果接收到新数据,放弃所有等待发送的数据 */
    /****************************************************************************/
    default:
        ps2_xmit.ps2_xmit=0x0;
        if((command!=PS2_CMD_SET_SAMPLE_RATE))      //if the command is not set sample rate, reset
        {   check_wheel_steps=0x0;            //the count of consecutive set sample rate commands received.
        }
        ps2_put_hostbyte(PS2_ACK);              //this command is a valid one, so ack it
        ps2_last_valid_cmd=command;                 //save it
        ps2_error_flag=0x0;
        if(ps2_pro_new_command(command))            //* 如果新命令正确,直接返回,否则出错统计
        {   return;
        }
        break;
    }
    ps2_error_flag+=0x1;        //* if we get here, the command was not recognized
    if((ps2_error_flag>=0x2))   //* 如果多次错误,发送PS2_ERROR
    {   ps2_put_hostbyte(PS2_ERROR);
        ps2_last_valid_cmd = 0x0;
        ps2_error_flag = 0x0;
        return;
    }
    else                        //* 一次错误,重新发送
    {   ps2_put_hostbyte(PS2_RESEND);
        return;
    }
}

/********************************************************************************/
/* function : ps2_reset()                                                       */
/* created  : hw-chen                                                           */
/* descript : 复位PS2鼠标                                                       */
/********************************************************************************/
void ps2_reset(void)
{   ps2_set_default_state();
    ps2_pro_bat();
}

/********************************************************************************/
/* function : ps2_resend()                                                      */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -