diff --git a/src/Makefile.am b/src/Makefile.am index c006144..e608b4f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -43,6 +43,7 @@ libdivecomputer_la_SOURCES = \ uwatec_memomouse.h uwatec_memomouse.c \ oceanic.h \ oceanic_atom2.h oceanic_atom2.c \ + ringbuffer.h ringbuffer.c \ utils.h utils.c if OS_WIN32 diff --git a/src/ringbuffer.c b/src/ringbuffer.c new file mode 100644 index 0000000..0ccec7c --- /dev/null +++ b/src/ringbuffer.c @@ -0,0 +1,79 @@ +#include + +#include "ringbuffer.h" + + +static unsigned int +normalize (unsigned int a, unsigned int size) +{ + return a % size; +} + + +static unsigned int +distance (unsigned int a, unsigned int b, unsigned int size) +{ + if (a <= b) { + return (b - a) % size; + } else { + return size - (a - b) % size; + } +} + + +static unsigned int +increment (unsigned int a, unsigned int delta, unsigned int size) +{ + return (a + delta) % size; +} + + +static unsigned int +decrement (unsigned int a, unsigned int delta, unsigned int size) +{ + if (delta <= a) { + return (a - delta) % size; + } else { + return size - (delta - a) % size; + } +} + + +unsigned int +ringbuffer_normalize (unsigned int a, unsigned int begin, unsigned int end) +{ + assert (end >= begin); + assert (a >= begin); + + return normalize (a, end - begin); +} + + +unsigned int +ringbuffer_distance (unsigned int a, unsigned int b, unsigned int begin, unsigned int end) +{ + assert (end >= begin); + assert (a >= begin); + + return distance (a, b, end - begin); +} + + +unsigned int +ringbuffer_increment (unsigned int a, unsigned int delta, unsigned int begin, unsigned int end) +{ + assert (end >= begin); + assert (a >= begin); + + return increment (a - begin, delta, end - begin) + begin; +} + + +unsigned int +ringbuffer_decrement (unsigned int a, unsigned int delta, unsigned int begin, unsigned int end) +{ + assert (end >= begin); + assert (a >= begin); + + return decrement (a - begin, delta, end - begin) + begin; +} diff --git a/src/ringbuffer.h b/src/ringbuffer.h new file mode 100644 index 0000000..0c4a117 --- /dev/null +++ b/src/ringbuffer.h @@ -0,0 +1,23 @@ +#ifndef RINGBUFFER_H +#define RINGBUFFER_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +unsigned int +ringbuffer_normalize (unsigned int a, unsigned int begin, unsigned int end); + +unsigned int +ringbuffer_distance (unsigned int a, unsigned int b, unsigned int begin, unsigned int end); + +unsigned int +ringbuffer_increment (unsigned int a, unsigned int delta, unsigned int begin, unsigned int end); + +unsigned int +ringbuffer_decrement (unsigned int a, unsigned int delta, unsigned int begin, unsigned int end); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* RINGBUFFER_H */ diff --git a/src/suunto_common.c b/src/suunto_common.c index 136bb96..5a8de36 100644 --- a/src/suunto_common.c +++ b/src/suunto_common.c @@ -2,29 +2,8 @@ #include #include "suunto_common.h" +#include "ringbuffer.h" -#define SUBSTRACT(a, x, begin, end) substract (a - begin, x, end - begin) + begin -#define DISTANCE(a, b, begin, end) distance (a, b, end - begin) - -static unsigned int -substract (unsigned int a, unsigned int x, unsigned int size) -{ - if (x <= a) { - return (a - x) % size; - } else { - return size - (x - a) % size; - } -} - -static unsigned int -distance (unsigned int a, unsigned int b, unsigned int size) -{ - if (a <= b) { - return (b - a) % size; - } else { - return size - (a - b) % size; - } -} int suunto_common_extract_dives (const unsigned char data[], unsigned int begin, unsigned int end, unsigned int eop, unsigned int peek, dive_callback_t callback, void *userdata) @@ -49,9 +28,9 @@ suunto_common_extract_dives (const unsigned char data[], unsigned int begin, uns // Check for an end of dive marker (of the next dive), // to find the start of the current dive. - unsigned int peek = SUBSTRACT (current, peek, begin, end); + unsigned int peek = ringbuffer_decrement (current, peek, begin, end); if (data[peek] == 0x80) { - unsigned int len = DISTANCE (current, previous, begin, end); + unsigned int len = ringbuffer_distance (current, previous, begin, end); if (current + len > end) { unsigned int a = end - current; unsigned int b = (current + len) - end; diff --git a/src/suunto_d9.c b/src/suunto_d9.c index 0078363..50aa87c 100644 --- a/src/suunto_d9.c +++ b/src/suunto_d9.c @@ -5,6 +5,7 @@ #include "suunto.h" #include "serial.h" #include "utils.h" +#include "ringbuffer.h" #define MAXRETRIES 2 @@ -16,24 +17,13 @@ message ("%s:%d: %s\n", __FILE__, __LINE__, expr); \ } -#define DISTANCE(a,b) distance (a, b, SUUNTO_D9_MEMORY_SIZE - 0x019A - 2) +#define DISTANCE(a,b) ringbuffer_distance (a, b, 0x019A, SUUNTO_D9_MEMORY_SIZE - 2) struct d9 { struct serial *port; }; -static unsigned int -distance (unsigned int a, unsigned int b, unsigned int size) -{ - if (a <= b) { - return (b - a) % size; - } else { - return size - (a - b) % size; - } -} - - int suunto_d9_open (d9 **out, const char* name) { diff --git a/src/suunto_vyper2.c b/src/suunto_vyper2.c index 4047d39..c7dcad9 100644 --- a/src/suunto_vyper2.c +++ b/src/suunto_vyper2.c @@ -5,6 +5,7 @@ #include "suunto.h" #include "serial.h" #include "utils.h" +#include "ringbuffer.h" #define MAXRETRIES 2 @@ -16,24 +17,13 @@ message ("%s:%d: %s\n", __FILE__, __LINE__, expr); \ } -#define DISTANCE(a,b) distance (a, b, SUUNTO_VYPER2_MEMORY_SIZE - 0x019A - 2) +#define DISTANCE(a,b) ringbuffer_distance (a, b, 0x019A, SUUNTO_VYPER2_MEMORY_SIZE - 2) struct vyper2 { struct serial *port; }; -static unsigned int -distance (unsigned int a, unsigned int b, unsigned int size) -{ - if (a <= b) { - return (b - a) % size; - } else { - return size - (a - b) % size; - } -} - - int suunto_vyper2_open (vyper2 **out, const char* name) { diff --git a/src/uwatec_aladin.c b/src/uwatec_aladin.c index 7c9bf0b..aa08370 100644 --- a/src/uwatec_aladin.c +++ b/src/uwatec_aladin.c @@ -4,6 +4,7 @@ #include "uwatec.h" #include "serial.h" #include "utils.h" +#include "ringbuffer.h" #define WARNING(expr) \ { \ @@ -15,24 +16,13 @@ rc == -1 ? UWATEC_ERROR_IO : UWATEC_ERROR_TIMEOUT \ ) +#define DISTANCE(a,b) ringbuffer_distance (a, b, 0, 0x600) + struct aladin { struct serial *port; }; -#define DISTANCE(a,b) distance (a, b, 0x600) - -static unsigned int -distance (unsigned int a, unsigned int b, unsigned int size) -{ - if (a <= b) { - return (b - a) % size; - } else { - return size - (a - b) % size; - } -} - - int uwatec_aladin_open (aladin **out, const char* name) {