code/WPILib/ADXL345_SPI.cpp
/**
 * Get the acceleration of all axes in Gs.
 * 
 * @return Acceleration measured on all axes of the ADXL345 in Gs.
 */
Get the acceleration of all axes in Gs.

@return Acceleration measured on all axes of the ADXL345 in Gs.
Get the acceleration of all axes in Gs . @return Acceleration measured on all axes of the ADXL345 in Gs .
ADXL345_SPI::AllAxes ADXL345_SPI::GetAccelerations()
{
	AllAxes data = {0.0};
	INT16 rawData[3];
	if (m_spi)
	{
		SPI::tFrameMode mode;
		bool activeLow;
		DigitalOutput *cs = m_spi->GetSlaveSelect(&mode, &activeLow);
		UINT32 bitsPerWord = m_spi->GetBitsPerWord();
		cs->Set(activeLow);
		m_spi->SetSlaveSelect(NULL);
		m_spi->SetBitsPerWord(8);
		m_spi->ApplyConfig();
		cs->Set(!activeLow);
		m_spi->Write(kAddress_Read | kAddress_MultiByte | kDataRegister);
		m_spi->Read();
		m_spi->SetBitsPerWord(16);
		m_spi->ApplyConfig();
		for (INT32 i=0; i<3; i++)
		{
			rawData[i] = (UINT16)m_spi->Read(true);
			rawData[i] = ((rawData[i] >> 8) & 0xFF) | (rawData[i] << 8);
		}
		cs->Set(activeLow);
		m_spi->SetSlaveSelect(cs, mode, activeLow);
		m_spi->SetBitsPerWord(bitsPerWord);
		m_spi->ApplyConfig();
		data.XAxis = rawData[0] * kGsPerLSB;
		data.YAxis = rawData[1] * kGsPerLSB;
		data.ZAxis = rawData[2] * kGsPerLSB;
	}
	return data;
}
ADXL345_SPI :: AllAxes ADXL345_SPI :: GetAccelerations ( ) { AllAxes data = { 0.0 } ; INT16 rawData [ 3 ] ; if ( m_spi ) { SPI :: tFrameMode mode ; bool activeLow ; DigitalOutput * cs = m_spi -> GetSlaveSelect ( & mode , & activeLow ) ; UINT32 bitsPerWord = m_spi -> GetBitsPerWord ( ) ; cs -> Set ( activeLow ) ; m_spi -> SetSlaveSelect ( NULL ) ; m_spi -> SetBitsPerWord ( 8 ) ; m_spi -> ApplyConfig ( ) ; cs -> Set ( ! activeLow ) ; m_spi -> Write ( kAddress_Read | kAddress_MultiByte | kDataRegister ) ; m_spi -> Read ( ) ; m_spi -> SetBitsPerWord ( 16 ) ; m_spi -> ApplyConfig ( ) ; for ( INT32 i = 0 ; i < 3 ; i ++ ) { rawData [ i ] = ( UINT16 ) m_spi -> Read ( true ) ; rawData [ i ] = ( ( rawData [ i ] >> 8 ) & 0xFF ) | ( rawData [ i ] << 8 ) ; } cs -> Set ( activeLow ) ; m_spi -> SetSlaveSelect ( cs , mode , activeLow ) ; m_spi -> SetBitsPerWord ( bitsPerWord ) ; m_spi -> ApplyConfig ( ) ; data . XAxis = rawData [ 0 ] * kGsPerLSB ; data . YAxis = rawData [ 1 ] * kGsPerLSB ; data . ZAxis = rawData [ 2 ] * kGsPerLSB ; } return data ; }
Get the acceleration of all axes in Gs.
Get the acceleration of all axes in Gs .
// Backup original settings. // Initialize the chip select to inactive. // Control the chip select manually. // 8-bit address // Assert chip select. // Select the data address. // 16-bits for each axis // SPI Interface can't read enough data in a single transaction to read all axes at once. // Sensor is little endian... swap bytes // Deassert chip select. // Restore original settings.