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.BufferCallback;
008import edu.wpi.first.hal.simulation.ConstBufferCallback;
009import edu.wpi.first.hal.simulation.NotifyCallback;
010import edu.wpi.first.hal.simulation.SPIDataJNI;
011import edu.wpi.first.hal.simulation.SpiReadAutoReceiveBufferCallback;
012
013/** A class for controlling a simulated SPI device. */
014public class SPISim {
015  private final int m_index;
016
017  /** Create a new simulated SPI device. */
018  public SPISim() {
019    m_index = 0;
020  }
021
022  /**
023   * Register a callback to be run when this device is initialized.
024   *
025   * @param callback the callback
026   * @param initialNotify whether to run the callback with the initial state
027   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
028   *     this object so GC doesn't cancel the callback.
029   */
030  public CallbackStore registerInitializedCallback(NotifyCallback callback, boolean initialNotify) {
031    int uid = SPIDataJNI.registerInitializedCallback(m_index, callback, initialNotify);
032    return new CallbackStore(m_index, uid, SPIDataJNI::cancelInitializedCallback);
033  }
034
035  /**
036   * Check whether this device has been initialized.
037   *
038   * @return true if initialized
039   */
040  public boolean getInitialized() {
041    return SPIDataJNI.getInitialized(m_index);
042  }
043
044  /**
045   * Define whether this device has been initialized.
046   *
047   * @param initialized whether this object is initialized
048   */
049  public void setInitialized(boolean initialized) {
050    SPIDataJNI.setInitialized(m_index, initialized);
051  }
052
053  /**
054   * Register a callback to be run whenever a `read` operation is executed.
055   *
056   * @param callback the callback
057   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
058   *     this object so GC doesn't cancel the callback.
059   */
060  public CallbackStore registerReadCallback(BufferCallback callback) {
061    int uid = SPIDataJNI.registerReadCallback(m_index, callback);
062    return new CallbackStore(m_index, uid, SPIDataJNI::cancelReadCallback);
063  }
064
065  /**
066   * Register a callback to be run whenever a `write` operation is executed.
067   *
068   * @param callback the callback
069   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
070   *     this object so GC doesn't cancel the callback.
071   */
072  public CallbackStore registerWriteCallback(ConstBufferCallback callback) {
073    int uid = SPIDataJNI.registerWriteCallback(m_index, callback);
074    return new CallbackStore(m_index, uid, SPIDataJNI::cancelWriteCallback);
075  }
076
077  public CallbackStore registerReadAutoReceiveBufferCallback(
078      SpiReadAutoReceiveBufferCallback callback) {
079    int uid = SPIDataJNI.registerReadAutoReceiveBufferCallback(m_index, callback);
080    return new CallbackStore(m_index, uid, SPIDataJNI::cancelReadAutoReceiveBufferCallback);
081  }
082
083  /** Reset all simulation data. */
084  public void resetData() {
085    SPIDataJNI.resetData(m_index);
086  }
087}