comparison Macro/PartialMap/macro.c @ 361:7c6ac7b88cda

Working support for Interconnect - Supports up to 255 slave nodes (you'll run into ScanCode limitations before then) - Requires most recent kll compiler update - Additional debugging output and stats counters - Noise and parity checking - Fixed TxFIFO issue when sending buffers larger than the FIFO - Cleaned up defaultMap.kll - Added ScanCode caching (reduces interconnect traffic significantly) - Interconnect module code is conditionally compiled into PartialMap module if required
author Jacob Alexander <haata@kiibohd.com>
date Sat, 15 Aug 2015 21:53:59 -0700
parents dbefb68411e1
children e4be15c39cce
comparison
equal deleted inserted replaced
360:dbefb68411e1 361:7c6ac7b88cda
154 // Pending Result Macro Index List 154 // Pending Result Macro Index List
155 // * Any result macro that needs processing from a previous macro processing loop 155 // * Any result macro that needs processing from a previous macro processing loop
156 uint16_t macroResultMacroPendingList[ ResultMacroNum ] = { 0 }; 156 uint16_t macroResultMacroPendingList[ ResultMacroNum ] = { 0 };
157 uint16_t macroResultMacroPendingListSize = 0; 157 uint16_t macroResultMacroPendingListSize = 0;
158 158
159 // Interconnect ScanCode Cache
160 #if defined(ConnectEnabled_define)
161 // TODO This can be shrunk by the size of the max node 0 ScanCode
162 TriggerGuide macroInterconnectCache[ MaxScanCode ];
163 uint8_t macroInterconnectCacheSize = 0;
164 #endif
165
159 166
160 167
161 // ----- Capabilities ----- 168 // ----- Capabilities -----
162 169
163 // Sets the given layer with the specified layerState 170 // Sets the given layer with the specified layerState
422 } 429 }
423 430
424 // Otherwise no defined Trigger Macro 431 // Otherwise no defined Trigger Macro
425 erro_msg("Scan Code has no defined Trigger Macro: "); 432 erro_msg("Scan Code has no defined Trigger Macro: ");
426 printHex( scanCode ); 433 printHex( scanCode );
434 print( NL );
427 return 0; 435 return 0;
428 } 436 }
429 437
430 438
431 // Update the scancode using a list of TriggerGuides 439 // Add an interconnect ScanCode
432 // TODO Handle led state and analog 440 // These are handled differently (less information is sent, hold/off states must be assumed)
433 inline void Macro_triggerState( void *triggers, uint8_t num ) 441 #if defined(ConnectEnabled_define)
434 { 442 inline void Macro_interconnectAdd( void *trigger_ptr )
435 // Copy each of the TriggerGuides to the TriggerListBuffer 443 {
436 for ( uint8_t c = 0; c < num; c++ ) 444 TriggerGuide *trigger = (TriggerGuide*)trigger_ptr;
437 macroTriggerListBuffer[ macroTriggerListBufferSize++ ] = ((TriggerGuide*)triggers)[ c ]; 445
438 } 446 // Error checking
447 uint8_t error = 0;
448 switch ( trigger->type )
449 {
450 case 0x00: // Normal key
451 switch ( trigger->state )
452 {
453 case 0x00:
454 case 0x01:
455 case 0x02:
456 case 0x03:
457 break;
458 default:
459 erro_print("Invalid key state");
460 error = 1;
461 break;
462 }
463 break;
464
465 // Invalid TriggerGuide type
466 default:
467 erro_print("Invalid type");
468 error = 1;
469 break;
470 }
471
472 // Display TriggerGuide
473 if ( error )
474 {
475 printHex( trigger->type );
476 print(" ");
477 printHex( trigger->state );
478 print(" ");
479 printHex( trigger->scanCode );
480 print( NL );
481 return;
482 }
483
484 // Add trigger to the Interconnect Cache
485 // During each processing loop, a scancode may be re-added depending on it's state
486 for ( uint8_t c = 0; c < macroInterconnectCacheSize; c++ )
487 {
488 // Check if the same ScanCode
489 if ( macroInterconnectCache[ c ].scanCode == trigger->scanCode )
490 {
491 // Update the state
492 macroInterconnectCache[ c ].state = trigger->state;
493 return;
494 }
495 }
496
497 // If not in the list, add it
498 macroInterconnectCache[ macroInterconnectCacheSize++ ] = *trigger;
499 }
500 #endif
439 501
440 502
441 // Update the scancode key state 503 // Update the scancode key state
442 // States: 504 // States:
443 // * 0x00 - Off 505 // * 0x00 - Off
445 // * 0x02 - Held 507 // * 0x02 - Held
446 // * 0x03 - Released 508 // * 0x03 - Released
447 // * 0x04 - Unpressed (this is currently ignored) 509 // * 0x04 - Unpressed (this is currently ignored)
448 inline void Macro_keyState( uint8_t scanCode, uint8_t state ) 510 inline void Macro_keyState( uint8_t scanCode, uint8_t state )
449 { 511 {
512 #if defined(ConnectEnabled_define)
513 // Only compile in if a Connect node module is available
514 if ( !Connect_master )
515 {
516 // ScanCodes are only added if there was a state change (on/off)
517 switch ( state )
518 {
519 case 0x00: // Off
520 case 0x02: // Held
521 return;
522 }
523 }
524 #endif
525
450 // Only add to macro trigger list if one of three states 526 // Only add to macro trigger list if one of three states
451 switch ( state ) 527 switch ( state )
452 { 528 {
453 case 0x01: // Pressed 529 case 0x01: // Pressed
454 case 0x02: // Held 530 case 0x02: // Held
468 // * 0x01 - Released 544 // * 0x01 - Released
469 // * 0x02-0xFF - Analog value (low to high) 545 // * 0x02-0xFF - Analog value (low to high)
470 inline void Macro_analogState( uint8_t scanCode, uint8_t state ) 546 inline void Macro_analogState( uint8_t scanCode, uint8_t state )
471 { 547 {
472 // Only add to macro trigger list if non-off 548 // Only add to macro trigger list if non-off
549 // TODO Handle change for interconnect
473 if ( state != 0x00 ) 550 if ( state != 0x00 )
474 { 551 {
475 macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode; 552 macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = scanCode;
476 macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state; 553 macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
477 macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x02; // Analog key 554 macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x02; // Analog key
485 // * 0x00 - Off 562 // * 0x00 - Off
486 // * 0x01 - On 563 // * 0x01 - On
487 inline void Macro_ledState( uint8_t ledCode, uint8_t state ) 564 inline void Macro_ledState( uint8_t ledCode, uint8_t state )
488 { 565 {
489 // Only add to macro trigger list if non-off 566 // Only add to macro trigger list if non-off
567 // TODO Handle change for interconnect
490 if ( state != 0x00 ) 568 if ( state != 0x00 )
491 { 569 {
492 macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = ledCode; 570 macroTriggerListBuffer[ macroTriggerListBufferSize ].scanCode = ledCode;
493 macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state; 571 macroTriggerListBuffer[ macroTriggerListBufferSize ].state = state;
494 macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x01; // LED key 572 macroTriggerListBuffer[ macroTriggerListBufferSize ].type = 0x01; // LED key
905 uint8_t latch_expire = macroTriggerListBuffer[ key ].state == 0x03; 983 uint8_t latch_expire = macroTriggerListBuffer[ key ].state == 0x03;
906 984
907 // Lookup Trigger List 985 // Lookup Trigger List
908 nat_ptr_t *triggerList = Macro_layerLookup( &macroTriggerListBuffer[ key ], latch_expire ); 986 nat_ptr_t *triggerList = Macro_layerLookup( &macroTriggerListBuffer[ key ], latch_expire );
909 987
988 // If there was an error during lookup, skip
989 if ( triggerList == 0 )
990 continue;
991
910 // Number of Triggers in list 992 // Number of Triggers in list
911 nat_ptr_t triggerListSize = triggerList[0]; 993 nat_ptr_t triggerListSize = triggerList[0];
912 994
913 // Iterate over triggerList to see if any TriggerMacros need to be added 995 // Iterate over triggerList to see if any TriggerMacros need to be added
914 // First item is the number of items in the TriggerList 996 // First item is the number of items in the TriggerList
951 // If this is a interconnect slave node, send all scancodes to master node 1033 // If this is a interconnect slave node, send all scancodes to master node
952 if ( !Connect_master ) 1034 if ( !Connect_master )
953 { 1035 {
954 if ( macroTriggerListBufferSize > 0 ) 1036 if ( macroTriggerListBufferSize > 0 )
955 { 1037 {
956 dbug_msg("Yuh"); 1038 Connect_send_ScanCode( Connect_id, macroTriggerListBuffer, macroTriggerListBufferSize );
957 printHex( macroTriggerListBufferSize );
958 print( NL );
959 //Connect_send_ScanCode( Connect_id, macroTriggerListBuffer, macroTriggerListBufferSize );
960 macroTriggerListBufferSize = 0; 1039 macroTriggerListBufferSize = 0;
961 } 1040 }
962 return; 1041 return;
963 } 1042 }
964 #endif 1043 #endif
965 1044
966 // Only do one round of macro processing between Output Module timer sends 1045 // Only do one round of macro processing between Output Module timer sends
967 if ( USBKeys_Sent != 0 ) 1046 if ( USBKeys_Sent != 0 )
968 return; 1047 return;
1048
1049 #if defined(ConnectEnabled_define)
1050 // Check if there are any ScanCodes in the interconnect cache to process
1051 if ( Connect_master && macroInterconnectCacheSize > 0 )
1052 {
1053 // Iterate over all the cache ScanCodes
1054 uint8_t currentInterconnectCacheSize = macroInterconnectCacheSize;
1055 macroInterconnectCacheSize = 0;
1056 for ( uint8_t c = 0; c < currentInterconnectCacheSize; c++ )
1057 {
1058 // Add to the trigger list
1059 macroTriggerListBuffer[ macroTriggerListBufferSize++ ] = macroInterconnectCache[ c ];
1060
1061 // TODO Handle other TriggerGuide types (e.g. analog)
1062 switch ( macroInterconnectCache[ c ].type )
1063 {
1064 // Normal (Press/Hold/Release)
1065 case 0x00:
1066 // Decide what to do based on the current state
1067 switch ( macroInterconnectCache[ c ].state )
1068 {
1069 // Re-add to interconnect cache in hold state
1070 case 0x01: // Press
1071 //case 0x02: // Hold // XXX Why does this not work? -HaaTa
1072 macroInterconnectCache[ c ].state = 0x02;
1073 macroInterconnectCache[ macroInterconnectCacheSize++ ] = macroInterconnectCache[ c ];
1074 break;
1075 case 0x03: // Remove
1076 break;
1077 // Otherwise, do not re-add
1078 }
1079 }
1080 }
1081 }
1082 #endif
969 1083
970 // If the pause flag is set, only process if the step counter is non-zero 1084 // If the pause flag is set, only process if the step counter is non-zero
971 if ( macroPauseMode ) 1085 if ( macroPauseMode )
972 { 1086 {
973 if ( macroStepCounter == 0 ) 1087 if ( macroStepCounter == 0 )