From cb1d860635ce329246b5a0faff8a6261739da68f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Wed, 7 Feb 2024 16:33:52 +0100 Subject: [PATCH] Cleanup PC entrypoint, remove MQTT CLI options --- src/dev/win32/hasp_win32.cpp | 1 + src/dev/win32/hasp_win32.h | 2 +- src/drv/tft/tft_driver_win32drv.cpp | 8 +- src/main_pc.cpp | 209 +++++++---------------- user_setups/win32/windows_gdi_64bits.ini | 4 +- user_setups/win32/windows_sdl_64bits.ini | 3 +- 6 files changed, 63 insertions(+), 164 deletions(-) diff --git a/src/dev/win32/hasp_win32.cpp b/src/dev/win32/hasp_win32.cpp index 46051804..213b8801 100644 --- a/src/dev/win32/hasp_win32.cpp +++ b/src/dev/win32/hasp_win32.cpp @@ -43,6 +43,7 @@ void Win32Device::show_info() LOG_VERBOSE(0, F("Processor : %s"), vendor); LOG_VERBOSE(0, F("CPU freq. : %i MHz"), get_cpu_frequency()); + LOG_VERBOSE(0, F("OS Version : %s"), get_core_version()); } const char* Win32Device::get_hostname() diff --git a/src/dev/win32/hasp_win32.h b/src/dev/win32/hasp_win32.h index 5e59ea73..e4b11746 100644 --- a/src/dev/win32/hasp_win32.h +++ b/src/dev/win32/hasp_win32.h @@ -43,7 +43,7 @@ class Win32Device : public BaseDevice { if(dwVersion < 0x80000000) dwBuild = (DWORD)(HIWORD(dwVersion)); char version[128]; - snprintf(version, sizeof(version), "Windows %d.%d-%d", dwMajorVersion, dwMinorVersion, dwBuild); + snprintf(version, sizeof(version), "Windows %d.%d.%d", dwMajorVersion, dwMinorVersion, dwBuild); _core_version = version; // _backlight_pin = -1; diff --git a/src/drv/tft/tft_driver_win32drv.cpp b/src/drv/tft/tft_driver_win32drv.cpp index 911cae2e..38babbb9 100644 --- a/src/drv/tft/tft_driver_win32drv.cpp +++ b/src/drv/tft/tft_driver_win32drv.cpp @@ -68,13 +68,7 @@ void TftWin32Drv::show_info() { splashscreen(); - unsigned long version = GetVersion(); - unsigned long major = LOBYTE(LOWORD(version)); - unsigned long minor = HIBYTE(LOWORD(version)); - unsigned long build = 0; - if(version < 0x80000000) build = HIWORD(version); - LOG_VERBOSE(TAG_TFT, F("Driver : Win32Drv")); - LOG_VERBOSE(TAG_TFT, F("Windows Version: %d.%d.%d"), major, minor, build); + LOG_VERBOSE(TAG_TFT, F("Driver : Win32Drv")); } void TftWin32Drv::splashscreen() diff --git a/src/main_pc.cpp b/src/main_pc.cpp index 6b2d0c83..5430b130 100644 --- a/src/main_pc.cpp +++ b/src/main_pc.cpp @@ -121,23 +121,19 @@ void setup() configSetup(); #endif - haspDevice.init(); // hardware setup - haspDevice.show_info(); // debug info + haspDevice.init(); // hardware setup // hal_setup(); guiSetup(); - LOG_DEBUG(TAG_MAIN, "%s %d", __FILE__, __LINE__); dispatchSetup(); // for hasp and oobe haspSetup(); #if HASP_USE_MQTT > 0 - LOG_DEBUG(TAG_MAIN, "%s %d", __FILE__, __LINE__); mqttSetup(); // Hasp must be running mqttStart(); #endif #if HASP_USE_GPIO > 0 - LOG_DEBUG(TAG_MAIN, "%s %d", __FILE__, __LINE__); gpioSetup(); #endif @@ -146,7 +142,6 @@ void setup() #endif mainLastLoopTime = millis(); // - 1000; // reset loop counter - LOG_DEBUG(TAG_MAIN, "%s %d", __FILE__, __LINE__); // delay(250); } @@ -191,7 +186,9 @@ void loop() if(mainLoopCounter == 0 || mainLoopCounter == 5) { haspDevice.loop_5s(); +#if HASP_USE_GPIO > 0 gpioEvery5Seconds(); +#endif #if HASP_USE_MQTT mqttEvery5Seconds(true); @@ -215,175 +212,86 @@ void loop() void usage(const char* progName, const char* version) { - std::cout << "\n\n" - << progName << " " << version << " [options]" << std::endl - << std::endl - << "Options:" << std::endl - << " -? | --help Print this help" << std::endl - << " -w | --width Width of the window" << std::endl - << " -h | --height Height of the window" << std::endl - << " --mqttname MQTT device name topic (default: computer hostname)" << std::endl - << " --mqtthost MQTT broker hostname or IP address" << std::endl - << " --mqttport MQTT broker port (default: 1883)" << std::endl - << " --mqttuser MQTT username" << std::endl - << " --mqttpass MQTT password" << std::endl - << " --mqttgroup MQTT groupname (default: plates)" << std::endl - << std::endl - // << " -t | --topic Base topic of the mqtt messages (default: hasp)" << std::endl - // << std::endl - // << " -f | --fullscreen Open the application fullscreen" << std::endl - // << " -v | --verbose Verbosity level" << std::endl - << std::endl; + std::cout + << "\n" + << progName << " " << version << " [options]" << std::endl + << std::endl + << "Options:" << std::endl + << " -h | --help Print this help" << std::endl + << " -W | --width Width of the window" << std::endl + << " -H | --height Height of the window" << std::endl + << " -C | --config Configuration directory (default: '~/.local/share/hasp' or 'AppData\\hasp\\hasp')" + << std::endl + << std::endl; fflush(stdout); -#if defined(WINDOWS) - static const char s[] = "\n"; - DWORD slen = lstrlen(s); - WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), s, slen, &slen, NULL); -#endif } int main(int argc, char* argv[]) { - bool showhelp = false; - int count; + bool showhelp = false; + bool console = true; + char config[PATH_MAX] = {'\0'}; #if defined(WINDOWS) InitializeConsoleOutput(); SetConsoleCP(65001); // 65001 = UTF-8 - static const char s[] = "tränenüberströmt™\n"; - DWORD slen = lstrlen(s); - WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), s, slen, &slen, NULL); - - HANDLE std_out = GetStdHandle(STD_OUTPUT_HANDLE); - if(std_out == INVALID_HANDLE_VALUE) { - return 66; - } - if(!WriteConsole(std_out, "Hello World!\n", 13, NULL, NULL)) { - return 67; - } #endif - char buf[4096]; // never know how much is needed - std::cout << "CWD: " << cwd(buf, sizeof buf) << std::endl; -#if USE_MONITOR - SDL_Init(0); // Needs to be initialized for GetPerfPath - cd(SDL_GetPrefPath("hasp", "hasp")); - SDL_Quit(); // We'll properly init later -#elif USE_WIN32DRV - if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, buf))) { - PathAppendA(buf, "hasp"); - PathAppendA(buf, "hasp"); - cd(buf); - } -#endif - std::cout << "CWD changed to: " << cwd(buf, sizeof buf) << std::endl; - - // Change to preferences dir - std::cout << "\nCommand-line arguments:\n"; - for(count = 0; count < argc; count++) - std::cout << " argv[" << count << "] " << argv[count] << std::endl << std::flush; - - StaticJsonDocument<1024> settings; - - for(count = 0; count < argc; count++) { - if(argv[count][0] == '-') { - - if(strncmp(argv[count], "--help", 6) == 0 || strncmp(argv[count], "-?", 2) == 0) { + for(int arg = 1; arg < argc; arg++) { + if(strncmp(argv[arg], "--help", 6) == 0 || strncmp(argv[arg], "-h", 2) == 0) { + showhelp = true; + } else if(strncmp(argv[arg], "--width", 7) == 0 || strncmp(argv[arg], "-W", 2) == 0) { + if(arg + 1 < argc) { + int w = atoi(argv[arg + 1]); + if(w > 0) tft_width = w; + arg++; + } else { + std::cout << "Missing width value" << std::endl; showhelp = true; } - - if(strncmp(argv[count], "--width", 7) == 0 || strncmp(argv[count], "-w", 2) == 0) { - int w = atoi(argv[count + 1]); - if(w > 0) tft_width = w; - } - - if(strncmp(argv[count], "--height", 8) == 0 || strncmp(argv[count], "-h", 2) == 0) { - int h = atoi(argv[count + 1]); + } else if(strncmp(argv[arg], "--height", 8) == 0 || strncmp(argv[arg], "-H", 2) == 0) { + if(arg + 1 < argc) { + int h = atoi(argv[arg + 1]); if(h > 0) tft_height = h; + arg++; + } else { + std::cout << "Missing height value" << std::endl; + showhelp = true; } - - if(strncmp(argv[count], "--mqttname", 10) == 0 || strncmp(argv[count], "-n", 2) == 0) { - std::cout << " argv[" << count << "] " << argv[count] << std::endl << std::flush; - fflush(stdout); - if(count + 1 < argc) { - haspDevice.set_hostname(argv[count + 1]); - settings["mqtt"]["name"] = argv[count + 1]; - } else { - showhelp = true; - } - } - - if(strncmp(argv[count], "--mqtthost", 10) == 0) { - std::cout << " argv[" << count << "] " << argv[count] << std::endl << std::flush; - fflush(stdout); - if(count + 1 < argc) { - settings["mqtt"]["host"] = argv[count + 1]; - } else { - showhelp = true; - } - } - - if(strncmp(argv[count], "--mqttport", 10) == 0) { - std::cout << " argv[" << count << "] " << argv[count] << std::endl << std::flush; - fflush(stdout); - if(count + 1 < argc) { - settings["mqtt"]["port"] = atoi(argv[count + 1]); - } else { - showhelp = true; - } - } - - if(strncmp(argv[count], "--mqttuser", 10) == 0) { - std::cout << " argv[" << count << "] " << argv[count] << std::endl << std::flush; - fflush(stdout); - if(count + 1 < argc) { - settings["mqtt"]["user"] = argv[count + 1]; - } else { - showhelp = true; - } - } - - if(strncmp(argv[count], "--mqttpass", 10) == 0) { - std::cout << " argv[" << count << "] " << argv[count] << std::endl << std::flush; - fflush(stdout); - if(count + 1 < argc) { - settings["mqtt"]["pass"] = argv[count + 1]; - } else { - showhelp = true; - } + } else if(strncmp(argv[arg], "--config", 8) == 0 || strncmp(argv[arg], "-C", 2) == 0) { + if(arg + 1 < argc) { + strcpy(config, argv[arg + 1]); + arg++; + } else { + std::cout << "Missing config directory" << std::endl; + showhelp = true; } + } else { + std::cout << "Unrecognized command line parameter: " << argv[arg] << std::endl; + showhelp = true; } } if(showhelp) { usage("openHASP", haspDevice.get_version()); - -#if defined(WINDOWS) - WriteConsole(std_out, "bye\n\n", 3, NULL, NULL); - std::cout << std::endl << std::flush; - fflush(stdout); - FreeConsole(); - exit(0); -#endif - return 0; + goto end; } - char buffer[2048]; - serializeJson(settings, buffer, sizeof(buffer)); - std::cout << buffer << std::endl << std::flush; - fflush(stdout); -#if HASP_USE_MQTT - mqttSetConfig(settings["mqtt"]); + if(config[0] == '\0') { +#if USE_MONITOR + SDL_Init(0); // Needs to be initialized for GetPerfPath + strcpy(config, SDL_GetPrefPath("hasp", "hasp")); + SDL_Quit(); // We'll properly init later +#elif USE_WIN32DRV + if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, config))) { + PathAppendA(config, "hasp"); + PathAppendA(config, "hasp"); + } #endif - // printf("%s %d\n", __FILE__, __LINE__); - // fflush(stdout); - - debugPrintHaspHeader(stdout); - LOG_INFO(TAG_MAIN, "resolution %d x %d", tft_width, tft_height); - LOG_INFO(TAG_MAIN, "pre setup"); + } + cd(config); setup(); - #if USE_MONITOR while(1) { loop(); @@ -395,14 +303,13 @@ int main(int argc, char* argv[]) } #endif +end: #if defined(WINDOWS) - WriteConsole(std_out, "bye\n\n", 3, NULL, NULL); std::cout << std::endl << std::flush; fflush(stdout); FreeConsole(); exit(0); #endif - return 0; } diff --git a/user_setups/win32/windows_gdi_64bits.ini b/user_setups/win32/windows_gdi_64bits.ini index 4330c02a..0344d2fc 100644 --- a/user_setups/win32/windows_gdi_64bits.ini +++ b/user_setups/win32/windows_gdi_64bits.ini @@ -1,7 +1,6 @@ [env:windows_gdi_64bits] platform = native@^1.1.4 extra_scripts = - tools/sdl2_build_extra.py tools/windows_build_extra.py build_flags = ${env.build_flags} @@ -65,13 +64,12 @@ build_flags = -lrpcrt4 -lcrypt32 -lmingw32 - -mwindows -lm -ldinput8 ;-ldxguid ;-ldxerr8 ;-luser32 - ;-lgdi32 + -lgdi32 -lwinmm -limm32 -lole32 diff --git a/user_setups/win32/windows_sdl_64bits.ini b/user_setups/win32/windows_sdl_64bits.ini index 30de808f..0caaccca 100644 --- a/user_setups/win32/windows_sdl_64bits.ini +++ b/user_setups/win32/windows_sdl_64bits.ini @@ -72,13 +72,12 @@ build_flags = -lmingw32 -lSDL2main -lSDL2 - -mwindows -lm -ldinput8 ;-ldxguid ;-ldxerr8 ;-luser32 - ;-lgdi32 + -lgdi32 -lwinmm -limm32 -lole32