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 edu.wpi.first.wpilibj.simulation;
006
007import edu.wpi.first.hal.simulation.NotifyCallback;
008import edu.wpi.first.hal.simulation.SPIAccelerometerDataJNI;
009
010/** A class to control a simulated accelerometer over SPI. */
011public class SPIAccelerometerSim {
012  private final int m_index;
013
014  /**
015   * Construct a new simulation object.
016   *
017   * @param index the HAL index of the accelerometer
018   */
019  public SPIAccelerometerSim(int index) {
020    m_index = index;
021  }
022
023  /**
024   * Register a callback to be run when this accelerometer activates.
025   *
026   * @param callback the callback
027   * @param initialNotify whether to run the callback with the initial state
028   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
029   *     this object so GC doesn't cancel the callback.
030   */
031  public CallbackStore registerActiveCallback(NotifyCallback callback, boolean initialNotify) {
032    int uid = SPIAccelerometerDataJNI.registerActiveCallback(m_index, callback, initialNotify);
033    return new CallbackStore(m_index, uid, SPIAccelerometerDataJNI::cancelActiveCallback);
034  }
035
036  /**
037   * Check whether the accelerometer is active.
038   *
039   * @return true if active
040   */
041  public boolean getActive() {
042    return SPIAccelerometerDataJNI.getActive(m_index);
043  }
044
045  /**
046   * Define whether this accelerometer is active.
047   *
048   * @param active the new state
049   */
050  public void setActive(boolean active) {
051    SPIAccelerometerDataJNI.setActive(m_index, active);
052  }
053
054  /**
055   * Register a callback to be run whenever the range changes.
056   *
057   * @param callback the callback
058   * @param initialNotify whether to call the callback with the initial state
059   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
060   *     this object so GC doesn't cancel the callback.
061   */
062  public CallbackStore registerRangeCallback(NotifyCallback callback, boolean initialNotify) {
063    int uid = SPIAccelerometerDataJNI.registerRangeCallback(m_index, callback, initialNotify);
064    return new CallbackStore(m_index, uid, SPIAccelerometerDataJNI::cancelRangeCallback);
065  }
066
067  /**
068   * Check the range of this accelerometer.
069   *
070   * @return the accelerometer range
071   */
072  public int getRange() {
073    return SPIAccelerometerDataJNI.getRange(m_index);
074  }
075
076  /**
077   * Change the range of this accelerometer.
078   *
079   * @param range the new accelerometer range
080   */
081  public void setRange(int range) {
082    SPIAccelerometerDataJNI.setRange(m_index, range);
083  }
084
085  /**
086   * Register a callback to be run whenever the X axis value changes.
087   *
088   * @param callback the callback
089   * @param initialNotify whether to call the callback with the initial state
090   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
091   *     this object so GC doesn't cancel the callback.
092   */
093  public CallbackStore registerXCallback(NotifyCallback callback, boolean initialNotify) {
094    int uid = SPIAccelerometerDataJNI.registerXCallback(m_index, callback, initialNotify);
095    return new CallbackStore(m_index, uid, SPIAccelerometerDataJNI::cancelXCallback);
096  }
097
098  /**
099   * Measure the X axis value.
100   *
101   * @return the X axis measurement
102   */
103  public double getX() {
104    return SPIAccelerometerDataJNI.getX(m_index);
105  }
106
107  /**
108   * Change the X axis value of the accelerometer.
109   *
110   * @param x the new reading of the X axis
111   */
112  public void setX(double x) {
113    SPIAccelerometerDataJNI.setX(m_index, x);
114  }
115
116  /**
117   * Register a callback to be run whenever the Y axis value changes.
118   *
119   * @param callback the callback
120   * @param initialNotify whether to call the callback with the initial state
121   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
122   *     this object so GC doesn't cancel the callback.
123   */
124  public CallbackStore registerYCallback(NotifyCallback callback, boolean initialNotify) {
125    int uid = SPIAccelerometerDataJNI.registerYCallback(m_index, callback, initialNotify);
126    return new CallbackStore(m_index, uid, SPIAccelerometerDataJNI::cancelYCallback);
127  }
128
129  /**
130   * Measure the Y axis value.
131   *
132   * @return the Y axis measurement
133   */
134  public double getY() {
135    return SPIAccelerometerDataJNI.getY(m_index);
136  }
137
138  /**
139   * Change the Y axis value of the accelerometer.
140   *
141   * @param y the new reading of the Y axis
142   */
143  public void setY(double y) {
144    SPIAccelerometerDataJNI.setY(m_index, y);
145  }
146
147  /**
148   * Register a callback to be run whenever the Z axis value changes.
149   *
150   * @param callback the callback
151   * @param initialNotify whether to call the callback with the initial state
152   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
153   *     this object so GC doesn't cancel the callback.
154   */
155  public CallbackStore registerZCallback(NotifyCallback callback, boolean initialNotify) {
156    int uid = SPIAccelerometerDataJNI.registerZCallback(m_index, callback, initialNotify);
157    return new CallbackStore(m_index, uid, SPIAccelerometerDataJNI::cancelZCallback);
158  }
159
160  /**
161   * Measure the Z axis value.
162   *
163   * @return the Z axis measurement
164   */
165  public double getZ() {
166    return SPIAccelerometerDataJNI.getZ(m_index);
167  }
168
169  /**
170   * Change the Z axis value of the accelerometer.
171   *
172   * @param z the new reading of the Z axis
173   */
174  public void setZ(double z) {
175    SPIAccelerometerDataJNI.setZ(m_index, z);
176  }
177
178  /** Reset all simulation data of this object. */
179  public void resetData() {
180    SPIAccelerometerDataJNI.resetData(m_index);
181  }
182}