From b14db404b55424ef783a163a02da8868120b36c9 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 30 Nov 2016 11:33:44 -0500 Subject: [PATCH] pyhelper: Move helper functions from serialqueue.c to new file Signed-off-by: Kevin O'Connor --- klippy/chelper.py | 4 +-- klippy/pyhelper.c | 62 ++++++++++++++++++++++++++++++++++++++++++++ klippy/pyhelper.h | 9 +++++++ klippy/serialqueue.c | 60 +----------------------------------------- 4 files changed, 74 insertions(+), 61 deletions(-) create mode 100644 klippy/pyhelper.c create mode 100644 klippy/pyhelper.h diff --git a/klippy/chelper.py b/klippy/chelper.py index cca91b43..7fb8fd98 100644 --- a/klippy/chelper.py +++ b/klippy/chelper.py @@ -7,9 +7,9 @@ import os, logging import cffi COMPILE_CMD = "gcc -Wall -g -O -shared -fPIC -o %s %s" -SOURCE_FILES = ['stepcompress.c', 'serialqueue.c'] +SOURCE_FILES = ['stepcompress.c', 'serialqueue.c', 'pyhelper.c'] DEST_LIB = "c_helper.so" -OTHER_FILES = ['list.h', 'serialqueue.h'] +OTHER_FILES = ['list.h', 'serialqueue.h', 'pyhelper.h'] defs_stepcompress = """ struct stepcompress *stepcompress_alloc(uint32_t max_error diff --git a/klippy/pyhelper.c b/klippy/pyhelper.c new file mode 100644 index 00000000..3de9c2e8 --- /dev/null +++ b/klippy/pyhelper.c @@ -0,0 +1,62 @@ +// Helper functions for C / Python interface +// +// Copyright (C) 2016 Kevin O'Connor +// +// This file may be distributed under the terms of the GNU GPLv3 license. + +#include // errno +#include // uint8_t +#include // fprintf +#include // strerror +#include // gettimeofday +#include // struct timespec +#include "pyhelper.h" // get_time + +// Return the current system time as a double +double +get_time(void) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.; +} + +// Fill a 'struct timespec' with a system time stored in a double +struct timespec +fill_time(double time) +{ + time_t t = time; + return (struct timespec) {t, (time - t)*1000000000. }; +} + +// Report 'errno' in a message written to stderr +void +report_errno(char *where, int rc) +{ + int e = errno; + fprintf(stderr, "Got error %d in %s: (%d)%s\n", rc, where, e, strerror(e)); +} + +// Return a hex character for a given number +#define GETHEX(x) ((x) < 10 ? '0' + (x) : 'e' + (x) - 10) + +// Translate a binary string into an ASCII string with escape sequences +char * +dump_string(char *outbuf, int outbuf_size, char *inbuf, int inbuf_size) +{ + char *outend = &outbuf[outbuf_size-5], *o = outbuf; + uint8_t *inend = (void*)&inbuf[inbuf_size], *p = (void*)inbuf; + while (p < inend && o < outend) { + uint8_t c = *p++; + if (c > 31 && c < 127 && c != '\\') { + *o++ = c; + continue; + } + *o++ = '\\'; + *o++ = 'x'; + *o++ = GETHEX(c >> 4); + *o++ = GETHEX(c & 0x0f); + } + *o = '\0'; + return outbuf; +} diff --git a/klippy/pyhelper.h b/klippy/pyhelper.h new file mode 100644 index 00000000..ccf16ab8 --- /dev/null +++ b/klippy/pyhelper.h @@ -0,0 +1,9 @@ +#ifndef PYHELPER_H +#define PYHELPER_H + +double get_time(void); +struct timespec fill_time(double time); +void report_errno(char *where, int rc); +char *dump_string(char *outbuf, int outbuf_size, char *inbuf, int inbuf_size); + +#endif // pyhelper.h diff --git a/klippy/serialqueue.c b/klippy/serialqueue.c index 0700ecc3..6274e277 100644 --- a/klippy/serialqueue.c +++ b/klippy/serialqueue.c @@ -12,7 +12,6 @@ // clock times, prioritizes commands, and handles retransmissions. A // background thread is launched to do this work and minimize latency. -#include // errno #include // ceil #include // poll #include // pthread_mutex_lock @@ -21,70 +20,13 @@ #include // snprintf #include // malloc #include // memset -#include // gettimeofday -#include // struct timespec #include // tcflush #include // pipe #include "list.h" // list_add_tail +#include "pyhelper.h" // get_time #include "serialqueue.h" // struct queue_message -/**************************************************************** - * Helper functions - ****************************************************************/ - -// Return the current system time as a double -static double -get_time(void) -{ - struct timeval tv; - gettimeofday(&tv, NULL); - return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.; -} - -#if 0 -// Fill a 'struct timespec' with a system time stored in a double -struct timespec -fill_time(double time) -{ - time_t t = time; - return (struct timespec) {t, (time - t)*1000000000. }; -} -#endif - -// Report 'errno' in a message written to stderr -void -report_errno(char *where, int rc) -{ - int e = errno; - fprintf(stderr, "Got error %d in %s: (%d)%s\n", rc, where, e, strerror(e)); -} - -// Return a hex character for a given number -#define GETHEX(x) ((x) < 10 ? '0' + (x) : 'e' + (x) - 10) - -// Translate a binary string into an ASCII string with escape sequences -char * -dump_string(char *outbuf, int outbuf_size, uint8_t *inbuf, int inbuf_size) -{ - char *outend = &outbuf[outbuf_size-5], *o = outbuf; - uint8_t *inend = &inbuf[inbuf_size], *p = inbuf; - while (p < inend && o < outend) { - uint8_t c = *p++; - if (c > 31 && c < 127 && c != '\\') { - *o++ = c; - continue; - } - *o++ = '\\'; - *o++ = 'x'; - *o++ = GETHEX(c >> 4); - *o++ = GETHEX(c & 0x0f); - } - *o = '\0'; - return outbuf; -} - - /**************************************************************** * Poll reactor ****************************************************************/