keycontrol_ll_encoding.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #ifndef KEYCONTROL_ENCODING_H
  2. #define KEYCONTROL_ENCODING_H
  3. #include <stdint.h> // uint32_t
  4. // -----------------------------------------------------------------------------
  5. // KeySwitch LowLevel Control Word Encoding in 32-bit unsigned integer
  6. // -----------------------------------------------------------------------------
  7. // There two modes supported - parallel control and serial control.
  8. // Only one mode can be selected in single template.
  9. //
  10. #define CONTROL_TYPE_REGISTER_1 (1UL<<31)
  11. #define CONTROL_TYPE_REGISTER_2 (1UL<<30)
  12. #define CONTROL_TYPE_REGISTER_1_2 (3UL<<30)
  13. #define CONTROL_TYPE_PARALLEL (0UL<<30)
  14. // -----------------------------------------------------------------------------
  15. // Check if the control code uses parallel control
  16. //
  17. #define CHECK_CONTROL_TYPE_PARALLEL( u32code ) (! ((u32code) & CONTROL_TYPE_REGISTER_1_2 ))
  18. // Check if the control code uses serial control
  19. //
  20. #define CHECK_CONTROL_TYPE_REGISTER( u32code ) ( ((u32code) & CONTROL_TYPE_REGISTER_1_2 ))
  21. // -----------------------------------------------------------------------------
  22. #define CHECK_CONTROL_REGISTER_1( u32code ) ( ((u32code) & CONTROL_TYPE_REGISTER_1 ))
  23. #define CHECK_CONTROL_REGISTER_2( u32code ) ( ((u32code) & CONTROL_TYPE_REGISTER_2 ))
  24. // -----------------------------------------------------------------------------
  25. // Extract the Register 1 control code
  26. //
  27. #define GET_CONTROL_REGISTER_1( u32code ) ( ((u32code) & 0x00FF ) >> 0 )
  28. // Extract the Register 2 control code
  29. //
  30. #define GET_CONTROL_REGISTER_2( u32code ) ( ((u32code) & 0xFF00 ) >> 8 )
  31. // Extract the ENABLE contol pin state
  32. //
  33. #define GET_CONTROL_ENABLEPIN( u32code ) ( ((u32code) & 0x10000 ))
  34. // Packing the control word into the 32-bit unsigned integer
  35. //
  36. #define MAKE_KEYSTATE_CONTROL( TYPE, A_8bit, B_8bit, C_8bit, D_6bit ) ((uint32_t)( \
  37. ((A_8bit) & 0x000000FF) << 0\
  38. |\
  39. ((B_8bit) & 0x000000FF) << 8\
  40. |\
  41. ((C_8bit) & 0x000000FF) << 16\
  42. |\
  43. ((D_6bit) & 0x0000007F) << 24\
  44. |\
  45. ((TYPE) & 0x80000000) \
  46. ))
  47. // Create KeySwitch control template using serial control (Register 1)
  48. //
  49. #define MAKE_KEYSTATE_REGISTER_1( REG1, EN ) \
  50. MAKE_KEYSTATE_CONTROL( CONTROL_TYPE_REGISTER_1, \
  51. ((REG1)&0xFF),\
  52. 0x00,\
  53. ((EN)&0x1),\
  54. 0x00 )
  55. // Create KeySwitch control template using serial control (Register 2)
  56. //
  57. #define MAKE_KEYSTATE_REGISTER_2( REG2, EN ) \
  58. MAKE_KEYSTATE_CONTROL( CONTROL_TYPE_REGISTER_2, \
  59. 0x00,\
  60. ((REG2)&0xFF),\
  61. ((EN)&0x1),\
  62. 0x00 )
  63. // Create KeySwitch control template using serial control (Registers 1 and 2)
  64. //
  65. #define MAKE_KEYSTATE_REGISTER_1_2( REG1, REG2, EN ) \
  66. MAKE_KEYSTATE_CONTROL( CONTROL_TYPE_REGISTER_1_2, \
  67. ((REG1)&0xFF),\
  68. ((REG2)&0xFF),\
  69. ((EN)&0x1),\
  70. 0x00 )
  71. // Create KeySwitch control template using parallel control
  72. //
  73. #define MAKE_KEYSTATE_PARALLEL( REG1, REG2, REG3, REG4 ) \
  74. MAKE_KEYSTATE_CONTROL( CONTROL_TYPE_PARALLEL, \
  75. ((REG1)&0xFF),\
  76. ((REG2)&0xFF),\
  77. ((REG3)&0xFF),\
  78. ((REG4)&0x3F) )
  79. #endif