Advertisement
baratiistok3

cpp file

Dec 23rd, 2019
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.00 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <math.h>
  3.  
  4. #include <FXOS8700CQ.h>
  5.  
  6. // Public Methods //////////////////////////////////////////////////////////////
  7.  
  8. FXOS8700CQ::FXOS8700CQ(byte addr)
  9. {
  10.     address = addr;
  11.     accelFSR = AFS_2g;     // Set the scale below either 2, 4 or 8
  12.     accelODR = AODR_200HZ; // In hybrid mode, accel/mag data sample rates are half of this value
  13.     magOSR = MOSR_5;     // Choose magnetometer oversample rate
  14. }
  15.  
  16. // Writes a register
  17. void FXOS8700CQ::writeReg(byte reg, byte value)
  18. {
  19.     Wire.beginTransmission(address);
  20.     Wire.write(reg);
  21.     Wire.write(value);
  22.     Wire.endTransmission();
  23. }
  24.  
  25. // Reads a register
  26. byte FXOS8700CQ::readReg(byte reg)
  27. {
  28.     // byte value;
  29.  
  30.     // Wire.beginTransmission(address);
  31.     // Wire.write(reg);
  32.     // Wire.endTransmission();
  33.     // Wire.requestFrom(address, (uint8_t)1);
  34.     // value = Wire.read();
  35.     // Wire.endTransmission();
  36.  
  37.     // return value;
  38.  
  39.  
  40.  
  41.      byte rdata = 0xFF;
  42.   Wire.beginTransmission(address);
  43.   // The address is an integer so we slit in into an MSB and LSB byte
  44.   Wire.write((int)(reg >> 8));   // MSB
  45.   Wire.write((int)(reg & 0xFF)); // LSB
  46.   Wire.endTransmission();
  47.   Wire.requestFrom(address, 1);
  48.   if (Wire.available())
  49.   {
  50.       rdata = Wire.read();
  51.   }
  52.   return rdata;
  53. }
  54.  
  55. void FXOS8700CQ::readRegs(byte reg, uint8_t count, byte dest[])
  56. {
  57.     uint8_t i = 0;
  58.  
  59.     Wire.beginTransmission(address);   // Initialize the Tx buffer
  60.     Wire.write(reg);                   // Put slave register address in Tx buffer
  61.     Wire.endTransmission(false);       // Send the Tx buffer, but send a restart to keep connection alive
  62.     Wire.requestFrom(address, count);  // Read bytes from slave register address
  63.  
  64.     while (Wire.available()) {
  65.         dest[i++] = Wire.read();   // Put read results in the Rx buffer
  66.     }
  67. }
  68.  
  69. // Read the accelerometer data
  70. void FXOS8700CQ::readAccelData()
  71. {
  72.     uint8_t rawData[6];  // x/y/z accel register data stored here
  73.  
  74.     readRegs(FXOS8700CQ_OUT_X_MSB, 6, &rawData[0]);  // Read the six raw data registers into data array
  75.     accelData.x = ((int16_t) rawData[0] << 8 | rawData[1]) >> 2;
  76.     accelData.y = ((int16_t) rawData[2] << 8 | rawData[3]) >> 2;
  77.     accelData.z = ((int16_t) rawData[4] << 8 | rawData[5]) >> 2;
  78. }
  79.  
  80. // Read the magnometer data
  81. void FXOS8700CQ::readMagData()
  82. {
  83.     uint8_t rawData[6];  // x/y/z accel register data stored here
  84.  
  85.     readRegs(FXOS8700CQ_M_OUT_X_MSB, 6, &rawData[0]);  // Read the six raw data registers into data array
  86.     magData.x = ((int16_t) rawData[0] << 8 | rawData[1]) >> 2;
  87.     magData.y = ((int16_t) rawData[2] << 8 | rawData[3]) >> 2;
  88.     magData.z = ((int16_t) rawData[4] << 8 | rawData[5]) >> 2;
  89. }
  90.  
  91. // Read the temperature data
  92. void FXOS8700CQ::readTempData()
  93. {
  94.     tempData = readReg(FXOS8700CQ_TEMP);
  95. }
  96.  
  97. // Put the FXOS8700CQ into standby mode.
  98. // It must be in standby for modifying most registers
  99. void FXOS8700CQ::standby()
  100. {
  101.     byte c = readReg(FXOS8700CQ_CTRL_REG1);
  102.     writeReg(FXOS8700CQ_CTRL_REG1, c & ~(0x01));
  103. }
  104.  
  105. // Put the FXOS8700CQ into active mode.
  106. // Needs to be in this mode to output data.
  107. void FXOS8700CQ::active()
  108. {
  109.     byte c = readReg(FXOS8700CQ_CTRL_REG1);
  110.     writeReg(FXOS8700CQ_CTRL_REG1, c | 0x01);
  111. }
  112.  
  113. void FXOS8700CQ::init()
  114. {
  115.     standby();  // Must be in standby to change registers
  116.  
  117.     // Configure the accelerometer
  118.     writeReg(FXOS8700CQ_XYZ_DATA_CFG, accelFSR);  // Choose the full scale range to 2, 4, or 8 g.
  119.     //writeReg(FXOS8700CQ_CTRL_REG1, readReg(FXOS8700CQ_CTRL_REG1) & ~(0x38)); // Clear the 3 data rate bits 5:3
  120.     if (accelODR <= 7)
  121.         writeReg(FXOS8700CQ_CTRL_REG1, readReg(FXOS8700CQ_CTRL_REG1) | (accelODR << 3));      
  122.     //writeReg(FXOS8700CQ_CTRL_REG2, readReg(FXOS8700CQ_CTRL_REG2) & ~(0x03)); // clear bits 0 and 1
  123.     //writeReg(FXOS8700CQ_CTRL_REG2, readReg(FXOS8700CQ_CTRL_REG2) |  (0x02)); // select normal(00) or high resolution (10) mode
  124.  
  125.     // Configure the magnetometer
  126.     writeReg(FXOS8700CQ_M_CTRL_REG1, 0x80 | magOSR << 2 | 0x03); // Set auto-calibration, set oversampling, enable hybrid mode
  127.                                              
  128.     // Configure interrupts 1 and 2
  129.     //writeReg(CTRL_REG3, readReg(CTRL_REG3) & ~(0x02)); // clear bits 0, 1
  130.     //writeReg(CTRL_REG3, readReg(CTRL_REG3) |  (0x02)); // select ACTIVE HIGH, push-pull interrupts    
  131.     //writeReg(CTRL_REG4, readReg(CTRL_REG4) & ~(0x1D)); // clear bits 0, 3, and 4
  132.     //writeReg(CTRL_REG4, readReg(CTRL_REG4) |  (0x1D)); // DRDY, Freefall/Motion, P/L and tap ints enabled  
  133.     //writeReg(CTRL_REG5, 0x01);  // DRDY on INT1, P/L and taps on INT2
  134.  
  135.     active();  // Set to active to start reading
  136. }
  137.  
  138. // Get accelerometer resolution
  139. float FXOS8700CQ::getAres(void)
  140. {
  141.     switch (accelFSR)
  142.     {
  143.         // Possible accelerometer scales (and their register bit settings) are:
  144.         // 2 gs (00), 4 gs (01), 8 gs (10).
  145.         case AFS_2g:
  146.             return 2.0/8192.0;
  147.         break;
  148.         case AFS_4g:
  149.             return 4.0/8192.0;
  150.         break;
  151.         case AFS_8g:
  152.             return 8.0/8192.0;
  153.         break;
  154.     }
  155.  
  156.     return 0.0;
  157. }
  158.  
  159. // Get magnometer resolution
  160. float FXOS8700CQ::getMres(void)
  161. {
  162.     return 10./32768.;
  163. }
  164. // Private Methods //////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement