├── Valo_mouse.py └── Valo_mouse.ino /Valo_mouse.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import os 3 | from mss import mss 4 | import numpy as np 5 | import win32api 6 | import serial 7 | 8 | fov = int(input("FOV: ")) 9 | sct = mss() 10 | 11 | 12 | arduino = serial.Serial('COM6', 115200) 13 | 14 | screenshot = sct.monitors[1] 15 | screenshot['left'] = int((screenshot['width'] / 2) - (fov / 2)) 16 | screenshot['top'] = int((screenshot['height'] / 2) - (fov / 2)) 17 | screenshot['width'] = fov 18 | screenshot['height'] = fov 19 | center = fov/2 20 | 21 | lower_color = np.array([140,111,160]) 22 | upper_color = np.array([148,154,194]) 23 | 24 | speed = float(input("SPEED: ")) 25 | 26 | def mousemove(x): 27 | #Convert the values to unsigned integers 28 | if x < 0: 29 | x = x+256 30 | if y < 0: 31 | y = y+256 32 | 33 | coord = [int(x),int(y)] 34 | arduino.write(coord) 35 | 36 | 37 | while True: 38 | if win32api.GetAsyncKeyState(0x01) < 0: 39 | 40 | img = np.array(sct.grab(screenshot)) 41 | hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) #convert 42 | mask = cv2.inRange(hsv, lower_color,upper_color) #color mask 43 | kernel = np.ones((3,3), np.uint8) 44 | dilated = cv2.dilate(mask,kernel,iterations=5) 45 | thresh = cv2.threshold(dilated, 60, 255, cv2.THRESH_BINARY)[1] #binary image 46 | contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) #detects contours 47 | if len(contours) != 0: 48 | mouse = cv2.moments(thresh) #centroid of the largest contour 49 | cX = (int(mouse["m10"] / mouse["m00"]) 50 | cY = (int(mouse["m01"] / mouse["m00"])) 51 | 52 | x = -(center - cX) if cX < center else cX - center 53 | y = -(center - cY) if cY < center else cY - center 54 | 55 | x2 = x * speed 56 | y2 = y * speed 57 | 58 | mousemove(x2,y2) -------------------------------------------------------------------------------- /Valo_mouse.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | USB Usb; 7 | USBHub Hub(&Usb); 8 | 9 | byte bf[2]; 10 | 11 | HIDBoot HidMouse(&Usb); 12 | 13 | String myString; 14 | int j = 0; 15 | int c = 0; 16 | int e = 0; 17 | int lmb = 0; 18 | int rmb = 0; 19 | int mmb = 0; 20 | int dx; 21 | int dy; 22 | int arr[2]; 23 | int arrv[8]; 24 | 25 | class MouseRptParser : public MouseReportParser 26 | { 27 | protected: 28 | void OnMouseMove (MOUSEINFO *mi); 29 | void OnLeftButtonUp (MOUSEINFO *mi); 30 | void OnLeftButtonDown (MOUSEINFO *mi); 31 | void OnRightButtonUp (MOUSEINFO *mi); 32 | void OnRightButtonDown (MOUSEINFO *mi); 33 | void OnMiddleButtonUp (MOUSEINFO *mi); 34 | void OnMiddleButtonDown (MOUSEINFO *mi); 35 | }; 36 | 37 | void MouseRptParser::OnMouseMove(MOUSEINFO *mi) 38 | { 39 | dx = mi->dX; 40 | dy = mi->dY; 41 | }; 42 | 43 | void MouseRptParser::OnLeftButtonUp (MOUSEINFO *mi) 44 | { 45 | lmb = 0; 46 | }; 47 | 48 | void MouseRptParser::OnLeftButtonDown (MOUSEINFO *mi) 49 | { 50 | lmb = 1; 51 | }; 52 | 53 | void MouseRptParser::OnRightButtonUp (MOUSEINFO *mi) 54 | { 55 | rmb = 0; 56 | }; 57 | 58 | void MouseRptParser::OnRightButtonDown (MOUSEINFO *mi) 59 | { 60 | rmb = 1; 61 | }; 62 | 63 | void MouseRptParser::OnMiddleButtonUp (MOUSEINFO *mi) 64 | { 65 | mmb = 0; 66 | }; 67 | 68 | void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi) 69 | { 70 | mmb = 1; 71 | }; 72 | 73 | MouseRptParser Prs; 74 | 75 | void setup() { 76 | delay(5000); 77 | Mouse.begin(); 78 | Serial.begin(115200); 79 | Serial.setTimeout(1); 80 | Usb.Init(); 81 | HidMouse.SetReportParser(0, &Prs); 82 | } 83 | 84 | void loop() { 85 | dx = 0; 86 | dy = 0; 87 | j = 0; 88 | c = 0; 89 | e = 0; 90 | Usb.Task(); 91 | //Clicking 92 | if (lmb == 0){ 93 | Mouse.release(MOUSE_LEFT); 94 | } 95 | 96 | else if (lmb == 1){ 97 | Mouse.press(MOUSE_LEFT); 98 | } 99 | 100 | if (rmb == 0){ 101 | Mouse.release(MOUSE_RIGHT); 102 | } 103 | 104 | else if (rmb == 1){ 105 | Mouse.press(MOUSE_RIGHT); 106 | } 107 | 108 | if (mmb == 0){ 109 | Mouse.release(MOUSE_MIDDLE); 110 | } 111 | 112 | else if (mmb == 1){ 113 | Mouse.press(MOUSE_MIDDLE); 114 | } 115 | 116 | if (Serial.available() > 0) { 117 | Serial.readBytes(bf, 2); 118 | int x = buffer[0]; 119 | int y = buffer[1]; 120 | 121 | // Convert the values back to signed integers 122 | if (x > 127) { 123 | x -= 256; 124 | } 125 | if (y > 127) { 126 | y -= 256; 127 | } 128 | Mouse.move(bf[0], bf[1], 0); 129 | } 130 | else { 131 | Mouse.move(dx, dy); 132 | } 133 | } 134 | --------------------------------------------------------------------------------