Can't seem to flash Model 01 anymore

I’m having issues flashing my Model 01. It seems I’ve come to a point where flashing makes no difference (except for possibly removing keys). At some point holding the prog-key stopped working, when looking in Chrysalis all keys had shifted (confirmed with using the keyboard), seemed like the prog-key (“Escape” in my layout) was missing.
See the screenshot below, note that I’m using a custom swedish colemak layout (all letters in regular colemak positions). The key in the bottom right is empty

After many flashing attempts, seemingly successful according to Arduino, another shift happened and then the next key was missing. See screenshot below, another key in the bottom right is empty

During this time I’ve tried to flash a default keyboardio ino and a previously working ino, both seemingly successful according to Arduino (log below).
I’ve previously encountered the issue with EEPROM overriding my flashed changes thinking the flashing didn’t work, however this time I have done “Reset EEPROM to factory defaults” many times in the Chrysalis software.
What I found really weird was to flash the default keyboardio qwerty layout but still seeing colemak in chrysalis even after resetting EEPROM.
I have also tried updating the firmware to latest version in chrysalis.

Waiting for upload port…
No upload port found, using COM5 as fallback
avrdude: Version 6.3-20190619Co"C:\Users\nerma\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/bin/avrdude" “-CC:\Users\nerma\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf” -v  -patmega32u4 -cavr109 -PCOM5 -b57600 -D “-Uflash:w:C:\Users\nerma\AppData\Local\arduino\sketches\337041E98EE3F14F0E706573798B8DC8/nn_model01.ino.hex:i”pyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/Copyright (c) 2007-2014 Joerg Wunsch
     System wide configuration file is "C:\Users\nerma\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf"

     Using Port                    : COM5
     Using Programmer              : avr109
     Overriding Baud Rate          : 57600
     AVR Part                      : ATmega32U4
     Chip Erase delay              : 9000 us
     PAGEL                         : PD7
     BS2                           : PA0
     RESET disposition             : dedicated
     RETRY pulse                   : SCK
     serial program mode           : yes
     parallel program mode         : yes
     Timeout                       : 200
     StabDelay                     : 100
     CmdexeDelay                   : 25
     SyncLoops                     : 32
     ByteDelay                     : 0
     PollIndex                     : 3
     PollValue                     : 0x53
     Memory Detail                 :

                              Block Poll               Page                       Polled
       Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
       ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
       eeprom        65    20     4    0 no       1024    4      0  9000  9000 0x00 0x00
       flash         65     6   128    0 yes     32768  128    256  4500  4500 0x00 0x00
       lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
       hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
       efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
       lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
       calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
       signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

     Programmer Type : butterfly
     Description     : Atmel AppNote AVR109 Boot Loader
Connecting to programmer: .Found programmer: Id = “CATERIN”; type = SSoftware Version = 1.0; No Hardware Version given.Programmer supports auto addr increment.Programmer supports buffered memory access with buffersize=128 bytes.
Programmer supports the following devices:Device code: 0x44
avrdude: devcode selected: 0x44avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.00s
avrdude: Device signature = 0x1e9587 (probably m32u4)avrdude: reading input file “C:\Users\nerma\AppData\Local\arduino\sketches\337041E98EE3F14F0E706573798B8DC8/nn_model01.ino.hex”avrdude: writing flash (28104 bytes):
Writing | ################################################## | 100% 3.62s
avrdude: 28104 bytes of flash writtenavrdude: verifying flash memory against C:\Users\nerma\AppData\Local\arduino\sketches\337041E98EE3F14F0E706573798B8DC8/nn_model01.ino.hex:avrdude: load data flash data from input file C:\Users\nerma\AppData\Local\arduino\sketches\337041E98EE3F14F0E706573798B8DC8/nn_model01.ino.hex:avrdude: input file C:\Users\nerma\AppData\Local\arduino\sketches\337041E98EE3F14F0E706573798B8DC8/nn_model01.ino.hex contains 28104 bytesavrdude: reading on-chip flash data:
Reading | ################################################## | 100% 1.76s
avrdude: verifying … 
avrdude: 28104 bytes of flash verified 
avrdude done.  Thank you.

While writing this I tried flashing a keyboardio qwerty layout again, that restored one of the keys at least, but I’m still seeing my shifted custom colemak layout.

While doing all this I noticed that the COM is different when in bootloader mode, I assume that is expected? I have also noticed that the COM has changed a few times, using COM4, COM5, COM6 and COM7.

How do I get out of this mess? If I could just reset everything to default Model 01 settings I’d probably find my way back to restoring my custom layout.

Backstory:
I have flashed the Model 01 previously and recently bought a Model 100, while making some changes configuring the Model 100 I now wanted to flash the changes to Model 01.
The flashing of Model 100 worked out great.
A few of the plugins I used for Model 100 weren’t found when trying to build for Model 01 on Arduino so I removed those.

After experimenting further, I found that the real issue seems to lie with the EEPROM/Chrysalis.

In my custom sketch I have defined the following (copied from somewhere)

USE_MAGIC_COMBOS({.action = toggleKeyboardProtocol,
                  // Left Fn + Esc + Shift
                  .keys = {R3C6, R2C6, R3C7}},
                 {.action = enterHardwareTestMode,
                  // Left Fn + Prog + LED
                  .keys = {R3C6, R0C0, R0C6}},
                 {.action = toggleKeymapSource,
                  // Left Fn + Prog + Shift
                  .keys = {R3C6, R0C0, R3C7}});

The above lets me switch between the keymap defined in the sketch and the one in Chrysalis, through the Fn + Prog + Shift command, doing so indeed confirms that my sketch is there intact and working perfectly.

What I find odd is then that I don’t seem to be able to “Reset EEPROM to factory defaults” in Chrysalis, it doesn’t seem to do anything. Doing so still makes the EEPROM keymap override the sketch keymap. So to me it seems like I can’t use the sketch keymap unless I specifically use the Fn + Prog + Shift command to switch keymap every time the keyboard is powered on.

I was able to restore a previously working EEPROM .json and manually adjust it in a text editor to get the exact same behavior as my sketch, making the “boot-into-EEPROM-issue” not much of an issue at the moment, although annoying.

Below is the code for my sketch:

// -*- mode: c++ -*-
// Copyright 2016-2022 Keyboardio, inc. <jesse@keyboard.io>
// See "LICENSE" for license details

#ifndef BUILD_INFORMATION
#define BUILD_INFORMATION "locally built"
#endif


/**
 * These #include directives pull in the Kaleidoscope firmware core,
 * as well as the Kaleidoscope plugins we use in the Model 01's firmware
 */

// The Kaleidoscope core
#include "Kaleidoscope.h"

// Support for storing the keymap in EEPROM
#include "Kaleidoscope-EEPROM-Settings.h"
#include "Kaleidoscope-EEPROM-Keymap.h"

// Support for communicating with the host via a simple Serial protocol
#include "Kaleidoscope-FocusSerial.h"

// Support for querying the firmware version via Focus
//#include "Kaleidoscope-FirmwareVersion.h" // UNSUPPORTED IN MODEL 01

// Support for keys that move the mouse
#include "Kaleidoscope-MouseKeys.h"

// Support for macros
#include "Kaleidoscope-Macros.h"

// Support for controlling the keyboard's LEDs
#include "Kaleidoscope-LEDControl.h"

// Support for "Numpad" mode, which is mostly just the Numpad specific LED mode
//#include "Kaleidoscope-NumPad.h"

// Support for the "Boot greeting" effect, which pulses the 'LED' button for 10s
// when the keyboard is connected to a computer (or that computer is powered on)
#include "Kaleidoscope-LEDEffect-BootGreeting.h"

// Support for LED modes that set all LEDs to a single color
#include "Kaleidoscope-LEDEffect-SolidColor.h"

// Support for an LED mode that makes all the LEDs 'breathe'
//#include "Kaleidoscope-LEDEffect-Breathe.h"

// Support for an LED mode that makes a red pixel chase a blue pixel across the keyboard
//#include "Kaleidoscope-LEDEffect-Chase.h"

// Support for LED modes that pulse the keyboard's LED in a rainbow pattern
#include "Kaleidoscope-LEDEffect-Rainbow.h"

// Support for an LED mode that lights up the keys as you press them
#include "Kaleidoscope-LED-Stalker.h"

// Support for an LED mode that prints the keys you press in letters 4px high
//#include "Kaleidoscope-LED-AlphaSquare.h"

// Support for shared palettes for other plugins, like Colormap below
//#include "Kaleidoscope-LED-Palette-Theme.h"

// Support for an LED mode that lets one configure per-layer color maps
//#include "Kaleidoscope-Colormap.h"

// Support for turning the LEDs off after a certain amount of time
#include "Kaleidoscope-IdleLEDs.h"

// Support for setting and saving the default LED mode
//#include "Kaleidoscope-DefaultLEDModeConfig.h" // UNSUPPORTED IN MODEL 01

// Support for changing the brightness of the LEDs
//#include "Kaleidoscope-LEDBrightnessConfig.h" // UNSUPPORTED IN MODEL 01

// Support for Keyboardio's internal keyboard testing mode
#include "Kaleidoscope-HardwareTestMode.h"

// Support for host power management (suspend & wakeup)
#include "Kaleidoscope-HostPowerManagement.h"

// Support for magic combos (key chords that trigger an action)
#include "Kaleidoscope-MagicCombo.h"

// Support for USB quirks, like changing the key state report protocol
#include "Kaleidoscope-USB-Quirks.h"

// Support for secondary actions on keys
//#include "Kaleidoscope-Qukeys.h" // UNNECESSARY

// Support for one-shot modifiers and layer keys
#include "Kaleidoscope-OneShot.h"
#include "Kaleidoscope-Escape-OneShot.h"

// Support for tap tance keys
#include "Kaleidoscope-TapDance.h"

// Support for highlighting active mod key
#include "Kaleidoscope-LED-ActiveModColor.h"

// Support for dynamic, Chrysalis-editable macros
#include "Kaleidoscope-DynamicMacros.h"

// Support for SpaceCadet keys
//#include "Kaleidoscope-SpaceCadet.h" // UNNECESSARY

// Support for editable layer names
//#include "Kaleidoscope-LayerNames.h" // UNSUPPORTED IN MODEL 01

#include "custom_key_defs.h"

/** This 'enum' is a list of all the macros used by the Model 01's firmware
  * The names aren't particularly important. What is important is that each
  * is unique.
  *
  * These are the names of your macros. They'll be used in two places.
  * The first is in your keymap definitions. There, you'll use the syntax
  * `M(MACRO_NAME)` to mark a specific keymap position as triggering `MACRO_NAME`
  *
  * The second usage is in the 'switch' statement in the `macroAction` function.
  * That switch statement actually runs the code associated with a macro when
  * a macro key is pressed.
  */

enum {
  AA
  ,AE
  ,OE
};

/** The Model 01's key layouts are defined as 'keymaps'. By default, there are three
  * keymaps: The standard QWERTY keymap, the "Function layer" keymap and the "Numpad"
  * keymap.
  *
  * Each keymap is defined as a list using the 'KEYMAP_STACKED' macro, built
  * of first the left hand's layout, followed by the right hand's layout.
  *
  * Keymaps typically consist mostly of `Key_` definitions. There are many, many keys
  * defined as part of the USB HID Keyboard specification. You can find the names
  * (if not yet the explanations) for all the standard `Key_` defintions offered by
  * Kaleidoscope in these files:
  *    https://github.com/keyboardio/Kaleidoscope/blob/master/src/kaleidoscope/key_defs/keyboard.h
  *    https://github.com/keyboardio/Kaleidoscope/blob/master/src/kaleidoscope/key_defs/consumerctl.h
  *    https://github.com/keyboardio/Kaleidoscope/blob/master/src/kaleidoscope/key_defs/sysctl.h
  *    https://github.com/keyboardio/Kaleidoscope/blob/master/src/kaleidoscope/key_defs/keymaps.h
  *
  * Additional things that should be documented here include
  *   using ___ to let keypresses fall through to the previously active layer
  *   using XXX to mark a keyswitch as 'blocked' on this layer
  *   using ShiftToLayer() and LockLayer() keys to change the active keymap.
  *   keeping NUM and FN consistent and accessible on all layers
  *
  * The PROG key is special, since it is how you indicate to the board that you
  * want to flash the firmware. However, it can be remapped to a regular key.
  * When the keyboard boots, it first looks to see whether the PROG key is held
  * down; if it is, it simply awaits further flashing instructions. If it is
  * not, it continues loading the rest of the firmware and the keyboard
  * functions normally, with whatever binding you have set to PROG. More detail
  * here: https://community.keyboard.io/t/how-the-prog-key-gets-you-into-the-bootloader/506/8
  *
  * The "keymaps" data structure is a list of the keymaps compiled into the firmware.
  * The order of keymaps in the list is important, as the ShiftToLayer(#) and LockLayer(#)
  * macros switch to key layers based on this list.
  *
  *
  *
  * A key defined as 'ShiftToLayer(FUNCTION)' will switch to FUNCTION while held.
  * Similarly, a key defined as 'LockLayer(NUMPAD)' will switch to NUMPAD when tapped.
  */

/**
  * Layers are "0-indexed" -- That is the first one is layer 0. The second one is layer 1.
  * The third one is layer 2.
  * This 'enum' lets us use names like QWERTY, FUNCTION, and NUMPAD in place of
  * the numbers 0, 1 and 2.

*/

enum { PRIMARY, NUMPAD, FUNCTION }; // layers

//#define PRIMARY_KEYMAP_COLEMAK_NN_EN
#define PRIMARY_KEYMAP_COLEMAK_NN_SE

// enum { LEFT_ENCLOSURES, RIGHT_ENCLOSURES };

/* This comment temporarily turns off astyle's indent enforcement
 *   so we can make the keymaps actually resemble the physical key layout better
 */
 // *INDENT-OFF*

KEYMAPS(

#if defined (PRIMARY_KEYMAP_COLEMAK_NN_EN)

[PRIMARY] = KEYMAP_STACKED
    (Key_Escape, Key_EXCLM, Key_DQuot, Key_STAR, Key_Equals, Key_AT, Key_PRCNT,
    Key_Pipe, Key_Q, Key_W, Key_F, Key_P, Key_G, Key_LPar,
    Key_AND, Key_A, Key_R, Key_S, Key_T, Key_D,
    OSM(LeftGui), Key_Z, Key_X, Key_C, Key_V, Key_B, Key_LT,
    OSM(LeftControl), Key_Backspace, OSM(LeftShift), OSM(LeftAlt),
    OSL(NUMPAD),

    Key_Tilde, Key_HASH, Key_Quest, Key_Minus, Key_Plus, Key_DOLLR, Key_CARET,
    Key_RPar, Key_J, Key_L, Key_U, Key_Y, Key_USc, Key_Colon,
    Key_H, Key_N, Key_E, Key_I, Key_O, Key_Semicolon,
    Key_GT, Key_K, Key_M, Key_Comma, Key_Period, Key_Slash, Key_PcApplication,
    Key_Tab, Key_Enter, Key_Spacebar, Key_Delete,
    OSL(FUNCTION)),

[NUMPAD] =  KEYMAP_STACKED
    (___, ___, ___, ___, ___, ___, ___,
    ___, ___, ___, ___, ___, Consumer_VolumeIncrement, Key_LeftBracket,
    Consumer_Mute, Consumer_ScanPreviousTrack, Consumer_ScanNextTrack, LGUI(Key_LeftArrow), LGUI(Key_RightArrow), Consumer_PlaySlashPause,
    ___, ___, ___, LCTRL(LGUI(Key_LeftArrow)), LCTRL(LGUI(Key_RightArrow)), Consumer_VolumeDecrement, ___,
    ___, ___, ___, ___,
    ___,

    ___, ___, Key_Keypad7, Key_Keypad8, Key_Keypad9, Key_KeypadSubtract, LockLayer(NUMPAD),
    Key_RightBracket, ___, Key_Keypad4, Key_Keypad5, Key_Keypad6, Key_KeypadAdd, ___,
    ___, Key_Keypad1, Key_Keypad2, Key_Keypad3, ___, ___,
    ___, ___, Key_Keypad0, ___, ___, ___, ___,
    ___, ___, ___, ___,
    ___),

[FUNCTION] =  KEYMAP_STACKED
    (___, ___, Key_Quote, ___, ___, ___, Key_F6,
    ___, Key_F1, Key_F2, Key_F3, Key_F4, Key_F5, Key_LCB,
    Key_Pause, Key_LeftArrow, Key_UpArrow, Key_DownArrow, Key_RightArrow, ___,
    ___, ___, ___, ___, ___, ___, ___,
    ___, ___, ___, ___,
    ___,

    Key_F7, Key_Insert, ___, ___, ___, ___, Key_LEDEffectNext,
    Key_RCB, Key_F8, Key_F9, Key_F10, Key_F11, Key_F12, ___,
    ___, Key_Home, Key_PageUp, Key_PageDown, Key_End, Key_PrintScreen,
    Key_Backtick, ___, M(AE), M(OE), M(AA), Key_Backslash, ___,
    ___, ___, ___, ___,
    ___)

#elif defined (PRIMARY_KEYMAP_COLEMAK_NN_SE)

[PRIMARY] = KEYMAP_STACKED
    (Key_Escape, Key_SE_EXCLM, Key_SE_DQuot, Key_SE_STAR, Key_SE_Equals, Key_SE_AT, Key_SE_PRCNT,
    Key_SE_Pipe, Key_Q, Key_W, Key_F, Key_P, Key_G, Key_SE_LPar,
    Key_SE_AND, Key_A, Key_R, Key_S, Key_T, Key_D,
    OSM(LeftGui), Key_Z, Key_X, Key_C, Key_V, Key_B, Key_SE_LT,
    OSM(LeftControl), Key_Backspace, OSM(LeftShift), OSM(LeftAlt),
    OSL(NUMPAD),

    Key_SE_Tilde, Key_SE_HASH, Key_SE_Quest, Key_SE_Minus, Key_SE_Plus, Key_SE_DOLLR, Key_SE_CARET,
    Key_SE_RPar, Key_J, Key_L, Key_U, Key_Y, Key_SE_USc, Key_SE_Colon,
    Key_H, Key_N, Key_E, Key_I, Key_O, Key_SE_Semicolon,
    Key_SE_GT, Key_K, Key_M, Key_SE_Comma, Key_SE_Period, Key_SE_Slash, Key_PcApplication,
    Key_Tab, Key_Enter, Key_Spacebar, Key_Delete,
    OSL(FUNCTION)),

[NUMPAD] =  KEYMAP_STACKED
   (___, ___, ___, ___, ___, ___, ___,
    ___, ___, ___, ___, ___, Consumer_VolumeIncrement, Key_SE_LBR,
    Consumer_Mute, Consumer_ScanPreviousTrack, Consumer_ScanNextTrack, LGUI(Key_LeftArrow), LGUI(Key_RightArrow), Consumer_PlaySlashPause,
    ___, ___, ___, LCTRL(LGUI(Key_LeftArrow)), LCTRL(LGUI(Key_RightArrow)), Consumer_VolumeDecrement, ___,
    ___, ___, ___, ___,
    ___,

    ___, ___, Key_Keypad7, Key_Keypad8, Key_Keypad9, Key_KeypadSubtract, LockLayer(NUMPAD),
    Key_SE_RBR, ___, Key_Keypad4, Key_Keypad5, Key_Keypad6, Key_KeypadAdd, ___,
    ___, Key_Keypad1, Key_Keypad2, Key_Keypad3, ___, ___,
    ___, ___, Key_Keypad0, ___, ___, ___, ___,
    ___, ___, ___, ___,
    ___),

[FUNCTION] =  KEYMAP_STACKED
    (___, ___, Key_SE_Quote, ___, ___, ___, Key_F6,
    ___, Key_F1, Key_F2, Key_F3, Key_F4, Key_F5, Key_SE_LCB,
    Key_Pause, Key_LeftArrow, Key_UpArrow, Key_DownArrow, Key_RightArrow, ___,
    ___, ___, ___, ___, ___, ___, Key_SE_Acute,
    ___, ___, ___, ___,
    ___,

    Key_F7, Key_Insert, ___, ___, ___, ___, Key_LEDEffectNext,
    Key_SE_RCB, Key_F8, Key_F9, Key_F10, Key_F11, Key_F12, ___,
    ___, Key_Home, Key_PageUp, Key_PageDown, Key_End, Key_PrintScreen,
    Key_SE_Backtick, Key_SE_Diaresis, Key_SE_AE, Key_SE_OE, Key_SE_AA, Key_SE_Backslash, ___,
    ___, ___, ___, ___,
    ___)
#else

#error "No default keymap defined. You should make sure that you have a line like '#define PRIMARY_KEYMAP_QWERTY' in your sketch"

#endif
) // KEYMAPS(

/* Re-enable astyle's indent enforcement */
// *INDENT-ON*

static void press(Key key) {
  Kaleidoscope.hid().keyboard().pressKey(key);
  //Kaleidoscope.hid().keyboard().sendKeyboardReport();
  Kaleidoscope.hid().keyboard().sendReport();
}

static void release(Key key) {
  Kaleidoscope.hid().keyboard().releaseKey(key);
  //Kaleidoscope.hid().keyboard().sendKeyboardReport();
  Kaleidoscope.hid().keyboard().sendReport();
}

static void tap(Key key) {
  press(key);
  release(key);
}

//const macro_t *typeAltCombination(uint8_t keyState, const Key key1, const Key key2, const Key key3) {
void typeAltCombination(uint8_t keyState, const Key key1, const Key key2, const Key key3) {
  if (!keyToggledOn(keyState)) {
    return;
  }
  press(Key_LeftAlt);
  tap(key1);
  tap(key2);
  tap(key3);
  release(Key_LeftAlt);
}

/** macroAction dispatches keymap events that are tied to a macro
    to that macro. It takes two uint8_t parameters.

    The first is the macro being called (the entry in the 'enum' earlier in this file).
    The second is the state of the keyswitch. You can use the keyswitch state to figure out
    if the key has just been toggled on, is currently pressed or if it's just been released.

    The 'switch' statement should have a 'case' for each entry of the macro enum.
    Each 'case' statement should call out to a function to handle the macro in question.

    I(20) is used to add delay between keys, otherwise we might get something random
 */

const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) {
  bool shiftOn = Kaleidoscope.hid().keyboard().wasModifierKeyActive(Key_LeftShift)
                 || Kaleidoscope.hid().keyboard().wasModifierKeyActive(Key_RightShift);
  switch (macroIndex) {    
    case AA:
      // Alt + 143 / 134
      if (shiftOn) {
        typeAltCombination(keyState, Key_Keypad1, Key_Keypad4, Key_Keypad3);
      }
      else {
        typeAltCombination(keyState, Key_Keypad1, Key_Keypad3, Key_Keypad4);
      }
      break;
    case AE:
      // Alt + 142 / 132
      if (shiftOn) {
        typeAltCombination(keyState, Key_Keypad1, Key_Keypad4, Key_Keypad2);
      }
      else {
        typeAltCombination(keyState, Key_Keypad1, Key_Keypad3, Key_Keypad2);
      }
      break;
    case OE:
      // Alt + 153 / 148
      if (shiftOn) {
        typeAltCombination(keyState, Key_Keypad1, Key_Keypad5, Key_Keypad3);
      }
      else {
        typeAltCombination(keyState, Key_Keypad1, Key_Keypad4, Key_Keypad8);
      }
      break;
  }
  return MACRO_NONE;
}

// void tapDanceAction(uint8_t tap_dance_index, KeyAddr key_addr, uint8_t tap_count,
                    // kaleidoscope::plugin::TapDance::ActionType tap_dance_action) {
  // switch (tap_dance_index) {
    // // Swedish
    // case LEFT_ENCLOSURES:
      // return tapDanceActionKeys(tap_count, tap_dance_action, Key_SE_LPar, Key_SE_LCB, Key_SE_LBR);
    // case RIGHT_ENCLOSURES:
      // return tapDanceActionKeys(tap_count, tap_dance_action, Key_SE_RPar, Key_SE_RCB, Key_SE_RBR);

    // // English
    // //case LEFT_ENCLOSURES:
    // //  return tapDanceActionKeys(tap_count, tap_dance_action, Key_LPar, Key_LCB, Key_LeftBracket);
    // //case RIGHT_ENCLOSURES:
    // //  return tapDanceActionKeys(tap_count, tap_dance_action, Key_RPar, Key_RCB, Key_RightBracket);
    // default:
      // Macros.type(PSTR("error"));
  // }
// }


// These 'solid' color effect definitions define a rainbow of
// LED color modes calibrated to draw 500mA or less on the
// Keyboardio Model 01.

static kaleidoscope::plugin::LEDSolidColor solidRed(160, 0, 0);
static kaleidoscope::plugin::LEDSolidColor solidOrange(140, 70, 0);
static kaleidoscope::plugin::LEDSolidColor solidGreen(0, 160, 0);
static kaleidoscope::plugin::LEDSolidColor solidBlue(0, 70, 130);
static kaleidoscope::plugin::LEDSolidColor solidIndigo(0, 0, 170);
static kaleidoscope::plugin::LEDSolidColor solidViolet(130, 0, 120);

/** toggleLedsOnSuspendResume toggles the LEDs off when the host goes to sleep,
 * and turns them back on when it wakes up.
 */
void toggleLedsOnSuspendResume(kaleidoscope::plugin::HostPowerManagement::Event event) {
  switch (event) {
  case kaleidoscope::plugin::HostPowerManagement::Suspend:
  case kaleidoscope::plugin::HostPowerManagement::Sleep:
    LEDControl.disable();
    break;
  case kaleidoscope::plugin::HostPowerManagement::Resume:
    LEDControl.enable();
    break;
  }
}

/** hostPowerManagementEventHandler dispatches power management events (suspend,
 * resume, and sleep) to other functions that perform action based on these
 * events.
 */
void hostPowerManagementEventHandler(kaleidoscope::plugin::HostPowerManagement::Event event) {
  toggleLedsOnSuspendResume(event);
}

/** This 'enum' is a list of all the magic combos used by the Model 01's
 * firmware The names aren't particularly important. What is important is that
 * each is unique.
 *
 * These are the names of your magic combos. They will be used by the
 * `USE_MAGIC_COMBOS` call below.
 */
enum {
  // Toggle between Boot (6-key rollover; for BIOSes and early boot) and NKRO
  // mode.
  COMBO_TOGGLE_NKRO_MODE,
  // Enter test mode
  COMBO_ENTER_TEST_MODE
};

/** Wrappers, to be used by MagicCombo. **/

/**
 * This simply toggles the keyboard protocol via USBQuirks, and wraps it within
 * a function with an unused argument, to match what MagicCombo expects.
 */
static void toggleKeyboardProtocol(uint8_t combo_index) {
  USBQuirks.toggleKeyboardProtocol();
}

/**
 * Toggles between using the built-in keymap, and the EEPROM-stored one.
 */
static void toggleKeymapSource(uint8_t combo_index) {
  if (Layer.getKey == Layer.getKeyFromPROGMEM) {
    Layer.getKey = EEPROMKeymap.getKey;
  } else {
    Layer.getKey = Layer.getKeyFromPROGMEM;
  }
}

/**
 *  This enters the hardware test mode
 */
static void enterHardwareTestMode(uint8_t combo_index) {
  HardwareTestMode.runTests();
}


/** Magic combo list, a list of key combo and action pairs the firmware should
 * recognise.
 */
USE_MAGIC_COMBOS({.action = toggleKeyboardProtocol,
                  // Left Fn + Esc + Shift
                  .keys = {R3C6, R2C6, R3C7}},
                 {.action = enterHardwareTestMode,
                  // Left Fn + Prog + LED
                  .keys = {R3C6, R0C0, R0C6}},
                 {.action = toggleKeymapSource,
                  // Left Fn + Prog + Shift
                  .keys = {R3C6, R0C0, R3C7}});

// First, tell Kaleidoscope which plugins you want to use.
// The order can be important. For example, LED effects are
// added in the order they're listed here.
KALEIDOSCOPE_INIT_PLUGINS(
  // ----------------------------------------------------------------------
  // Chrysalis plugins

  // The EEPROMSettings & EEPROMKeymap plugins make it possible to have an
  // editable keymap in EEPROM.
  EEPROMSettings,
  EEPROMKeymap,

  // Focus allows bi-directional communication with the host, and is the
  // interface through which the keymap in EEPROM can be edited.
  Focus,

  // FocusSettingsCommand adds a few Focus commands, intended to aid in
  // changing some settings of the keyboard, such as the default layer (via the
  // `settings.defaultLayer` command)
  FocusSettingsCommand,

  // FocusEEPROMCommand adds a set of Focus commands, which are very helpful in
  // both debugging, and in backing up one's EEPROM contents.
  FocusEEPROMCommand,

  // The FirmwareVersion plugin lets Chrysalis query the version of the firmware
  // programmatically.
  //FirmwareVersion,

  // The LayerNames plugin allows Chrysalis to display - and edit - custom layer
  // names, to be shown instead of the default indexes.
  //LayerNames,

  // Enables setting, saving (via Chrysalis), and restoring (on boot) the
  // default LED mode.
  //DefaultLEDModeConfig,

  // Enables controlling (and saving) the brightness of the LEDs via Focus.
  //LEDBrightnessConfig,

  // ----------------------------------------------------------------------
  // Keystroke-handling plugins

  // The Qukeys plugin enables the "Secondary action" functionality in
  // Chrysalis. Keys with secondary actions will have their primary action
  // performed when tapped, but the secondary action when held.
  //Qukeys,

  // SpaceCadet can turn your shifts into parens on tap, while keeping them as
  // Shifts when held. SpaceCadetConfig lets Chrysalis configure some aspects of
  // the plugin.
  //SpaceCadet,
  //SpaceCadetConfig,

  // Enables the "Sticky" behavior for modifiers, and the "Layer shift when
  // held" functionality for layer keys.
  OneShot,
  //OneShotConfig,
  EscapeOneShot,
  //EscapeOneShotConfig,

  // The macros plugin adds support for macros
  Macros,

  // Enables dynamic, Chrysalis-editable macros.
  DynamicMacros,

  // The MouseKeys plugin lets you add keys to your keymap which move the mouse.
  MouseKeys,
  //MouseKeysConfig,

  // The MagicCombo plugin lets you use key combinations to trigger custom
  // actions - a bit like Macros, but triggered by pressing multiple keys at the
  // same time.
  MagicCombo,

  // ----------------------------------------------------------------------
  // LED mode plugins

  // The boot greeting effect pulses the LED button for 10 seconds after the
  // keyboard is first connected
  BootGreetingEffect,

  // LEDControl provides support for other LED modes
  LEDControl,

  // We start with the LED effect that turns off all the LEDs.
  LEDOff,

  // The rainbow effect changes the color of all of the keyboard's keys at the same time
  // running through all the colors of the rainbow.
  //LEDRainbowEffect,

  // The rainbow wave effect lights up your keyboard with all the colors of a rainbow
  // and slowly moves the rainbow across your keyboard
  LEDRainbowWaveEffect,

  // The chase effect follows the adventure of a blue pixel which chases a red pixel across
  // your keyboard. Spoiler: the blue pixel never catches the red pixel
  //LEDChaseEffect,

  // These static effects turn your keyboard's LEDs a variety of colors
  //solidRed,
  //solidOrange,
  //solidGreen,
  //solidBlue,
  //solidIndigo,
  //solidViolet,

  // The breathe effect slowly pulses all of the LEDs on your keyboard
  //LEDBreatheEffect,

  // The AlphaSquare effect prints each character you type, using your
  // keyboard's LEDs as a display
  //AlphaSquareEffect,

  // The stalker effect lights up the keys you've pressed recently
  //StalkerEffect,

  // The LED Palette Theme plugin provides a shared palette for other plugins,
  // like Colormap below
  //LEDPaletteTheme,

  // The Colormap effect makes it possible to set up per-layer colormaps
  //ColormapEffect,

  // The numpad plugin is responsible for lighting up the 'numpad' mode
  // with a custom LED effect
  //NumPad,

  // The HostPowerManagement plugin allows us to turn LEDs off when then host
  // goes to sleep, and resume them when it wakes up.
  HostPowerManagement,

  // Turns LEDs off after a configurable amount of idle time.
  IdleLEDs,
  PersistentIdleLEDs,


  TapDance,

  ActiveModColorEffect,

  // ----------------------------------------------------------------------
  // Miscellaneous plugins

  // The USBQuirks plugin lets you do some things with USB that we aren't
  // comfortable - or able - to do automatically, but can be useful
  // nevertheless. Such as toggling the key report protocol between Boot (used
  // by BIOSes) and Report (NKRO).
  USBQuirks,

  // The hardware test mode, which can be invoked by tapping Prog, LED and the
  // left Fn button at the same time.
  HardwareTestMode  //,
);

/** The 'setup' function is one of the two standard Arduino sketch functions.
 * It's called when your keyboard first powers up. This is where you set up
 * Kaleidoscope and any plugins.
 */
void setup() {
  // First, call Kaleidoscope's internal setup function
  Kaleidoscope.setup();

  // Set the hue of the boot greeting effect to something that will result in a
  // nice green color.
  BootGreetingEffect.hue = 85;

  // While we hope to improve this in the future, the NumPad plugin
  // needs to be explicitly told which keymap layer is your numpad layer
  //NumPad.numPadLayer = NUMPAD;

  // We configure the AlphaSquare effect to use RED letters
  //AlphaSquare.color = CRGB(255, 0, 0);

  // Set the rainbow effects to be reasonably bright, but low enough
  // to mitigate audible noise in some environments.
  LEDRainbowEffect.brightness(170);
  LEDRainbowWaveEffect.brightness(160);

  // Set the action key the test mode should listen for to Left Fn
  HardwareTestMode.setActionKey(R3C6);

  // The LED Stalker mode has a few effects. The one we like is called
  // 'BlazingTrail'. For details on other options, see
  // https://github.com/keyboardio/Kaleidoscope/blob/master/docs/plugins/LED-Stalker.md
  //StalkerEffect.variant = STALKER(BlazingTrail);

  TapDance.time_out = 300;
  //OneShot.time_out = 1000;
  OneShot.setTimeout(1000);
  //IdleLEDs.setIdleTimeoutSeconds(300);

  //ActiveModColorEffect.highlight_color = CRGB(0x00, 0xff, 0x00);
  //ActiveModColorEffect.sticky_color = CRGB(0xff, 0x80, 0x00);
  ActiveModColorEffect.setHighlightColor(CRGB(0x00, 0xff, 0x00));
  //ActiveModColorEffect.setStickyColor(CRGB(0xff, 0x80, 0x00));
  ActiveModColorEffect.setOnestickyColor(CRGB(0xff, 0x80, 0x00));

  // To make the keymap editable without flashing new firmware, we store
  // additional layers in EEPROM. For now, we reserve space for eight layers. If
  // one wants to use these layers, just set the default layer to one in EEPROM,
  // by using the `settings.defaultLayer` Focus command, or by using the
  // `keymap.onlyCustom` command to use EEPROM layers only.
  EEPROMKeymap.setup(8);

  // We need to tell the Colormap plugin how many layers we want to have custom
  // maps for. To make things simple, we set it to eight layers, which is how
  // many editable layers we have (see above).
  //ColormapEffect.max_layers(8);

  // For Dynamic Macros, we need to reserve storage space for the editable
  // macros. A kilobyte is a reasonable default.
  DynamicMacros.reserve_storage(1024);

  // If there's a default layer set in EEPROM, we should set that as the default
  // here.
  Layer.move(EEPROMSettings.default_layer());

  // To avoid any surprises, SpaceCadet is turned off by default. However, it
  // can be permanently enabled via Chrysalis, so we should only disable it if
  // no configuration exists.
  //SpaceCadetConfig.disableSpaceCadetIfUnconfigured();

  // Editable layer names are stored in EEPROM too, and we reserve 16 bytes per
  // layer for them. We need one extra byte per layer for bookkeeping, so we
  // reserve 17 / layer in total.
  //LayerNames.reserve_storage(17 * 8);

  // Unless configured otherwise with Chrysalis, we want to make sure that the
  // firmware starts with LED effects off. This avoids over-taxing devices that
  // don't have a lot of power to share with USB devices
  //DefaultLEDModeConfig.activateLEDModeIfUnconfigured(&LEDOff);
}

/** loop is the second of the standard Arduino sketch functions.
  * As you might expect, it runs in a loop, never exiting.
  *
  * For Kaleidoscope-based keyboard firmware, you usually just want to
  * call Kaleidoscope.loop(); and not do anything custom here.
  */

void loop() {
  Kaleidoscope.loop();
}