qmk

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.z3bra.org/qmk.git
Log | Files | Refs | Submodules | LICENSE

zeal60.c (9120B)


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