Advertisement
Guest User

mouse.cpp

a guest
Jun 25th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <drivers/mouse.h>
  2.  
  3. using namespace hardware;
  4. using namespace drivers;
  5.  
  6. void print(uint8_t* str);
  7. void setCursorPosition(uint8_t newx, uint8_t newy);
  8.  
  9. void MouseEventHandler::onMouseDown(uint8_t button) {}
  10.  
  11. void MouseEventHandler::onMouseUp(uint8_t button) {}
  12.  
  13. void MouseEventHandler::onMouseMove(int x, int y) {}
  14.  
  15. MouseDriver::MouseDriver(InterruptManager* interruptManager, MouseEventHandler* events)
  16. :dataport(0x60),
  17.  commandport(0x64),
  18.  InterruptHandler(interruptManager, 0x2C) {
  19.  
  20.   offset = 0;
  21.   buttons = 0;
  22.   x = 0;
  23.   y = 0;
  24.  
  25.   eventHandler = events;
  26.  
  27.   eventHandler->onMouseMove(x, y);
  28.  
  29. }
  30.  
  31. void MouseDriver::activate() {
  32.  
  33.   commandport.write(0xA8);
  34.   commandport.write(0x20); // command 0x60 = read controller command byte
  35.   uint8_t status = dataport.read() | 2;
  36.   commandport.write(0x60); // command 0x60 = set controller command byte
  37.   dataport.write(status);
  38.  
  39.   commandport.write(0xD4);
  40.   dataport.write(0xF4);
  41.   dataport.read();
  42.  
  43. }
  44.  
  45. uint32_t MouseDriver::handleInterrupt(uint32_t esp) {
  46.  
  47.   uint8_t status = commandport.read();
  48.   if (!(status & 0x20))
  49.     return esp;
  50.  
  51.   buffer[offset] = dataport.read();
  52.   offset = (offset + 1) % 3;
  53.  
  54.   if(offset == 0) {
  55.     if(buffer[1] != 0 || buffer[2] != 0) {
  56.      
  57.       x += buffer[1];
  58.       if(x >= 80) x = 79;
  59.       if(x < 0) x = 0;
  60.       y -= buffer[2];
  61.       if(y >= 25) y = 24;
  62.       if(y < 0) y = 0;
  63.      
  64.       eventHandler->onMouseMove(x, y);
  65.     }
  66.   }
  67.   return esp;
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement