Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions arduino/Ecam/ecamlib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "ecamlib.h"

float bytesToFloat(unsigned char b0, unsigned char b1, unsigned char b2, unsigned char b3) {
union {
float f;
unsigned char b[4];
} number;

number.b[0] = b0;
number.b[1] = b1;
number.b[2] = b2;
number.b[3] = b3;

return number.f;
}

int bytesToInt(unsigned char b0, unsigned char b1) {
union {
int i;
unsigned char b[2];
} number;

number.b[0] = b0;
number.b[1] = b1;

return number.i;
}

long bytesToLong(unsigned char b0, unsigned char b1, unsigned char b2, unsigned char b3) {
union {
long l;
unsigned char b[4];
} number;

number.b[0] = b0;
number.b[1] = b1;
number.b[2] = b2;
number.b[3] = b3;

return number.l;
}
46 changes: 46 additions & 0 deletions arduino/Ecam/ecamlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef ecamlib_h
#define ecamlib_h

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include "pins_arduino.h"
#include "WConstants.h"
#endif

/*
C++ performs name mangling, in order to be able to
call C functions from it we need to tell it to use
the C convention for these functions.
*/
#ifdef __cplusplus
extern "C" {
#endif

/*
bytesToFloat expects an array of four bytes and
converts them to a float value
*/
float bytesToFloat(unsigned char b0, unsigned char b1, unsigned char b2, unsigned char b3);

/*
bytesToInt expects an array of two bytes and
converts them to a int value. Because on the
Arduino Uno ints are 16-bits.
*/
int bytesToInt(unsigned char b0, unsigned char b1);

/*
bytesToLong expects an array of four bytes and
converts them to a long value. Because on the
Arduino Uno longs are 32-bits.
*/
long bytesToLong(unsigned char b0, unsigned char b1, unsigned char b2, unsigned char b3);


#ifdef __cplusplus
}
#endif

#endif
Loading