
#include "windows.h"

//.............加速度構造体......................
struct ShockData
{
    int status;
    short int  x0;
    short int  y0;
    short int  x1;
    short int  y1;
    short int  x2;
    short int  y2;
    short int  x3;
    short int  y3;
    short int  x4;
    short int  y4;
    short int  x5;
    short int  y5;
    short int  x6;
    short int  y6;
    short int  x7;
    short int  y7;
    short int  x8;
    short int  y8;
    short int  x9;
    short int  y9;
    short int  x10;
    short int  y10;
    short int  x11;
    short int  y11;
    short int  x12;
    short int  y12;
    short int  x13;
    short int  y13;
    short int  unknown0;
    short int  unknown1;
};

typedef struct _ACCELREPORT {
        int      PresentState;        // Current internal state (stable: 0, unstable1: 1: unstable2: 2)
        unsigned short LatestRawAccelDataX; // latest raw acceleration data of X axis    <-- works!
 		unsigned short LatestRawAccelDataY; // latest raw acceleration data of Y axis    <-- works!
 		unsigned short LatestAccelDataX;    // latest acceleration data of X axis (average in 40ms)   <-- Works even better?
 		unsigned short LatestAccelDataY;    // latest acceleration data of Y axis (average in 40ms)   <-- Works even better?
 		char     Temperature;         // latest temperature
 		unsigned short LatestZeroG_X;       // latest zero-G offset of X axis  <-- Seems to be the current notion of "center"
 		unsigned short LatestZeroG_Y;       // latest zero-G offset of Y axis  <-- ""
} ACCELREPORT, *PACCELREPORT;

typedef int SENSOR_API;
typedef SENSOR_API (__stdcall *readSensor_t)(PACCELREPORT  pAcceleration);


//.............加速度取得用クラス...................
class Accelerometer{
    private:
        HINSTANCE lib;
        HINSTANCE hDLLLib;
        ACCELREPORT data;
        readSensor_t sensorFunction;
        int OffsetX;
        int OffsetY;
    protected:
    public:
        Accelerometer();
        ~Accelerometer();
        ShockData AccelerometerData;
        bool GetAccelerometerData(void);
        int  Status(void);
        int  X;
        int  Y;
};
//.............コンストラクタ.......................
Accelerometer::Accelerometer()
{
    try
    {
      HINSTANCE hDLLLib = LoadLibrary("Sensor.dll");
      if(hDLLLib!=NULL) {
        sensorFunction = (readSensor_t) GetProcAddress(hDLLLib, "ShockproofGetAccelerometerData");
      }
    }
    catch(...){}
    OffsetX = 0;
    OffsetY = 0;
    if ( sensorFunction(&data) )
    {
        OffsetX = -data.LatestRawAccelDataX;
        OffsetY = -data.LatestRawAccelDataY;
        GetAccelerometerData();
    }
}
//.............デストラクタ....................
Accelerometer::~Accelerometer()
{
  FreeLibrary(hDLLLib);
}
//.............加速度取得する.....................
bool Accelerometer::GetAccelerometerData(void)
{
    try
    {
        sensorFunction(&data);
        X = data.LatestRawAccelDataX + OffsetX;
        Y = data.LatestRawAccelDataY + OffsetY;
        return true;
    }
    catch(...)
    {
        return false;
    }
}
//.............状況出力.......................
int Accelerometer::Status(void)
{
    AccelerometerData.x0=X;
    AccelerometerData.y0=Y;
    return AccelerometerData.status;
}
 
