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.hal; 006 007import java.nio.ByteBuffer; 008import java.util.ArrayList; 009import java.util.List; 010 011/** 012 * JNI Wrapper for HAL<br> 013 * . 014 */ 015@SuppressWarnings({"AbbreviationAsWordInName", "MethodName"}) 016public final class HAL extends JNIWrapper { 017 public static native void waitForDSData(); 018 019 public static native boolean initialize(int timeout, int mode); 020 021 public static native void shutdown(); 022 023 public static native boolean hasMain(); 024 025 public static native void runMain(); 026 027 public static native void exitMain(); 028 029 private static native void simPeriodicBeforeNative(); 030 031 private static final List<Runnable> s_simPeriodicBefore = new ArrayList<>(); 032 033 public static class SimPeriodicBeforeCallback implements AutoCloseable { 034 private SimPeriodicBeforeCallback(Runnable r) { 035 m_run = r; 036 } 037 038 @Override 039 public void close() { 040 synchronized (s_simPeriodicBefore) { 041 s_simPeriodicBefore.remove(m_run); 042 } 043 } 044 045 private final Runnable m_run; 046 } 047 048 /** 049 * Registers a callback to be run by IterativeRobotBase prior to the user's simulationPeriodic 050 * code. 051 * 052 * @param r runnable 053 * @return Callback object (must be retained for callback to stay active). 054 */ 055 public static SimPeriodicBeforeCallback registerSimPeriodicBeforeCallback(Runnable r) { 056 synchronized (s_simPeriodicBefore) { 057 s_simPeriodicBefore.add(r); 058 } 059 return new SimPeriodicBeforeCallback(r); 060 } 061 062 /** 063 * Runs SimPeriodicBefore callbacks. IterativeRobotBase calls this prior to the user's 064 * simulationPeriodic code. 065 */ 066 public static void simPeriodicBefore() { 067 simPeriodicBeforeNative(); 068 synchronized (s_simPeriodicBefore) { 069 for (Runnable r : s_simPeriodicBefore) { 070 r.run(); 071 } 072 } 073 } 074 075 private static native void simPeriodicAfterNative(); 076 077 private static final List<Runnable> s_simPeriodicAfter = new ArrayList<>(); 078 079 public static class SimPeriodicAfterCallback implements AutoCloseable { 080 private SimPeriodicAfterCallback(Runnable r) { 081 m_run = r; 082 } 083 084 @Override 085 public void close() { 086 synchronized (s_simPeriodicAfter) { 087 s_simPeriodicAfter.remove(m_run); 088 } 089 } 090 091 private final Runnable m_run; 092 } 093 094 /** 095 * Registers a callback to be run by IterativeRobotBase after the user's simulationPeriodic code. 096 * 097 * @param r runnable 098 * @return Callback object (must be retained for callback to stay active). 099 */ 100 public static SimPeriodicAfterCallback registerSimPeriodicAfterCallback(Runnable r) { 101 synchronized (s_simPeriodicAfter) { 102 s_simPeriodicAfter.add(r); 103 } 104 return new SimPeriodicAfterCallback(r); 105 } 106 107 /** 108 * Runs SimPeriodicAfter callbacks. IterativeRobotBase calls this after the user's 109 * simulationPeriodic code. 110 */ 111 public static void simPeriodicAfter() { 112 simPeriodicAfterNative(); 113 synchronized (s_simPeriodicAfter) { 114 for (Runnable r : s_simPeriodicAfter) { 115 r.run(); 116 } 117 } 118 } 119 120 public static native void observeUserProgramStarting(); 121 122 public static native void observeUserProgramDisabled(); 123 124 public static native void observeUserProgramAutonomous(); 125 126 public static native void observeUserProgramTeleop(); 127 128 public static native void observeUserProgramTest(); 129 130 public static void report(int resource, int instanceNumber) { 131 report(resource, instanceNumber, 0, ""); 132 } 133 134 public static void report(int resource, int instanceNumber, int context) { 135 report(resource, instanceNumber, context, ""); 136 } 137 138 /** 139 * Report the usage of a resource of interest. 140 * 141 * <p>Original signature: <code>uint32_t report(tResourceType, uint8_t, uint8_t, const 142 * char*)</code> 143 * 144 * @param resource one of the values in the tResourceType above (max value 51). 145 * @param instanceNumber an index that identifies the resource instance. 146 * @param context an optional additional context number for some cases (such as module number). 147 * Set to 0 to omit. 148 * @param feature a string to be included describing features in use on a specific resource. 149 * Setting the same resource more than once allows you to change the feature string. 150 * @return TODO 151 */ 152 public static native int report(int resource, int instanceNumber, int context, String feature); 153 154 public static native int nativeGetControlWord(); 155 156 @SuppressWarnings("MissingJavadocMethod") 157 public static void getControlWord(ControlWord controlWord) { 158 int word = nativeGetControlWord(); 159 controlWord.update( 160 (word & 1) != 0, 161 ((word >> 1) & 1) != 0, 162 ((word >> 2) & 1) != 0, 163 ((word >> 3) & 1) != 0, 164 ((word >> 4) & 1) != 0, 165 ((word >> 5) & 1) != 0); 166 } 167 168 private static native int nativeGetAllianceStation(); 169 170 @SuppressWarnings("MissingJavadocMethod") 171 public static AllianceStationID getAllianceStation() { 172 switch (nativeGetAllianceStation()) { 173 case 0: 174 return AllianceStationID.Red1; 175 case 1: 176 return AllianceStationID.Red2; 177 case 2: 178 return AllianceStationID.Red3; 179 case 3: 180 return AllianceStationID.Blue1; 181 case 4: 182 return AllianceStationID.Blue2; 183 case 5: 184 return AllianceStationID.Blue3; 185 default: 186 return null; 187 } 188 } 189 190 @SuppressWarnings("MissingJavadocMethod") 191 public static native boolean isNewControlData(); 192 193 @SuppressWarnings("MissingJavadocMethod") 194 public static native void releaseDSMutex(); 195 196 @SuppressWarnings("MissingJavadocMethod") 197 public static native boolean waitForDSDataTimeout(double timeout); 198 199 public static final int kMaxJoystickAxes = 12; 200 public static final int kMaxJoystickPOVs = 12; 201 202 public static native short getJoystickAxes(byte joystickNum, float[] axesArray); 203 204 public static native short getJoystickPOVs(byte joystickNum, short[] povsArray); 205 206 public static native int getJoystickButtons(byte joystickNum, ByteBuffer count); 207 208 public static native int setJoystickOutputs( 209 byte joystickNum, int outputs, short leftRumble, short rightRumble); 210 211 public static native int getJoystickIsXbox(byte joystickNum); 212 213 public static native int getJoystickType(byte joystickNum); 214 215 public static native String getJoystickName(byte joystickNum); 216 217 public static native int getJoystickAxisType(byte joystickNum, byte axis); 218 219 public static native double getMatchTime(); 220 221 public static native boolean getSystemActive(); 222 223 public static native boolean getBrownedOut(); 224 225 public static native int getMatchInfo(MatchInfoData info); 226 227 public static native int sendError( 228 boolean isError, 229 int errorCode, 230 boolean isLVCode, 231 String details, 232 String location, 233 String callStack, 234 boolean printMsg); 235 236 public static native int sendConsoleLine(String line); 237 238 public static native int getPortWithModule(byte module, byte channel); 239 240 public static native int getPort(byte channel); 241 242 private HAL() {} 243}