X-Git-Url: http://git.smho.de/gw/?p=owTools.git;a=blobdiff_plain;f=src%2Fmain.cpp;fp=src%2Fmain.cpp;h=7c28e1216dd2a3801956ed0a5deb65db58c024cd;hp=c36dd8883011d9151771597a9432ec3b8b23c35c;hb=1a6465a924428af072a8eb5e75ee547c394f4d8e;hpb=039b202e5c68834801d23e22eecc7cae2879ea83 diff --git a/src/main.cpp b/src/main.cpp index c36dd88..7c28e12 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,26 +30,34 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#include "owTools.h" +#ifdef LINUX #include #include "mySensorDB.h" -#include -#include -#include #include -#include /* For O_RDWR */ #include #include #include +#endif + +#ifdef WIN +#include "Windows.h" +#include "time.h" +#endif + +#include +#include +#include +#include /* For O_RDWR */ #include -#include "owTools.h" #include "hexfile.h" #include #include #include #include - +#ifdef LINUX #define RB "\e(0\x6a\e(B" // 188 Right Bottom corner #define RT "\e(0\x6b\e(B" // 187 Right Top corner @@ -63,7 +71,21 @@ #define TC "\e(0\x77\e(B" // 203 Top Cross #define VL "\e(0\x78\e(B" // 186 Vertical Line #define SP " " // space string - +#endif +#ifdef WIN +#define RB " "// 188 Right Bottom corner +#define RT " "// 187 Right Top corner +#define LT " "// 201 Left Top cornet +#define LB " "// 200 Left Bottom corner +#define MC " " // 206 Midle Cross +#define HL " "// 205 Horizontal Line +#define LC " "// 204 Left Cross +#define RC " "// 185 Right Cross +#define BC " "// 202 Bottom Cross +#define TC " "// 203 Top Cross +#define VL " "// 186 Vertical Line +#define SP " " // space string +#endif void printhelp() { printf("owTools - Programm for reading und controlling 1-Wire Devices from www.tm3d.de\n\n"); @@ -137,17 +159,17 @@ int getArgi(std::string flag) { return atoi(getArg(flag).c_str()); } - +#ifdef LINUX int database=0; mySensorDB *sdb=NULL; - - +#endif +/* snum_t getArgsnum(std::string flag) { snum_t snum; snum.num=0; return snum; } - +*/ int findCPU(std::string cpu) { std::ifstream fileInput; std::string line; @@ -192,7 +214,7 @@ void continuous(std::vector *devices,int intervall,int headline,int s for (owDevice* dev :*devices) { if (dev->configstate!=OWDCS_NONE) { for (size_t i=0;i<4;i++) { - if (dev->config->getPropertyID(i)!=0) + if (dev->config->getPropertyID((uint8_t)i)!=0) printf("\033[1;34m%02X.%02X%02X\033[0m\t",dev->getNum().byte[7],dev->getNum().byte[1],dev->getNum().byte[0]); } } @@ -201,8 +223,8 @@ void continuous(std::vector *devices,int intervall,int headline,int s for (owDevice* dev :*devices) { if (dev->configstate!=OWDCS_NONE) { for (size_t i=0;i<4;i++) { - if (dev->config->getPropertyID(i)!=0) - printf("\033[0;36m%s\033[0m\t",dev->config->getQuantity(i).substr(0,7).c_str()); + if (dev->config->getPropertyID((uint8_t)i)!=0) + printf("\033[0;36m%s\033[0m\t",dev->config->getQuantity((uint8_t)i).substr(0,7).c_str()); } } } @@ -210,10 +232,10 @@ void continuous(std::vector *devices,int intervall,int headline,int s for (owDevice* dev :*devices) { if (dev->configstate!=OWDCS_NONE) { for (size_t i=0;i<4;i++) { - if (dev->config->getPropertyID(i)!=0) { - size_t l=utf8_length(dev->config->getUnit(i)); + if (dev->config->getPropertyID((uint8_t)i)!=0) { + size_t l=utf8_length(dev->config->getUnit((uint8_t)i)); std::string ls=" "; - printf("\033[3;34m[%s]%s\033[0m\t",dev->config->getUnit(i).c_str(),ls.substr(0,5-l).c_str()); + printf("\033[3;34m[%s]%s\033[0m\t",dev->config->getUnit((uint8_t)i).c_str(),ls.substr(0,5-l).c_str()); } } } @@ -221,19 +243,26 @@ void continuous(std::vector *devices,int intervall,int headline,int s } printf("\n"); - +#ifdef LINUX if (database) { //owi->log->setLogLevel(0); for (owDevice* dev : *devices) { sdb->createDeviceTable(dev); } } +#endif } } time_t t=time(NULL); int st=(int)t; +#ifdef LINUX struct tm tm = *localtime(&t); +#endif +#ifdef WIN + struct tm tm; + localtime_s(&tm,&t); +#endif printf("%d-%02d-%02d %02d:%02d:%02d\t", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); for (owDevice* dev :*devices) { for(int k=0;kmaxrepeat;k++){ @@ -244,7 +273,7 @@ void continuous(std::vector *devices,int intervall,int headline,int s if (owi->log->last()configstate!=OWDCS_NONE) { for (size_t i=0;i<4;i++) { - if (dev->config->getPropertyID(i)!=0) { + if (dev->config->getPropertyID((int8_t)i)!=0) { if (dev->values[i]<=-10) printf("%0.2f \t",dev->values[i]); else if (dev->values[i]<0) @@ -261,9 +290,11 @@ void continuous(std::vector *devices,int intervall,int headline,int s } } } +#ifdef LINUX if (database) { sdb->insertValues(dev); } +#endif } else { owi->log->setLogLevel(OWLOG_INFO); owi->log->set(OWLOG_ERROR,"Too many errors, mybee conection is dead."); @@ -273,7 +304,14 @@ void continuous(std::vector *devices,int intervall,int headline,int s } printf("\n"); - while (((int)time(NULL))<(st+intervall)) sleep(1); + while (((int)time(NULL)) < (st + intervall)) { +#ifdef LINUX + sleep(1); +#endif +#ifdef WIN + Sleep(1000); +#endif + } } } @@ -290,14 +328,14 @@ void device_menu(owDevice* d) { printf("Values info: "); int tm3d=d->readConfig(); for (size_t i=0;i<4;i++) { - printf("%s in %s",d->config->getQuantity(i).c_str(),d->config->getUnit(i).c_str()); + printf("%s in %s",d->config->getQuantity((uint8_t)i).c_str(),d->config->getUnit((uint8_t)i).c_str()); if (i<4) printf("; "); } if (tm3d) printf(" (tm3d.de)"); else printf(" (default)"); printf("\n"); d->convertAll(); for (size_t i=0;ivalues.size();i++) { - printf("\033[1;33m%0.4f %s\033[0m ",d->values[i],d->config->getUnit(i).c_str()); + printf("\033[1;33m%0.4f %s\033[0m ",d->values[i],d->config->getUnit((uint8_t)i).c_str()); } printf("\n"); for(int i=0;i<70;i++) printf(HL);printf("\n"); @@ -405,19 +443,13 @@ int main(int argc, char *argv[]) { std::transform(adapter.begin(), adapter.end(),adapter.begin(), ::toupper); std::string s; - +#ifdef LINUX if(adapter.find("COM")!=std::string::npos) { owi=new owCOMInterface(); int port=atoi(adapter.substr(adapter.find("COM")+3).c_str()); printf("Open /dev/ttyS%i\n",port); owi->InitAdapter(port); } else - if(adapter.find("ARDUINO")!=std::string::npos) { - owi=new owARDUINOInterface(); - int port=atoi(adapter.substr(adapter.find("ARDUINO")+7).c_str()); - printf("Open /dev/ttyS%i\n",port); - owi->InitAdapter(port); - } else if(adapter.find("USB")!=std::string::npos) { owi=new owUSBInterface(); int port=atoi(adapter.substr(adapter.find("USB")+3).c_str()); @@ -443,12 +475,49 @@ int main(int argc, char *argv[]) { } else { printf("\033[1;31mGPIO works with Raspberry PI only \033[0m\n"); } - } - if (owi==NULL) { + } else +#endif + if (adapter.find("ARDUINO") != std::string::npos) { + owi = new owARDUINOInterface(); + int port = atoi(adapter.substr(adapter.find("ARDUINO") + 7).c_str()); + printf("Open /dev/ttyS%i\n", port); + owi->InitAdapter(port); + } + +#ifdef WIN + else + if (adapter.find("USB") != std::string::npos) { + printf("USB \n"); + owi = new owTMEXWIN(); + int port = atoi(adapter.substr(adapter.find("USB") + 3).c_str()); + printf("Open the %i. USB Adapter\n", port); + int err; + char cs[20]; + sprintf_s(cs, 20, "{%i,6}", port); + if ((err = owi->InitAdapter(cs))==0) { + printf("ERROR Init USB Adapter\n"); + exit(0); + } + } + else + if (adapter.find("COM") != std::string::npos) { + printf("USB \n"); + owi = new owTMEXWIN(); + int port = atoi(adapter.substr(adapter.find("COM") + 3).c_str()); + printf("Open Adapter on COM%i\n", port); + int err; + char cs[20]; + sprintf_s(cs, 20, "{%i,5}", port); + if ((err = owi->InitAdapter(cs))==0) { + printf("ERROR Init Serial Adapter\n"); + exit(0); + } + } +#endif + if (owi == NULL) { printf("No 1-Wiremaster found\n"); return 0; } - setLogMode(); @@ -466,6 +535,7 @@ int main(int argc, char *argv[]) { if ((s=getArg("p")) !="") { pause=atoi(s.c_str()); } else pause=30; +#ifdef LINUX if ((s=getArg("d")) !="") { reload=1; printf("Use Database\n"); @@ -482,7 +552,8 @@ int main(int argc, char *argv[]) { exit(1); } database=1; - } + } +#endif continuous(&(owi->devices),pause,1,reload); @@ -529,7 +600,12 @@ int main(int argc, char *argv[]) { if ((getArg("g"))=="1") { printf("get ID from Server\n"); char s[255]; +#ifdef LINUX sprintf(s,"wget -q http://www.tm3d.de/shop99/owid.php?fam=%02X -O id.txt",snum.byte[0]); +#endif +#ifdef WIN + sprintf_s(s, "wget -q http://www.tm3d.de/shop99/owid.php?fam=%02X -O id.txt", snum.byte[0]); +#endif int err; if ((err=system(s))==0) { printf("OK!\n"); @@ -546,7 +622,7 @@ int main(int argc, char *argv[]) { if (c=='x') br=1; } else { if ((c==',')|(c=='}')) { - isnum.byte[i]=strtol(s, NULL, 16); + isnum.byte[i]=(uint8_t)strtol(s, NULL, 16); //printf("%x\n",strtol(s, NULL, 16)); i++; br=0; @@ -619,7 +695,12 @@ int main(int argc, char *argv[]) { colm.push_back(""); } else { char hs[50]; +#ifdef LINUX sprintf(hs,"%0.3f",dev->values[i]); +#endif +#ifdef WIN + sprintf_s(hs, "%0.3f", dev->values[i]); +#endif colm.push_back(hs); } if (max