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 010/** DMA sample. */ 011public class DMASample { 012 /** DMA read status. */ 013 public enum DMAReadStatus { 014 /** OK status. */ 015 kOk(1), 016 /** Timeout status. */ 017 kTimeout(2), 018 /** Error status. */ 019 kError(3); 020 021 private final int value; 022 023 DMAReadStatus(int value) { 024 this.value = value; 025 } 026 027 /** 028 * Returns the DMAReadStatus value. 029 * 030 * @return The DMAReadStatus value. 031 */ 032 public int getValue() { 033 return value; 034 } 035 036 /** 037 * Constructs a DMAReadStatus from a raw value. 038 * 039 * @param value raw value 040 * @return enum value 041 */ 042 public static DMAReadStatus getValue(int value) { 043 if (value == 1) { 044 return kOk; 045 } else if (value == 2) { 046 return kTimeout; 047 } 048 return kError; 049 } 050 } 051 052 private final DMAJNISample m_dmaSample = new DMAJNISample(); 053 054 /** Default constructor. */ 055 public DMASample() {} 056 057 /** 058 * Retrieves a new DMA sample. 059 * 060 * @param dma DMA object. 061 * @param timeoutSeconds Timeout in seconds for retrieval. 062 * @return DMA read status. 063 */ 064 public DMAReadStatus update(DMA dma, double timeoutSeconds) { 065 return DMAReadStatus.getValue(m_dmaSample.update(dma.m_dmaHandle, timeoutSeconds)); 066 } 067 068 /** 069 * Returns the DMA sample time in microseconds. 070 * 071 * @return The DMA sample time in microseconds. 072 */ 073 public long getTime() { 074 return m_dmaSample.getTime(); 075 } 076 077 /** 078 * Returns the DMA sample timestamp in seconds. 079 * 080 * @return The DMA sample timestamp in seconds. 081 */ 082 public double getTimeStamp() { 083 return getTime() * 1.0e-6; 084 } 085 086 /** 087 * Returns the DMA sample capture size. 088 * 089 * @return The DMA sample capture size. 090 */ 091 public int getCaptureSize() { 092 return m_dmaSample.getCaptureSize(); 093 } 094 095 /** 096 * Returns the number of DMA trigger channels. 097 * 098 * @return The number of DMA trigger channels. 099 */ 100 public int getTriggerChannels() { 101 return m_dmaSample.getTriggerChannels(); 102 } 103 104 /** 105 * Returns the number of remaining samples. 106 * 107 * @return The number of remaining samples. 108 */ 109 public int getRemaining() { 110 return m_dmaSample.getRemaining(); 111 } 112 113 /** 114 * Returns raw encoder value from DMA. 115 * 116 * @param encoder Encoder used for DMA. 117 * @return Raw encoder value from DMA. 118 */ 119 public int getEncoderRaw(Encoder encoder) { 120 return m_dmaSample.getEncoder(encoder.m_encoder); 121 } 122 123 /** 124 * Returns encoder distance from DMA. 125 * 126 * @param encoder Encoder used for DMA. 127 * @return Encoder distance from DMA. 128 */ 129 public double getEncoderDistance(Encoder encoder) { 130 double val = getEncoderRaw(encoder); 131 val *= encoder.getDecodingScaleFactor(); 132 val *= encoder.getDistancePerPulse(); 133 return val; 134 } 135 136 /** 137 * Returns raw encoder period from DMA. 138 * 139 * @param encoder Encoder used for DMA. 140 * @return Raw encoder period from DMA. 141 */ 142 public int getEncoderPeriodRaw(Encoder encoder) { 143 return m_dmaSample.getEncoderPeriod(encoder.m_encoder); 144 } 145 146 /** 147 * Returns counter value from DMA. 148 * 149 * @param counter Counter used for DMA. 150 * @return Counter value from DMA. 151 */ 152 public int getCounter(Counter counter) { 153 return m_dmaSample.getCounter(counter.m_counter); 154 } 155 156 /** 157 * Returns counter period from DMA. 158 * 159 * @param counter Counter used for DMA. 160 * @return Counter period from DMA. 161 */ 162 public int getCounterPeriod(Counter counter) { 163 return m_dmaSample.getCounterPeriod(counter.m_counter); 164 } 165 166 /** 167 * Returns digital source value from DMA. 168 * 169 * @param digitalSource DigitalSource used for DMA. 170 * @return DigitalSource value from DMA. 171 */ 172 public boolean getDigitalSource(DigitalSource digitalSource) { 173 return m_dmaSample.getDigitalSource(digitalSource.getPortHandleForRouting()); 174 } 175 176 /** 177 * Returns raw analog input value from DMA. 178 * 179 * @param analogInput AnalogInput used for DMA. 180 * @return Raw analog input value from DMA. 181 */ 182 public int getAnalogInputRaw(AnalogInput analogInput) { 183 return m_dmaSample.getAnalogInput(analogInput.m_port); 184 } 185 186 /** 187 * Returns analog input voltage from DMA. 188 * 189 * @param analogInput AnalogInput used for DMA. 190 * @return Analog input voltage from DMA. 191 */ 192 public double getAnalogInputVoltage(AnalogInput analogInput) { 193 return AnalogJNI.getAnalogValueToVolts(analogInput.m_port, getAnalogInputRaw(analogInput)); 194 } 195 196 /** 197 * Returns averaged raw analog input value from DMA. 198 * 199 * @param analogInput AnalogInput used for DMA. 200 * @return Averaged raw analog input value from DMA. 201 */ 202 public int getAveragedAnalogInputRaw(AnalogInput analogInput) { 203 return m_dmaSample.getAnalogInputAveraged(analogInput.m_port); 204 } 205 206 /** 207 * Returns averaged analog input voltage from DMA. 208 * 209 * @param analogInput AnalogInput used for DMA. 210 * @return Averaged analog input voltage from DMA. 211 */ 212 public double getAveragedAnalogInputVoltage(AnalogInput analogInput) { 213 return AnalogJNI.getAnalogValueToVolts( 214 analogInput.m_port, getAveragedAnalogInputRaw(analogInput)); 215 } 216 217 /** 218 * Returns raw duty cycle output from DMA. 219 * 220 * @param dutyCycle DutyCycle used for DMA. 221 * @return Raw duty cycle output from DMA. 222 */ 223 public int getDutyCycleOutputRaw(DutyCycle dutyCycle) { 224 return m_dmaSample.getDutyCycleOutput(dutyCycle.m_handle); 225 } 226 227 /** 228 * Returns duty cycle output (0-1) from DMA. 229 * 230 * @param dutyCycle DutyCycle used for DMA. 231 * @return Duty cycle output (0-1) from DMA. 232 */ 233 public double getDutyCycleOutput(DutyCycle dutyCycle) { 234 return m_dmaSample.getDutyCycleOutput(dutyCycle.m_handle) 235 / (double) dutyCycle.getOutputScaleFactor(); 236 } 237}