📄 mouse.h
字号:
#define uart_debug_mouse 1
//==========================io fifo
#define io_fifo_size 16
//不得大于256个
uint8 io_fifo_buf[io_fifo_size];
uchar io_dat_cnt=0;//io fifo dat 数
unsigned char io_headp=0;
unsigned char io_lastp=0;
//==============================
void w_io_fifo(uchar dat)
{
if(io_dat_cnt<io_fifo_size) {io_dat_cnt++;io_fifo_buf[io_lastp]=dat;
if(io_lastp<(io_fifo_size-1)) io_lastp++;
else {io_lastp=0; }
} //return ok;
//return error;
}
uchar r_io_fifo(void)
{ uchar i;
if(io_dat_cnt) { i=io_fifo_buf[io_headp];
if(io_headp<(io_fifo_size-1)) io_headp++;
else {io_headp=0;}
io_dat_cnt--;return i;
}
else {return error; }
}
//==================================================
#define int_2 B,2
#define PS2dat D,7
#define PS2clk int_2
//===================================
#define Int2down_req 0
#define Int2up_req 1
//===================================
void use_interrupt2(unsigned char int_mode) {
SETBIT(GICR,INT2);MCUCSR&=(~(1<<6));(MCUCSR|=(int_mode<<6));in(int_2);
}
volatile uint8 ps_m_dat=0,PSDAT,ps_m_cnt=0;
//==============================
SIGNAL(SIG_INTERRUPT2)
{
if(ps_m_cnt<=10)
{ps_m_dat>>=1;//接受数据 低->高
ps_m_cnt++;
if(r_bit(PS2dat)) {ps_m_dat|=0x80;if(ps_m_cnt==1) ps_m_cnt=0;}
else {if(ps_m_cnt==11) ps_m_cnt=0; }
if (ps_m_cnt==9) PSDAT=ps_m_dat;
if (ps_m_cnt==11) {w_io_fifo(PSDAT);ps_m_cnt=0; }//
}
//if cnt >=12 that index a error clk on the wire
}//
#define WhileTout(condi,VTout,lim) VTout=0;while(condi) {_delay_us(20); if(VTout++>(lim/20)) break; }
uint8 Command2mouse(unsigned char dat)
{ uint8 i=0,datTP;
uint16 Toutcnt=0;
datTP=dat;
out(PS2clk);out(PS2dat);
clr(PS2clk);set(PS2dat);//sclk=0; sda = 1;
_delay_us(40);
_delay_us(40);
_delay_us(40);
_delay_us(40);
clr(PS2dat);//sda = 0;
_delay_us(30);
slb(PS2clk);//sclk= 1; //release clk line
in(PS2clk);
//====================data
WhileTout((r_bit(PS2clk)),Toutcnt,1000);
if(Toutcnt>48) {
in(PS2dat);in(PS2clk);return error;}
for (i=0; i<8; i++)
{
if(dat&0x01) slb(PS2dat); // sda=mouse_bit0;
else clb(PS2dat);
dat>>=1;//wm_dat>>=1;
WhileTout((r_bit(PS2clk)==0),Toutcnt,1000);
//while(sclk==0);
WhileTout((r_bit(PS2clk)),Toutcnt,1000);
}
uint8 j=0;
for (i=0; i<8; i++)
{if(datTP&0x01) j++;
datTP>>=1;
}
j%=2;
if(j) clb(PS2dat);
else slb(PS2dat); // sda=!P; // /parity
//================data over
WhileTout((r_bit(PS2clk)==0),Toutcnt,1000);
WhileTout((r_bit(PS2clk)),Toutcnt,1000);
slb(PS2dat);//sda=1;
in(PS2dat);
in(PS2clk);
WhileTout((r_bit(PS2clk) ||r_bit(PS2dat) ),Toutcnt,1000);
WhileTout((r_bit(PS2clk)==0 ||r_bit(PS2dat)==0 ),Toutcnt,1000);
return ok;
}
uint8 mouse_read(void)
{uint8 i;
uint16 waitcnt=0;
here:if(io_dat_cnt) {i=r_io_fifo();
#if uart_debug_mouse==1
uart_send1(i);
#endif
return i;}
else {if(waitcnt++>60) return error;
else {_delay_ms(5);goto here;}}
}
uint8 mouse_read2(void)
{uint8 i;
here: if(io_dat_cnt) {i=r_io_fifo();return i;}
else goto here;
}
//**********************************************if(ps_m_cnt>=11) {ps_m_cnt=0;}
#define MOUSE_Reset 0xff
#define MOUSE_Set_Resolution 0xe8
#define MOUSE_Set_Scaling 0xe6
#define MOUSE_Set_Remote_Mode 0xf0
#define MOUSE_Set_Stream_Mode 0xea
#define MOUSE_Get_device_ID 0xf2
#define MOUSE_Set_Sample_Rate 0xf3
#define MOUSE_Acknowledge 0xfa
#define MOUSE_REsend 0xfE
uint8 reset_mouse(void)
{
slb(PS2dat);slb(PS2clk);//sda=1;
in(PS2dat); in(PS2clk);
use_interrupt2(Int2down_req);
#if uart_debug_mouse==1
uart_send1('A');
#endif
//========reset orguint8 retry=0;
for(retry=0;retry<8;retry++)
{if(Command2mouse(MOUSE_Reset)==error) // 重启
return error;
if(mouse_read()!=MOUSE_REsend) break; // 鼠标的回答
}
mouse_read(); // 重启成功. 0xaa
mouse_read(); //回应鼠标ID 0x00
//======================
#if uart_debug_mouse==1
uart_send1('B');
#endif
for(retry=0;retry<8;retry++)
{Command2mouse(MOUSE_Reset); // 重启
if(mouse_read()!=MOUSE_REsend) break; // 鼠标的回答
}
mouse_read(); // 重启成功. 0xaa
mouse_read(); //回应鼠标ID 0x00
//================== //attempt to enter MS scroll mouse mode
Command2mouse(MOUSE_Set_Sample_Rate); // 进入滚轮模式
mouse_read(); // 读鼠标的应答
Command2mouse(200);
mouse_read();
Command2mouse(MOUSE_Set_Sample_Rate); // 进入滚轮模式
mouse_read(); // 读鼠标的应答
Command2mouse(100);
mouse_read();
Command2mouse(MOUSE_Set_Sample_Rate); // 进入滚轮模式
mouse_read(); // 读鼠标的应答
Command2mouse(80);
mouse_read();
//======================
Command2mouse(MOUSE_Get_device_ID); // mouse id get
mouse_read(); // 读鼠标的应 fa
if( mouse_read()!=0x03) return error; // 读鼠标的应答 id 0x03 is scroll mouse
//==========
Command2mouse( MOUSE_Set_Resolution); //set
mouse_read(); // 读鼠标的应答
Command2mouse(0x03); //8counters per mm
mouse_read();
//===============
Command2mouse(MOUSE_Set_Scaling ); //set 1:1
mouse_read(); // 读鼠标的应答
//===============
Command2mouse( MOUSE_Set_Sample_Rate); //set
mouse_read(); // 读鼠标的应答
Command2mouse(40); //8counters per mm
mouse_read();
//===============
for(retry=0;retry<8;retry++)//enable it
{Command2mouse(0xf4); //
if(mouse_read()==MOUSE_Acknowledge) break; // 鼠标的回答
}
#if uart_debug_mouse==1
uart_send1('C');
#endif
return ok;
//============now it is over
}
#define LeftBnt 0
#define RightBtn 1
#define MidBtn 2
#define Always1 3
#define XsignB 4
#define YsignB 5
#define XOverFlow 6
#define yOverFlow 7
//===================
uint8 MouseMsg;
void MouseSev(void)
{
uint8 mouse_x,mouse_y,mouse_z,mouse_status;
if(io_dat_cnt>=4 )
{
mouse_status = mouse_read2();//读取四个字节的信号
mouse_x = mouse_read2();
mouse_y = mouse_read2();
mouse_z = mouse_read2();
//if(mouse_status&(1<<LeftBnt)) //uart_send1('1');
//if(mouse_status&(1<<RightBtn)) //uart_send1('2');
//if(mouse_status&(1<<MidBtn)) //uart_send1('3');
//uart_send1(mouse_z);
MouseMsg=0;
MouseMsg|=mouse_status&07;
MouseMsg|=((mouse_z<<4)&0xf0);
if(MouseMsg) OSTaskResume(1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -