Permalink
Browse files

MacOS Compilable

  • Loading branch information...
1 parent 2268369 commit cda56aafbbaf6f3cad79227651a5c3ad2177896e @ysuga ysuga committed May 5, 2012
Showing with 125 additions and 19 deletions.
  1. +71 −0 example/YConsole.h
  2. +10 −3 example/demo.cpp
  3. BIN include/Roomba.h
  4. +4 −4 include/RoombaException.h
  5. +15 −7 include/Thread.h
  6. +1 −1 src/Roomba.cpp
  7. +1 −1 src/SerialPort.cpp
  8. +23 −3 src/Thread.cpp
View
@@ -1,44 +1,115 @@
#ifndef YCONSOLE_HEADER_INCLUDED
#define YCONSOLE_HEADER_INCLUDED
+enum {
+ UP = 65536,
+ DOWN,
+ LEFT,
+ RIGHT,
+ SPACE,
+ ESCAPE,
+};
+
+
#ifdef __cplusplus
namespace net {
namespace ysuga {
#endif
#ifdef WIN32
#include <conio.h>
+#else
+#include <termios.h>
+ 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
}
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
}
View
@@ -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;
-}
+}
View
Binary file not shown.
@@ -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
+#endif
View
@@ -22,14 +22,19 @@
#define LIBTHREAD_API __declspec(dllimport)
#endif
+#else
+#define LIBTHREAD_API
+
+
#endif // ifdef WIN32
#ifdef WIN32
#include <windows.h>
#define THREAD_ROUTINE DWORD WINAPI
#else
-
+#include <pthread.h>
+#define THREAD_ROUTINE void*
#endif
@@ -41,22 +46,24 @@ namespace net {
#ifdef WIN32
HANDLE m_Handle;
#else
-
+ pthread_mutex_t m_Handle;
#endif
public:
Mutex() {
#ifdef WIN32
m_Handle = ::CreateMutex(NULL, 0, NULL);
+#else
+ pthread_mutex_init(&m_Handle, NULL);
#endif
}
virtual ~Mutex() {
#ifdef WIN32
::CloseHandle(m_Handle);
#else
-
+ pthread_mutex_destroy(&m_Handle);
#endif
}
@@ -65,15 +72,16 @@ namespace net {
#ifdef WIN32
::WaitForSingleObject(m_Handle, INFINITE);
#else
-
+
+ pthread_mutex_lock(&m_Handle);
#endif
}
void Unlock() {
#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.
- *******************************************************/
+ *******************************************************/
View
@@ -702,4 +702,4 @@ unsigned short Roomba::getLeftEncoderCounts()
unsigned short buf;
RequestSensor(RIGHT_ENCODER_COUNTS, &buf);
return buf;
-}
+}
View
@@ -20,7 +20,7 @@
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
-#include <termio.h>
+#include <termios.h>
#include <errno.h>
#include <signal.h>
#define _POSIX_SOURCE 1
View
@@ -1,5 +1,13 @@
+
#include "Thread.h"
+#ifndef WIN32
+#include <time.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <errno.h>
+#endif
+
using namespace net::ysuga;
Thread::Thread(void)
@@ -17,24 +25,36 @@ 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;
}
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
}

0 comments on commit cda56aa

Please sign in to comment.