diff --git a/example/YConsole.h b/example/YConsole.h index cba6639..3416a89 100644 --- a/example/YConsole.h +++ b/example/YConsole.h @@ -1,6 +1,16 @@ #ifndef YCONSOLE_HEADER_INCLUDED #define YCONSOLE_HEADER_INCLUDED +enum { + UP = 65536, + DOWN, + LEFT, + RIGHT, + SPACE, + ESCAPE, +}; + + #ifdef __cplusplus namespace net { namespace ysuga { @@ -8,30 +18,56 @@ namespace net { #ifdef WIN32 #include +#else +#include + struct termios m_oldTermios; #endif static void init_scr() { #ifdef WIN32 system("cls"); +#else + #endif } static void clear_scr() { #ifdef WIN32 system("cls"); +#else + system("clear"); #endif } static void exit_scr() { #ifdef WIN32 system("cls"); +#else + struct termios myTermios; + tcgetattr(fileno(stdin), &m_oldTermios); + tcgetattr(fileno(stdin), &myTermios); + + myTermios.c_cc[VTIME] = 0; +#ifdef linux + myTermios.c_cc[VMIN] = 0; +#else // MacOS + myTermios.c_cc[VMIN] = 1; +#endif + myTermios.c_lflag &= (~ECHO & ~ICANON); + tcsetattr(fileno(stdin), TCSANOW, &myTermios); #endif } static int myKbhit() { #ifdef WIN32 return _kbhit(); +#else + int key = getchar(); + if(key == -1 || key == 0) { + return 1; + } + return 0; #endif } @@ -39,6 +75,41 @@ static int myKbhit() { static int myGetch() { #ifdef WIN32 return _getch(); +#else + int keys[5] = {-1, -1, -1, -1, -1}; + int key = getchar(); + switch(key) { + case -1: + case 0: + return -1; + + case ' ': + return SPACE; + case 27: + key = getchar(); + switch(key) { + case -1: + return ESCAPE; + case 79: + key = getchar(); + return key; + case '[': + for(int i = 0;i < 5;i++) { + if(key == -1 || key == 27) break; + keys[i] = key = getchar(); + } + ungetc(key, stdin); + + switch(keys[0]) { + case 65: return UP; + case 66: return DOWN; + case 67: return RIGHT; + case 68: return LEFT; + default: return keys[0]; + } + } + } + return key; #endif } diff --git a/example/demo.cpp b/example/demo.cpp index 9ed9b0c..a9b45c4 100644 --- a/example/demo.cpp +++ b/example/demo.cpp @@ -7,13 +7,20 @@ using namespace net::ysuga; using namespace net::ysuga::roomba; +void usage() { + std::cout << "USAGE: demo /dev/ttyUSB0" << std::endl; +} -int main(void) { +int main(const int argc, const char* argv[]) { + if(argc != 2) { + usage(); + return 0; + } try { bool endflag = false; init_scr(); - Roomba roomba("\\\\.\\COM16"); + Roomba roomba(argv[1]); roomba.safeControl(); roomba.runAsync(); @@ -199,4 +206,4 @@ int main(void) { std::cout << "Hit Enter Key to Exit." << std::endl; getchar(); return 0; -} \ No newline at end of file +} diff --git a/include/Roomba.h b/include/Roomba.h index a4b5638..d8ec4ad 100644 Binary files a/include/Roomba.h and b/include/Roomba.h differ diff --git a/include/RoombaException.h b/include/RoombaException.h index 25027c2..882deb8 100644 --- a/include/RoombaException.h +++ b/include/RoombaException.h @@ -21,12 +21,12 @@ namespace net { this->m_msg = msg; } - virtual ~RoombaException() { + virtual ~RoombaException() throw() { } public: - const char* what() const { + const char* what() const throw() { return m_msg.c_str(); } }; @@ -40,7 +40,7 @@ namespace net { PreconditionNotMetError() : RoombaException("Pre-Condition Not Met") { } - ~PreconditionNotMetError() { + ~PreconditionNotMetError() throw() { } }; @@ -49,4 +49,4 @@ namespace net { } } }; -#endif \ No newline at end of file +#endif diff --git a/include/Thread.h b/include/Thread.h index 17e1940..a81596c 100644 --- a/include/Thread.h +++ b/include/Thread.h @@ -22,6 +22,10 @@ #define LIBTHREAD_API __declspec(dllimport) #endif +#else +#define LIBTHREAD_API + + #endif // ifdef WIN32 @@ -29,7 +33,8 @@ #include #define THREAD_ROUTINE DWORD WINAPI #else - +#include +#define THREAD_ROUTINE void* #endif @@ -41,7 +46,7 @@ namespace net { #ifdef WIN32 HANDLE m_Handle; #else - + pthread_mutex_t m_Handle; #endif @@ -49,6 +54,8 @@ namespace net { Mutex() { #ifdef WIN32 m_Handle = ::CreateMutex(NULL, 0, NULL); +#else + pthread_mutex_init(&m_Handle, NULL); #endif } @@ -56,7 +63,7 @@ namespace net { #ifdef WIN32 ::CloseHandle(m_Handle); #else - + pthread_mutex_destroy(&m_Handle); #endif } @@ -65,7 +72,8 @@ namespace net { #ifdef WIN32 ::WaitForSingleObject(m_Handle, INFINITE); #else - + + pthread_mutex_lock(&m_Handle); #endif } @@ -73,7 +81,7 @@ namespace net { #ifdef WIN32 ::ReleaseMutex(m_Handle); #else - + pthread_mutex_unlock(&m_Handle); #endif } }; @@ -85,7 +93,7 @@ namespace net { HANDLE m_Handle; DWORD m_ThreadId; #else - + pthread_t m_Handle; #endif public: LIBTHREAD_API Thread(void); @@ -112,4 +120,4 @@ namespace net { /******************************************************* * Copyright 2010, ysuga.net all rights reserved. - *******************************************************/ \ No newline at end of file + *******************************************************/ diff --git a/src/Roomba.cpp b/src/Roomba.cpp index 33da63b..7eb133c 100644 --- a/src/Roomba.cpp +++ b/src/Roomba.cpp @@ -702,4 +702,4 @@ unsigned short Roomba::getLeftEncoderCounts() unsigned short buf; RequestSensor(RIGHT_ENCODER_COUNTS, &buf); return buf; -} \ No newline at end of file +} diff --git a/src/SerialPort.cpp b/src/SerialPort.cpp index 91766b9..6700773 100644 --- a/src/SerialPort.cpp +++ b/src/SerialPort.cpp @@ -20,7 +20,7 @@ #include #include #include -#include +#include #include #include #define _POSIX_SOURCE 1 diff --git a/src/Thread.cpp b/src/Thread.cpp index 8c08f12..d5ab119 100644 --- a/src/Thread.cpp +++ b/src/Thread.cpp @@ -1,5 +1,13 @@ + #include "Thread.h" +#ifndef WIN32 +#include +#include +#include +#include +#endif + using namespace net::ysuga; Thread::Thread(void) @@ -17,11 +25,11 @@ THREAD_ROUTINE StartRoutine(void* arg) { Thread* threadObject = (Thread*)arg; threadObject->Run(); - + threadObject->Exit(0); #ifdef WIN32 ExitThread(0); #else - + pthread_exit(0); #endif return 0; } @@ -29,12 +37,24 @@ THREAD_ROUTINE StartRoutine(void* arg) void Thread::Start() { +#ifdef WIN32 m_Handle = CreateThread(NULL, 0, StartRoutine, (LPVOID)this, 0, &m_ThreadId); +#else + int ret = pthread_create(&m_Handle, NULL, StartRoutine, (void*)this); + if(ret != 0) { + perror("pthread_create"); + } +#endif } void Thread::Join() { +#ifdef WIN32 WaitForSingleObject(m_Handle, INFINITE); +#else + void* retval; + pthread_join(m_Handle, &retval); +#endif } void Thread::Sleep(unsigned long milliSeconds) @@ -53,6 +73,6 @@ void Thread::Exit(unsigned long exitCode) { #ifdef WIN32 ExitThread(exitCode); #else - + pthread_exit(0); #endif }