Moved all array reversal functions to a common file.

This commit is contained in:
Jef Driesen 2008-07-30 09:03:37 +00:00
parent 7c03ddcb15
commit 5e8d18944a
6 changed files with 57 additions and 65 deletions

View File

@ -47,6 +47,7 @@ libdivecomputer_la_SOURCES = \
oceanic_atom2.h oceanic_atom2.c \
ringbuffer.h ringbuffer.c \
checksum.h checksum.c \
array.h array.c \
utils.h utils.c
if OS_WIN32

29
src/array.c Normal file
View File

@ -0,0 +1,29 @@
#include "array.h"
void
array_reverse_bytes (unsigned char data[], unsigned int size)
{
for (unsigned int i = 0; i < size / 2; ++i) {
unsigned char hlp = data[i];
data[i] = data[size - 1 - i];
data[size - 1 - i] = hlp;
}
}
void
array_reverse_bits (unsigned char data[], unsigned int size)
{
for (unsigned int i = 0; i < size; ++i) {
unsigned char j = 0;
j = (data[i] & 0x01) << 7;
j += (data[i] & 0x02) << 5;
j += (data[i] & 0x04) << 3;
j += (data[i] & 0x08) << 1;
j += (data[i] & 0x10) >> 1;
j += (data[i] & 0x20) >> 3;
j += (data[i] & 0x40) >> 5;
j += (data[i] & 0x80) >> 7;
data[i] = j;
}
}

17
src/array.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef ARRAY_H
#define ARRAY_H
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
void
array_reverse_bytes (unsigned char data[], unsigned int size);
void
array_reverse_bits (unsigned char data[], unsigned int size);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* ARRAY_H */

View File

@ -7,6 +7,7 @@
#include "suunto_common.h"
#include "serial.h"
#include "checksum.h"
#include "array.h"
#include "utils.h"
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
@ -129,17 +130,6 @@ suunto_vyper_device_close (device_t *abstract)
}
static void
suunto_vyper_reverse (unsigned char data[], unsigned int size)
{
for (unsigned int i = 0; i < size / 2; ++i) {
unsigned char hlp = data[i];
data[i] = data[size-1-i];
data[size-1-i] = hlp;
}
}
static device_status_t
suunto_vyper_send_testcmd (suunto_vyper_device_t *device, const unsigned char* data, unsigned int size)
{
@ -488,7 +478,7 @@ suunto_vyper_device_read_dive (device_t *abstract, unsigned char data[], unsigne
if (len == 0) {
WARNING ("Null package received.");
#ifndef NDEBUG
suunto_vyper_reverse (data, nbytes);
array_reverse_bytes (data, nbytes);
message ("Vyper%sProfile=\"", init ? "First" : "");
for (unsigned int i = 0; i < nbytes; ++i) {
message("%02x", data[i]);
@ -510,7 +500,7 @@ suunto_vyper_device_read_dive (device_t *abstract, unsigned char data[], unsigne
// dive is send first (which allows you to download only the new dives),
// but also the contents of each dive is reversed. Therefore, we reverse
// the bytes again before returning them to the application.
suunto_vyper_reverse (data, nbytes);
array_reverse_bytes (data, nbytes);
#ifndef NDEBUG
message ("Vyper%sProfile=\"", init ? "First" : "");

View File

@ -7,6 +7,7 @@
#include "utils.h"
#include "ringbuffer.h"
#include "checksum.h"
#include "array.h"
#define WARNING(expr) \
{ \
@ -120,35 +121,6 @@ uwatec_aladin_device_close (device_t *abstract)
}
static void
uwatec_aladin_reverse_bits (unsigned char data[], unsigned int size)
{
for (unsigned int i = 0; i < size; ++i) {
unsigned char j = 0;
j = (data[i] & 0x01) << 7;
j += (data[i] & 0x02) << 5;
j += (data[i] & 0x04) << 3;
j += (data[i] & 0x08) << 1;
j += (data[i] & 0x10) >> 1;
j += (data[i] & 0x20) >> 3;
j += (data[i] & 0x40) >> 5;
j += (data[i] & 0x80) >> 7;
data[i] = j;
}
}
static void
uwatec_aladin_reverse_bytes (unsigned char data[], unsigned int size)
{
for (unsigned int i = 0; i < size / 2; ++i) {
unsigned char hlp = data[i];
data[i] = data[size-1-i];
data[size-1-i] = hlp;
}
}
static device_status_t
uwatec_aladin_device_dump (device_t *abstract, unsigned char data[], unsigned int size)
{
@ -181,7 +153,7 @@ uwatec_aladin_device_dump (device_t *abstract, unsigned char data[], unsigned in
}
// Reverse the bit order.
uwatec_aladin_reverse_bits (answer, sizeof (answer));
array_reverse_bits (answer, sizeof (answer));
// Verify the checksum of the package.
unsigned short crc =
@ -270,7 +242,7 @@ uwatec_aladin_extract_dives (const unsigned char* data, unsigned int size, dive_
// Convert the timestamp from the Aladin (big endian)
// to the Memomouse format (little endian).
uwatec_aladin_reverse_bytes (buffer + 11, 4);
array_reverse_bytes (buffer + 11, 4);
unsigned int len = 0;
if (profiles) {

View File

@ -6,6 +6,7 @@
#include "uwatec_memomouse.h"
#include "serial.h"
#include "checksum.h"
#include "array.h"
#include "utils.h"
#define WARNING(expr) \
@ -140,24 +141,6 @@ uwatec_memomouse_device_set_timestamp (device_t *abstract, unsigned int timestam
}
static void
uwatec_memomouse_reverse (unsigned char data[], unsigned int size)
{
for (unsigned int i = 0; i < size; ++i) {
unsigned char j = 0;
j = (data[i] & 0x01) << 7;
j += (data[i] & 0x02) << 5;
j += (data[i] & 0x04) << 3;
j += (data[i] & 0x08) << 1;
j += (data[i] & 0x10) >> 1;
j += (data[i] & 0x20) >> 3;
j += (data[i] & 0x40) >> 5;
j += (data[i] & 0x80) >> 7;
data[i] = j;
}
}
static device_status_t
uwatec_memomouse_confirm (uwatec_memomouse_device_t *device, unsigned char value)
{
@ -187,7 +170,7 @@ uwatec_memomouse_read_packet (uwatec_memomouse_device_t *device, unsigned char d
}
// Reverse the bits.
uwatec_memomouse_reverse (data, 1);
array_reverse_bits (data, 1);
// Verify the header of the package.
unsigned int len = data[0];
@ -204,7 +187,7 @@ uwatec_memomouse_read_packet (uwatec_memomouse_device_t *device, unsigned char d
}
// Reverse the bits.
uwatec_memomouse_reverse (data + 1, len + 1);
array_reverse_bits (data + 1, len + 1);
// Verify the checksum of the package.
unsigned char crc = data[len + 1];
@ -362,7 +345,7 @@ uwatec_memomouse_dump (uwatec_memomouse_device_t *device, unsigned char *data[],
(device->timestamp >> 24) & 0xFF,
0x00}; // Outer packet checksum.
command[8] = checksum_xor_uint8 (command, 8, 0x00);
uwatec_memomouse_reverse (command, sizeof (command));
array_reverse_bits (command, sizeof (command));
// Wait a small amount of time before sending the command.
// Without this delay, the transfer will fail most of the time.