Просмотр исходного кода

Pretty printed the Arduino code with astyle

Baozhu Zuo 6 лет назад
Родитель
Сommit
d5c5274658
3 измененных файлов с 214 добавлено и 235 удалено
  1. 124 136
      LSM303D.cpp
  2. 26 27
      LSM303D.h
  3. 64 72
      examples/Accelerometer_Compass/Accelerometer_Compass.ino

+ 124 - 136
LSM303D.cpp

@@ -1,58 +1,58 @@
 /* LSM303DLM Example Code base on LSM303DLH example code by Jim Lindblom SparkFun Electronics
-   
+
    date: 9/6/11
    license: Creative commons share-alike v3.0
-   
+
    Modified by:Frankie.Chu
    Modified by:Jacky.Zhang 2014-12-11: Ported to 6-Axis Accelerometer&Compass of Seeed Studio
    Modified by:Jacky.Zhang 2015-1-6: added SPI driver
-   
+
    Summary:
    Show how to calculate level and tilt-compensated heading using
    the snazzy LSM303DLH 3-axis magnetometer/3-axis accelerometer.
-   
+
    Firmware:
    You can set the accelerometer's full-scale range by setting
    the SCALE constant to either 2, 4, or 8. This value is used
    in the initLSM303() function. For the most part, all other
    registers in the LSM303 will be at their default value.
-   
+
    Use the write() and read() functions to write
    to and read from the LSM303's internal registers.
-   
+
    Use getLSM303_accel() and getLSM303_mag() to get the acceleration
    and magneto values from the LSM303. You'll need to pass each of
    those functions an array, where the data will be stored upon
    return from the void.
-   
+
    getHeading() calculates a heading assuming the sensor is level.
    A float between 0 and 360 is returned. You need to pass it a
-   array with magneto values. 
-   
+   array with magneto values.
+
    getTiltHeading() calculates a tilt-compensated heading.
    A float between 0 and 360 degrees is returned. You need
    to pass this function both a magneto and acceleration array.
-   
+
    Headings are calculated as specified in AN3192:
    http://www.sparkfun.com/datasheets/Sensors/Magneto/Tilt%20Compensated%20Compass.pdf
 */
 
 /*
-hardware & software comment
+  hardware & software comment
 
-I2C mode:
-1, solder the jumper "I2C EN" and the jumper of ADDR to 0x1E
-2, use Lsm303d.initI2C() function to initialize the Grove by I2C
+  I2C mode:
+  1, solder the jumper "I2C EN" and the jumper of ADDR to 0x1E
+  2, use Lsm303d.initI2C() function to initialize the Grove by I2C
 
-SPI mode:
+  SPI mode:
 
-1, break the jumper "I2C_EN" and the jumper ADDR to any side
-2, define a pin as chip select for SPI protocol.
-3, use Lsm303d.initSPI(SPI_CS) function to initialize the Grove by SPI
-SPI.h sets these for us in arduino
-const int16_t SDI = 11;
-const int16_t SDO = 12;
-const int16_t SCL = 13;
+  1, break the jumper "I2C_EN" and the jumper ADDR to any side
+  2, define a pin as chip select for SPI protocol.
+  3, use Lsm303d.initSPI(SPI_CS) function to initialize the Grove by SPI
+  SPI.h sets these for us in arduino
+  const int16_t SDI = 11;
+  const int16_t SDO = 12;
+  const int16_t SCL = 13;
 */
 
 
@@ -130,141 +130,129 @@ byte Read  = 0B10000000;
 byte Write = 0B00000000;
 
 //I2C mode
-char LSM303D::initI2C()
-{
-	char rtn = -1;
-    
-    _mode = 0;//I2C mode
-	Wire.begin();  // Start up I2C, required for LSM303 communication
-	rtn = config();
-    
-	return rtn;
+char LSM303D::initI2C() {
+  char rtn = -1;
+
+  _mode = 0;//I2C mode
+  Wire.begin();  // Start up I2C, required for LSM303 communication
+  rtn = config();
+
+  return rtn;
 }
 
 //SPI mode
-char LSM303D::initSPI(char cspin)
-{
-	char rtn = -1;
-    
-    _mode = 1;//SPI mode
-    _cs = cspin;
-    pinMode(_cs, OUTPUT);//initialize the chip select pins;
-    SPI.begin();//start the SPI library;
-    rtn = config();
-    
-	return rtn;
+char LSM303D::initSPI(char cspin) {
+  char rtn = -1;
+
+  _mode = 1;//SPI mode
+  _cs = cspin;
+  pinMode(_cs, OUTPUT);//initialize the chip select pins;
+  SPI.begin();//start the SPI library;
+  rtn = config();
+
+  return rtn;
 }
 
-char LSM303D::config()
-{
-    char rtn = -1;
-    
-    if(read(WHO_AM_I) != 0x49) return rtn; // return wrong if no LSM303D was found 
-    
-	write(0x57, CTRL_REG1);  // 0x57 = ODR=50hz, all accel axes on
-	write((3<<6)|(0<<3), CTRL_REG2);  // set full-scale
-	write(0x00, CTRL_REG3);  // no interrupt
-	write(0x00, CTRL_REG4);  // no interrupt
-	write((4<<2), CTRL_REG5);  // 0x10 = mag 50Hz output rate
-	write(MAG_SCALE_2, CTRL_REG6); //magnetic scale = +/-1.3Gauss
-	write(0x00, CTRL_REG7);  // 0x00 = continouous conversion mode
-	rtn = 0;
-    
-	return rtn;
+char LSM303D::config() {
+  char rtn = -1;
+
+  if (read(WHO_AM_I) != 0x49) {
+    return rtn;  // return wrong if no LSM303D was found
+  }
+
+  write(0x57, CTRL_REG1);  // 0x57 = ODR=50hz, all accel axes on
+  write((3 << 6) | (0 << 3), CTRL_REG2); // set full-scale
+  write(0x00, CTRL_REG3);  // no interrupt
+  write(0x00, CTRL_REG4);  // no interrupt
+  write((4 << 2), CTRL_REG5); // 0x10 = mag 50Hz output rate
+  write(MAG_SCALE_2, CTRL_REG6); //magnetic scale = +/-1.3Gauss
+  write(0x00, CTRL_REG7);  // 0x00 = continouous conversion mode
+  rtn = 0;
+
+  return rtn;
 }
 
-unsigned char LSM303D::read(unsigned char address)
-{
-	char temp = 0x00;
-    
-    if(_mode == 0)//I2C mode
-    {
-        Wire.beginTransmission(LSM303D_ADDR);
-        Wire.write(address);
-        Wire.endTransmission();
-        Wire.requestFrom(LSM303D_ADDR, 1);
-        while(!Wire.available());
-        temp = Wire.read();
-        Wire.endTransmission();
-    }
-    else//SPI Mode
-    {
-        digitalWrite(_cs, LOW); 
-        SPI.transfer(Read | address);
-        temp = SPI.transfer(0x00);
-        digitalWrite(_cs, HIGH);
-    }
-    
-	return temp;
+unsigned char LSM303D::read(unsigned char address) {
+  char temp = 0x00;
+
+  if (_mode == 0) { //I2C mode
+    Wire.beginTransmission(LSM303D_ADDR);
+    Wire.write(address);
+    Wire.endTransmission();
+    Wire.requestFrom(LSM303D_ADDR, 1);
+    while (!Wire.available());
+    temp = Wire.read();
+    Wire.endTransmission();
+  } else { //SPI Mode
+    digitalWrite(_cs, LOW);
+    SPI.transfer(Read | address);
+    temp = SPI.transfer(0x00);
+    digitalWrite(_cs, HIGH);
+  }
+
+  return temp;
 }
 
-void LSM303D::write(unsigned char data, unsigned char address)
-{
-    if(_mode == 0)
-    {
-        Wire.beginTransmission(LSM303D_ADDR); 
-        Wire.write(address);
-        Wire.write(data);
-        Wire.endTransmission();
-    }
-    else
-    {
-        digitalWrite(_cs, LOW);
-        SPI.transfer(Write | address);
-        SPI.transfer(data);
-        digitalWrite(_cs, HIGH);
-    }
+void LSM303D::write(unsigned char data, unsigned char address) {
+  if (_mode == 0) {
+    Wire.beginTransmission(LSM303D_ADDR);
+    Wire.write(address);
+    Wire.write(data);
+    Wire.endTransmission();
+  } else {
+    digitalWrite(_cs, LOW);
+    SPI.transfer(Write | address);
+    SPI.transfer(data);
+    digitalWrite(_cs, HIGH);
+  }
 }
 
-char LSM303D::isMagReady()
-{
-	char temp;
-    
-	temp = read(STATUS_REG_M) & 0x03;
-    
-	return temp;
+char LSM303D::isMagReady() {
+  char temp;
+
+  temp = read(STATUS_REG_M) & 0x03;
+
+  return temp;
 }
 
-void LSM303D::getMag(int16_t * rawValues)
-{
-	rawValues[X] = ((int16_t)read(OUT_X_H_M) << 8) | (read(OUT_X_L_M));
-	rawValues[Y] = ((int16_t)read(OUT_Y_H_M) << 8) | (read(OUT_Y_L_M));
-	rawValues[Z] = ((int16_t)read(OUT_Z_H_M) << 8) | (read(OUT_Z_L_M));
+void LSM303D::getMag(int16_t* rawValues) {
+  rawValues[X] = ((int16_t)read(OUT_X_H_M) << 8) | (read(OUT_X_L_M));
+  rawValues[Y] = ((int16_t)read(OUT_Y_H_M) << 8) | (read(OUT_Y_L_M));
+  rawValues[Z] = ((int16_t)read(OUT_Z_H_M) << 8) | (read(OUT_Z_L_M));
 }
 
-void LSM303D::getAccel(int16_t * rawValues)
-{
-	rawValues[X] = ((int16_t)read(OUT_X_H_A) << 8) | (read(OUT_X_L_A));
-	rawValues[Y] = ((int16_t)read(OUT_Y_H_A) << 8) | (read(OUT_Y_L_A));
-	rawValues[Z] = ((int16_t)read(OUT_Z_H_A) << 8) | (read(OUT_Z_L_A));
+void LSM303D::getAccel(int16_t* rawValues) {
+  rawValues[X] = ((int16_t)read(OUT_X_H_A) << 8) | (read(OUT_X_L_A));
+  rawValues[Y] = ((int16_t)read(OUT_Y_H_A) << 8) | (read(OUT_Y_L_A));
+  rawValues[Z] = ((int16_t)read(OUT_Z_H_A) << 8) | (read(OUT_Z_L_A));
 }
 
-float LSM303D::getHeading(int16_t * magValue)
-{
-	// see section 1.2 in app note AN3192
-	float heading = 180*atan2(magValue[Y], magValue[X])/PI;  // assume pitch, roll are 0
+float LSM303D::getHeading(int16_t* magValue) {
+  // see section 1.2 in app note AN3192
+  float heading = 180 * atan2(magValue[Y], magValue[X]) / PI; // assume pitch, roll are 0
 
-	if (heading <0)
-	heading += 360;
+  if (heading < 0) {
+    heading += 360;
+  }
 
-	return heading;
+  return heading;
 }
 
-float LSM303D::getTiltHeading(int16_t * magValue, float * accelValue)
-{
-	// see appendix A in app note AN3192 
-	float pitch = asin(-accelValue[X]);
-	float roll = asin(accelValue[Y]/cos(pitch));
-
-	float xh = magValue[X] * cos(pitch) + magValue[Z] * sin(pitch);
-	float yh = magValue[X] * sin(roll) * sin(pitch) + magValue[Y] * cos(roll) - magValue[Z] * sin(roll) * cos(pitch);
-	float zh = -magValue[X] * cos(roll) * sin(pitch) + magValue[Y] * sin(roll) + magValue[Z] * cos(roll) * cos(pitch);
-	float heading = 180 * atan2(yh, xh)/PI;
-
-	if (yh >= 0)
-		return heading;
-	else
-		return (360 + heading);
+float LSM303D::getTiltHeading(int16_t* magValue, float* accelValue) {
+  // see appendix A in app note AN3192
+  float pitch = asin(-accelValue[X]);
+  float roll = asin(accelValue[Y] / cos(pitch));
+
+  float xh = magValue[X] * cos(pitch) + magValue[Z] * sin(pitch);
+  float yh = magValue[X] * sin(roll) * sin(pitch) + magValue[Y] * cos(roll) - magValue[Z] * sin(roll) * cos(pitch);
+  float zh = -magValue[X] * cos(roll) * sin(pitch) + magValue[Y] * sin(roll) + magValue[Z] * cos(roll) * cos(pitch);
+  float heading = 180 * atan2(yh, xh) / PI;
+
+  if (yh >= 0) {
+    return heading;
+  } else {
+    return (360 + heading);
+  }
 }
 
 LSM303D Lsm303d;

+ 26 - 27
LSM303D.h

@@ -1,38 +1,38 @@
 /* LSM303DLM Example Code base on LSM303DLH example code by Jim Lindblom SparkFun Electronics
-   
+
    date: 9/6/11
    license: Creative commons share-alike v3.0
-   
+
    Modified by:Frankie.Chu
    Modified by:Jacky.Zhang 2014-12-11: Ported to 6-Axis Accelerometer&Compass of Seeed Studio
    Modified by:Jacky.Zhang 2015-1-6: added SPI driver
-   
+
    Summary:
    Show how to calculate level and tilt-compensated heading using
    the snazzy LSM303DLH 3-axis magnetometer/3-axis accelerometer.
-   
+
    Firmware:
    You can set the accelerometer's full-scale range by setting
    the SCALE constant to either 2, 4, or 8. This value is used
    in the initLSM303() function. For the most part, all other
    registers in the LSM303 will be at their default value.
-   
+
    Use the write() and read() functions to write
    to and read from the LSM303's internal registers.
-   
+
    Use getLSM303_accel() and getLSM303_mag() to get the acceleration
    and magneto values from the LSM303. You'll need to pass each of
    those functions an array, where the data will be stored upon
    return from the void.
-   
+
    getHeading() calculates a heading assuming the sensor is level.
    A float between 0 and 360 is returned. You need to pass it a
-   array with magneto values. 
-   
+   array with magneto values.
+
    getTiltHeading() calculates a tilt-compensated heading.
    A float between 0 and 360 degrees is returned. You need
    to pass this function both a magneto and acceleration array.
-   
+
    Headings are calculated as specified in AN3192:
    http://www.sparkfun.com/datasheets/Sensors/Magneto/Tilt%20Compensated%20Compass.pdf
 */
@@ -48,23 +48,22 @@
 #define Y 1
 #define Z 2
 
-class LSM303D
-{
-	public:
-
-		char initI2C();
-        char initSPI(char cspin);
-        char config();
-		unsigned char read(unsigned char address);
-		void write(unsigned char data, unsigned char address);
-		char isMagReady();
-		void getMag(int16_t * rawValues);
-		void getAccel(int16_t * rawValues);
-		float getHeading(int16_t * magValue);
-		float getTiltHeading(int16_t * magValue, float * accelValue);
-	private:
-        unsigned char _mode;
-        unsigned char _cs;
+class LSM303D {
+ public:
+
+  char initI2C();
+  char initSPI(char cspin);
+  char config();
+  unsigned char read(unsigned char address);
+  void write(unsigned char data, unsigned char address);
+  char isMagReady();
+  void getMag(int16_t* rawValues);
+  void getAccel(int16_t* rawValues);
+  float getHeading(int16_t* magValue);
+  float getTiltHeading(int16_t* magValue, float* accelValue);
+ private:
+  unsigned char _mode;
+  unsigned char _cs;
 };
 
 extern  LSM303D Lsm303d;

+ 64 - 72
examples/Accelerometer_Compass/Accelerometer_Compass.ino

@@ -1,58 +1,58 @@
 /* LSM303DLM Example Code base on LSM303DLH example code by Jim Lindblom SparkFun Electronics
-   
+
    date: 9/6/11
    license: Creative commons share-alike v3.0
-   
+
    Modified by:Frankie.Chu
    Modified by:Jacky.Zhang 2014-12-11: Ported to 6-Axis Accelerometer&Compass of Seeed Studio
    Modified by:Jacky.Zhang 2015-1-6: added SPI driver
-   
+
    Summary:
    Show how to calculate level and tilt-compensated heading using
    the snazzy LSM303DLH 3-axis magnetometer/3-axis accelerometer.
-   
+
    Firmware:
    You can set the accelerometer's full-scale range by setting
    the SCALE constant to either 2, 4, or 8. This value is used
    in the initLSM303() function. For the most part, all other
    registers in the LSM303 will be at their default value.
-   
+
    Use the write() and read() functions to write
    to and read from the LSM303's internal registers.
-   
+
    Use getLSM303_accel() and getLSM303_mag() to get the acceleration
    and magneto values from the LSM303. You'll need to pass each of
    those functions an array, where the data will be stored upon
    return from the void.
-   
+
    getHeading() calculates a heading assuming the sensor is level.
    A float between 0 and 360 is returned. You need to pass it a
-   array with magneto values. 
-   
+   array with magneto values.
+
    getTiltHeading() calculates a tilt-compensated heading.
    A float between 0 and 360 degrees is returned. You need
    to pass this function both a magneto and acceleration array.
-   
+
    Headings are calculated as specified in AN3192:
    http://www.sparkfun.com/datasheets/Sensors/Magneto/Tilt%20Compensated%20Compass.pdf
 */
 
 /*
-hardware & software comment
+  hardware & software comment
 
-I2C mode:
-1, solder the jumper "I2C EN" and the jumper of ADDR to 0x1E
-2, use Lsm303d.initI2C() function to initialize the Grove by I2C
+  I2C mode:
+  1, solder the jumper "I2C EN" and the jumper of ADDR to 0x1E
+  2, use Lsm303d.initI2C() function to initialize the Grove by I2C
 
-SPI mode:
+  SPI mode:
 
-1, break the jumper "I2C_EN" and the jumper ADDR to any side
-2, define a pin as chip select for SPI protocol.
-3, use Lsm303d.initSPI(SPI_CS) function to initialize the Grove by SPI
-SPI.h sets these for us in arduino
-const int SDI = 11;
-const int SDO = 12;
-const int SCL = 13;
+  1, break the jumper "I2C_EN" and the jumper ADDR to any side
+  2, define a pin as chip select for SPI protocol.
+  3, use Lsm303d.initSPI(SPI_CS) function to initialize the Grove by SPI
+  SPI.h sets these for us in arduino
+  const int SDI = 11;
+  const int SDO = 12;
+  const int SCL = 13;
 */
 
 #include <LSM303D.h>
@@ -67,58 +67,50 @@ float heading, titleHeading;
 
 #define SPI_CS 10
 
-void setup()
-{
-	char rtn = 0;
-    Serial.begin(9600);  // Serial is used for debugging
-    Serial.println("\r\npower on");
-    rtn = Lsm303d.initI2C();
-    //rtn = Lsm303d.initSPI(SPI_CS);
-    if(rtn != 0)  // Initialize the LSM303, using a SCALE full-scale range
-	{
-		Serial.println("\r\nLSM303D is not found");
-		while(1);
-	}
-	else
-	{
-		Serial.println("\r\nLSM303D is found");
-	}
+void setup() {
+  char rtn = 0;
+  Serial.begin(9600);  // Serial is used for debugging
+  Serial.println("\r\npower on");
+  rtn = Lsm303d.initI2C();
+  //rtn = Lsm303d.initSPI(SPI_CS);
+  if (rtn != 0) { // Initialize the LSM303, using a SCALE full-scale range
+    Serial.println("\r\nLSM303D is not found");
+    while (1);
+  } else {
+    Serial.println("\r\nLSM303D is found");
+  }
 }
 
-void loop()
-{
-	Serial.println("\r\n**************");
-	//getLSM303_accel(accel);  // get the acceleration values and store them in the accel array
-	Lsm303d.getAccel(accel);
-	while(!Lsm303d.isMagReady());// wait for the magnetometer readings to be ready
-	Lsm303d.getMag(mag);  // get the magnetometer values, store them in mag
-	
-	for (int i=0; i<3; i++)
-	{
-		realAccel[i] = accel[i] / pow(2, 15) * ACCELE_SCALE;  // calculate real acceleration values, in units of g
-	}
-	heading = Lsm303d.getHeading(mag);
-	titleHeading = Lsm303d.getTiltHeading(mag, realAccel);
-	
-	printValues();
-	
-	delay(200);  // delay for serial readability
+void loop() {
+  Serial.println("\r\n**************");
+  //getLSM303_accel(accel);  // get the acceleration values and store them in the accel array
+  Lsm303d.getAccel(accel);
+  while (!Lsm303d.isMagReady()); // wait for the magnetometer readings to be ready
+  Lsm303d.getMag(mag);  // get the magnetometer values, store them in mag
+
+  for (int i = 0; i < 3; i++) {
+    realAccel[i] = accel[i] / pow(2, 15) * ACCELE_SCALE;  // calculate real acceleration values, in units of g
+  }
+  heading = Lsm303d.getHeading(mag);
+  titleHeading = Lsm303d.getTiltHeading(mag, realAccel);
+
+  printValues();
+
+  delay(200);  // delay for serial readability
 }
 
-void printValues()
-{  
-	Serial.println("Acceleration of X,Y,Z is");
-	for (int i=0; i<3; i++)
-	{
-		Serial.print(realAccel[i]);
-		Serial.println("g");
-	}
-	/* print both the level, and tilt-compensated headings below to compare */
-	Serial.println("The clockwise angle between the magnetic north and x-axis: ");
-	Serial.print(heading, 3); // this only works if the sensor is level
-	Serial.println(" degrees");
-	Serial.print("The clockwise angle between the magnetic north and the projection");
-	Serial.println(" of the positive x-axis in the horizontal plane: ");
-	Serial.print(titleHeading, 3);  // see how awesome tilt compensation is?!
-	Serial.println(" degrees");
+void printValues() {
+  Serial.println("Acceleration of X,Y,Z is");
+  for (int i = 0; i < 3; i++) {
+    Serial.print(realAccel[i]);
+    Serial.println("g");
+  }
+  /* print both the level, and tilt-compensated headings below to compare */
+  Serial.println("The clockwise angle between the magnetic north and x-axis: ");
+  Serial.print(heading, 3); // this only works if the sensor is level
+  Serial.println(" degrees");
+  Serial.print("The clockwise angle between the magnetic north and the projection");
+  Serial.println(" of the positive x-axis in the horizontal plane: ");
+  Serial.print(titleHeading, 3);  // see how awesome tilt compensation is?!
+  Serial.println(" degrees");
 }