Moved all ringbuffer operations to a common location.

This commit is contained in:
Jef Driesen 2008-06-20 13:20:29 +00:00
parent 27ce6102ee
commit d629e02ca2
7 changed files with 113 additions and 61 deletions

View File

@ -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

79
src/ringbuffer.c Normal file
View File

@ -0,0 +1,79 @@
#include <assert.h>
#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;
}

23
src/ringbuffer.h Normal file
View File

@ -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 */

View File

@ -2,29 +2,8 @@
#include <string.h>
#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;

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{