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