Advertisement
Guest User

Untitled

a guest
Dec 15th, 2014
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. #include <minix/sysutil.h>
  2. #include <minix/syslib.h>
  3. #include <minix/drivers.h>
  4. #include <stdio.h>
  5.  
  6. #include "Mouse.h"
  7.  
  8. #define defaultColor1 BLACK
  9. #define defaultColor2 WHITE
  10. #define defaultSize 3
  11.  
  12. //STATIC VARIABLES
  13.  
  14. //The controller sends the movement information to the PC via a serial
  15.  
  16. //line in 3-byte data packet
  17.  
  18. static long int data_packet[3]; //3 byte data packet
  19. static long unsigned int code;
  20. static long unsigned int data;
  21.  
  22. //COUNTER
  23.  
  24. static int counter_mouse;
  25.  
  26. static int counter_packets;
  27.  
  28. //COORDENATES
  29.  
  30. static unsigned int horiz;
  31.  
  32. static unsigned int vert;
  33.  
  34. //As the previous lab we need a function to read from the input buffer
  35.  
  36. //if OK it will return the data i.e the response, if not
  37.  
  38. //we'll -1 (ERROR STATUS)
  39.  
  40. static int hook_id = 12;
  41.  
  42. int mouse_read_inb(){//sys_inb;no arguments
  43.  
  44. unsigned long stat, data;
  45.  
  46. int safetyCounter = 0;
  47.  
  48. while( safetyCounter < 3 ) {
  49.  
  50. if ( sys_inb(STAT_REG, &stat) != OK )
  51.  
  52. return 1;
  53.  
  54. if( stat & OBF ) {
  55.  
  56. sys_inb(OUT_BUF, &data);
  57.  
  58. //if ( (stat &(PAR_ERR | TO_ERR)) == 0 )
  59.  
  60. return data;
  61.  
  62. //else
  63.  
  64. //return -1;
  65.  
  66. }
  67.  
  68. tickdelay(micros_to_ticks(DELAY_US));
  69.  
  70. safetyCounter++;
  71.  
  72. }
  73.  
  74. return 1;
  75.  
  76. }
  77.  
  78. //in addition, on this lab we're going use a similar function than
  79.  
  80. //the previous one but instead to read the input buffer, we're
  81.  
  82. //going to check the output buffer
  83.  
  84. int mouse_write_outb(unsigned char port, unsigned char cmd){
  85.  
  86. unsigned long stat;
  87.  
  88. while( 1 ) {
  89.  
  90. sys_inb(STAT_REG, &stat);
  91.  
  92. if(( stat & IBF ) == 0) {
  93.  
  94. if ( sys_outb(port, cmd) != OK )
  95.  
  96. return 1;
  97.  
  98. sys_inb(OUT_BUF, &stat);
  99.  
  100. if(stat == ACK)
  101.  
  102. return 0;
  103.  
  104. else
  105.  
  106. return -1;
  107.  
  108. }
  109.  
  110. tickdelay(micros_to_ticks(DELAY_US));
  111.  
  112. //timer_test_int(2);
  113.  
  114. }
  115.  
  116. }
  117.  
  118. Mouse* mouse = NULL;
  119.  
  120. Mouse mousePreviousState;
  121.  
  122. Mouse* newMouse();
  123.  
  124. void enableMouse(){
  125.  
  126. printf("Aqui enMouse\n");
  127.  
  128. mouse_write_outb(IN_BUF, KBCTOMOUSE);
  129.  
  130. mouse_write_outb(OUT_BUF, EN_DATA_REP);
  131.  
  132. }
  133.  
  134. Mouse* getMouse(){
  135.  
  136. printf("Aqui getMouse\n");
  137.  
  138. if(!mouse){
  139.  
  140. enableMouse();
  141.  
  142. mouse = newMouse();
  143.  
  144. }
  145.  
  146. return mouse;
  147.  
  148. }
  149.  
  150. Mouse* newMouse(){
  151.  
  152. printf("Aqui newMouse\n");
  153.  
  154. Mouse* mouse = (Mouse*) malloc(sizeof(Mouse));
  155.  
  156. mouse->x = 0;
  157.  
  158. mouse->y = 0;
  159.  
  160. mouse->size = defaultSize;
  161.  
  162. mouse->color1 = defaultColor1;
  163.  
  164. mouse->color2 = defaultColor2;
  165.  
  166. }
  167.  
  168. void updateMouse(){
  169.  
  170. data_packet[counter_mouse] = mouse_read_inb();
  171.  
  172. if ((counter_mouse) == 0) {
  173.  
  174. if ((data_packet[counter_mouse] & BIT(3)) == 0) {
  175.  
  176. return;
  177.  
  178. }
  179.  
  180. }
  181.  
  182. counter_mouse++;
  183.  
  184. if(counter_mouse == 3)
  185.  
  186. {
  187.  
  188. counter_mouse = 0;
  189.  
  190. counter_packets++;
  191.  
  192. printf("\nCheguei aqui1");
  193.  
  194. //show_packet();
  195.  
  196. }
  197.  
  198. }
  199.  
  200. void drawCrosshair(Mouse* mouse){
  201.  
  202. printf("Aqui crosshair\n");
  203.  
  204. int x = mouse->x, y=mouse->y;
  205.  
  206. int size = mouse->size;
  207.  
  208. printf("Aqui drawcross2\n");
  209.  
  210. //black border
  211.  
  212. vg_draw_line(x-size, y-1, x+size, y-1, mouse->color1);
  213.  
  214. vg_draw_line(x-size, y+1, x+size, y+1, mouse->color1);
  215.  
  216. vg_draw_line(x-1, y-size, x-1, y+size, mouse->color1);
  217.  
  218. vg_draw_line(x+1, y-size, x+1, y+size, mouse->color1);
  219.  
  220. printf("Aqui drawcross3\n");
  221.  
  222. vg_draw_line(x-size, y, x+size, y, mouse->color2);
  223.  
  224. vg_draw_line(x, y-size, x, y+size, mouse->color2);
  225.  
  226. printf("Aqui drawcross4\n");
  227.  
  228. }
  229.  
  230. void drawMouse(){
  231.  
  232. printf("Aqui drawMouse\n");
  233.  
  234. drawCrosshair(getMouse());
  235.  
  236. getMouse()-> draw = 0;
  237.  
  238. printf("Aqui drawMouseFim\n");
  239.  
  240. }
  241.  
  242. void deleteMouse(){
  243.  
  244. printf("Aqui deleteMouse\n");
  245.  
  246. free(getMouse());
  247.  
  248. }
  249.  
  250. int mouseInside(int x1, int x2, int y1, int y2){
  251.  
  252. return x1 <= getMouse()->x && getMouse()->x <= x2 && y1 <=getMouse()->y && getMouse()->y <= y2;
  253.  
  254. }
  255.  
  256. //SUBSCRIBERS
  257.  
  258. int mouse_subscribe_int(void ) {//like we did on the previous lab, we need to subscribe some interrupts, in this case, the mouse interrupts.
  259.  
  260. //SUBSCRIPTION
  261.  
  262. int mask = BIT(hook_id);
  263.  
  264. if (sys_irqsetpolicy(MOUSE_IRQ,IRQ_REENABLE | IRQ_EXCLUSIVE,&hook_id));
  265.  
  266. if (sys_irqenable(&hook_id) != OK)
  267.  
  268. return -1;
  269.  
  270. return mask;
  271.  
  272. }
  273.  
  274. int mouse_unsubscribe_int() {
  275.  
  276. //UNSUBSCRIPTION
  277.  
  278. mouse_write_outb(IN_BUF, KBCTOMOUSE);
  279.  
  280. mouse_write_outb(OUT_BUF, SET_DEF);
  281.  
  282. if (sys_irqrmpolicy(&hook_id) != OK) //unsubscribes a previous interrupt with the specified hook_id
  283.  
  284. return 1;
  285.  
  286. return 0;
  287.  
  288. if(sys_irqdisable(&hook_id) != OK) //disables interrupts on the IRQ line associated with the specified hook_id.
  289.  
  290. return 1;
  291.  
  292. return 0;
  293.  
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement