]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - keyboards/wilba_tech/wt_main.c
Added macros to Dynamic Keymaps, Zeal60 RGB backlight improvements (#4520)
[qmk_firmware.git] / keyboards / wilba_tech / wt_main.c
index f514dbefcda22ee1abdb6b52282f4e899ea4cd0c..de6d7b92c176e7fbf61da89145a0f5c3bf61d165 100644 (file)
 
 #include "quantum.h"
 #include "keyboards/wilba_tech/wt_mono_backlight.h"
+#include "keyboards/zeal60/zeal60_api.h" // Temporary hack
+#include "keyboards/zeal60/zeal60_keycodes.h" // Temporary hack
+
+#include "raw_hid.h"
+#include "dynamic_keymap.h"
+#include "timer.h"
+#include "tmk_core/common/eeprom.h"
+
+bool eeprom_is_valid(void)
+{
+       return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC &&
+                       eeprom_read_byte(((void*)EEPROM_VERSION_ADDR)) == EEPROM_VERSION);
+}
+
+void eeprom_set_valid(bool valid)
+{
+       eeprom_update_word(((void*)EEPROM_MAGIC_ADDR), valid ? EEPROM_MAGIC : 0xFFFF);
+       eeprom_update_byte(((void*)EEPROM_VERSION_ADDR), valid ? EEPROM_VERSION : 0xFF);
+}
+
+void eeprom_reset(void)
+{
+       // Set the Zeal60 specific EEPROM state as invalid.
+       eeprom_set_valid(false);
+       // Set the TMK/QMK EEPROM state as invalid.
+       eeconfig_disable();
+}
+
+#ifdef RAW_ENABLE
+
+void raw_hid_receive( uint8_t *data, uint8_t length )
+{
+       uint8_t *command_id = &(data[0]);
+       uint8_t *command_data = &(data[1]);
+       switch ( *command_id )
+       {
+               case id_get_protocol_version:
+               {
+                       command_data[0] = PROTOCOL_VERSION >> 8;
+                       command_data[1] = PROTOCOL_VERSION & 0xFF;
+                       break;
+               }
+               case id_get_keyboard_value:
+               {
+                       if ( command_data[0] == id_uptime )
+                       {
+                               uint32_t value = timer_read32();
+                               command_data[1] = (value >> 24 ) & 0xFF;
+                               command_data[2] = (value >> 16 ) & 0xFF;
+                               command_data[3] = (value >> 8 ) & 0xFF;
+                               command_data[4] = value & 0xFF;
+                       }
+                       else
+                       {
+                               *command_id = id_unhandled;
+                       }
+                       break;
+               }
+#ifdef DYNAMIC_KEYMAP_ENABLE
+               case id_dynamic_keymap_get_keycode:
+               {
+                       uint16_t keycode = dynamic_keymap_get_keycode( command_data[0], command_data[1], command_data[2] );
+                       command_data[3] = keycode >> 8;
+                       command_data[4] = keycode & 0xFF;
+                       break;
+               }
+               case id_dynamic_keymap_set_keycode:
+               {
+                       dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
+                       break;
+               }
+               case id_dynamic_keymap_reset:
+               {
+                       dynamic_keymap_reset();
+                       break;
+               }
+               case id_dynamic_keymap_macro_get_count:
+               {
+                       command_data[0] = dynamic_keymap_macro_get_count();
+                       break;
+               }
+               case id_dynamic_keymap_macro_get_buffer_size:
+               {
+                       uint16_t size = dynamic_keymap_macro_get_buffer_size();
+                       command_data[0] = size >> 8;
+                       command_data[1] = size & 0xFF;
+                       break;
+               }
+               case id_dynamic_keymap_macro_get_buffer:
+               {
+                       uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
+                       uint16_t size = command_data[2]; // size <= 28
+                       dynamic_keymap_macro_get_buffer( offset, size, &command_data[3] );
+                       break;
+               }
+               case id_dynamic_keymap_macro_set_buffer:
+               {
+                       uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
+                       uint16_t size = command_data[2]; // size <= 28
+                       dynamic_keymap_macro_set_buffer( offset, size, &command_data[3] );
+                       break;
+               }
+               case id_dynamic_keymap_macro_reset:
+               {
+                       dynamic_keymap_macro_reset();
+                       break;
+               }
+               case id_dynamic_keymap_get_layer_count:
+               {
+                       command_data[0] = dynamic_keymap_get_layer_count();
+                       break;
+               }
+               case id_dynamic_keymap_get_buffer:
+               {
+                       uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
+                       uint16_t size = command_data[2]; // size <= 28
+                       dynamic_keymap_get_buffer( offset, size, &command_data[3] );
+                       break;
+               }
+               case id_dynamic_keymap_set_buffer:
+               {
+                       uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
+                       uint16_t size = command_data[2]; // size <= 28
+                       dynamic_keymap_set_buffer( offset, size, &command_data[3] );
+                       break;
+               }
+#endif // DYNAMIC_KEYMAP_ENABLE
+               case id_eeprom_reset:
+               {
+                       eeprom_reset();
+                       break;
+               }
+               case id_bootloader_jump:
+               {
+                       // Need to send data back before the jump
+                       // Informs host that the command is handled
+                       raw_hid_send( data, length );
+                       // Give host time to read it
+                       wait_ms(100);
+                       bootloader_jump();
+                       break;
+               }
+               default:
+               {
+                       // Unhandled message.
+                       *command_id = id_unhandled;
+                       break;
+               }
+       }
+
+       // Return same buffer with values changed
+       raw_hid_send( data, length );
+
+}
+
+#endif
+
+void main_init(void)
+{
+       // If the EEPROM has the magic, the data is good.
+       // OK to load from EEPROM.
+       if (eeprom_is_valid()) {
+               //backlight_config_load();
+       } else  {
+               // If the EEPROM has not been saved before, or is out of date,
+               // save the default values to the EEPROM. Default values
+               // come from construction of the zeal_backlight_config instance.
+               //backlight_config_save();
+#ifdef DYNAMIC_KEYMAP_ENABLE
+               // This resets the keymaps in EEPROM to what is in flash.
+               dynamic_keymap_reset();
+               // This resets the macros in EEPROM to nothing.
+               dynamic_keymap_macro_reset();
+#endif
+               // Save the magic number last, in case saving was interrupted
+               eeprom_set_valid(true);
+       }
+
+       // Initialize LED drivers for backlight.
+       backlight_init_drivers();
+
+       backlight_timer_init();
+       backlight_timer_enable();
+}
 
 void bootmagic_lite(void)
 {
@@ -32,9 +216,7 @@ void bootmagic_lite(void)
        // If the Esc (matrix 0,0) is held down on power up,
        // reset the EEPROM valid state and jump to bootloader.
        if ( matrix_get_row(0) & (1<<0) ) {
-               // Set the TMK/QMK EEPROM state as invalid.
-               eeconfig_disable();
-               // Jump to bootloader.
+               eeprom_reset();
                bootloader_jump();
        }
 }
@@ -42,14 +224,53 @@ void bootmagic_lite(void)
 void matrix_init_kb(void)
 {
        bootmagic_lite();
-       backlight_init_drivers();
-       backlight_timer_init();
-       backlight_timer_enable();
+       main_init();
        matrix_init_user();
 }
 
 void matrix_scan_kb(void)
 {
+       // This only updates the LED driver buffers if something has changed.
        backlight_update_pwm_buffers();
        matrix_scan_user();
 }
+
+bool process_record_kb(uint16_t keycode, keyrecord_t *record)
+{
+       switch(keycode) {
+               case FN_MO13:
+                       if (record->event.pressed) {
+                               layer_on(1);
+                               update_tri_layer(1, 2, 3);
+                       } else {
+                               layer_off(1);
+                               update_tri_layer(1, 2, 3);
+                       }
+                       return false;
+                       break;
+               case FN_MO23:
+                       if (record->event.pressed) {
+                               layer_on(2);
+                               update_tri_layer(1, 2, 3);
+                       } else {
+                               layer_off(2);
+                               update_tri_layer(1, 2, 3);
+                       }
+                       return false;
+                       break;
+       }
+
+#ifdef DYNAMIC_KEYMAP_ENABLE
+       // Handle macros
+       if (record->event.pressed) {
+               if ( keycode >= MACRO00 && keycode <= MACRO15 )
+               {
+                       uint8_t id = keycode - MACRO00;
+                       dynamic_keymap_macro_send(id);
+                       return false;
+               }
+       }
+#endif //DYNAMIC_KEYMAP_ENABLE
+
+       return process_record_user(keycode, record);
+}