<commit_before>/**
 * @file Nanoshield_IMU.h
 * 
 * A library to access the Nanoshield IMU v2.0.
 * It uses the LSM303D for accelerometer, magnetometer and temperature.
 * 
 * Copyright (c) 2015 Circuitar
 * This software is released under the MIT license. See the attached LICENSE file for details.
 */
#include <Nanoshield_IMU.h>

#define INT16_T_TOP (32767.0)

Nanoshield_IMU::Nanoshield_IMU(int addr) {
  lsm303dAddress = LSM303D_ADDRESS | addr;

  hasBegun = false;
  accelScale = 2;
  magnetScale = 2;

  regCtrl0 = 0;
  regCtrl1 = 0 | LSM303D_AODR_1600  // Accelerometer data rate 1600Hz.
               | LSM303D_BDU        // Waits a read operation ends before updates a output register.
               | LSM303D_AZEN       // Accelerometer Z axis enabled.
               | LSM303D_AYEN       // Accelerometer Y axis enabled.
               | LSM303D_AXEN;      // Accelerometer X axis enabled.
  regCtrl2 = 0 | LSM303D_ABW_773    // Accelerometer anti-alias filter bandwidth 773Hz.
               | LSM303D_AFS_2G;    // Accelerometer full-scale +/- 2g.
  regCtrl5 = 0 | LSM303D_MODR_100;  // Magnetic data rate 100Hz.
  regCtrl6 = 0 | LSM303D_MFS_2GAUSS;    // Magnetic full-scale +/- 2gauss.
  regCtrl7 = 0 | LSM303D_MD_CONTINUOUS; // Magnetometer in continuous mode.

  fifoCtrl = 0;                     // FIFO disabled.

  // Interrupt generators disabled.
  igCfg1 = 0;
  igThs1 = 0; 
  igDur1 = 0; 
  igCfg2 = 0; 
  igThs2 = 0; 
  igDur2 = 0; 
}

void Nanoshield_IMU::begin() {
  Wire.begin();

  writeToLSM303DRegister(LSM303D_CTRL1, regCtrl1);
  writeToLSM303DRegister(LSM303D_CTRL2, regCtrl2);
  writeToLSM303DRegister(LSM303D_CTRL5, regCtrl5);
  writeToLSM303DRegister(LSM303D_CTRL6, regCtrl6);
  writeToLSM303DRegister(LSM303D_CTRL7, regCtrl7);

  // Not initializing CTRL0 and FIFO_CTRL with 0 results in some problems with FIFO.
  writeToLSM303DRegister(LSM303D_CTRL0, 0);
  writeToLSM303DRegister(LSM303D_FIFO_CTRL, 0);

  // Just rewrite CTRL0 and FIFO_CTRL if they are set to something
  if(regCtrl0 != 0 || fifoCtrl != 0) {
    writeToLSM303DRegister(LSM303D_CTRL0, regCtrl0);
    writeToLSM303DRegister(LSM303D_FIFO_CTRL, fifoCtrl);
  }

  writeToLSM303DRegister(LSM303D_IG_CFG1, 0);

  writeToLSM303DRegister(LSM303D_IG_CFG1, igCfg1);
  writeToLSM303DRegister(LSM303D_IG_THS1, igThs1);
  writeToLSM303DRegister(LSM303D_IG_DUR1, igDur1);
  writeToLSM303DRegister(LSM303D_IG_CFG2, igCfg2);
  writeToLSM303DRegister(LSM303D_IG_THS2, igThs2);
  writeToLSM303DRegister(LSM303D_IG_DUR2, igDur2);

  hasBegun = true;
}

void Nanoshield_IMU::setAccelerometerPowerDown() {
  regCtrl1 &= ~LSM303D_AODR_MASK;

  writeIfHasBegun(LSM303D_CTRL1, regCtrl1);
}

void Nanoshield_IMU::setAccelerometerDataRate(int8_t drate) {
  regCtrl1 &= ~LSM303D_AODR_MASK;<commit_msg>Removed IG_CFG register initialization.<commit_after>/**
 * @file Nanoshield_IMU.h
 * 
 * A library to access the Nanoshield IMU v2.0.
 * It uses the LSM303D for accelerometer, magnetometer and temperature.
 * 
 * Copyright (c) 2015 Circuitar
 * This software is released under the MIT license. See the attached LICENSE file for details.
 */
#include <Nanoshield_IMU.h>

#define INT16_T_TOP (32767.0)

Nanoshield_IMU::Nanoshield_IMU(int addr) {
  lsm303dAddress = LSM303D_ADDRESS | addr;

  hasBegun = false;
  accelScale = 2;
  magnetScale = 2;

  regCtrl0 = 0;
  regCtrl1 = 0 | LSM303D_AODR_1600  // Accelerometer data rate 1600Hz.
               | LSM303D_BDU        // Waits a read operation ends before updates a output register.
               | LSM303D_AZEN       // Accelerometer Z axis enabled.
               | LSM303D_AYEN       // Accelerometer Y axis enabled.
               | LSM303D_AXEN;      // Accelerometer X axis enabled.
  regCtrl2 = 0 | LSM303D_ABW_773    // Accelerometer anti-alias filter bandwidth 773Hz.
               | LSM303D_AFS_2G;    // Accelerometer full-scale +/- 2g.
  regCtrl5 = 0 | LSM303D_MODR_100;  // Magnetic data rate 100Hz.
  regCtrl6 = 0 | LSM303D_MFS_2GAUSS;    // Magnetic full-scale +/- 2gauss.
  regCtrl7 = 0 | LSM303D_MD_CONTINUOUS; // Magnetometer in continuous mode.

  fifoCtrl = 0;                     // FIFO disabled.

  // Interrupt generators disabled.
  igCfg1 = 0;
  igThs1 = 0; 
  igDur1 = 0; 
  igCfg2 = 0; 
  igThs2 = 0; 
  igDur2 = 0; 
}

void Nanoshield_IMU::begin() {
  Wire.begin();

  writeToLSM303DRegister(LSM303D_CTRL1, regCtrl1);
  writeToLSM303DRegister(LSM303D_CTRL2, regCtrl2);
  writeToLSM303DRegister(LSM303D_CTRL5, regCtrl5);
  writeToLSM303DRegister(LSM303D_CTRL6, regCtrl6);
  writeToLSM303DRegister(LSM303D_CTRL7, regCtrl7);

  // Not initializing CTRL0 and FIFO_CTRL with 0 results in some problems with FIFO.
  writeToLSM303DRegister(LSM303D_CTRL0, 0);
  writeToLSM303DRegister(LSM303D_FIFO_CTRL, 0);

  // Just rewrite CTRL0 and FIFO_CTRL if they are set to something
  if(regCtrl0 != 0 || fifoCtrl != 0) {
    writeToLSM303DRegister(LSM303D_CTRL0, regCtrl0);
    writeToLSM303DRegister(LSM303D_FIFO_CTRL, fifoCtrl);
  }

  writeToLSM303DRegister(LSM303D_IG_CFG1, igCfg1);
  writeToLSM303DRegister(LSM303D_IG_THS1, igThs1);
  writeToLSM303DRegister(LSM303D_IG_DUR1, igDur1);
  writeToLSM303DRegister(LSM303D_IG_CFG2, igCfg2);
  writeToLSM303DRegister(LSM303D_IG_THS2, igThs2);
  writeToLSM303DRegister(LSM303D_IG_DUR2, igDur2);

  hasBegun = true;
}

void Nanoshield_IMU::setAccelerometerPowerDown() {
  regCtrl1 &= ~LSM303D_AODR_MASK;

  writeIfHasBegun(LSM303D_CTRL1, regCtrl1);
}

void Nanoshield_IMU::setAccelerometerDataRate(int8_t drate) {
  regCtrl1 &= ~LSM303D_AODR_MASK;
{'fraction_non_alphanumeric': 0.08710550045085663, 'fraction_numerical': 0.08836789900811542, 'mean_word_length': 3.6762225969645868, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2438865', 'n_tokens_mistral': 2524, 'n_tokens_neox': 2174, 'n_words': 434}