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