commit 55fcbfac376977cebd6e20bcb9df5f82dba16aa3 Author: Razvalyaev Date: Tue May 20 15:52:32 2025 +0300 проект в ардуино для мышки diff --git a/mouse/BatteryMonitor.h b/mouse/BatteryMonitor.h new file mode 100644 index 0000000..556c769 --- /dev/null +++ b/mouse/BatteryMonitor.h @@ -0,0 +1,39 @@ +#ifndef BATTERY_MONITOR_H +#define BATTERY_MONITOR_H + +#include + +class BatteryMonitor { +private: + uint8_t _adcPin; + const float _vref = 3.3; + const int _adcMax = 4095; + const float _voltageDividerRatio; + const float _vMax; + const float _vMin; + +public: + BatteryMonitor(uint8_t adcPin, + float voltageDividerRatio = 2.0, + float vMax = 4.2, + float vMin = 3.0) : + _adcPin(adcPin), + _voltageDividerRatio(voltageDividerRatio), + _vMax(vMax), + _vMin(vMin) + { + pinMode(_adcPin, INPUT); + } + + uint8_t readLevelPercent() { + int raw = analogRead(_adcPin); + float voltage = (raw * (_vref / _adcMax)) * _voltageDividerRatio; + + if (voltage >= _vMax) return 100; + if (voltage <= _vMin) return 0; + + return (uint8_t)(100 * (voltage - _vMin) / (_vMax - _vMin)); + } +}; + +#endif // BATTERY_MONITOR_H diff --git a/mouse/mouse.ino b/mouse/mouse.ino new file mode 100644 index 0000000..17aab46 --- /dev/null +++ b/mouse/mouse.ino @@ -0,0 +1,84 @@ +#include +#include "EspUsbHost.h" + + +#define SHOW_REAL_BATTERY + + + +#ifdef SHOW_REAL_BATTERY +#define BATTERY_UPDATE_INTERVAL 5000 // например, 30000 мс = 30 секунд +#define BATTERY_ADC_PIN 6 +#define BATTERY_VOLTAGE_DIVIDER_RATIO 2.0f +#define BATTERY_VOLTAGE_MAX 5.0f +#define BATTERY_VOLTAGE_MIN 3.5f +#include "BatteryMonitor.h" +BatteryMonitor battery(BATTERY_ADC_PIN, BATTERY_VOLTAGE_DIVIDER_RATIO, BATTERY_VOLTAGE_MAX, BATTERY_VOLTAGE_MIN); + +#endif + + +BleMouse bleMouse("Ball Mouse"); + +// Расширяем класс для обработки USB мыши +class MyEspUsbHost : public EspUsbHost { + void onMouseButtons(hid_mouse_report_t report, uint8_t last_buttons) { + if (!bleMouse.isConnected()) return; + + // Нажатия и отпускания кнопок + if ((last_buttons & MOUSE_BUTTON_LEFT) != (report.buttons & MOUSE_BUTTON_LEFT)) { + if (report.buttons & MOUSE_BUTTON_LEFT) bleMouse.press(MOUSE_LEFT); + else bleMouse.release(MOUSE_LEFT); + } + if ((last_buttons & MOUSE_BUTTON_RIGHT) != (report.buttons & MOUSE_BUTTON_RIGHT)) { + if (report.buttons & MOUSE_BUTTON_RIGHT) bleMouse.press(MOUSE_RIGHT); + else bleMouse.release(MOUSE_RIGHT); + } + if ((last_buttons & MOUSE_BUTTON_MIDDLE) != (report.buttons & MOUSE_BUTTON_MIDDLE)) { + if (report.buttons & MOUSE_BUTTON_MIDDLE) bleMouse.press(MOUSE_MIDDLE); + else bleMouse.release(MOUSE_MIDDLE); + } + // Дополнительные кнопки (если поддерживаются) + } + + void onMouseMove(hid_mouse_report_t report) { + if (!bleMouse.isConnected()) return; + + // Передаём перемещения и скролл через BLE + bleMouse.move(report.x, report.y, report.wheel); + } +}; + +MyEspUsbHost usbHost; + +void setup() { + Serial.begin(115200); + delay(500); + + + bleMouse.begin(); + Serial.println("BLE Mouse started"); + + usbHost.begin(); + Serial.println("USB Host started"); +} + +void loop() { + usbHost.task(); + +#ifdef SHOW_REAL_BATTERY + // чтение уровня батареи + static unsigned long lastBatteryUpdate = 0; + if (bleMouse.isConnected()) { + unsigned long now = millis(); + if (now - lastBatteryUpdate > BATTERY_UPDATE_INTERVAL) { + lastBatteryUpdate = now; + + uint8_t batteryLevel = battery.readLevelPercent(); + Serial.printf("Battery Level: %d%%\n", batteryLevel); + + bleMouse.setBatteryLevel(batteryLevel); + } + } +#endif +}