@ -24,28 +24,18 @@ namespace plugin {
// --- state ---
// --- state ---
uint32_t TapDance : : end_time_ ;
uint32_t TapDance : : end_time_ ;
uint16_t TapDance : : time_out = 200 ;
uint16_t TapDance : : time_out = 200 ;
uint8_t TapDance : : tap_count_ [ 16 ] ;
TapDance : : TapDanceState TapDance : : state_ [ TapDance : : TAPDANCE_KEY_COUNT ] ;
uint16_t TapDance : : pressed_state_ ;
uint16_t TapDance : : triggered_state_ ;
uint16_t TapDance : : release_next_state_ ;
Key TapDance : : last_tap_dance_key_ ;
Key TapDance : : last_tap_dance_key_ ;
byte TapDance : : last_tap_dance_row_ ;
byte TapDance : : last_tap_dance_row_ ;
byte TapDance : : last_tap_dance_col_ ;
byte TapDance : : last_tap_dance_col_ ;
// --- helpers ---
# define isTapDance(k) (k.raw >= ranges::TD_FIRST && k.raw <= ranges::TD_LAST)
# define isInSeq(k) (last_tap_dance_key_.raw == k.raw)
# define stillHeld(idx) (tap_count_[idx])
# define isActive() (last_tap_dance_key_.raw != Key_NoKey.raw)
// --- actions ---
// --- actions ---
void TapDance : : interrupt ( byte row , byte col ) {
void TapDance : : interrupt ( byte row , byte col ) {
uint8_t idx = last_tap_dance_key_ . raw - ranges : : TD_FIRST ;
uint8_t idx = last_tap_dance_key_ . raw - ranges : : TD_FIRST ;
tapDanceAction ( idx , last_tap_dance_row_ , last_tap_dance_col_ , tap_coun t_[ idx ] , Interrupt ) ;
tapDanceAction ( idx , last_tap_dance_row_ , last_tap_dance_col_ , state_ [ idx ] . count , Interrupt ) ;
bitWrite( triggered_state_ , idx , 1 ) ;
state_ [ idx ] . triggered = true ;
end_time_ = 0 ;
end_time_ = 0 ;
@ -53,7 +43,7 @@ void TapDance::interrupt(byte row, byte col) {
kaleidoscope : : hid : : sendKeyboardReport ( ) ;
kaleidoscope : : hid : : sendKeyboardReport ( ) ;
kaleidoscope : : hid : : releaseAllKeys ( ) ;
kaleidoscope : : hid : : releaseAllKeys ( ) ;
if ( bitRead( pressed_state_ , idx ) )
if ( state_[ idx ] . pressed )
return ;
return ;
release ( idx ) ;
release ( idx ) ;
@ -62,10 +52,10 @@ void TapDance::interrupt(byte row, byte col) {
void TapDance : : timeout ( void ) {
void TapDance : : timeout ( void ) {
uint8_t idx = last_tap_dance_key_ . raw - ranges : : TD_FIRST ;
uint8_t idx = last_tap_dance_key_ . raw - ranges : : TD_FIRST ;
tapDanceAction ( idx , last_tap_dance_row_ , last_tap_dance_col_ , tap_coun t_[ idx ] , Timeout ) ;
tapDanceAction ( idx , last_tap_dance_row_ , last_tap_dance_col_ , s tate _[ idx ] . count , Timeout ) ;
bitWrite( triggered_state_ , idx , 1 ) ;
state_[ idx ] . triggered = true ;
if ( bitRead( pressed_state_ , idx ) )
if ( state_[ idx ] . pressed )
return ;
return ;
last_tap_dance_key_ . raw = Key_NoKey . raw ;
last_tap_dance_key_ . raw = Key_NoKey . raw ;
@ -77,18 +67,18 @@ void TapDance::release(uint8_t tap_dance_index) {
end_time_ = 0 ;
end_time_ = 0 ;
last_tap_dance_key_ . raw = Key_NoKey . raw ;
last_tap_dance_key_ . raw = Key_NoKey . raw ;
bitClear( pressed_state_ , tap_dance_index ) ;
state_[ tap_dance_index ] . pressed = false ;
bitClear( triggered_state_ , tap_dance_index ) ;
state_[ tap_dance_index ] . triggered = false ;
bitWrite( release_next_state_ , tap_dance_index , 1 ) ;
state_[ tap_dance_index ] . release_next = true ;
}
}
void TapDance : : tap ( void ) {
void TapDance : : tap ( void ) {
uint8_t idx = last_tap_dance_key_ . raw - ranges : : TD_FIRST ;
uint8_t idx = last_tap_dance_key_ . raw - ranges : : TD_FIRST ;
tap_coun t_[ idx ] + + ;
s tate _[ idx ] . count + + ;
end_time_ = millis ( ) + time_out ;
end_time_ = millis ( ) + time_out ;
tapDanceAction ( idx , last_tap_dance_row_ , last_tap_dance_col_ , tap_coun t_[ idx ] , Tap ) ;
tapDanceAction ( idx , last_tap_dance_row_ , last_tap_dance_col_ , s tate _[ idx ] . count , Tap ) ;
}
}
// --- api ---
// --- api ---
@ -123,8 +113,8 @@ EventHandlerResult TapDance::onKeyswitchEvent(Key &mapped_key, byte row, byte co
if ( keyState & INJECTED )
if ( keyState & INJECTED )
return EventHandlerResult : : OK ;
return EventHandlerResult : : OK ;
if ( ! isTapDance ( mapped_key ) ) {
if ( mapped_key . raw < ranges : : TD_FIRST | | mapped_key . raw > ranges : : TD_LAST ) {
if ( ! isActive ( ) )
if ( last_tap_dance_key_ . raw = = Key_NoKey . raw )
return EventHandlerResult : : OK ;
return EventHandlerResult : : OK ;
if ( keyToggledOn ( keyState ) )
if ( keyToggledOn ( keyState ) )
@ -140,11 +130,11 @@ EventHandlerResult TapDance::onKeyswitchEvent(Key &mapped_key, byte row, byte co
uint8_t tap_dance_index = mapped_key . raw - ranges : : TD_FIRST ;
uint8_t tap_dance_index = mapped_key . raw - ranges : : TD_FIRST ;
if ( keyToggledOff ( keyState ) )
if ( keyToggledOff ( keyState ) )
bitClear( pressed_state_ , tap_dance_index ) ;
state_[ tap_dance_index ] . pressed = false ;
if ( ! isInSeq ( mapped_key ) ) {
if ( last_tap_dance_key_ . raw ! = mapped_key . raw ) {
if ( ! isActive ( ) ) {
if ( last_tap_dance_key_ . raw = = Key_NoKey . raw ) {
if ( bitRead( triggered_state_ , tap_dance_index ) ) {
if ( state_[ tap_dance_index ] . triggered ) {
if ( keyToggledOff ( keyState ) ) {
if ( keyToggledOff ( keyState ) ) {
release ( tap_dance_index ) ;
release ( tap_dance_index ) ;
}
}
@ -160,7 +150,7 @@ EventHandlerResult TapDance::onKeyswitchEvent(Key &mapped_key, byte row, byte co
return EventHandlerResult : : EVENT_CONSUMED ;
return EventHandlerResult : : EVENT_CONSUMED ;
} else {
} else {
if ( keyToggledOff ( keyState ) & & st illHeld( tap_dance_index ) ) {
if ( keyToggledOff ( keyState ) & & st ate_[ tap_dance_index ] . count ) {
release ( tap_dance_index ) ;
release ( tap_dance_index ) ;
return EventHandlerResult : : EVENT_CONSUMED ;
return EventHandlerResult : : EVENT_CONSUMED ;
}
}
@ -182,30 +172,30 @@ EventHandlerResult TapDance::onKeyswitchEvent(Key &mapped_key, byte row, byte co
last_tap_dance_key_ . raw = mapped_key . raw ;
last_tap_dance_key_ . raw = mapped_key . raw ;
last_tap_dance_row_ = row ;
last_tap_dance_row_ = row ;
last_tap_dance_col_ = col ;
last_tap_dance_col_ = col ;
bitSet( pressed_state_ , tap_dance_index ) ;
state_[ tap_dance_index ] . pressed = true ;
if ( keyToggledOn ( keyState ) ) {
if ( keyToggledOn ( keyState ) ) {
tap ( ) ;
tap ( ) ;
return EventHandlerResult : : EVENT_CONSUMED ;
return EventHandlerResult : : EVENT_CONSUMED ;
}
}
if ( bitRead( triggered_state_ , tap_dance_index ) )
if ( state_[ tap_dance_index ] . triggered )
tapDanceAction ( tap_dance_index , row , col , tap_coun t_[ tap_dance_index ] , Hold ) ;
tapDanceAction ( tap_dance_index , row , col , s tate _[ tap_dance_index ] . count , Hold ) ;
return EventHandlerResult : : EVENT_CONSUMED ;
return EventHandlerResult : : EVENT_CONSUMED ;
}
}
EventHandlerResult TapDance : : afterEachCycle ( ) {
EventHandlerResult TapDance : : afterEachCycle ( ) {
for ( uint8_t i = 0 ; i < 16 ; i + + ) {
for ( uint8_t i = 0 ; i < TAPDANCE_KEY_COUNT ; i + + ) {
if ( ! bitRead( release_next_state_ , i ) )
if ( ! state_[ i ] . release_next )
continue ;
continue ;
tapDanceAction ( i , last_tap_dance_row_ , last_tap_dance_col_ , tap_coun t_[ i ] , Release ) ;
tapDanceAction ( i , last_tap_dance_row_ , last_tap_dance_col_ , s tate _[ i ] . count , Release ) ;
tap_coun t_[ i ] = 0 ;
s tate _[ i ] . count = 0 ;
bitClear( release_next_state_ , i ) ;
state_[ i ] . release_next = false ;
}
}
if ( ! isActive ( ) )
if ( last_tap_dance_key_ . raw = = Key_NoKey . raw )
return EventHandlerResult : : OK ;
return EventHandlerResult : : OK ;
if ( end_time_ & & millis ( ) > end_time_ )
if ( end_time_ & & millis ( ) > end_time_ )