MOUSE_PS2.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. ----------------------------------------------------------------------
  3. File : GUITOUCH.C
  4. Purpose : Touch screen manager
  5. ----------------------------------------------------------------------
  6. This module handles the touch screen. It is configured in the file
  7. GUITouch.conf.h (Should be located in the Config\ directory).
  8. ----------------------------------------------------------------------
  9. */
  10. #include <rtthread.h>
  11. #include <event.h>
  12. #include <rtgui_server.h>
  13. #include <lcd.h>
  14. #define MOUSE_CLK (1<<2 ) //GPG2
  15. #define MOUSE_DATA (1<<11) //GPE11
  16. #define MOUSE_CLK_H() (GPGDAT |= MOUSE_CLK)
  17. #define MOUSE_CLK_L() (GPGDAT &= ~MOUSE_CLK)
  18. #define MOUSE_CLK_OUT() (GPGCON = GPGCON &(~(0x03<<4)) | (0x01<<4))
  19. #define MOUSE_DATA_H() (GPEDAT |= MOUSE_DATA)
  20. #define MOUSE_DATA_L() (GPEDAT &= ~MOUSE_DATA)
  21. #define MOUSE_DATA_IN() (GPECON = GPECON &(~(0x03<<22)))
  22. #define MOUSE_DATA_OUT() (GPECON = GPECON &(~(0x03<<22))|(0x01<<22))
  23. #define MOUSE_DATA_GET() (GPEDAT & MOUSE_DATA)
  24. static rt_int32_t _ScreenX = 0; /* x-pos */
  25. static rt_int32_t _ScreenY = 0; /* y-pos */
  26. static rt_int32_t _NumBytesInBuffer = 0; /* bytes in rx buffer */
  27. static rt_uint8_t _Buttons = 0; /* button status */
  28. static rt_uint8_t _abInBuffer[3]; /* mouse rx buffer */
  29. /*********************************************************************
  30. * PID (Pointer input device ... mouse/touch)
  31. **********************************************************************
  32. */
  33. typedef struct
  34. {
  35. struct rt_device parent;
  36. struct rt_timer* timer;
  37. int x,y;
  38. rt_uint8_t Pressed;
  39. } PS2_MOUSE;
  40. static PS2_MOUSE *s_mouse = RT_NULL;
  41. /*********************************************************************
  42. * _EvaPacket
  43. **********************************************************************
  44. 从鼠标接收的数据包队列:
  45. | D7 D6 D5 D4 D3 D2 D1 D0
  46. ---------------------------------------------------------
  47. 1st byte | -- -- Y- X- 1 -- LB RB
  48. 2nd byte | X7 X6 X5 X4 X3 X2 X1 X0
  49. 3rd byte | Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
  50. */
  51. static void _EvaPacket(void)
  52. {
  53. char a;
  54. rtgui_event_mouse_t emouse;
  55. emouse.parent.sender = RT_NULL;
  56. _Buttons = _abInBuffer[0] & 0x03;
  57. a = _abInBuffer[1];
  58. //测试X移动符号
  59. if(_abInBuffer[0] & 0x10)
  60. {
  61. a = -a;
  62. _ScreenX -= a;
  63. } /* direction is negative, move left */
  64. else
  65. {
  66. _ScreenX += a;
  67. }
  68. a = _abInBuffer[2];
  69. //测试Y移动符号
  70. if(_abInBuffer[0] & 0x20)
  71. {
  72. a = -a;
  73. _ScreenY += a;
  74. } /* direction is negative, move down */
  75. else
  76. {
  77. _ScreenY -= a;
  78. }
  79. /* check min/max positions */
  80. if(_ScreenX < 0)
  81. {
  82. _ScreenX = 0;
  83. }
  84. else if(_ScreenX > LCD_XSIZE-1)
  85. {
  86. _ScreenX = LCD_XSIZE-1;
  87. }
  88. if(_ScreenY < 0)
  89. {
  90. _ScreenY = 0;
  91. }
  92. else if(_ScreenY > LCD_YSIZE-1)
  93. {
  94. _ScreenY = LCD_YSIZE-1;
  95. }
  96. /* signal new mouse data */
  97. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  98. emouse.x = _ScreenX;
  99. emouse.y = _ScreenY;
  100. emouse.button = _Buttons;
  101. //GUI_MOUSE_StoreState(&emouse);
  102. rtgui_server_post_event((&emouse.parent), sizeof(emouse));
  103. }
  104. /*********************************************************************
  105. Mouse receive interrupt handler
  106. The PS2 mouse interrupt gets in three bytes from the mouse, then wakes
  107. up the mouse LSR.
  108. **********************************************************************/
  109. void GUI_MOUSE_DRIVER_PS2_OnRx(rt_uint8_t Data)
  110. {
  111. if(!_NumBytesInBuffer)
  112. {
  113. /* check for start frame */
  114. if((Data & 0x0c) == 0x08)
  115. {
  116. _abInBuffer[0] = Data;
  117. _NumBytesInBuffer++;
  118. }
  119. }
  120. else
  121. {
  122. _abInBuffer[_NumBytesInBuffer] = Data;
  123. _NumBytesInBuffer++;
  124. if(_NumBytesInBuffer >= 3)
  125. {
  126. _EvaPacket();
  127. _NumBytesInBuffer = 0;
  128. }
  129. }
  130. }
  131. /*********************************************************************
  132. *
  133. * GUI_MOUSE_DRIVER_PS2_Init
  134. *
  135. **********************************************************************
  136. */
  137. void mouse_PS2_init(void)
  138. {
  139. _NumBytesInBuffer = 0;
  140. }
  141. static void mouse_timer_fire(void* parameter)
  142. {
  143. }
  144. void rtgui_PS2_hw_init(void)
  145. {
  146. s_mouse = (PS2_MOUSE*)rt_malloc (sizeof(PS2_MOUSE));
  147. if(s_mouse == RT_NULL) return; /* no memory yet */
  148. /* clear device structure */
  149. rt_memset(&(s_mouse->parent), 0, sizeof(struct rt_device));
  150. /* init device structure */
  151. s_mouse->parent.type = RT_Device_Class_Unknown;
  152. s_mouse->parent.init = RT_NULL;//rtgui_mouse_init;
  153. s_mouse->parent.control = RT_NULL;//rtgui_touch_control;
  154. s_mouse->parent.private = RT_NULL;
  155. /* create 1/8 second timer */
  156. s_mouse->timer = rt_timer_create("mouse", mouse_timer_fire, RT_NULL,
  157. RT_TICK_PER_SECOND/10, RT_TIMER_FLAG_PERIODIC);
  158. /* register touch device to RT-Thread */
  159. rt_device_register(&(s_mouse->parent), "mouse", RT_DEVICE_FLAG_RDWR);
  160. s_mouse->parent.init(RT_NULL);
  161. }