logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

via_test.c (3729B)


  1. // Copyright 2022 Jason Williams (@wilba)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. // This is a test harness for VIA custom UI.
  4. //
  5. // It handles channel IDs 0-7, value IDs 0-7.
  6. //
  7. // It's useful for testing custom UI on a PCB without compiling in
  8. // features, especially features that will cause firmware to freeze
  9. // if the PCB doesn't have support.
  10. //
  11. // To use:
  12. // - add `SRC = keyboards/wilba_tech/via_test.c` to rules.mk
  13. // - add `#define VIA_EEPROM_CUSTOM_CONFIG_SIZE 128` to config.h
  14. // (or change to match CHANNELS*VALUES*2)
  15. #include "via.h"
  16. #ifdef VIA_ENABLE
  17. #define CHANNELS 8
  18. #define VALUES 8
  19. uint8_t g_value[CHANNELS][VALUES][2];
  20. void values_init(void)
  21. {
  22. for ( uint8_t channel_id = 0; channel_id < CHANNELS; channel_id++ ) {
  23. for ( uint8_t value_id = 0; value_id < VALUES; value_id++ ) {
  24. g_value[channel_id][value_id][0] = 0x00;
  25. g_value[channel_id][value_id][1] = 0x00;
  26. }
  27. }
  28. }
  29. void values_load(void)
  30. {
  31. eeprom_read_block( g_value, ((void*)VIA_EEPROM_CUSTOM_CONFIG_ADDR), VIA_EEPROM_CUSTOM_CONFIG_SIZE );
  32. }
  33. void values_save(void)
  34. {
  35. eeprom_update_block( g_value, ((void*)VIA_EEPROM_CUSTOM_CONFIG_ADDR), VIA_EEPROM_CUSTOM_CONFIG_SIZE );
  36. }
  37. // We do this to test if VIA is sending save commands per channel
  38. // Not relevant for real situations
  39. void values_save_on_channel(uint8_t channel_id)
  40. {
  41. uint16_t offset = channel_id * VALUES * 2;
  42. eeprom_update_block( ((void*)g_value) + offset,
  43. ((void*)VIA_EEPROM_CUSTOM_CONFIG_ADDR) + offset,
  44. VALUES * 2 );
  45. }
  46. void via_init_kb(void)
  47. {
  48. values_init();
  49. // If the EEPROM has the magic, the data is good.
  50. // OK to load from EEPROM
  51. if (via_eeprom_is_valid()) {
  52. values_load();
  53. } else {
  54. values_save();
  55. // DO NOT set EEPROM valid here, let caller do this
  56. }
  57. }
  58. void set_value( uint8_t channel_id, uint8_t *data )
  59. {
  60. // data = [ value_id, value_data ]
  61. uint8_t *value_id = &(data[0]);
  62. uint8_t *value_data = &(data[1]);
  63. if ( *value_id >= 0 && *value_id < VALUES ) {
  64. g_value[channel_id][*value_id][0] = value_data[0];
  65. g_value[channel_id][*value_id][1] = value_data[1];
  66. }
  67. }
  68. void get_value( uint8_t channel_id, uint8_t *data )
  69. {
  70. // data = [ value_id, value_data ]
  71. uint8_t *value_id = &(data[0]);
  72. uint8_t *value_data = &(data[1]);
  73. if ( *value_id >= 0 && *value_id < VALUES ) {
  74. value_data[0] = g_value[channel_id][*value_id][0];
  75. value_data[1] = g_value[channel_id][*value_id][1];
  76. }
  77. }
  78. void via_custom_value_command(uint8_t *data, uint8_t length) {
  79. // data = [ command_id, channel_id, value_id, value_data ]
  80. uint8_t *command_id = &(data[0]);
  81. uint8_t *channel_id = &(data[1]);
  82. uint8_t *value_id_and_data = &(data[2]);
  83. if ( *channel_id >= 0 && *channel_id < CHANNELS ) {
  84. switch ( *command_id )
  85. {
  86. case id_custom_set_value:
  87. {
  88. set_value(*channel_id,value_id_and_data);
  89. break;
  90. }
  91. case id_custom_get_value:
  92. {
  93. get_value(*channel_id,value_id_and_data);
  94. break;
  95. }
  96. case id_custom_save:
  97. {
  98. //for ( uint8_t i=0; i<CHANNELS; i++) {
  99. values_save_on_channel(*channel_id);
  100. //}
  101. break;
  102. }
  103. default:
  104. {
  105. // Unhandled message.
  106. *command_id = id_unhandled;
  107. break;
  108. }
  109. }
  110. return;
  111. }
  112. else {
  113. *command_id = id_unhandled;
  114. }
  115. // DO NOT call raw_hid_send(data,length) here, let caller do this
  116. }
  117. #endif // VIA_ENABLE