Skip to content

Commit

Permalink
Move feature suspend logic out of platform specific code (qmk#14210)
Browse files Browse the repository at this point in the history
  • Loading branch information
drashna authored Aug 30, 2021
1 parent 104f068 commit e570eb8
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 179 deletions.
96 changes: 96 additions & 0 deletions quantum/quantum.c
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,99 @@ void api_send_unicode(uint32_t unicode) {
__attribute__((weak)) void startup_user() {}

__attribute__((weak)) void shutdown_user() {}

/** \brief Run keyboard level Power down
*
* FIXME: needs doc
*/
__attribute__((weak)) void suspend_power_down_user(void) {}
/** \brief Run keyboard level Power down
*
* FIXME: needs doc
*/
__attribute__((weak)) void suspend_power_down_kb(void) { suspend_power_down_user(); }

void suspend_power_down_quantum(void) {
#ifndef NO_SUSPEND_POWER_DOWN
// Turn off backlight
# ifdef BACKLIGHT_ENABLE
backlight_set(0);
# endif

# ifdef LED_MATRIX_ENABLE
led_matrix_task();
# endif
# ifdef RGB_MATRIX_ENABLE
rgb_matrix_task();
# endif

// Turn off LED indicators
uint8_t leds_off = 0;
# if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
if (is_backlight_enabled()) {
// Don't try to turn off Caps Lock indicator as it is backlight and backlight is already off
leds_off |= (1 << USB_LED_CAPS_LOCK);
}
# endif
led_set(leds_off);

// Turn off audio
# ifdef AUDIO_ENABLE
stop_all_notes();
# endif

// Turn off underglow
# if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
rgblight_suspend();
# endif

# if defined(LED_MATRIX_ENABLE)
led_matrix_set_suspend_state(true);
# endif
# if defined(RGB_MATRIX_ENABLE)
rgb_matrix_set_suspend_state(true);
# endif

# ifdef OLED_ENABLE
oled_off();
# endif
# ifdef ST7565_ENABLE
st7565_off();
# endif
#endif
}

/** \brief run user level code immediately after wakeup
*
* FIXME: needs doc
*/
__attribute__((weak)) void suspend_wakeup_init_user(void) {}

/** \brief run keyboard level code immediately after wakeup
*
* FIXME: needs doc
*/
__attribute__((weak)) void suspend_wakeup_init_kb(void) { suspend_wakeup_init_user(); }

__attribute__((weak)) void suspend_wakeup_init_quantum(void) {
// Turn on backlight
#ifdef BACKLIGHT_ENABLE
backlight_init();
#endif

// Restore LED indicators
led_set(host_keyboard_leds());

// Wake up underglow
#if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
rgblight_wakeup();
#endif

#if defined(LED_MATRIX_ENABLE)
led_matrix_set_suspend_state(false);
#endif
#if defined(RGB_MATRIX_ENABLE)
rgb_matrix_set_suspend_state(false);
#endif
suspend_wakeup_init_kb();
}
98 changes: 2 additions & 96 deletions tmk_core/common/avr/suspend.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,6 @@
# include "vusb.h"
#endif

#ifdef BACKLIGHT_ENABLE
# include "backlight.h"
#endif

#ifdef AUDIO_ENABLE
# include "audio.h"
#endif /* AUDIO_ENABLE */

#if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
# include "rgblight.h"
#endif

#ifdef LED_MATRIX_ENABLE
# include "led_matrix.h"
#endif
#ifdef RGB_MATRIX_ENABLE
# include "rgb_matrix.h"
#endif

/** \brief Suspend idle
*
* FIXME: needs doc
Expand All @@ -50,17 +31,6 @@ void suspend_idle(uint8_t time) {

// TODO: This needs some cleanup

/** \brief Run keyboard level Power down
*
* FIXME: needs doc
*/
__attribute__((weak)) void suspend_power_down_user(void) {}
/** \brief Run keyboard level Power down
*
* FIXME: needs doc
*/
__attribute__((weak)) void suspend_power_down_kb(void) { suspend_power_down_user(); }

#if !defined(NO_SUSPEND_POWER_DOWN) && defined(WDT_vect)

// clang-format off
Expand Down Expand Up @@ -135,41 +105,9 @@ void suspend_power_down(void) {
if (!vusb_suspended) return;
#endif

suspend_power_down_kb();
suspend_power_down_quantum();

#ifndef NO_SUSPEND_POWER_DOWN
// Turn off backlight
# ifdef BACKLIGHT_ENABLE
backlight_set(0);
# endif

// Turn off LED indicators
uint8_t leds_off = 0;
# if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
if (is_backlight_enabled()) {
// Don't try to turn off Caps Lock indicator as it is backlight and backlight is already off
leds_off |= (1 << USB_LED_CAPS_LOCK);
}
# endif
led_set(leds_off);

// Turn off audio
# ifdef AUDIO_ENABLE
stop_all_notes();
# endif

// Turn off underglow
# if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
rgblight_suspend();
# endif

# if defined(LED_MATRIX_ENABLE)
led_matrix_set_suspend_state(true);
# endif
# if defined(RGB_MATRIX_ENABLE)
rgb_matrix_set_suspend_state(true);
# endif

// Enter sleep state if possible (ie, the MCU has a watchdog timeout interrupt)
# if defined(WDT_vect)
power_down(WDTO_15MS);
Expand All @@ -189,18 +127,6 @@ bool suspend_wakeup_condition(void) {
return false;
}

/** \brief run user level code immediately after wakeup
*
* FIXME: needs doc
*/
__attribute__((weak)) void suspend_wakeup_init_user(void) {}

/** \brief run keyboard level code immediately after wakeup
*
* FIXME: needs doc
*/
__attribute__((weak)) void suspend_wakeup_init_kb(void) { suspend_wakeup_init_user(); }

/** \brief run immediately after wakeup
*
* FIXME: needs doc
Expand All @@ -209,27 +135,7 @@ void suspend_wakeup_init(void) {
// clear keyboard state
clear_keyboard();

// Turn on backlight
#ifdef BACKLIGHT_ENABLE
backlight_init();
#endif

// Restore LED indicators
led_set(host_keyboard_leds());

// Wake up underglow
#if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
rgblight_wakeup();
#endif

#if defined(LED_MATRIX_ENABLE)
led_matrix_set_suspend_state(false);
#endif
#if defined(RGB_MATRIX_ENABLE)
rgb_matrix_set_suspend_state(false);
#endif

suspend_wakeup_init_kb();
suspend_wakeup_init_quantum();
}

#if !defined(NO_SUSPEND_POWER_DOWN) && defined(WDT_vect)
Expand Down
85 changes: 2 additions & 83 deletions tmk_core/common/chibios/suspend.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,6 @@
#include "led.h"
#include "wait.h"

#ifdef AUDIO_ENABLE
# include "audio.h"
#endif /* AUDIO_ENABLE */

#ifdef BACKLIGHT_ENABLE
# include "backlight.h"
#endif

#if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
# include "rgblight.h"
#endif

#ifdef LED_MATRIX_ENABLE
# include "led_matrix.h"
#endif
#ifdef RGB_MATRIX_ENABLE
# include "rgb_matrix.h"
#endif

/** \brief suspend idle
*
* FIXME: needs doc
Expand All @@ -40,61 +21,12 @@ void suspend_idle(uint8_t time) {
wait_ms(time);
}

/** \brief Run keyboard level Power down
*
* FIXME: needs doc
*/
__attribute__((weak)) void suspend_power_down_user(void) {}
/** \brief Run keyboard level Power down
*
* FIXME: needs doc
*/
__attribute__((weak)) void suspend_power_down_kb(void) { suspend_power_down_user(); }

/** \brief suspend power down
*
* FIXME: needs doc
*/
void suspend_power_down(void) {
#ifdef BACKLIGHT_ENABLE
backlight_set(0);
#endif

#ifdef LED_MATRIX_ENABLE
led_matrix_task();
#endif
#ifdef RGB_MATRIX_ENABLE
rgb_matrix_task();
#endif

// Turn off LED indicators
uint8_t leds_off = 0;
#if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
if (is_backlight_enabled()) {
// Don't try to turn off Caps Lock indicator as it is backlight and backlight is already off
leds_off |= (1 << USB_LED_CAPS_LOCK);
}
#endif
led_set(leds_off);

// TODO: figure out what to power down and how
// shouldn't power down TPM/FTM if we want a breathing LED
// also shouldn't power down USB
#if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
rgblight_suspend();
#endif

#if defined(LED_MATRIX_ENABLE)
led_matrix_set_suspend_state(true);
#endif
#if defined(RGB_MATRIX_ENABLE)
rgb_matrix_set_suspend_state(true);
#endif
#ifdef AUDIO_ENABLE
stop_all_notes();
#endif /* AUDIO_ENABLE */

suspend_power_down_kb();
suspend_power_down_quantum();
// on AVR, this enables the watchdog for 15ms (max), and goes to
// SLEEP_MODE_PWR_DOWN

Expand Down Expand Up @@ -151,19 +83,6 @@ void suspend_wakeup_init(void) {
host_system_send(0);
host_consumer_send(0);
#endif /* EXTRAKEY_ENABLE */
#ifdef BACKLIGHT_ENABLE
backlight_init();
#endif /* BACKLIGHT_ENABLE */
led_set(host_keyboard_leds());
#if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
rgblight_wakeup();
#endif

#if defined(LED_MATRIX_ENABLE)
led_matrix_set_suspend_state(false);
#endif
#if defined(RGB_MATRIX_ENABLE)
rgb_matrix_set_suspend_state(false);
#endif
suspend_wakeup_init_kb();
suspend_wakeup_init_quantum();
}
2 changes: 2 additions & 0 deletions tmk_core/common/suspend.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ void suspend_wakeup_init(void);

void suspend_wakeup_init_user(void);
void suspend_wakeup_init_kb(void);
void suspend_wakeup_init_quantum(void);
void suspend_power_down_user(void);
void suspend_power_down_kb(void);
void suspend_power_down_quantum(void);

#ifndef USB_SUSPEND_WAKEUP_DELAY
# define USB_SUSPEND_WAKEUP_DELAY 0
Expand Down

0 comments on commit e570eb8

Please sign in to comment.