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;
006
007import edu.wpi.first.hal.DMAJNI;
008import edu.wpi.first.wpilibj.motorcontrol.PWMMotorController;
009
010public class DMA implements AutoCloseable {
011  final int m_dmaHandle;
012
013  public DMA() {
014    m_dmaHandle = DMAJNI.initialize();
015  }
016
017  @Override
018  public void close() {
019    DMAJNI.free(m_dmaHandle);
020  }
021
022  public void setPause(boolean pause) {
023    DMAJNI.setPause(m_dmaHandle, pause);
024  }
025
026  public void setTimedTrigger(double periodSeconds) {
027    DMAJNI.setTimedTrigger(m_dmaHandle, periodSeconds);
028  }
029
030  public void setTimedTriggerCycles(int cycles) {
031    DMAJNI.setTimedTriggerCycles(m_dmaHandle, cycles);
032  }
033
034  public void addEncoder(Encoder encoder) {
035    DMAJNI.addEncoder(m_dmaHandle, encoder.m_encoder);
036  }
037
038  public void addEncoderPeriod(Encoder encoder) {
039    DMAJNI.addEncoderPeriod(m_dmaHandle, encoder.m_encoder);
040  }
041
042  public void addCounter(Counter counter) {
043    DMAJNI.addCounter(m_dmaHandle, counter.m_counter);
044  }
045
046  public void addCounterPeriod(Counter counter) {
047    DMAJNI.addCounterPeriod(m_dmaHandle, counter.m_counter);
048  }
049
050  public void addDigitalSource(DigitalSource digitalSource) {
051    DMAJNI.addDigitalSource(m_dmaHandle, digitalSource.getPortHandleForRouting());
052  }
053
054  public void addDutyCycle(DutyCycle dutyCycle) {
055    DMAJNI.addDutyCycle(m_dmaHandle, dutyCycle.m_handle);
056  }
057
058  public void addAnalogInput(AnalogInput analogInput) {
059    DMAJNI.addAnalogInput(m_dmaHandle, analogInput.m_port);
060  }
061
062  public void addAveragedAnalogInput(AnalogInput analogInput) {
063    DMAJNI.addAveragedAnalogInput(m_dmaHandle, analogInput.m_port);
064  }
065
066  public void addAnalogAccumulator(AnalogInput analogInput) {
067    DMAJNI.addAnalogAccumulator(m_dmaHandle, analogInput.m_port);
068  }
069
070  /**
071   * Sets an external DMA trigger.
072   *
073   * @param source the source to trigger from.
074   * @param rising trigger on rising edge.
075   * @param falling trigger on falling edge.
076   * @return the index of the trigger
077   */
078  public int setExternalTrigger(DigitalSource source, boolean rising, boolean falling) {
079    return DMAJNI.setExternalTrigger(
080        m_dmaHandle,
081        source.getPortHandleForRouting(),
082        source.getAnalogTriggerTypeForRouting(),
083        rising,
084        falling);
085  }
086
087  public int setPwmEdgeTrigger(PWMMotorController pwm, boolean rising, boolean falling) {
088    return DMAJNI.setExternalTrigger(m_dmaHandle, pwm.getPwmHandle(), 0, rising, falling);
089  }
090
091  public int setPwmEdgeTrigger(PWM pwm, boolean rising, boolean falling) {
092    return DMAJNI.setExternalTrigger(m_dmaHandle, pwm.getHandle(), 0, rising, falling);
093  }
094
095  public void clearSensors() {
096    DMAJNI.clearSensors(m_dmaHandle);
097  }
098
099  public void clearExternalTriggers() {
100    DMAJNI.clearExternalTriggers(m_dmaHandle);
101  }
102
103  public void start(int queueDepth) {
104    DMAJNI.startDMA(m_dmaHandle, queueDepth);
105  }
106
107  public void stop() {
108    DMAJNI.stopDMA(m_dmaHandle);
109  }
110}