/*******************************************************************************
* Start of Arduino_GFX setting
*
* Arduino_GFX try to find the settings depends on selected board in Arduino IDE
* Or you can define the display dev kit not in the board list
* Defalult pin list for non display dev kit:
* Arduino Nano, Micro and more: CS: 9, DC: 8, RST: 7, BL: 6, SCK: 13, MOSI: 11, MISO: 12
* ESP32 various dev board : CS: 5, DC: 27, RST: 33, BL: 22, SCK: 18, MOSI: 23, MISO: nil
* ESP32-C3 various dev board : CS: 7, DC: 2, RST: 1, BL: 3, SCK: 4, MOSI: 6, MISO: nil
* ESP32-S2 various dev board : CS: 34, DC: 35, RST: 33, BL: 21, SCK: 36, MOSI: 35, MISO: nil
* ESP32-S3 various dev board : CS: 40, DC: 41, RST: 42, BL: 48, SCK: 36, MOSI: 35, MISO: nil
* ESP8266 various dev board : CS: 15, DC: 4, RST: 2, BL: 5, SCK: 14, MOSI: 13, MISO: 12
* Raspberry Pi Pico dev board : CS: 17, DC: 27, RST: 26, BL: 28, SCK: 18, MOSI: 19, MISO: 16
* RTL8720 BW16 old patch core : CS: 18, DC: 17, RST: 2, BL: 23, SCK: 19, MOSI: 21, MISO: 20
* RTL8720_BW16 Official core : CS: 9, DC: 8, RST: 6, BL: 3, SCK: 10, MOSI: 12, MISO: 11
* RTL8722 dev board : CS: 18, DC: 17, RST: 22, BL: 23, SCK: 13, MOSI: 11, MISO: 12
* RTL8722_mini dev board : CS: 12, DC: 14, RST: 15, BL: 13, SCK: 11, MOSI: 9, MISO: 10
* Seeeduino XIAO dev board : CS: 3, DC: 2, RST: 1, BL: 0, SCK: 8, MOSI: 10, MISO: 9
* Teensy 4.1 dev board : CS: 39, DC: 41, RST: 40, BL: 22, SCK: 13, MOSI: 11, MISO: 12
******************************************************************************/
#include <Arduino_GFX_Library.h>
#define GFX_BL DF_GFX_BL // default backlight pin, you may replace DF_GFX_BL to actual backlight pin
/* More dev device declaration: https://github.com/moononournation/Arduino_GFX/wiki/Dev-Device-Declaration */
#if defined(DISPLAY_DEV_KIT)
Arduino_GFX *gfx = create_default_Arduino_GFX();
#else /* !defined(DISPLAY_DEV_KIT) */
/* More data bus class: https://github.com/moononournation/Arduino_GFX/wiki/Data-Bus-Class */
#if 0
Arduino_DataBus *bus = create_default_Arduino_DataBus();
#else
Arduino_DataBus *bus = new Arduino_ESP32SPI(5 /* DC */, 7 /* CS */, 4 /* SCK */, 6 /* MOSI */, -1 /* MISO */, HSPI /* spi_num */);
#endif
/* More display class: https://github.com/moononournation/Arduino_GFX/wiki/Display-Class */
#if 0
Arduino_GFX *gfx = new Arduino_ILI9341(bus, DF_GFX_RST, 0 /* rotation */, false /* IPS */);
#else
Arduino_GFX *gfx = new Arduino_GC9A01(bus, 9 /* RST */, 0 /* rotation */, true /* IPS */);
#endif
#endif /* !defined(DISPLAY_DEV_KIT) */
/*******************************************************************************
* End of Arduino_GFX setting
******************************************************************************/
#define DF_GFX_BL 8
/**
* This example turns the ESP32 into a Bluetooth LE keyboard and mouse that writes
* some words, presses Enter, presses a media key and then Ctrl+Alt+Delete,
* then moves and the scrolls the mouse and clicks it.
*/
#include <BleCombo.h>
//extern BleComboKeyboard Keyboard("SmartKnob","RSmakerStudio",100);
/***************************定义引脚********************************/
int pinA = 18; // Connected to CLK on KY-040 CLK接 GPIO18
int pinB = 19; // Connected to DT on KY-040 DT接GPIO19 SW是按键,不用接。
const int analogInPin = 2; // Analog input pin that the potentiometer is attached to
const int led = 22; // Analog output pin that the LED is attached to
/***************************定义参数********************************/
int sensorValue = 0; // 模拟接口寄存
int encoderPosCount = 0;
int pinALast;
int aVal;
boolean bCW;
int ButtonVal = 0;
int ButtonState = 0; //0:无按下,1:上,2:下,3:左;4:右,5:中间
void setup() {
Serial.begin(9600); //初始化串口
/**************** 显示屏初始化 开始***************************/
gfx->begin();
gfx->fillScreen(BLACK);
#ifdef GFX_BL
pinMode(GFX_BL, OUTPUT);
digitalWrite(GFX_BL, HIGH);
#endif
gfx->setCursor(50, 50);
gfx->setTextSize(3,3,1);
gfx->setTextColor(RED);
gfx->println("ESP32 ");
gfx->setCursor(60, 80);
gfx->setTextSize(3,3,1);
gfx->setTextColor(YELLOW);
gfx->println("SmartKnob");
gfx->setCursor(60, 120);
gfx->setTextSize(2,2,1);
gfx->setTextColor(ORANGE);
gfx->println("Mouse Mode");
/**************** 显示屏初始化 结束***************************/
/**************** 初始化旋钮IO 开始***************************/
pinMode (pinA,INPUT);
pinMode (pinB,INPUT);
pinALast = digitalRead(pinA);
/**************** 初始化旋钮IO 结束***************************/
Serial.println("Starting work!"); //输出开始
Keyboard.deviceName = "SmartKnob"; //蓝牙ID
Keyboard.begin(); //初始化蓝牙键盘
Mouse.begin(); //初始化蓝牙鼠标
}
void loop() {
if(Keyboard.isConnected()) {
// 读取模拟信号:按键
sensorValue = analogRead(analogInPin);
/********************** 五向按键 开始************************/
ButtonVal = sensorValue;
if (ButtonVal > 30){
if (ButtonVal < 500){
ButtonState = 1; //按键上
Mouse.move(0,-1);
}
else if (ButtonVal < 700){
ButtonState = 2; //按键下
Mouse.move(0,1);
}
else if (ButtonVal < 1000){
ButtonState = 3; //按键左
Mouse.move(-1,0);
}
else if (ButtonVal < 1400){
ButtonState = 4; //按键右
Mouse.move(1,0);
}
else if (ButtonVal < 2200){
ButtonState = 5; //按键中间
Mouse.click(MOUSE_LEFT);
}
//输出结果测试
Serial.printf("ButtonState: %d\n",ButtonState);
}
else
{
ButtonState = 0; //无按键按下
}
/********************** 五向按键 结束************************/
/********************** 360度编码器旋钮 开始************************/
aVal = digitalRead(pinA);
if (aVal != pinALast){ // Means the knob is rotating
// if the knob is rotating, we need to determine direction
// We do that by reading pin B.
if (digitalRead(pinB) != aVal) { // Means pin A Changed first - We're Rotating Clockwise
encoderPosCount ++;
bCW = true;
} else {// Otherwise B changed first and we're moving CCW
bCW = false;
encoderPosCount--;
}
Serial.printf("Rotated: ");
if (bCW){
Serial.printf("clockwise ");
Mouse.move(0,0,-1);
}else{
Serial.printf("counterclockwise ");
Mouse.move(0,0,1);
}
Serial.printf("Encoder Position: %d\n",encoderPosCount);
}
pinALast = aVal;
/********************** 360度编码器旋钮 结束************************/
delay(50);
/*
Serial.println("Sending 'Hello world'");
Keyboard.println("Hello World");
delay(1000);
Serial.println("Sending Enter key...");
Keyboard.write(KEY_RETURN);
delay(1000);
Serial.println("Sending Play/Pause media key...");
Keyboard.write(KEY_MEDIA_PLAY_PAUSE);
delay(1000);*/
/*Serial.println("Sending Ctrl+Alt+Delete...");
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_DELETE);
delay(100);*/
//Keyboard.releaseAll();
/*
unsigned long startTime;
Serial.println("Move mouse pointer up");
startTime = millis();
while(millis()<startTime+1000) {
Mouse.move(0,-1);
delay(5);
}
Serial.println("Move mouse pointer left");
startTime = millis();
while(millis()<startTime+1000) {
Mouse.move(-1,0);
delay(5);
}
Serial.println("Move mouse pointer down");
startTime = millis();
while(millis()<startTime+1000) {
Mouse.move(0,1);
delay(5);
}
Serial.println("Move mouse pointer right");
startTime = millis();
while(millis()<startTime+1000) {
Mouse.move(1,0);
delay(5);
}
Serial.println("Scroll Down");
Mouse.move(0,0,-1);
delay(500);
Serial.println("Left click");
Mouse.click(MOUSE_LEFT);
delay(500);
Serial.println("Right click");
Mouse.click(MOUSE_RIGHT);
delay(500);*/
/*Serial.println("Scroll wheel click");
Mouse.click(MOUSE_MIDDLE);
delay(500);
Serial.println("Back button click");
Mouse.click(MOUSE_BACK);
delay(500);
Serial.println("Forward button click");
Mouse.click(MOUSE_FORWARD);
delay(500);
Serial.println("Click left+right mouse button at the same time");
Mouse.click(MOUSE_LEFT | MOUSE_RIGHT);
delay(500);
Serial.println("Click left+right mouse button and scroll wheel at the same time");
Mouse.click(MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE);
delay(500);*/
}
else
{
Serial.println("Waiting 2 seconds..."); //输出等待时间提示
delay(2000); //延时
}
/**************** 显示屏控制 开始***************************/
//gfx->setCursor(random(gfx->width()), random(gfx->height()));
//gfx->setTextColor(random(0xffff), random(0xffff));
//gfx->setTextSize(random(6) /* x scale */, random(6) /* y scale */, random(2) /* pixel_margin */);
//gfx->println("SmartKnob");
/**************** 显示屏控制 结束***************************/
//delay(1000); // 1 second
}