📄 lcdprocinterface.cpp
字号:
/* * Roadnav * LCDprocInterface.cpp * * Copyright (c) 2004 - 2007 Richard L. Lynch <rllynch@users.sourceforge.net> * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License * as published by the Free Software Foundation. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ ///////////////////////////////////////////////////////////////////////////////// \file////// Provides functions for interfacing to an LCDproc server./////////////////////////////////////////////////////////////////////////////////#ifdef HAVE_CONFIG_H# include <config.h>#endif#ifdef _MSC_VER#pragma warning(disable: 4786)#pragma warning(disable: 4800)#endif#include <wx/wx.h>#include <wx/socket.h>#include "App.h"#include "LCDprocInterface.h"#include "LCDprocSettings.h"using namespace std;static vector<wxString> g_vLCDprocVariableNames;static LCDprocSettings g_cLCDprocSettings;static wxSocketClient g_sockLCDproc;//////////////////////////////////////////////////////////////////////////////////// \brief Mangle a variable name (as provided to LCDprocSetText) into/// the internal name used in g_vLCDprocVariableNames and g_cLCDprocSettings.////// Right now, this just prepends and appends square brackets onto the/// variable name./////////////////////////////////////////////////////////////////////////////////wxString MangleVariableName(wxString strName){ return wxT("[") + strName + wxT("]");}//////////////////////////////////////////////////////////////////////////////////// \brief Obtain a copy of the current LCDproc settings./////////////////////////////////////////////////////////////////////////////////LCDprocSettings GetLCDprocSettings(){ LCDprocSettings cRtn; g_pConfig->SetPath(wxT("LCDproc")); cRtn.Read(); g_pConfig->SetPath(wxT("..")); return cRtn;}//////////////////////////////////////////////////////////////////////////////////// \brief Change the LCDproc settings to the provided settings and restart/// the LCDproc code./////////////////////////////////////////////////////////////////////////////////void SetLCDprocSettings(LCDprocSettings cSettings){ g_pConfig->DeleteGroup(wxT("LCDproc")); g_pConfig->SetPath(wxT("LCDproc")); cSettings.Write(); g_pConfig->SetPath(wxT("..")); LCDprocCleanup(); LCDprocInit(); }//////////////////////////////////////////////////////////////////////////////////// \brief Returns a list of the variable names the LCDproc interface code/// knows about./////////////////////////////////////////////////////////////////////////////////vector<wxString> GetLCDprocVariableNames(){ return g_vLCDprocVariableNames;}//////////////////////////////////////////////////////////////////////////////////// \brief Internal function used to send the supplied text to the LCDproc/// server./////////////////////////////////////////////////////////////////////////////////void SendLCDprocMessage(char * pszMsg){ if (g_sockLCDproc.IsConnected()) { g_sockLCDproc.Write(pszMsg, strlen(pszMsg)); if (g_sockLCDproc.WaitForRead(1, 0)) { char szBuf[256]; g_sockLCDproc.Read(szBuf, 256); } }}//////////////////////////////////////////////////////////////////////////////////// \brief Initialize the LCDproc code.////// \todo May hang Roadnav for a little while if the LCDproc server is /// unreachable. Force shorter timeout./////////////////////////////////////////////////////////////////////////////////bool LCDprocInit(){ wxIPV4address sAddr; char szMsg[1024]; unsigned int iScreen; // Load settings g_cLCDprocSettings = GetLCDprocSettings(); // Load variable names g_vLCDprocVariableNames.clear(); g_vLCDprocVariableNames.push_back(MangleVariableName(wxT("GPS Speed"))); g_vLCDprocVariableNames.push_back(MangleVariableName(wxT("GPS Full Address"))); g_vLCDprocVariableNames.push_back(MangleVariableName(wxT("GPS Street"))); g_vLCDprocVariableNames.push_back(MangleVariableName(wxT("GPS City"))); g_vLCDprocVariableNames.push_back(MangleVariableName(wxT("GPS State"))); g_vLCDprocVariableNames.push_back(MangleVariableName(wxT("GPS Heading"))); g_vLCDprocVariableNames.push_back(MangleVariableName(wxT("Directions - Next Step"))); g_vLCDprocVariableNames.push_back(MangleVariableName(wxT("Directions - Line 1"))); g_vLCDprocVariableNames.push_back(MangleVariableName(wxT("Directions - Line 2"))); g_vLCDprocVariableNames.push_back(MangleVariableName(wxT("Directions - Line 3"))); g_vLCDprocVariableNames.push_back(MangleVariableName(wxT("Directions - Line 4"))); // Stop if LCDproc is disabled if (!g_cLCDprocSettings.m_bLCDprocEnabled) return false; // Parse address sAddr.Hostname(g_cLCDprocSettings.m_strHostAndPort.BeforeFirst(wxT(':'))); sAddr.Service(atoi(g_cLCDprocSettings.m_strHostAndPort.AfterFirst(wxT(':')).mb_str(*wxConvCurrent))); // Try to connect g_sockLCDproc.Connect(sAddr, true); // Abort if not connected. if (!g_sockLCDproc.IsConnected()) { ::wxMessageBox(wxString::Format(wxT("Error connecting to LCDproc server (%s)!"), g_cLCDprocSettings.m_strHostAndPort.c_str()), wxT("Error"), wxOK); return true; } // Send greeting sprintf(szMsg, "hello\n"); SendLCDprocMessage(szMsg); // Configure client settings sprintf(szMsg, "client_set -name Roadnav\n"); SendLCDprocMessage(szMsg); // Set up each screen for (iScreen = 0; iScreen < g_cLCDprocSettings.m_vScreens.size(); iScreen++) { unsigned int iWidget; sprintf(szMsg, "screen_add %d\n", iScreen); SendLCDprocMessage(szMsg); // Set up each widget for (iWidget = 0; iWidget < g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets.size(); iWidget++) { char * pszType = NULL; char szSetup[1024]; *szSetup = 0; if (g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_strType == wxT("Text")) { wxString strValue = wxT(""); pszType = "string"; if (g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_strValue.Left(1) != wxT("[")) strValue = g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_strValue; sprintf(szSetup, "widget_set %d %d %d %d \"%s\"\n", iScreen, iWidget, g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_iCol, g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_iRow, static_cast<const char *>(strValue.mb_str(*wxConvCurrent)) ); } if (g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_strType == wxT("Title")) { wxString strValue = wxT(""); pszType = "title"; if (g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_strValue.Left(1) != wxT("[")) strValue = g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_strValue; sprintf(szSetup, "widget_set %d %d \"%s\"\n", iScreen, iWidget, static_cast<const char *>(strValue.mb_str(*wxConvCurrent)) ); } if (g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_strType == wxT("Horizontal Bar")) { pszType = "hbar"; sprintf(szSetup, "widget_set %d %d %d %d %d\n", iScreen, iWidget, g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_iCol, g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_iRow, 80 ); } if (g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_strType == wxT("Vertical Bar")) { pszType = "vbar"; sprintf(szSetup, "widget_set %d %d %d %d %d\n", iScreen, iWidget, g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_iCol, g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_iRow, 80 ); } if (pszType) { sprintf(szMsg, "widget_add %d %d %s\n", iScreen, iWidget, pszType); SendLCDprocMessage(szMsg); } if (*szSetup) SendLCDprocMessage(szSetup); } } return false;}//////////////////////////////////////////////////////////////////////////////////// \brief Change the value of one of the LCDproc variables./////////////////////////////////////////////////////////////////////////////////bool LCDprocSetText(wxString strName, wxString strValue){ unsigned int iScreen; wxString strMangled; strMangled = MangleVariableName(strName); #ifdef __WXDEBUG__ // Ensure the variable name is listed in g_vLCDprocVariableNames unsigned int i; for (i = 0; i < g_vLCDprocVariableNames.size() && g_vLCDprocVariableNames[i] != MangleVariableName(strName); i++); wxASSERT(i < g_vLCDprocVariableNames.size());#endif // Strip CRs and LFs strValue.Replace(wxT("\r\n"), wxT(" ")); strValue.Replace(wxT("\r"), wxT(" ")); strValue.Replace(wxT("\n"), wxT(" ")); // Update the variable on each screen for (iScreen = 0; iScreen < g_cLCDprocSettings.m_vScreens.size(); iScreen++) { unsigned int iWidget; // Check each widget for (iWidget = 0; iWidget < g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets.size(); iWidget++) { if (g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_strType == wxT("Text")) { if (g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_strValue == strMangled) { char szMsg[1024]; sprintf(szMsg, "widget_set %d %d %d %d \"%s\"\n", iScreen, iWidget, g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_iCol, g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_iRow, static_cast<const char *>(strValue.mb_str(*wxConvCurrent)) ); SendLCDprocMessage(szMsg); } } if (g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_strType == wxT("Title")) { if (g_cLCDprocSettings.m_vScreens[iScreen].m_vWidgets[iWidget].m_strValue == strMangled) { char szMsg[1024]; sprintf(szMsg, "widget_set %d %d \"%s\"\n", iScreen, iWidget, static_cast<const char *>(strValue.mb_str(*wxConvCurrent)) ); SendLCDprocMessage(szMsg); } } } } return false;}//////////////////////////////////////////////////////////////////////////////////// \brief Clean up the LCDproc code - free memory and close the connection/// to the LCDproc server./////////////////////////////////////////////////////////////////////////////////void LCDprocCleanup(){ g_vLCDprocVariableNames.clear(); g_sockLCDproc.Close(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -