]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/tests/peripherals/MMA7660/MMA7660.cpp
3 MMA7660::MMA7660(PinName sda
, PinName scl
, bool active
) : _i2c(sda
, scl
)
10 //Since the MMA lacks a WHO_AM_I register, we can only check if there is a device that answers to the I2C address
11 bool MMA7660::testConnection( void )
13 if (_i2c
.write(MMA7660_ADDRESS
, NULL
, 0) == 0 )
19 void MMA7660::setActive(bool state
)
21 char modereg
= read(MMA7660_MODE_R
);
24 //If it somehow was in testmode, disable that
25 if (modereg
&& (1<<2)) {
27 write(MMA7660_MODE_R
, modereg
);
31 write(MMA7660_MODE_R
, modereg
);
34 void MMA7660::readData(int *data
)
39 wait(0.012 + 1/samplerate
); //Wait until new sample is ready, my experience is that 1/samplerate isnt needed, but datasheet says so
47 read(MMA7660_XOUT_R
, temp
, 3);
48 for (int i
= 0; i
<3; i
++) {
53 data
[i
] = (signed char)temp
[i
];
62 void MMA7660::readData(float *data
)
66 for (int i
= 0; i
<3; i
++)
67 data
[i
] = intdata
[i
]/MMA7660_SENSITIVITY
;
70 float MMA7660::x( void )
75 float MMA7660::y( void )
80 float MMA7660::z( void )
86 void MMA7660::setSampleRate(int samplerate
)
88 setActive(false); //Not allowed to be active to change anything
89 int rates
[] = {120, 64, 32, 16, 8, 4, 2, 1}; //Alowed samplerates (and their number in array is also number required for MMA)
90 int sampleLoc
= 0, sampleError
= 10000, temp
;
91 for (int i
= 0; i
<8; i
++) {
92 temp
= abs( rates
[i
] - samplerate
);
93 if (temp
<sampleError
) {
99 //Update the samplerate reg
100 temp
= read(MMA7660_SR_R
);
101 temp
&= ~0x07; //Awake sample rate are lowest 3 bit
103 write(MMA7660_SR_R
, temp
);
104 this->samplerate
= rates
[sampleLoc
];
105 setActive(active
); //Restore previous active state
109 MMA7660::Orientation
MMA7660::getSide( void )
111 char tiltreg
= read(MMA7660_TILT_R
);
113 //We care about 2 LSBs
116 return MMA7660::Front
;
118 return MMA7660::Back
;
119 return MMA7660::Unknown
;
122 MMA7660::Orientation
MMA7660::getOrientation( void )
124 char tiltreg
= read(MMA7660_TILT_R
);
126 //We care about bit 2, 3 and 4 (counting from zero)
130 return MMA7660::Left
;
132 return MMA7660::Right
;
134 return MMA7660::Down
;
137 return MMA7660::Unknown
;
142 //////////////////////////////////////////////
143 ///////////////PRIVATE////////////////////////
144 //////////////////////////////////////////////
147 void MMA7660::write(char address
, char data
)
153 _i2c
.write(MMA7660_ADDRESS
, temp
, 2);
156 char MMA7660::read(char address
)
159 _i2c
.write(MMA7660_ADDRESS
, &address
, 1, true);
160 _i2c
.read(MMA7660_ADDRESS
, &retval
, 1);
164 void MMA7660::read(char address
, char *data
, int length
)
166 _i2c
.write(MMA7660_ADDRESS
, &address
, 1, true);
167 _i2c
.read(MMA7660_ADDRESS
, data
, length
);
170 float MMA7660::getSingle( int number
)
174 wait(0.012 + 1/samplerate
); //Wait until new sample is ready
182 temp
= read(MMA7660_XOUT_R
+ number
);
192 return temp
/ MMA7660_SENSITIVITY
;