Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MacOS Compilable

  • Loading branch information...
commit cda56aafbbaf6f3cad79227651a5c3ad2177896e 1 parent 2268369
@ysuga ysuga authored
View
71 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 <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
}
@@ -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
}
View
13 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;
-}
+}
View
BIN  include/Roomba.h
Binary file not shown
View
8 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
+#endif
View
22 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 <windows.h>
#define THREAD_ROUTINE DWORD WINAPI
#else
-
+#include <pthread.h>
+#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.
- *******************************************************/
+ *******************************************************/
View
2  src/Roomba.cpp
@@ -702,4 +702,4 @@ unsigned short Roomba::getLeftEncoderCounts()
unsigned short buf;
RequestSensor(RIGHT_ENCODER_COUNTS, &buf);
return buf;
-}
+}
View
2  src/SerialPort.cpp
@@ -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
26 src/Thread.cpp
@@ -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,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
}
Please sign in to comment.
Something went wrong with that request. Please try again.