From eb524225a5e8fca138792c32222c661d47cd9152 Mon Sep 17 00:00:00 2001 From: Christian Tacke Date: Fri, 21 Aug 2020 00:28:57 +0200 Subject: [PATCH] Tool to find key codes --- test/CMakeLists.txt | 12 +++- test/findcode.c | 156 ++++++++++++++++++++++++++++++++++++++++++++ test/test.c | 6 +- 3 files changed, 169 insertions(+), 5 deletions(-) create mode 100644 test/findcode.c diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 219a2eb..b1e6ac3 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,6 @@ add_executable(cmmk-test test.c) add_executable(cmmk-debug debug.c) +add_executable(cmmk-find-code findcode.c) find_package(HIDAPI QUIET COMPONENTS libusb) add_definitions(-DCMMK_TRACE) @@ -7,9 +8,11 @@ add_definitions(-DCMMK_TRACE) IF (WIN32) target_link_libraries(cmmk-test cmmk_shared) target_link_libraries(cmmk-debug cmmk_shared) + target_link_libraries(cmmk-find-code cmmk_shared) ELSE() target_link_libraries(cmmk-test cmmk_shared hidapi-hidraw) target_link_libraries(cmmk-debug cmmk_shared hidapi-hidraw) + target_link_libraries(cmmk-find-code cmmk_shared hidapi-hidraw) ENDIF() target_include_directories(cmmk-test PRIVATE @@ -20,11 +23,16 @@ target_include_directories(cmmk-debug PRIVATE ${libcmmk_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}) +target_include_directories(cmmk-find-code PRIVATE + ${libcmmk_SOURCE_DIR}/include + ${CMAKE_CURRENT_SOURCE_DIR}) + + IF (NOT WIN32) - install(TARGETS cmmk-test cmmk-debug + install(TARGETS cmmk-test cmmk-debug cmmk-find-code CONFIGURATIONS Debug RUNTIME DESTINATION bin/debug) - install(TARGETS cmmk-test cmmk-debug + install(TARGETS cmmk-test cmmk-debug cmmk-find-code CONFIGURATIONS Release RUNTIME DESTINATION bin/release) ENDIF() diff --git a/test/findcode.c b/test/findcode.c new file mode 100644 index 0000000..dcb517b --- /dev/null +++ b/test/findcode.c @@ -0,0 +1,156 @@ +/* + * This file is part of libcmmk. + * + * libcmmk is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * libcmmk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with libcmmk. If not, see . + */ +#include + +#ifdef WIN32 +#include +#else +#include /* usleep() */ +#include +#include /* do we need this? */ +#endif + +#include /* memset() */ +#include +#include + +int g_stop = 0; + +#ifdef WIN32 +BOOL WINAPI ctrl_handler ( DWORD ctrlType ) +{ + /* We only care about ctrl+c events. */ + if ( ctrlType == CTRL_C_EVENT ) { + g_stop = 1; + return TRUE; + } + return FALSE; +} +#else +static void interrupted ( int sig ) +{ + ( void ) sig; + + g_stop = 1; +} +#endif + +void sleep_ms ( int ms ) +{ +#ifdef WIN32 + Sleep ( ms ); +#elif _POSIX_C_SOURCE >= 199309L + struct timespec t; + t.tv_sec = ms / 1000; + t.tv_nsec = ( ms % 1000 ) * 1000000; + nanosleep ( &t, NULL ); +#else + usleep ( ms * 1000 ); +#endif +} + +int main ( int argc, char** argv ) +{ + ( void ) argc; + ( void ) argv; + + struct cmmk state = {0}; + + int product; + + if ( cmmk_find_device ( &product ) != 0 ) { + return 1; + } + printf ( "Product: %s\n", cmmk_product_to_str ( product ) ); + + if ( cmmk_attach ( &state, product, -1 ) != 0 ) { + return 1; + } + printf ( "Layout: %s\n", cmmk_layout_to_str ( state.layout ) ); + +#ifdef WIN32 + SetConsoleCtrlHandler ( ctrl_handler, TRUE ); +#else + signal ( SIGINT, interrupted ); +#endif + + g_stop = 0; + + struct rgb offcolor = MKRGB ( 0x000000 ); + struct rgb color1 = MKRGB ( 0x00ff00 ); + struct rgb color2 = MKRGB ( 0xff0000 ); + cmmk_set_control_mode ( &state, CMMK_MANUAL ); + cmmk_set_all_single ( &state, &offcolor ); + + int max = 200; + int start = 0; + int c; + + if ( argc > 1 ) { + start = atoi ( argv[1] ); + } + if ( argc > 2 ) { + max = atoi ( argv[2] ); + } + + if ( start > max ) { + fprintf ( stderr, "Start (%i) has to be smaller than max (%i) value\n", start, max ); + } + + int step = ( max-start+1 ) /2; + while ( start < max ) { + fprintf ( stdout, "Checking from %i to %i\n", start, max -1 ); + for ( int i = start; i < max; ++i ) { + if ( i < start + step ) { + //first half + cmmk_set_single_key_by_id ( &state, i, &color1 ); + } else { + //second half + cmmk_set_single_key_by_id ( &state, i, &color2 ); + } + } + fprintf ( stdout, "Is your key illuminated in green? (%i to %i)? [y/N] ", start, start + step - 1 ); + fflush ( stdout ); + do { + c = getchar(); + } while ( !g_stop && c != 'y' && c != 'Y' && c != 'n' && c != 'N' ); + if ( g_stop ) { + goto finish; + } + if ( c == 'y' || c == 'Y' ) { + max = start + step; + fprintf ( stdout, "Got it\n" ); + } else { + //next interval + start += step; + fprintf ( stdout, "It is in the other half! \n" ); + } + cmmk_set_all_single ( &state, &offcolor ); + //half the interval + if ( step == 1 ) { + break; + } + step = ( step + 1 ) / 2; + }; + fprintf ( stdout, "\nYour key is %i\n", start ); + +finish: + cmmk_set_control_mode ( &state, CMMK_FIRMWARE ); + cmmk_detach ( &state ); + + return 0; +} diff --git a/test/test.c b/test/test.c index 849a4a9..9f76662 100644 --- a/test/test.c +++ b/test/test.c @@ -103,9 +103,9 @@ int test_multilayer(struct cmmk *dev) cmmk_set_control_mode(dev, CMMK_EFFECT); cmmk_switch_multilayer(dev, 1); - cmmk_set_effect_fully_lit(dev, &f); - cmmk_set_effect_wave(dev, &w); - cmmk_set_effect_raindrops(dev, &r); + cmmk_set_effect_fully_lit(dev, &f); + cmmk_set_effect_wave(dev, &w); + cmmk_set_effect_raindrops(dev, &r); cmmk_switch_multilayer(dev, 0); cmmk_set_multilayer_map(dev, &map);