1////////////////////////////////////////////////////////////////////////////////
2// //
3// This software is supplied under the terms of a license agreement or //
4// nondisclosure agreement with Mitov Software and may not be copied //
5// or disclosed except in accordance with the terms of that agreement. //
6// Copyright(c) 2002-2016 Mitov Software. All Rights Reserved. //
7// //
8////////////////////////////////////////////////////////////////////////////////
9
10#ifndef _MITOV_ACCELEROMETER_h
11#define _MITOV_ACCELEROMETER_h
12
13#include <Mitov.h>
14
15namespace Mitov
16{
17 class Memsic2125Accelerometer : public OpenWire::Component
18 {
19 typedef OpenWire::Component inherited;
20
21 public:
22 OpenWire::SinkPin XInputPin;
23 OpenWire::SinkPin YInputPin;
24
25 OpenWire::SourcePin XOutputPin;
26 OpenWire::SourcePin YOutputPin;
27
28 public:
29 bool Enabled = true;
30
31 protected:
32 class DataSet : public OpenWire::Object
33 {
34 protected:
35 OpenWire::SourcePin *FOutputPin;
36
37 unsigned long FStartTime;
38 bool FOldValue;
39 const Memsic2125Accelerometer *FOwner;
40
41 public:
42 virtual void DoReceive( void *_Data )
43 {
44 if( ! FOwner->Enabled )
45 return;
46
47 bool AValue = *(bool *)_Data;
48 if( FOldValue == AValue )
49 return;
50
51 unsigned long ANow = micros();
52 FOldValue = AValue;
53 if( AValue )
54 {
55 FStartTime = ANow;
56 return;
57 }
58
59 long APeriod = ANow - FStartTime;
60
61 if( APeriod == 0 )
62 APeriod = 1;
63
64 float AAcceleration = (( APeriod / 10 ) - 500) * 8;
65
66 FOutputPin->Notify( &AAcceleration );
67 }
68
69 public:
70 DataSet( const Memsic2125Accelerometer *AOwner, OpenWire::SinkPin *AInputPin, OpenWire::SourcePin *AOutputPin ) :
71 FOwner( AOwner ),
72 FOutputPin( AOutputPin ),
73 FOldValue( false ),
74 FStartTime( 0 )
75 {
76 AInputPin->SetCallback( this, (OpenWire::TOnPinReceive)&DataSet::DoReceive );
77 }
78 };
79
80
81 protected:
82 DataSet FXDataSet;
83 DataSet FYDataSet;
84
85 public:
86 Memsic2125Accelerometer() :
87 FXDataSet( this, &XInputPin, &XOutputPin ),
88 FYDataSet( this, &YInputPin, &YOutputPin )
89 {
90 }
91 };
92}
93
94#endif