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.AnalogJNI; 008import edu.wpi.first.hal.DMAJNISample; 009 010public class DMASample { 011 public enum DMAReadStatus { 012 kOk(1), 013 kTimeout(2), 014 kError(3); 015 016 private final int value; 017 018 DMAReadStatus(int value) { 019 this.value = value; 020 } 021 022 public int getValue() { 023 return value; 024 } 025 026 /** 027 * Constructs a DMAReadStatus from a raw value. 028 * 029 * @param value raw value 030 * @return enum value 031 */ 032 public static DMAReadStatus getValue(int value) { 033 if (value == 1) { 034 return kOk; 035 } else if (value == 2) { 036 return kTimeout; 037 } 038 return kError; 039 } 040 } 041 042 private final DMAJNISample m_dmaSample = new DMAJNISample(); 043 044 public DMAReadStatus update(DMA dma, double timeoutSeconds) { 045 return DMAReadStatus.getValue(m_dmaSample.update(dma.m_dmaHandle, timeoutSeconds)); 046 } 047 048 public int getCaptureSize() { 049 return m_dmaSample.getCaptureSize(); 050 } 051 052 public int getTriggerChannels() { 053 return m_dmaSample.getTriggerChannels(); 054 } 055 056 public int getRemaining() { 057 return m_dmaSample.getRemaining(); 058 } 059 060 public long getTime() { 061 return m_dmaSample.getTime(); 062 } 063 064 public double getTimeStamp() { 065 return getTime() * 1.0e-6; 066 } 067 068 public int getEncoderRaw(Encoder encoder) { 069 return m_dmaSample.getEncoder(encoder.m_encoder); 070 } 071 072 /** 073 * Gets the scaled encoder distance for this sample. 074 * 075 * @param encoder the encoder to use to read 076 * @return the distance 077 */ 078 public double getEncoderDistance(Encoder encoder) { 079 double val = getEncoderRaw(encoder); 080 val *= encoder.getDecodingScaleFactor(); 081 val *= encoder.getDistancePerPulse(); 082 return val; 083 } 084 085 public int getEncoderPeriodRaw(Encoder encoder) { 086 return m_dmaSample.getEncoderPeriod(encoder.m_encoder); 087 } 088 089 public int getCounter(Counter counter) { 090 return m_dmaSample.getCounter(counter.m_counter); 091 } 092 093 public int getCounterPeriod(Counter counter) { 094 return m_dmaSample.getCounterPeriod(counter.m_counter); 095 } 096 097 public boolean getDigitalSource(DigitalSource digitalSource) { 098 return m_dmaSample.getDigitalSource(digitalSource.getPortHandleForRouting()); 099 } 100 101 public int getAnalogInputRaw(AnalogInput analogInput) { 102 return m_dmaSample.getAnalogInput(analogInput.m_port); 103 } 104 105 public double getAnalogInputVoltage(AnalogInput analogInput) { 106 return AnalogJNI.getAnalogValueToVolts(analogInput.m_port, getAnalogInputRaw(analogInput)); 107 } 108 109 public int getAveragedAnalogInputRaw(AnalogInput analogInput) { 110 return m_dmaSample.getAnalogInputAveraged(analogInput.m_port); 111 } 112 113 public double getAveragedAnalogInputVoltage(AnalogInput analogInput) { 114 return AnalogJNI.getAnalogValueToVolts( 115 analogInput.m_port, getAveragedAnalogInputRaw(analogInput)); 116 } 117 118 public int getDutyCycleOutputRaw(DutyCycle dutyCycle) { 119 return m_dmaSample.getDutyCycleOutput(dutyCycle.m_handle); 120 } 121 122 public double getDutyCycleOutput(DutyCycle dutyCycle) { 123 return m_dmaSample.getDutyCycleOutput(dutyCycle.m_handle) 124 / (double) dutyCycle.getOutputScaleFactor(); 125 } 126}