qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

via_test.c (3729B)


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