165 lines
3.5 KiB
C++
165 lines
3.5 KiB
C++
#include <Wire.h>
|
|
#include <Adafruit_RGBLCDShield.h>
|
|
#include <utility/Adafruit_MCP23017.h>
|
|
#include "Keyboard.h"
|
|
#include "Mouse.h"
|
|
|
|
// The shield uses the I2C SCL and SDA pins. On classic Arduinos
|
|
// this is Analog 4 and 5 so you can't use those for analogRead() anymore
|
|
// However, you can connect other I2C sensors to the I2C bus and share
|
|
// the I2C bus.
|
|
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
|
|
|
|
// These #defines make it easy to set the backlight color
|
|
#define RED 0x1
|
|
#define YELLOW 0x3
|
|
#define GREEN 0x2
|
|
#define TEAL 0x6
|
|
#define BLUE 0x4
|
|
#define VIOLET 0x5
|
|
#define WHITE 0x7
|
|
|
|
|
|
unsigned long lcdTimeout = 1500;
|
|
unsigned long lcdLastSet = 0;
|
|
|
|
unsigned long debounceDelay = 50;
|
|
|
|
typedef struct
|
|
{
|
|
unsigned long lastDebounceTime = 0;
|
|
bool state = false;
|
|
bool lastState = false;
|
|
} DebounceButton;
|
|
|
|
DebounceButton up, down, left, right, select;
|
|
|
|
typedef enum {
|
|
MODE_LEFT_CLICK,
|
|
MODE_WALK_FORWARD,
|
|
MODE_COUNT
|
|
} Mode;
|
|
Mode currMode = MODE_LEFT_CLICK;
|
|
|
|
char *modeNames[] = {
|
|
"Left click ",
|
|
"Walk forward "
|
|
};
|
|
|
|
bool doClicking = false;
|
|
unsigned long lastClickTime = 0;
|
|
int clickColor = 0;
|
|
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
|
|
// set columns and rows:
|
|
lcd.begin(16, 2);
|
|
|
|
Mouse.begin();
|
|
Keyboard.begin();
|
|
|
|
lcd.print("AutoClicker 3000");
|
|
lcd.setCursor(0, 1);
|
|
lcd.print("For Jasper");
|
|
lcd.setBacklight(YELLOW);
|
|
|
|
lcdLastSet = millis();
|
|
}
|
|
|
|
void debounce(DebounceButton &b, bool reading) {
|
|
if ((millis() - b.lastDebounceTime) > debounceDelay) {
|
|
b.lastState = b.state;
|
|
b.state = reading;
|
|
b.lastDebounceTime = millis();
|
|
}
|
|
}
|
|
|
|
bool triggered(DebounceButton &b) {
|
|
return b.state and !b.lastState;
|
|
}
|
|
|
|
int clickDelay = 100; // millis
|
|
int delayChange = 10; // millis
|
|
|
|
void loop() {
|
|
uint8_t buttons = lcd.readButtons();
|
|
|
|
debounce(up, buttons & BUTTON_UP);
|
|
debounce(down, buttons & BUTTON_DOWN);
|
|
debounce(left, buttons & BUTTON_LEFT);
|
|
debounce(right, buttons & BUTTON_RIGHT);
|
|
debounce(select, buttons & BUTTON_SELECT);
|
|
|
|
if (buttons) {
|
|
// if (buttons & BUTTON_UP) {
|
|
// //Keyboard.write('u');
|
|
// Serial.println("UP");
|
|
// lcdLastSet = millis();
|
|
// }
|
|
//
|
|
|
|
if (triggered(down)) {
|
|
doClicking = !doClicking;
|
|
lcd.setCursor(0, 1);
|
|
if (doClicking) {
|
|
lcd.print("Start clicking ");
|
|
} else {
|
|
lcd.print("Stop clicking ");
|
|
}
|
|
lcd.setBacklight(WHITE);
|
|
lcdLastSet = millis();
|
|
}
|
|
|
|
if (triggered(left)) {
|
|
lcd.setCursor(0, 1);
|
|
lcd.print("Slower ");
|
|
// slow down clicking
|
|
if (clickDelay < 1000) {
|
|
clickDelay += delayChange;
|
|
}
|
|
lcdLastSet = millis();
|
|
}
|
|
|
|
if (triggered(right)) {
|
|
lcd.setCursor(0, 1);
|
|
lcd.print("Faster ");
|
|
// speed up clicking
|
|
if (clickDelay > 50) {
|
|
clickDelay -= delayChange;
|
|
}
|
|
lcdLastSet = millis();
|
|
}
|
|
|
|
if (triggered(select)) {
|
|
currMode = (currMode + 1) % MODE_COUNT;
|
|
lcd.setCursor(0, 0);
|
|
lcd.print(modeNames[currMode]);
|
|
}
|
|
}
|
|
|
|
if (doClicking) {
|
|
unsigned long time = millis();
|
|
if ((unsigned long)(time - lastClickTime) >= clickDelay) {
|
|
if (clickColor == RED) {
|
|
clickColor = WHITE;
|
|
} else {
|
|
clickColor = RED;
|
|
}
|
|
lcd.setBacklight(clickColor);
|
|
Mouse.click(MOUSE_LEFT);
|
|
lastClickTime = time;
|
|
}
|
|
}
|
|
|
|
// clear second lcd line after a while
|
|
if (lcdLastSet > 0 && millis() > lcdLastSet + lcdTimeout) {
|
|
lcd.setCursor(0, 0);
|
|
lcd.print(modeNames[currMode]);
|
|
lcd.setCursor(0, 1);
|
|
lcd.print(" ");
|
|
lcdLastSet = 0;
|
|
}
|
|
}
|