Skip to content

Instantly share code, notes, and snippets.

@sonictruth
Last active June 16, 2023 12:25
Show Gist options
  • Save sonictruth/ba69c6a29b81ccf1faa8a12699640246 to your computer and use it in GitHub Desktop.
Save sonictruth/ba69c6a29b81ccf1faa8a12699640246 to your computer and use it in GitHub Desktop.
ESP32 Chiupimouse
#include <wifiboy32.h>
#include <BleMouse.h>
#include "wb32-demo2.h"
BleMouse bleMouse("ChiupiMouse", "Chiupix", 90);
uint16_t key;
uint16_t lastKey;
bool enabled = false;
long lastDoTime = 0;
void setup() {
Serial.begin(9600);
Serial.println("ChiupiMouse Init");
wb32_init();
setup_sound();
bleMouse.begin();
wb32_fillScreen(0);
wb32_setTextColor(wbWHITE, wbBLACK);
wb32_drawString("ChiupiMouse", 0, 0, 1, 5);
wb32_drawString("1. Connect to ChiupiMouse", 0, 50, 1, 2);
wb32_drawString("2. Press green button", 0, 80, 1, 2);
buzz();
}
void setup_sound() {
ledcSetup(1, 400, 8); // ledc/pwm ch#1, freq=400, duty resolution=8bit (0-255)
ledcAttachPin(25, 1); // IO#25=buzzer, attached to pwm#1
}
void make_sound(int volume) {
ledcWrite(1, volume);
}
void sound_freq(int freq) {
ledcSetup(1, freq, 8);
}
void buzz() {
sound_freq(400);
make_sound(30);
delay(100);
sound_freq(100);
make_sound(30);
delay(200);
make_sound(0);
}
uint16_t getkey() {
return (key = digitalRead(17) * 128 + digitalRead(32) * 64 + digitalRead(33) * 32 + digitalRead(27) * 16
+ digitalRead(35) * 8 + digitalRead(34) * 4 + digitalRead(39) * 2 + digitalRead(23) * 1);
}
void doThisAtEvery(int ms) {
if (millis() - lastDoTime >= ms) {
Serial.println("Execute");
lastDoTime = millis();
if (enabled && bleMouse.isConnected()) {
int x = random(-2, 2);
int y = random(-2, 2);
bleMouse.move(x, y, 0);
char buffer[40];
wb32_setTextColor(wbWHITE, wbBLACK);
sprintf(buffer, "X=%d Y=%d MS=%d ", x, y, ms);
wb32_drawString(buffer, 0, 80, 1, 1);
}
}
}
void loop() {
getkey();
if (key != lastKey) {
if (key == 17) {
enabled = !enabled;
wb32_fillScreen(0);
wb32_setTextColor(wbBLUE, wbBLACK);
wb32_drawString("ChiupiMouse", 0, 0, 1, 5);
if (enabled) {
wb32_setTextColor(wbGREEN, wbBLACK);
wb32_drawString("ON", 0, 50, 1, 3);
buzz();
doThisAtEvery(1);
} else {
wb32_setTextColor(wbRED, wbBLACK);
wb32_drawString("OFF", 0, 50, 1, 3);
buzz();
}
}
lastKey = key;
}
doThisAtEvery(random(5000, 15000));
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment