@@ -83,51 +83,77 @@ uint16_t check_led_status( uint16_t key ){
83
83
// what to do on a media key press
84
84
void media_control_send (uint16_t keycode ){
85
85
86
- uint8_t media_state [1 ]= {0 };
87
- SET_BIT (media_state [0 ],(keycode - KC_MEDIA_NEXT_TRACK ));
86
+ uint8_t media_state [2 ] = {0 };
87
+ if (keycode == KC_MEDIA_NEXT_TRACK ){
88
+ media_state [1 ] = 10 ;
89
+ }
90
+ if (keycode == KC_MEDIA_PREV_TRACK ){
91
+ media_state [1 ] = 111 ;
92
+ }
93
+ if (keycode == KC_MEDIA_STOP ){
94
+ media_state [1 ] = 12 ;
95
+ }
96
+ if (keycode == KC_MEDIA_PLAY_PAUSE ){
97
+ media_state [1 ] = 5 ;
98
+ }
99
+ if (keycode == KC_AUDIO_MUTE ){
100
+ media_state [1 ] = 1 ;
101
+ }
102
+ if (keycode == KC_AUDIO_VOL_UP ){
103
+ SET_BIT (media_state [0 ],6 );
104
+ }
105
+ if (keycode == KC_AUDIO_VOL_DOWN ){
106
+ SET_BIT (media_state [0 ],7 );
107
+ }
108
+
88
109
xQueueSend (media_q ,(void * )& media_state , (TickType_t ) 0 );
89
- vTaskDelay (5 /portTICK_PERIOD_MS );
90
110
}
91
111
92
112
void media_control_release (uint16_t keycode ){
93
- uint8_t media_state [1 ] = {0 };
113
+ uint8_t media_state [2 ] = {0 };
94
114
xQueueSend (media_q ,(void * )& media_state , (TickType_t ) 0 );
95
- vTaskDelay (5 /portTICK_PERIOD_MS );
96
115
}
97
116
98
117
118
+ //used for debouncing
119
+ static uint32_t millis () {
120
+ return esp_timer_get_time () / 1000 ;
121
+ }
99
122
123
+ uint32_t prev_time = 0 ;
100
124
// adjust current layer
101
125
void layer_adjust ( uint16_t keycode ){
126
+ uint32_t cur_time = millis ();
127
+ if (cur_time - prev_time > DEBOUNCE ){
128
+ if (layer_hold_flag == 0 ){
129
+ switch (keycode ){
130
+ case DEFAULT :
131
+ current_layout = 0 ;
132
+ break ;
102
133
103
- if (layer_hold_flag == 0 ){
104
- switch (keycode ){
105
- case DEFAULT :
106
- current_layout = 0 ;
107
- break ;
108
-
109
- case LOWER :
110
- if (current_layout == 0 ){
111
- current_layout = MAX_LAYER ;
134
+ case LOWER :
135
+ if (current_layout == 0 ){
136
+ current_layout = MAX_LAYER ;
137
+ break ;
138
+ }
139
+ current_layout -- ;
112
140
break ;
113
- }
114
- current_layout -- ;
115
- break ;
116
141
117
- case RAISE :
118
- if (current_layout == MAX_LAYER ){
119
- current_layout = 0 ;
142
+ case RAISE :
143
+ if (current_layout == MAX_LAYER ){
144
+ current_layout = 0 ;
145
+ break ;
146
+ }
147
+ current_layout ++ ;
120
148
break ;
121
149
}
122
- current_layout ++ ;
123
- break ;
150
+ #ifdef OLED_ENABLE
151
+ xQueueSend (layer_recieve_q ,& current_layout , (TickType_t ) 0 );
152
+ #endif
153
+ ESP_LOGI (KEY_PRESS_TAG ,"Layer modified!, Current layer: %d " ,current_layout );
124
154
}
125
- #ifdef OLED_ENABLE
126
- xQueueSend (layer_recieve_q ,& current_layout , (TickType_t ) 0 );
127
- #endif
128
- vTaskDelay (300 /portTICK_PERIOD_MS );
129
- ESP_LOGI (KEY_PRESS_TAG ,"Layer modified!, Current layer: %d " ,current_layout );
130
155
}
156
+ prev_time = cur_time ;
131
157
}
132
158
133
159
@@ -140,29 +166,31 @@ uint8_t *check_key_state( uint16_t **keymap){
140
166
uint8_t matrix_state [MATRIX_ROWS ][MATRIX_COLS ]= {0 };
141
167
memcpy (matrix_state ,matrix_states [pad ], sizeof (matrix_state ) );
142
168
143
- for (uint8_t col = (MATRIX_COLS * pad ); col < ((pad + 1 )* (MATRIX_COLS )); col ++ ){
169
+ for (uint8_t col = (MATRIX_COLS * pad ); col < ((pad + 1 )* (MATRIX_COLS )); col ++ ){
144
170
for (uint8_t row = 0 ; row < MATRIX_ROWS ; row ++ ){
145
171
146
- uint16_t report_index = (2 + col + row * KEYMAP_COLS );
172
+ uint16_t report_index = (2 + col + row * KEYMAP_COLS );
147
173
keycode = keymap [row ][col ];
148
174
149
175
//checking if the keycode is transparent
150
- if (keycode == KC_TRNS ){
151
- if (current_layout == 0 ){
152
- keycode = * default_layouts [MAX_LAYER ][row ][col ];
176
+ if (keycode == KC_TRNS ){
177
+ if (current_layout == 0 ){
178
+ keycode = * default_layouts [MAX_LAYER ][row ][col ];
153
179
}else {
154
- keycode = * default_layouts [current_layout - 1 ][row ][col ];
180
+ keycode = * default_layouts [current_layout - 1 ][row ][col ];
155
181
}
156
182
}
157
183
158
- led_status = check_led_status (keycode );
159
- if (matrix_state [row ][col - MATRIX_COLS * pad ]== 1 ){
184
+ led_status = check_led_status (keycode );
185
+ if (matrix_state [row ][col - MATRIX_COLS * pad ] == 1 ){
160
186
161
187
//checking for layer hold
162
- if ((keycode >=LAYER_HOLD_BASE_VAL )&& (keycode <= LAYER_HOLD_MAX_VAL )&& (layer_hold_flag == 0 )){
188
+ if ((keycode >= LAYER_HOLD_BASE_VAL )&& (keycode <= LAYER_HOLD_MAX_VAL )&& (layer_hold_flag == 0 )){
163
189
prev_layout = current_layout ;
164
- current_layout = (keycode - LAYER_HOLD_BASE_VAL );
165
- layer_hold_flag = 1 ;
190
+ if (layer_hold_flag == 0 ){
191
+ current_layout = (keycode - LAYER_HOLD_BASE_VAL );
192
+ layer_hold_flag = 1 ;
193
+ }
166
194
#ifdef OLED_ENABLE
167
195
xQueueSend (layer_recieve_q ,& current_layout , (TickType_t ) 0 );
168
196
#endif
@@ -172,24 +200,24 @@ uint8_t *check_key_state( uint16_t **keymap){
172
200
}
173
201
174
202
// checking for layer adjust keycodes
175
- if ((keycode >= LAYERS_BASE_VAL )&& (keycode < MACRO_BASE_VAL )){
203
+ if ((keycode >= LAYERS_BASE_VAL ) && (keycode < MACRO_BASE_VAL )){
176
204
layer_adjust (keycode );
177
205
continue ;
178
206
}
179
207
180
208
// checking for macros
181
- if ((keycode >= MACRO_BASE_VAL )&& (keycode <=LAYER_HOLD_BASE_VAL )){
209
+ if ((keycode >= MACRO_BASE_VAL ) && (keycode <=LAYER_HOLD_BASE_VAL )){
182
210
for (uint8_t i = 0 ; i < 3 ; i ++ ){
183
- uint16_t key = macros [MACRO_BASE_VAL - keycode ][i ];
184
- current_report [REPORT_LEN - 1 - i ] = key ;
185
- modifier |= check_modifier (key );
211
+ uint16_t key = macros [MACRO_BASE_VAL - keycode ][i ];
212
+ current_report [REPORT_LEN - 1 - i ] = key ;
213
+ modifier |= check_modifier (key );
186
214
printf ("\nmodifier:%d" ,modifier );
187
215
}
188
216
continue ;
189
217
}
190
218
191
219
// checking for media control keycodes
192
- if ((keycode >= KC_MEDIA_NEXT_TRACK )&& (keycode <= KC_AUDIO_VOL_DOWN )){
220
+ if ((keycode >= KC_MEDIA_NEXT_TRACK )&& (keycode <= KC_AUDIO_VOL_DOWN )){
193
221
media_control_send (keycode );
194
222
}
195
223
@@ -199,16 +227,16 @@ uint8_t *check_key_state( uint16_t **keymap){
199
227
// continue;
200
228
// }
201
229
202
- if (current_report [report_index ]== 0 ){
203
- modifier |= check_modifier (keycode );
204
- current_report [report_index ]= keycode ;
230
+ if (current_report [report_index ] == 0 ){
231
+ modifier |= check_modifier (keycode );
232
+ current_report [report_index ] = keycode ;
205
233
206
234
}
207
235
}
208
- if (matrix_state [row ][col - MATRIX_COLS * pad ]== 0 ){
236
+ if (matrix_state [row ][col - MATRIX_COLS * pad ] == 0 ){
209
237
210
238
//checking for layer hold release
211
- if ((layouts [prev_layout ][row ][col ] >=LAYER_HOLD_BASE_VAL )&& (keycode <= LAYER_HOLD_MAX_VAL )&& (layer_hold_flag == 1 )){
239
+ if ((layouts [prev_layout ][row ][col ] >= LAYER_HOLD_BASE_VAL ) && (keycode <= LAYER_HOLD_MAX_VAL ) && (layer_hold_flag == 1 )){
212
240
current_layout = 0 ;
213
241
layer_hold_flag = 0 ;
214
242
#ifdef OLED_ENABLE
@@ -218,25 +246,25 @@ uint8_t *check_key_state( uint16_t **keymap){
218
246
}
219
247
220
248
//checking if macro was released
221
- if ((keycode >= MACRO_BASE_VAL )&& (keycode <= LAYER_HOLD_BASE_VAL )){
222
- for (uint8_t i = 0 ; i < 3 ; i ++ ){
223
- uint16_t key = macros [MACRO_BASE_VAL - keycode ][i ];
224
- current_report [REPORT_LEN - 1 - i ]= 0 ;
225
- modifier &= ~check_modifier (key );
249
+ if ((keycode >= MACRO_BASE_VAL ) && (keycode <= LAYER_HOLD_BASE_VAL )){
250
+ for (uint8_t i = 0 ; i < 3 ; i ++ ){
251
+ uint16_t key = macros [MACRO_BASE_VAL - keycode ][i ];
252
+ current_report [REPORT_LEN - 1 - i ] = 0 ;
253
+ modifier &= ~check_modifier (key );
226
254
}
227
255
}
228
256
229
- if (current_report [report_index ]!= 0 ){
230
- if (led_status != 0 ){
231
- led_status = 0 ;
257
+ if (current_report [report_index ] != 0 ){
258
+ if (led_status != 0 ){
259
+ led_status = 0 ;
232
260
}
233
261
234
- modifier &= ~check_modifier (keycode );
235
- current_report [KEY_STATE [row ][col ]]= 0 ;
236
- current_report [report_index ]= 0 ;
262
+ modifier &= ~check_modifier (keycode );
263
+ current_report [KEY_STATE [row ][col ]] = 0 ;
264
+ current_report [report_index ] = 0 ;
237
265
238
266
// checking for media control keycodes
239
- if ((keycode >= KC_MEDIA_NEXT_TRACK )&& (keycode <= KC_AUDIO_VOL_DOWN )){
267
+ if ((keycode >= KC_MEDIA_NEXT_TRACK ) && (keycode <= KC_AUDIO_VOL_DOWN )){
240
268
media_control_release (keycode );
241
269
}
242
270
}
0 commit comments