aboutsummaryrefslogtreecommitdiff
path: root/keyboard/hhkb/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboard/hhkb/matrix.c')
-rw-r--r--keyboard/hhkb/matrix.c27
1 files changed, 25 insertions, 2 deletions
diff --git a/keyboard/hhkb/matrix.c b/keyboard/hhkb/matrix.c
index b0af4baa5..fb9699794 100644
--- a/keyboard/hhkb/matrix.c
+++ b/keyboard/hhkb/matrix.c
@@ -27,8 +27,15 @@ 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
37static uint32_t matrix_last_modified = 0;
38
32// matrix state buffer(1:on, 0:off) 39// matrix state buffer(1:on, 0:off)
33static matrix_row_t *matrix; 40static matrix_row_t *matrix;
34static matrix_row_t *matrix_prev; 41static matrix_row_t *matrix_prev;
@@ -72,7 +79,8 @@ uint8_t matrix_scan(void)
72 matrix_prev = matrix; 79 matrix_prev = matrix;
73 matrix = tmp; 80 matrix = tmp;
74 81
75 KEY_POWER_ON(); 82 // power on
83 if (!KEY_POWER_STATE()) KEY_POWER_ON();
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,16 @@ 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();
138 }
139 // power off
140 if (KEY_POWER_STATE() &&
141 (USB_DeviceState == DEVICE_STATE_Suspended ||
142 USB_DeviceState == DEVICE_STATE_Unattached ) &&
143 timer_elapsed32(matrix_last_modified) > MATRIX_POWER_SAVE) {
144 KEY_POWER_OFF();
145 suspend_power_down();
129 } 146 }
130 KEY_POWER_OFF();
131 return 1; 147 return 1;
132} 148}
133 149
@@ -165,3 +181,10 @@ void matrix_print(void)
165 xprintf("%02X: %08b\n", row, bitrev(matrix_get_row(row))); 181 xprintf("%02X: %08b\n", row, bitrev(matrix_get_row(row)));
166 } 182 }
167} 183}
184
185void matrix_power_up(void) {
186 KEY_POWER_ON();
187}
188void matrix_power_down(void) {
189 KEY_POWER_OFF();
190}