Skip to content

Commit

Permalink
Merge pull request #5 from tmpvar/osx-support
Browse files Browse the repository at this point in the history
add support for OSX
  • Loading branch information
chamnit committed May 26, 2015
2 parents c74758d + c675cd6 commit 1a89ea1
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 0 deletions.
Binary file added EEPROM.DAT
Binary file not shown.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# along with Grbl. If not, see <http://www.gnu.org/licenses/>.

# PLATFORM = WINDOWS
# PLATFORM = OSX
PLATFORM = LINUX

#The original grbl code, except those files overriden by sim
Expand All @@ -39,6 +40,7 @@ VALIDATOR_NAME = gvalidate.exe
FLAGS = -g -O3
COMPILE = $(CC) -Wall $(FLAGS) -DF_CPU=$(CLOCK) -include config.h -I. -DPLAT_$(PLATFORM)
LINUX_LIBRARIES = -lrt -pthread
OSX_LIBRARIES =
WINDOWS_LIBRARIES =

# symbolic targets:
Expand Down
1 change: 1 addition & 0 deletions deps/libuv
2 changes: 2 additions & 0 deletions platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#ifdef PLAT_LINUX
#include "platform_linux.h"
#elif PLAT_OSX
#include "platform_osx.h"
#else
#include "platform_windows.h"
#endif
Expand Down
146 changes: 146 additions & 0 deletions platform_OSX.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
platform_LINUX.c - linux specific functions with generic cross-platform interface
Part of Grbl Simulator
Copyright (c) 2014 Adam Shelly
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <time.h>
#include <sys/time.h>
#include "platform.h"

#define MS_PER_SEC 1000000

//any platform-specific setup that must be done before sim starts here
clock_serv_t cclock;

void platform_init() {
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);


mach_timebase_info(&timebase);
conversion_factor = (double)timebase.numer / (double)timebase.denom;
}

//cleanup int here;
void platform_terminate()
{
mach_port_deallocate(mach_task_self(), cclock);
}

//returns a free-running 32 bit nanosecond counter which rolls over
uint32_t platform_ns()
{
static uint32_t gTimeBase = 0;
static uint32_t timestamp;

mach_timespec_t ts;

clock_get_time(cclock, &ts);

timestamp = ts.tv_sec*1e9+ts.tv_nsec;
if (gTimeBase== 0){gTimeBase=timestamp;}
return timestamp - gTimeBase;;
}

//sleep in microseconds
void platform_sleep(long microsec)
{
struct timespec ts={0};
while (microsec >= MS_PER_SEC){
ts.tv_sec++;
microsec-=MS_PER_SEC;
}
ts.tv_nsec = microsec*1000;
nanosleep(&ts,NULL);
}

#define SIM_ECHO_TERMINAL 1 //use this to make grbl_sim act like a serial terminal with local echo on.

//set terminal to allow kbhit detection
void enable_kbhit(int dir)
{
static struct termios oldt, newt;

if ( dir == 1 )
{
tcgetattr( STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~( ICANON );
if (!SIM_ECHO_TERMINAL) {
newt.c_lflag &= ~( ECHO );
}
tcsetattr( STDIN_FILENO, TCSANOW, &newt);
}
else
tcsetattr( STDIN_FILENO, TCSANOW, &oldt);
}

//detect key pressed
int kbhit (void)
{
struct timeval tv={0};
fd_set rdfs={{0}};
int retval;

/* tv.tv_sec = 0; */
/* tv.tv_usec = 0; */

/* FD_ZERO(&rdfs); */
FD_SET (STDIN_FILENO, &rdfs);

select(STDIN_FILENO+1, &rdfs, NULL, NULL, &tv);
retval = FD_ISSET(STDIN_FILENO, &rdfs);

return retval;
}

plat_thread_t* platform_start_thread(plat_threadfunc_t threadfunc) {
plat_thread_t* th = malloc(sizeof(plat_thread_t));
if (pthread_create(&th->tid, NULL, threadfunc, &th->exit)){
free(th);
return NULL;
}
return th;
}

//ask thread to exit nicely, wait
void platform_stop_thread(plat_thread_t* th){
th->exit = 1;
pthread_join(th->tid,NULL);
}

//force-kill thread
void platform_kill_thread(plat_thread_t* th){
th->exit = 1;
pthread_cancel(th->tid);
}

//return char if one available.
uint8_t platform_poll_stdin() {
uint8_t char_in=0;
enable_kbhit(1);
if ( kbhit()) {
char_in = getchar();
}
enable_kbhit(0);
return char_in;
}
21 changes: 21 additions & 0 deletions platform_osx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifdef platform_h
#error "platform implementation already defined"
#else

#include <pthread.h>
#include <mach/mach_time.h>

mach_timebase_info_data_t timebase;
double conversion_factor;

typedef struct {
pthread_t tid;
int exit;
} plat_thread_t;

typedef void*(*plat_threadfunc_t)(void*);
#define PLAT_THREAD_FUNC(name,arg) void* name(void* arg)

#define PLATFORM_EXTRA_CR '\r'

#endif

0 comments on commit 1a89ea1

Please sign in to comment.