qmk

QMK Firmware
git clone git://git.z3bra.org/qmk.git
Log | Files | Refs | Submodules | LICENSE

wt_main.c (9702B)


      1 /* Copyright 2018 Jason Williams (Wilba)
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 2 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15  */
     16 
     17 #include "quantum.h"
     18 
     19 // Check that no backlight functions are called
     20 #if RGB_BACKLIGHT_ENABLED
     21 #include "keyboards/wilba_tech/wt_rgb_backlight.h"
     22 #endif // RGB_BACKLIGHT_ENABLED
     23 #ifdef WT_MONO_BACKLIGHT
     24 #include "keyboards/wilba_tech/wt_mono_backlight.h"
     25 #endif // WT_MONO_BACKLIGHT
     26 #include "keyboards/wilba_tech/via_api.h" // Temporary hack
     27 #include "keyboards/wilba_tech/via_keycodes.h" // Temporary hack
     28 
     29 #include "raw_hid.h"
     30 #include "dynamic_keymap.h"
     31 #include "timer.h"
     32 #include "tmk_core/common/eeprom.h"
     33 
     34 bool eeprom_is_valid(void)
     35 {
     36 	return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC &&
     37 			eeprom_read_byte(((void*)EEPROM_VERSION_ADDR)) == EEPROM_VERSION);
     38 }
     39 
     40 void eeprom_set_valid(bool valid)
     41 {
     42 	eeprom_update_word(((void*)EEPROM_MAGIC_ADDR), valid ? EEPROM_MAGIC : 0xFFFF);
     43 	eeprom_update_byte(((void*)EEPROM_VERSION_ADDR), valid ? EEPROM_VERSION : 0xFF);
     44 }
     45 
     46 void eeprom_reset(void)
     47 {
     48 	// Set the Zeal60 specific EEPROM state as invalid.
     49 	eeprom_set_valid(false);
     50 	// Set the TMK/QMK EEPROM state as invalid.
     51 	eeconfig_disable();
     52 }
     53 
     54 #ifdef RAW_ENABLE
     55 
     56 void raw_hid_receive( uint8_t *data, uint8_t length )
     57 {
     58 	uint8_t *command_id = &(data[0]);
     59 	uint8_t *command_data = &(data[1]);
     60 	switch ( *command_id )
     61 	{
     62 		case id_get_protocol_version:
     63 		{
     64 			command_data[0] = PROTOCOL_VERSION >> 8;
     65 			command_data[1] = PROTOCOL_VERSION & 0xFF;
     66 			break;
     67 		}
     68 		case id_get_keyboard_value:
     69 		{
     70 			if ( command_data[0] == id_uptime )
     71 			{
     72 				uint32_t value = timer_read32();
     73 				command_data[1] = (value >> 24 ) & 0xFF;
     74 				command_data[2] = (value >> 16 ) & 0xFF;
     75 				command_data[3] = (value >> 8 ) & 0xFF;
     76 				command_data[4] = value & 0xFF;
     77 			}
     78 			else
     79 			{
     80 				*command_id = id_unhandled;
     81 			}
     82 			break;
     83 		}
     84 #ifdef DYNAMIC_KEYMAP_ENABLE
     85 		case id_dynamic_keymap_get_keycode:
     86 		{
     87 			uint16_t keycode = dynamic_keymap_get_keycode( command_data[0], command_data[1], command_data[2] );
     88 			command_data[3] = keycode >> 8;
     89 			command_data[4] = keycode & 0xFF;
     90 			break;
     91 		}
     92 		case id_dynamic_keymap_set_keycode:
     93 		{
     94 			dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
     95 			break;
     96 		}
     97 		case id_dynamic_keymap_reset:
     98 		{
     99 			dynamic_keymap_reset();
    100 			break;
    101 		}
    102 		case id_dynamic_keymap_macro_get_count:
    103 		{
    104 			command_data[0] = dynamic_keymap_macro_get_count();
    105 			break;
    106 		}
    107 		case id_dynamic_keymap_macro_get_buffer_size:
    108 		{
    109 			uint16_t size = dynamic_keymap_macro_get_buffer_size();
    110 			command_data[0] = size >> 8;
    111 			command_data[1] = size & 0xFF;
    112 			break;
    113 		}
    114 		case id_dynamic_keymap_macro_get_buffer:
    115 		{
    116 			uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
    117 			uint16_t size = command_data[2]; // size <= 28
    118 			dynamic_keymap_macro_get_buffer( offset, size, &command_data[3] );
    119 			break;
    120 		}
    121 		case id_dynamic_keymap_macro_set_buffer:
    122 		{
    123 			uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
    124 			uint16_t size = command_data[2]; // size <= 28
    125 			dynamic_keymap_macro_set_buffer( offset, size, &command_data[3] );
    126 			break;
    127 		}
    128 		case id_dynamic_keymap_macro_reset:
    129 		{
    130 			dynamic_keymap_macro_reset();
    131 			break;
    132 		}
    133 		case id_dynamic_keymap_get_layer_count:
    134 		{
    135 			command_data[0] = dynamic_keymap_get_layer_count();
    136 			break;
    137 		}
    138 		case id_dynamic_keymap_get_buffer:
    139 		{
    140 			uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
    141 			uint16_t size = command_data[2]; // size <= 28
    142 			dynamic_keymap_get_buffer( offset, size, &command_data[3] );
    143 			break;
    144 		}
    145 		case id_dynamic_keymap_set_buffer:
    146 		{
    147 			uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
    148 			uint16_t size = command_data[2]; // size <= 28
    149 			dynamic_keymap_set_buffer( offset, size, &command_data[3] );
    150 			break;
    151 		}
    152 #endif // DYNAMIC_KEYMAP_ENABLE
    153 #if RGB_BACKLIGHT_ENABLED
    154 		case id_backlight_config_set_value:
    155 		{
    156 			backlight_config_set_value(command_data);
    157 			break;
    158 		}
    159 		case id_backlight_config_get_value:
    160 		{
    161 			backlight_config_get_value(command_data);
    162 			break;
    163 		}
    164 		case id_backlight_config_save:
    165 		{
    166 			backlight_config_save();
    167 			break;
    168 		}
    169 #endif // RGB_BACKLIGHT_ENABLED
    170 		case id_eeprom_reset:
    171 		{
    172 			eeprom_reset();
    173 			break;
    174 		}
    175 		case id_bootloader_jump:
    176 		{
    177 			// Need to send data back before the jump
    178 			// Informs host that the command is handled
    179 			raw_hid_send( data, length );
    180 			// Give host time to read it
    181 			wait_ms(100);
    182 			bootloader_jump();
    183 			break;
    184 		}
    185 		default:
    186 		{
    187 			// Unhandled message.
    188 			*command_id = id_unhandled;
    189 			break;
    190 		}
    191 	}
    192 
    193 	// Return same buffer with values changed
    194 	raw_hid_send( data, length );
    195 
    196 }
    197 
    198 #endif
    199 
    200 void main_init(void)
    201 {
    202 	// If the EEPROM has the magic, the data is good.
    203 	// OK to load from EEPROM.
    204 	if (eeprom_is_valid()) {
    205 #if RGB_BACKLIGHT_ENABLED
    206 		backlight_config_load();
    207 #endif // RGB_BACKLIGHT_ENABLED
    208 	} else	{
    209 #if RGB_BACKLIGHT_ENABLED
    210 		// If the EEPROM has not been saved before, or is out of date,
    211 		// save the default values to the EEPROM. Default values
    212 		// come from construction of the zeal_backlight_config instance.
    213 		backlight_config_save();
    214 #endif // RGB_BACKLIGHT_ENABLED
    215 #ifdef DYNAMIC_KEYMAP_ENABLE
    216 		// This resets the keymaps in EEPROM to what is in flash.
    217 		dynamic_keymap_reset();
    218 		// This resets the macros in EEPROM to nothing.
    219 		dynamic_keymap_macro_reset();
    220 #endif // DYNAMIC_KEYMAP_ENABLE
    221 		// Save the magic number last, in case saving was interrupted
    222 		eeprom_set_valid(true);
    223 	}
    224 	
    225 #if RGB_BACKLIGHT_ENABLED
    226 	// Initialize LED drivers for backlight.
    227 	backlight_init_drivers();
    228 
    229 	backlight_timer_init();
    230 	backlight_timer_enable();
    231 #endif // RGB_BACKLIGHT_ENABLED
    232 #ifdef WT_MONO_BACKLIGHT
    233 	// Initialize LED drivers for backlight.
    234 	backlight_init_drivers();
    235 
    236 	backlight_timer_init();
    237 	backlight_timer_enable();
    238 #endif // WT_MONO_BACKLIGHT
    239 }
    240 
    241 void bootmagic_lite(void)
    242 {
    243 	// The lite version of TMK's bootmagic.
    244 	// 100% less potential for accidentally making the
    245 	// keyboard do stupid things.
    246 
    247 	// We need multiple scans because debouncing can't be turned off.
    248 	matrix_scan();
    249 	wait_ms(DEBOUNCE);
    250 	wait_ms(DEBOUNCE);
    251 	matrix_scan();
    252 
    253 	// If the Esc (matrix 0,0) is held down on power up,
    254 	// reset the EEPROM valid state and jump to bootloader.
    255 	if ( matrix_get_row(0) & (1<<0) ) {
    256 		eeprom_reset();
    257 		bootloader_jump();
    258 	}
    259 }
    260 
    261 void matrix_init_kb(void)
    262 {
    263 	bootmagic_lite();
    264 	main_init();
    265 	matrix_init_user();
    266 }
    267 
    268 void matrix_scan_kb(void)
    269 {
    270 #if RGB_BACKLIGHT_ENABLED
    271 	// This only updates the LED driver buffers if something has changed.
    272 	backlight_update_pwm_buffers();
    273 #endif // RGB_BACKLIGHT_ENABLED
    274 #ifdef WT_MONO_BACKLIGHT
    275 	// This only updates the LED driver buffers if something has changed.
    276 	backlight_update_pwm_buffers();
    277 #endif
    278 	matrix_scan_user();
    279 }
    280 
    281 bool process_record_kb(uint16_t keycode, keyrecord_t *record)
    282 {
    283 #if RGB_BACKLIGHT_ENABLED
    284 	process_record_backlight(keycode, record);
    285 #endif // RGB_BACKLIGHT_ENABLED
    286 
    287 	switch(keycode) {
    288 		case FN_MO13:
    289 			if (record->event.pressed) {
    290 				layer_on(1);
    291 				update_tri_layer(1, 2, 3);
    292 			} else {
    293 				layer_off(1);
    294 				update_tri_layer(1, 2, 3);
    295 			}
    296 			return false;
    297 			break;
    298 		case FN_MO23:
    299 			if (record->event.pressed) {
    300 				layer_on(2);
    301 				update_tri_layer(1, 2, 3);
    302 			} else {
    303 				layer_off(2);
    304 				update_tri_layer(1, 2, 3);
    305 			}
    306 			return false;
    307 			break;
    308 	}
    309 
    310 #ifdef DYNAMIC_KEYMAP_ENABLE
    311 	// Handle macros
    312 	if (record->event.pressed) {
    313 		if ( keycode >= MACRO00 && keycode <= MACRO15 )
    314 		{
    315 			uint8_t id = keycode - MACRO00;
    316 			dynamic_keymap_macro_send(id);
    317 			return false;
    318 		}
    319 	}
    320 #endif //DYNAMIC_KEYMAP_ENABLE
    321 
    322 	return process_record_user(keycode, record);
    323 }
    324 
    325 // This overrides the one in quantum/keymap_common.c
    326 uint16_t keymap_function_id_to_action( uint16_t function_id )
    327 {
    328 	// Zeal60 specific "action functions" are 0xF00 to 0xFFF
    329 	// i.e. F(0xF00) to F(0xFFF) are mapped to
    330 	// enum zeal60_action_functions by masking last 8 bits.
    331 	if ( function_id >= 0x0F00 && function_id <= 0x0FFF )
    332 	{
    333 		uint8_t id = function_id & 0xFF;
    334 		switch ( id ) {
    335 			case TRIPLE_TAP_1_3:
    336 			case TRIPLE_TAP_2_3:
    337 			{
    338 				return ACTION_FUNCTION_TAP(id);
    339 				break;
    340 			}
    341 			default:
    342 				break;
    343 		}
    344 	}
    345 
    346 	return pgm_read_word(&fn_actions[function_id]);
    347 }
    348 
    349 
    350 // Zeal60 specific "action functions"
    351 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
    352 {
    353 	switch (id)
    354 	{
    355 	case TRIPLE_TAP_1_3:
    356 	case TRIPLE_TAP_2_3:
    357 		if (record->event.pressed) {
    358 			layer_on( id == TRIPLE_TAP_1_3 ? 1 : 2 );
    359 			if (record->tap.count && !record->tap.interrupted) {
    360 				if (record->tap.count >= 3) {
    361 					layer_invert(3);
    362 				}
    363 			} else {
    364 				record->tap.count = 0;
    365 			}
    366 		} else {
    367 			layer_off( id == TRIPLE_TAP_1_3 ? 1 : 2 );
    368 		}
    369 		break;
    370 	}
    371 }
    372 
    373 void led_set_kb(uint8_t usb_led)
    374 {
    375 #if RGB_BACKLIGHT_ENABLED
    376 	backlight_set_indicator_state(usb_led);
    377 #endif // RGB_BACKLIGHT_ENABLED
    378 }
    379 
    380 void suspend_power_down_kb(void)
    381 {
    382 #if RGB_BACKLIGHT_ENABLED
    383 	backlight_set_suspend_state(true);
    384 #endif // RGB_BACKLIGHT_ENABLED
    385 }
    386 
    387 void suspend_wakeup_init_kb(void)
    388 {
    389 #if RGB_BACKLIGHT_ENABLED
    390 	backlight_set_suspend_state(false);
    391 #endif // RGB_BACKLIGHT_ENABLED
    392 }
    393