diff --git a/src/oceanic_common.c b/src/oceanic_common.c index 856af73..230fe0a 100644 --- a/src/oceanic_common.c +++ b/src/oceanic_common.c @@ -31,10 +31,10 @@ #define PAGESIZE 0x10 -#define RB_LOGBOOK_DISTANCE(a,b,l) ringbuffer_distance (a, b, l->rb_logbook_begin, l->rb_logbook_end) +#define RB_LOGBOOK_DISTANCE(a,b,l) ringbuffer_distance (a, b, 0, l->rb_logbook_begin, l->rb_logbook_end) #define RB_LOGBOOK_INCR(a,b,l) ringbuffer_increment (a, b, l->rb_logbook_begin, l->rb_logbook_end) -#define RB_PROFILE_DISTANCE(a,b,l) ringbuffer_distance (a, b, l->rb_profile_begin, l->rb_profile_end) +#define RB_PROFILE_DISTANCE(a,b,l) ringbuffer_distance (a, b, 0, l->rb_profile_begin, l->rb_profile_end) #define RB_PROFILE_INCR(a,b,l) ringbuffer_increment (a, b, l->rb_profile_begin, l->rb_profile_end) diff --git a/src/ringbuffer.c b/src/ringbuffer.c index f0c2f7a..e0da155 100644 --- a/src/ringbuffer.c +++ b/src/ringbuffer.c @@ -32,12 +32,14 @@ normalize (unsigned int a, unsigned int size) static unsigned int -distance (unsigned int a, unsigned int b, unsigned int size) +distance (unsigned int a, unsigned int b, int mode, unsigned int size) { - if (a <= b) { + if (a < b) { return (b - a) % size; - } else { + } else if (a > b) { return size - (a - b) % size; + } else { + return (mode == 0 ? 0 : size); } } @@ -71,12 +73,12 @@ 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) +ringbuffer_distance (unsigned int a, unsigned int b, int mode, unsigned int begin, unsigned int end) { assert (end >= begin); assert (a >= begin); - return distance (a, b, end - begin); + return distance (a, b, mode, end - begin); } diff --git a/src/ringbuffer.h b/src/ringbuffer.h index 405e55f..f6401e8 100644 --- a/src/ringbuffer.h +++ b/src/ringbuffer.h @@ -30,7 +30,7 @@ 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); +ringbuffer_distance (unsigned int a, unsigned int b, int mode, unsigned int begin, unsigned int end); unsigned int ringbuffer_increment (unsigned int a, unsigned int delta, unsigned int begin, unsigned int end); diff --git a/src/suunto_common.c b/src/suunto_common.c index 4fe8cff..313e981 100644 --- a/src/suunto_common.c +++ b/src/suunto_common.c @@ -27,7 +27,7 @@ #include "ringbuffer.h" #include "array.h" -#define RB_PROFILE_DISTANCE(a,b,l) ringbuffer_distance (a, b, l->rb_profile_begin, l->rb_profile_end) +#define RB_PROFILE_DISTANCE(a,b,l) ringbuffer_distance (a, b, 0, l->rb_profile_begin, l->rb_profile_end) #define RB_PROFILE_PEEK(a,l) ringbuffer_decrement (a, l->peek, l->rb_profile_begin, l->rb_profile_end) void diff --git a/src/suunto_common2.c b/src/suunto_common2.c index 6f9f270..1333b47 100644 --- a/src/suunto_common2.c +++ b/src/suunto_common2.c @@ -39,7 +39,7 @@ #define RB_PROFILE_BEGIN 0x019A #define RB_PROFILE_END SZ_MEMORY - 2 -#define RB_PROFILE_DISTANCE(a,b) ringbuffer_distance (a, b, RB_PROFILE_BEGIN, RB_PROFILE_END) +#define RB_PROFILE_DISTANCE(a,b) ringbuffer_distance (a, b, 0, RB_PROFILE_BEGIN, RB_PROFILE_END) #define BACKEND(abstract) ((suunto_common2_device_backend_t *) abstract->backend) diff --git a/src/suunto_solution.c b/src/suunto_solution.c index 20c35c0..b1b8d23 100644 --- a/src/suunto_solution.c +++ b/src/suunto_solution.c @@ -325,7 +325,7 @@ suunto_solution_extract_dives (device_t *abstract, const unsigned char data[], u // to find the start of the current dive. unsigned int peek = ringbuffer_increment (current, 2, RB_PROFILE_BEGIN, RB_PROFILE_END); if (data[peek] == 0x80) { - unsigned int len = ringbuffer_distance (previous, current, RB_PROFILE_BEGIN, RB_PROFILE_END); + unsigned int len = ringbuffer_distance (previous, current, 0, RB_PROFILE_BEGIN, RB_PROFILE_END); if (callback && !callback (buffer + idx, len, userdata)) return DEVICE_STATUS_SUCCESS; diff --git a/src/uwatec_aladin.c b/src/uwatec_aladin.c index 8b364bb..6d08f91 100644 --- a/src/uwatec_aladin.c +++ b/src/uwatec_aladin.c @@ -40,7 +40,7 @@ #define RB_PROFILE_BEGIN 0x000 #define RB_PROFILE_END 0x600 #define RB_PROFILE_NEXT(a) ringbuffer_increment (a, 1, RB_PROFILE_BEGIN, RB_PROFILE_END) -#define RB_PROFILE_DISTANCE(a,b) ringbuffer_distance (a, b, RB_PROFILE_BEGIN, RB_PROFILE_END) +#define RB_PROFILE_DISTANCE(a,b) ringbuffer_distance (a, b, 0, RB_PROFILE_BEGIN, RB_PROFILE_END) #define HEADER 4