|
| 1 | +/* |
| 2 | + * A demo for Wio Terminal to simulate mouse by buttons. |
| 3 | + * Such as Mouse Up, Mouse Down, Mouse Left, Mouse Right, |
| 4 | + * Click the left mouse button, Click the right mouse button, |
| 5 | + * Up roll, Down roll and etc. |
| 6 | + * |
| 7 | + * Copyright (c) 2020 seeed technology co., ltd. |
| 8 | + * Author : weihong.cai (weihong.cai@seeed.cc) |
| 9 | + * Create Time : July 2020 |
| 10 | + * Change Log : |
| 11 | + * |
| 12 | + * The MIT License (MIT) |
| 13 | + * |
| 14 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 15 | + * of this software and associated documentation files (the "Software"), to deal |
| 16 | + * in the Software without restriction, including without limitation the rights |
| 17 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 18 | + * copies of the Software, and to permit persons to whom the Software istm |
| 19 | + * furnished to do so, subject to the following conditions: |
| 20 | + * |
| 21 | + * The above copyright notice and this permission notice shall be included in |
| 22 | + * all copies or substantial portions of the Software. |
| 23 | + * |
| 24 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 25 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 26 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 27 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 28 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 29 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INcommInterface |
| 30 | + * THE SOFTWARE. |
| 31 | + * |
| 32 | + * Usage(in Wio Terminal): |
| 33 | + * Press the WIO_5S_UP --------------------> Mouse Up |
| 34 | + * Press the WIO_5S_DOWN --------------------> Mouse Down |
| 35 | + * Press the WIO_5S_LEFT --------------------> Mouse Left |
| 36 | + * Press the WIO_5S_RIGHT --------------------> Mouse Right |
| 37 | + * Press the BUTTON_3 ------------------------> Click the left mouse button |
| 38 | + * Press the BUTTON_2 ------------------------> Click the right mouse button |
| 39 | + * Press the WIO_5S_PRESS and WIO_5S_UP ----> Up roll |
| 40 | + * Press the WIO_5S_PRESS and WIO_5S_DOWN ----> Down roll |
| 41 | + * |
| 42 | + * Some tips: |
| 43 | + * 1. If your PC unables to recognize USB device leading the Wio Terminal can’t work. |
| 44 | + * You can solve this problem through updating your ArduinoCore. |
| 45 | + * Please follow this: https://forum.seeedstudio.com/t/seeeduino-xiao-cant-simulate-keys-pressed/252819/6?u=weihong.cai |
| 46 | + * |
| 47 | + * You can know more about the Wio Terminal from: https://wiki.seeedstudio.com/Wio-Terminal-Getting-Started/ |
| 48 | + * If you have any questions, you can leave a message on the forum: https://forum.seeedstudio.com |
| 49 | + */ |
| 50 | + |
| 51 | +#include "Mouse.h" |
| 52 | + |
| 53 | +/*----------------define the button pins---------------------------*/ |
| 54 | +const int upButton = WIO_5S_UP; |
| 55 | +const int downButton = WIO_5S_DOWN; |
| 56 | +const int leftButton = WIO_5S_LEFT; |
| 57 | +const int rightButton = WIO_5S_RIGHT; |
| 58 | +const int mouseWheel = WIO_5S_PRESS; |
| 59 | +const int mouseBttonLeft = BUTTON_3; |
| 60 | +const int mouseBttonRight = BUTTON_2; |
| 61 | + |
| 62 | + // output range of X or Y movement; affects movement speed |
| 63 | +int range = 2; |
| 64 | + |
| 65 | + // response delay of the mouse, in ms |
| 66 | +int responseDelay = 5; |
| 67 | + |
| 68 | + // the time record paramas |
| 69 | +unsigned long _currentMillis; |
| 70 | +unsigned long _previousMillis; |
| 71 | + |
| 72 | +void setup() { |
| 73 | + // initialize the buttons' inputs: |
| 74 | + pinMode(upButton, INPUT); |
| 75 | + pinMode(downButton, INPUT); |
| 76 | + pinMode(leftButton, INPUT); |
| 77 | + pinMode(rightButton, INPUT); |
| 78 | + pinMode(mouseWheel, INPUT); |
| 79 | + pinMode(mouseBttonLeft, INPUT); |
| 80 | + pinMode(mouseBttonRight, INPUT); |
| 81 | + |
| 82 | + // initialize mouse control: |
| 83 | + Mouse.begin(); |
| 84 | +} |
| 85 | + |
| 86 | +void loop() { |
| 87 | + // read the button state: |
| 88 | + int upState = digitalRead(upButton); |
| 89 | + int downState = digitalRead(downButton); |
| 90 | + int rightState = digitalRead(rightButton); |
| 91 | + int leftState = digitalRead(leftButton); |
| 92 | + int clickState_mouseWheel = digitalRead(mouseWheel); |
| 93 | + int clickState_mouseButtonLeft = digitalRead(mouseBttonLeft); |
| 94 | + int clickState_mouseButtonRight = digitalRead(mouseBttonRight); |
| 95 | + |
| 96 | + // calculate the movement distance based on the button states: |
| 97 | + int xDistance = (leftState - rightState) * range; |
| 98 | + int yDistance = (upState - downState) * range; |
| 99 | + |
| 100 | +/*------------------Mouse Move--------------------------------------*/ |
| 101 | + // if X or Y is non-zero, move: |
| 102 | + if ((xDistance != 0) || (yDistance != 0)) { |
| 103 | + Mouse.move(xDistance, yDistance, 0); |
| 104 | + } |
| 105 | + |
| 106 | +/*-------------Mouse Button Left Click------------------------------*/ |
| 107 | + // if the mouse button left is pressed: |
| 108 | + if (clickState_mouseButtonLeft == LOW) { |
| 109 | + // if the mouse is not pressed, press it: |
| 110 | + if (!Mouse.isPressed(MOUSE_LEFT)) { |
| 111 | + Mouse.press(MOUSE_LEFT); |
| 112 | + //Mouse.click(MOUSE_LEFT); |
| 113 | + } |
| 114 | + } |
| 115 | + // else the mouse button left is not pressed: |
| 116 | + else { |
| 117 | + // if the mouse is pressed, release it: |
| 118 | + if (Mouse.isPressed(MOUSE_LEFT)) { |
| 119 | + Mouse.release(MOUSE_LEFT); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | +/*-------------Mouse Button Right Click-----------------------------*/ |
| 124 | + // if the mouse button right is pressed: |
| 125 | + if (clickState_mouseButtonRight == LOW) { |
| 126 | + // if the mouse is not pressed, press it: |
| 127 | + if (!Mouse.isPressed(MOUSE_RIGHT)) { |
| 128 | + Mouse.press(MOUSE_RIGHT); |
| 129 | + //Mouse.click(MOUSE_RIGHT); |
| 130 | + } |
| 131 | + } |
| 132 | + // else the mouse button right is not pressed: |
| 133 | + else { |
| 134 | + // if the mouse is pressed, release it: |
| 135 | + if (Mouse.isPressed(MOUSE_RIGHT)) { |
| 136 | + Mouse.release(MOUSE_RIGHT); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | +/*------------------Up roll----------------------------------------*/ |
| 141 | + if ((upState == LOW) && (clickState_mouseWheel == LOW)) { |
| 142 | + Mouse.move(0, 0, 1); |
| 143 | + My_delay(200); |
| 144 | + } |
| 145 | + |
| 146 | +/*------------------Down roll--------------------------------------*/ |
| 147 | + if ((downState == LOW) && (clickState_mouseWheel == LOW)) { |
| 148 | + Mouse.move(0, 0, -1); |
| 149 | + My_delay(200); |
| 150 | + } |
| 151 | + |
| 152 | +/*-----------------------------------------------------------------*/ |
| 153 | + // a delay so the mouse doesn't move too fast: |
| 154 | + My_delay(responseDelay); |
| 155 | +} |
| 156 | + |
| 157 | +// a delay function uses millis() |
| 158 | +void My_delay(int Time) |
| 159 | +{ |
| 160 | + while((_currentMillis - _previousMillis) <= Time) |
| 161 | + { |
| 162 | + _currentMillis = millis(); |
| 163 | + } |
| 164 | + _previousMillis = _currentMillis; |
| 165 | +} |
0 commit comments