001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package org.wpilib.simulation;
006
007import java.util.Objects;
008import org.wpilib.hardware.accelerometer.ADXL345_I2C;
009import org.wpilib.hardware.hal.SimDouble;
010
011/** Class to control a simulated ADXL345. */
012public class ADXL345Sim {
013  private SimDouble m_simX;
014  private SimDouble m_simY;
015  private SimDouble m_simZ;
016
017  /**
018   * Constructor.
019   *
020   * @param device The device to simulate
021   */
022  public ADXL345Sim(ADXL345_I2C device) {
023    SimDeviceSim simDevice =
024        new SimDeviceSim(
025            "Accel:ADXL345_I2C"
026                + "["
027                + device.getPort().value
028                + ","
029                + device.getDeviceAddress()
030                + "]");
031    initSim(simDevice);
032  }
033
034  private void initSim(SimDeviceSim simDevice) {
035    Objects.requireNonNull(simDevice);
036
037    m_simX = simDevice.getDouble("x");
038    m_simY = simDevice.getDouble("y");
039    m_simZ = simDevice.getDouble("z");
040
041    Objects.requireNonNull(m_simX);
042    Objects.requireNonNull(m_simY);
043    Objects.requireNonNull(m_simZ);
044  }
045
046  /**
047   * Sets the X acceleration.
048   *
049   * @param accel The X acceleration.
050   */
051  public void setX(double accel) {
052    m_simX.set(accel);
053  }
054
055  /**
056   * Sets the Y acceleration.
057   *
058   * @param accel The Y acceleration.
059   */
060  public void setY(double accel) {
061    m_simY.set(accel);
062  }
063
064  /**
065   * Sets the Z acceleration.
066   *
067   * @param accel The Z acceleration.
068   */
069  public void setZ(double accel) {
070    m_simZ.set(accel);
071  }
072}