diff options
| -rw-r--r-- | common/keyboard.c | 23 | ||||
| -rw-r--r-- | common/keyboard.h | 3 | ||||
| -rw-r--r-- | common/matrix.h | 5 | ||||
| -rw-r--r-- | keyboard/hhkb_rn42/hhkb_avr.h | 5 | ||||
| -rw-r--r-- | keyboard/hhkb_rn42/matrix.c | 28 | ||||
| -rw-r--r-- | keyboard/hhkb_rn42/rn42/main.c | 2 |
6 files changed, 60 insertions, 6 deletions
diff --git a/common/keyboard.c b/common/keyboard.c index 9a809ff4a..dde91a296 100644 --- a/common/keyboard.c +++ b/common/keyboard.c | |||
| @@ -15,6 +15,8 @@ You should have received a copy of the GNU General Public License | |||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ | 16 | */ |
| 17 | #include <stdint.h> | 17 | #include <stdint.h> |
| 18 | #include <util/delay.h> | ||
| 19 | #include <avr/wdt.h> | ||
| 18 | #include "keyboard.h" | 20 | #include "keyboard.h" |
| 19 | #include "matrix.h" | 21 | #include "matrix.h" |
| 20 | #include "keymap.h" | 22 | #include "keymap.h" |
| @@ -30,12 +32,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 30 | #include "bootmagic.h" | 32 | #include "bootmagic.h" |
| 31 | #include "eeconfig.h" | 33 | #include "eeconfig.h" |
| 32 | #include "backlight.h" | 34 | #include "backlight.h" |
| 35 | #include "suspend.h" | ||
| 33 | #ifdef MOUSEKEY_ENABLE | 36 | #ifdef MOUSEKEY_ENABLE |
| 34 | # include "mousekey.h" | 37 | # include "mousekey.h" |
| 35 | #endif | 38 | #endif |
| 36 | #ifdef PS2_MOUSE_ENABLE | 39 | #ifdef PS2_MOUSE_ENABLE |
| 37 | # include "ps2_mouse.h" | 40 | # include "ps2_mouse.h" |
| 38 | #endif | 41 | #endif |
| 42 | #include "lufa.h" | ||
| 39 | 43 | ||
| 40 | 44 | ||
| 41 | #ifdef MATRIX_HAS_GHOST | 45 | #ifdef MATRIX_HAS_GHOST |
| @@ -83,7 +87,25 @@ void keyboard_task(void) | |||
| 83 | static uint8_t led_status = 0; | 87 | static uint8_t led_status = 0; |
| 84 | matrix_row_t matrix_row = 0; | 88 | matrix_row_t matrix_row = 0; |
| 85 | matrix_row_t matrix_change = 0; | 89 | matrix_row_t matrix_change = 0; |
| 90 | static uint32_t last_key_time = 0; | ||
| 86 | 91 | ||
| 92 | /* | ||
| 93 | #define SLEEP_TIME_MS 10000 | ||
| 94 | // (USB_DeviceState == DEVICE_STATE_Suspended) { | ||
| 95 | //if (timer_elapsed32(last_key_time) > SLEEP_TIME_MS) { | ||
| 96 | // TODO: remove LUFA dependent code | ||
| 97 | if (!USB_IsInitialized && timer_elapsed32(last_key_time) > SLEEP_TIME_MS) { | ||
| 98 | matrix_power_down(); | ||
| 99 | // TODO: power down only when no USB connection | ||
| 100 | // Or it makes USB connection lost or suspended | ||
| 101 | suspend_power_down(WDTO_15MS); | ||
| 102 | matrix_power_up(); | ||
| 103 | } | ||
| 104 | else { | ||
| 105 | matrix_power_down(); | ||
| 106 | matrix_power_up(); | ||
| 107 | } | ||
| 108 | */ | ||
| 87 | matrix_scan(); | 109 | matrix_scan(); |
| 88 | for (uint8_t r = 0; r < MATRIX_ROWS; r++) { | 110 | for (uint8_t r = 0; r < MATRIX_ROWS; r++) { |
| 89 | matrix_row = matrix_get_row(r); | 111 | matrix_row = matrix_get_row(r); |
| @@ -105,6 +127,7 @@ void keyboard_task(void) | |||
| 105 | }); | 127 | }); |
| 106 | // record a processed key | 128 | // record a processed key |
| 107 | matrix_prev[r] ^= ((matrix_row_t)1<<c); | 129 | matrix_prev[r] ^= ((matrix_row_t)1<<c); |
| 130 | last_key_time = timer_read32(); | ||
| 108 | // process a key per task call | 131 | // process a key per task call |
| 109 | goto MATRIX_LOOP_END; | 132 | goto MATRIX_LOOP_END; |
| 110 | } | 133 | } |
diff --git a/common/keyboard.h b/common/keyboard.h index 60f8a89d1..6442716fc 100644 --- a/common/keyboard.h +++ b/common/keyboard.h | |||
| @@ -62,6 +62,9 @@ void keyboard_init(void); | |||
| 62 | void keyboard_task(void); | 62 | void keyboard_task(void); |
| 63 | void keyboard_set_leds(uint8_t leds); | 63 | void keyboard_set_leds(uint8_t leds); |
| 64 | 64 | ||
| 65 | __attribute__ ((weak)) void matrix_power_up(void) {} | ||
| 66 | __attribute__ ((weak)) void matrix_power_down(void) {} | ||
| 67 | |||
| 65 | #ifdef __cplusplus | 68 | #ifdef __cplusplus |
| 66 | } | 69 | } |
| 67 | #endif | 70 | #endif |
diff --git a/common/matrix.h b/common/matrix.h index d1d14a6ea..23fef78f7 100644 --- a/common/matrix.h +++ b/common/matrix.h | |||
| @@ -53,4 +53,9 @@ matrix_row_t matrix_get_row(uint8_t row); | |||
| 53 | void matrix_print(void); | 53 | void matrix_print(void); |
| 54 | 54 | ||
| 55 | 55 | ||
| 56 | /* power control */ | ||
| 57 | void matrix_power_up(void); | ||
| 58 | void matrix_power_down(void); | ||
| 59 | |||
| 60 | |||
| 56 | #endif | 61 | #endif |
diff --git a/keyboard/hhkb_rn42/hhkb_avr.h b/keyboard/hhkb_rn42/hhkb_avr.h index 313effedf..f007d7667 100644 --- a/keyboard/hhkb_rn42/hhkb_avr.h +++ b/keyboard/hhkb_rn42/hhkb_avr.h | |||
| @@ -43,11 +43,10 @@ static inline void KEY_PREV_ON(void) { (PORTB |= (1<<7)); } | |||
| 43 | static inline void KEY_PREV_OFF(void) { (PORTB &= ~(1<<7)); } | 43 | static inline void KEY_PREV_OFF(void) { (PORTB &= ~(1<<7)); } |
| 44 | #ifdef HHKB_POWER_SAVING | 44 | #ifdef HHKB_POWER_SAVING |
| 45 | static inline void KEY_POWER_ON(void) { | 45 | static inline void KEY_POWER_ON(void) { |
| 46 | _delay_ms(10); // TODO: sleep to save power | ||
| 47 | DDRB = 0xFF; PORTB = 0x40; // change pins output | 46 | DDRB = 0xFF; PORTB = 0x40; // change pins output |
| 48 | DDRD |= (1<<4); PORTD |= (1<<4); // MOS FET switch on | 47 | DDRD |= (1<<4); PORTD |= (1<<4); // MOS FET switch on |
| 49 | /* Without this wait you will miss or get false key events. */ | 48 | /* Without this wait you will miss or get false key events. */ |
| 50 | _delay_ms(1); // wait for powering up | 49 | _delay_ms(5); // wait for powering up |
| 51 | } | 50 | } |
| 52 | static inline void KEY_POWER_OFF(void) { | 51 | static inline void KEY_POWER_OFF(void) { |
| 53 | /* input with pull-up consumes less than without it when pin is open. */ | 52 | /* input with pull-up consumes less than without it when pin is open. */ |
| @@ -74,7 +73,7 @@ static inline void KEY_INIT(void) | |||
| 74 | KEY_UNABLE(); | 73 | KEY_UNABLE(); |
| 75 | KEY_PREV_OFF(); | 74 | KEY_PREV_OFF(); |
| 76 | 75 | ||
| 77 | KEY_POWER_OFF(); | 76 | KEY_POWER_ON(); |
| 78 | } | 77 | } |
| 79 | static inline void KEY_SELECT(uint8_t ROW, uint8_t COL) | 78 | static inline void KEY_SELECT(uint8_t ROW, uint8_t COL) |
| 80 | { | 79 | { |
diff --git a/keyboard/hhkb_rn42/matrix.c b/keyboard/hhkb_rn42/matrix.c index b0af4baa5..c6c444ff5 100644 --- a/keyboard/hhkb_rn42/matrix.c +++ b/keyboard/hhkb_rn42/matrix.c | |||
| @@ -27,8 +27,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 27 | #include "timer.h" | 27 | #include "timer.h" |
| 28 | #include "matrix.h" | 28 | #include "matrix.h" |
| 29 | #include "hhkb_avr.h" | 29 | #include "hhkb_avr.h" |
| 30 | #include <avr/wdt.h> | ||
| 31 | #include "suspend.h" | ||
| 32 | #include "lufa.h" | ||
| 30 | 33 | ||
| 31 | 34 | ||
| 35 | // matrix power saving | ||
| 36 | #define MATRIX_POWER_SAVE 10000 | ||
| 37 | static uint32_t matrix_last_modified = 0; | ||
| 38 | static bool matrix_power = true; | ||
| 39 | |||
| 32 | // matrix state buffer(1:on, 0:off) | 40 | // matrix state buffer(1:on, 0:off) |
| 33 | static matrix_row_t *matrix; | 41 | static matrix_row_t *matrix; |
| 34 | static matrix_row_t *matrix_prev; | 42 | static matrix_row_t *matrix_prev; |
| @@ -72,7 +80,7 @@ uint8_t matrix_scan(void) | |||
| 72 | matrix_prev = matrix; | 80 | matrix_prev = matrix; |
| 73 | matrix = tmp; | 81 | matrix = tmp; |
| 74 | 82 | ||
| 75 | KEY_POWER_ON(); | 83 | matrix_power_up(); |
| 76 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | 84 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { |
| 77 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { | 85 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { |
| 78 | KEY_SELECT(row, col); | 86 | KEY_SELECT(row, col); |
| @@ -126,8 +134,9 @@ uint8_t matrix_scan(void) | |||
| 126 | // This takes 25us or more to make sure KEY_STATE returns to idle state. | 134 | // This takes 25us or more to make sure KEY_STATE returns to idle state. |
| 127 | _delay_us(75); | 135 | _delay_us(75); |
| 128 | } | 136 | } |
| 137 | if (matrix[row] ^ matrix_prev[row]) matrix_last_modified = timer_read32(); | ||
| 129 | } | 138 | } |
| 130 | KEY_POWER_OFF(); | 139 | matrix_power_down(); |
| 131 | return 1; | 140 | return 1; |
| 132 | } | 141 | } |
| 133 | 142 | ||
| @@ -165,3 +174,18 @@ void matrix_print(void) | |||
| 165 | xprintf("%02X: %08b\n", row, bitrev(matrix_get_row(row))); | 174 | xprintf("%02X: %08b\n", row, bitrev(matrix_get_row(row))); |
| 166 | } | 175 | } |
| 167 | } | 176 | } |
| 177 | |||
| 178 | void matrix_power_up(void) { | ||
| 179 | if (matrix_power) return; | ||
| 180 | KEY_POWER_ON(); | ||
| 181 | matrix_power = true; | ||
| 182 | } | ||
| 183 | void matrix_power_down(void) { | ||
| 184 | if (!matrix_power) return; | ||
| 185 | // doesn't power save while USB connection is active | ||
| 186 | if (USB_DeviceState == DEVICE_STATE_Configured) return; | ||
| 187 | if (timer_elapsed32(matrix_last_modified) <= MATRIX_POWER_SAVE) return; | ||
| 188 | KEY_POWER_OFF(); | ||
| 189 | suspend_power_down(WDTO_15MS); | ||
| 190 | matrix_power = false; | ||
| 191 | } | ||
diff --git a/keyboard/hhkb_rn42/rn42/main.c b/keyboard/hhkb_rn42/rn42/main.c index a3a37c074..43d887067 100644 --- a/keyboard/hhkb_rn42/rn42/main.c +++ b/keyboard/hhkb_rn42/rn42/main.c | |||
| @@ -85,7 +85,7 @@ int main(void) | |||
| 85 | print("Keyboard start.\n"); | 85 | print("Keyboard start.\n"); |
| 86 | while (1) { | 86 | while (1) { |
| 87 | while (USB_DeviceState == DEVICE_STATE_Suspended) { | 87 | while (USB_DeviceState == DEVICE_STATE_Suspended) { |
| 88 | suspend_power_down(); | 88 | suspend_power_down(WDTO_120MS); |
| 89 | if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) { | 89 | if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) { |
| 90 | USB_Device_SendRemoteWakeup(); | 90 | USB_Device_SendRemoteWakeup(); |
| 91 | } | 91 | } |
