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