Skip to content

Commit

Permalink
MacOS Compilable
Browse files Browse the repository at this point in the history
  • Loading branch information
ysuga committed May 5, 2012
1 parent 2268369 commit cda56aa
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 19 deletions.
71 changes: 71 additions & 0 deletions example/YConsole.h
@@ -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
}

Expand Down
13 changes: 10 additions & 3 deletions example/demo.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -199,4 +206,4 @@ int main(void) {
std::cout << "Hit Enter Key to Exit." << std::endl;
getchar();
return 0;
}
}
Binary file modified include/Roomba.h
Binary file not shown.
8 changes: 4 additions & 4 deletions include/RoombaException.h
Expand Up @@ -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();
}
};
Expand All @@ -40,7 +40,7 @@ namespace net {
PreconditionNotMetError() : RoombaException("Pre-Condition Not Met") {
}

~PreconditionNotMetError() {
~PreconditionNotMetError() throw() {
}
};

Expand All @@ -49,4 +49,4 @@ namespace net {
}
}
};
#endif
#endif
22 changes: 15 additions & 7 deletions include/Thread.h
Expand Up @@ -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


Expand All @@ -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
}

Expand All @@ -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
}
};
Expand All @@ -85,7 +93,7 @@ namespace net {
HANDLE m_Handle;
DWORD m_ThreadId;
#else

pthread_t m_Handle;
#endif
public:
LIBTHREAD_API Thread(void);
Expand All @@ -112,4 +120,4 @@ namespace net {

/*******************************************************
* Copyright 2010, ysuga.net all rights reserved.
*******************************************************/
*******************************************************/
2 changes: 1 addition & 1 deletion src/Roomba.cpp
Expand Up @@ -702,4 +702,4 @@ unsigned short Roomba::getLeftEncoderCounts()
unsigned short buf;
RequestSensor(RIGHT_ENCODER_COUNTS, &buf);
return buf;
}
}
2 changes: 1 addition & 1 deletion src/SerialPort.cpp
Expand Up @@ -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
Expand Down
26 changes: 23 additions & 3 deletions 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)
Expand All @@ -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)
Expand All @@ -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.