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 public void setX(double x) { 120 AccelerometerDataJNI.setX(m_index, x); 121 } 122 123 /** 124 * Register a callback to be run whenever the Y axis value changes. 125 * 126 * @param callback the callback 127 * @param initialNotify whether to call the callback with the initial state 128 * @return the {@link CallbackStore} object associated with this callback. Save a reference to 129 * this object so GC doesn't cancel the callback. 130 */ 131 public CallbackStore registerYCallback(NotifyCallback callback, boolean initialNotify) { 132 int uid = AccelerometerDataJNI.registerYCallback(m_index, callback, initialNotify); 133 return new CallbackStore(m_index, uid, AccelerometerDataJNI::cancelYCallback); 134 } 135 136 /** 137 * Measure the Y axis value. 138 * 139 * @return the Y axis measurement 140 */ 141 public double getY() { 142 return AccelerometerDataJNI.getY(m_index); 143 } 144 145 /** 146 * Change the Y axis value of the accelerometer. 147 * 148 * @param y the new reading of the Y axis 149 */ 150 public void setY(double y) { 151 AccelerometerDataJNI.setY(m_index, y); 152 } 153 154 /** 155 * Register a callback to be run whenever the Z axis value changes. 156 * 157 * @param callback the callback 158 * @param initialNotify whether to call the callback with the initial state 159 * @return the {@link CallbackStore} object associated with this callback. Save a reference to 160 * this object so GC doesn't cancel the callback. 161 */ 162 public CallbackStore registerZCallback(NotifyCallback callback, boolean initialNotify) { 163 int uid = AccelerometerDataJNI.registerZCallback(m_index, callback, initialNotify); 164 return new CallbackStore(m_index, uid, AccelerometerDataJNI::cancelZCallback); 165 } 166 167 /** 168 * Measure the Z axis value. 169 * 170 * @return the Z axis measurement 171 */ 172 public double getZ() { 173 return AccelerometerDataJNI.getZ(m_index); 174 } 175 176 /** 177 * Change the Z axis value of the accelerometer. 178 * 179 * @param z the new reading of the Z axis 180 */ 181 public void setZ(double z) { 182 AccelerometerDataJNI.setZ(m_index, z); 183 } 184 185 /** Reset all simulation data of this object. */ 186 public void resetData() { 187 AccelerometerDataJNI.resetData(m_index); 188 } 189}