From df952f687cac0bfda845322f35f627eefd97f5d6 Mon Sep 17 00:00:00 2001 From: gsd Date: Wed, 27 Nov 2024 17:09:37 +0300 Subject: [PATCH] sleep mode --- README | 1 + main/main.ino | 49 ++++++++++++++++++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/README b/README index 7781f04..735d04f 100644 --- a/README +++ b/README @@ -9,6 +9,7 @@ Features: - Extra scheme to battery level report. - NumLock, CapsLock, ScrollLock led work, but not corretly. (Works after add battery lvl scheme). - GAMERS RGB LED to response keyboard status +- Base sleep mode Used components: (main to works) diff --git a/main/main.ino b/main/main.ino index 3712962..016b859 100644 --- a/main/main.ino +++ b/main/main.ino @@ -7,6 +7,7 @@ //#define DEBUG 1 #define BAT_SUPPORT 1 #define EXTERNAL_RGB_LED_SUPPORT 1 +#define BASE_SLEEP_SUPPORT 1 using namespace fabgl; @@ -45,8 +46,13 @@ uint8_t convertToKeyCode(VirtualKey virtualKey); } #endif -long lastTouch = 0; -boolean sleeped = false; +#if defined(BASE_SLEEP_SUPPORT) + long lastTouch = 0; + boolean sleeped = false; + #define SLEEPING_DELAY 500 // in milliseconds + #define SLEEPING_IF_NOT_CONNECT 1000 // in milliseconds + #define GO_TO_SLEEP_AFTER 30 // in seconds +#endif void setup() { @@ -87,7 +93,9 @@ void setup() digitalWrite(LED_GREEN, HIGH); #endif - lastTouch = millis(); + #if defined(BASE_SLEEP_SUPPORT) + lastTouch = millis(); + #endif } static void updateModifiers(VirtualKey virtualKey, bool keyDown) @@ -224,31 +232,43 @@ void loop() if (!connected) { - delay(1000); + #if defined(BASE_SLEEP_SUPPORT) + delay(SLEEPING_IF_NOT_CONNECT); + #endif + return; } if (!keyboard->virtualKeyAvailable()) { - if (!sleeped && millis() - lastTouch > 60 * 1000) { + #if defined(BASE_SLEEP_SUPPORT) + if (sleeped) { + #if defined (EXTERNAL_RGB_LED_SUPPORT) + digitalWrite(LED_GREEN, LOW); + #endif + delay(SLEEPING_DELAY/2); + #if defined (EXTERNAL_RGB_LED_SUPPORT) + digitalWrite(LED_GREEN, HIGH); + #endif + delay(SLEEPING_DELAY/2); + } + + if (millis() - lastTouch > GO_TO_SLEEP_AFTER * 1000) { sleeped = true; #if defined (EXTERNAL_RGB_LED_SUPPORT) - digitalWrite(LED_RED, HIGH); + digitalWrite(LED_GREEN, HIGH); #endif } else { if (sleeped) { #if defined (EXTERNAL_RGB_LED_SUPPORT) - digitalWrite(LED_RED, LOW); + digitalWrite(LED_GREEN, LOW); #endif } sleeped = false; } + #endif - if (sleeped) { - delay(300); - } - - return; + return; } bool keyDown; @@ -299,7 +319,10 @@ void loop() } } - lastTouch = millis(); + #if defined(BASE_SLEEP_SUPPORT) + lastTouch = millis(); + #endif + bleKeyboard.sendReport(&report); }