RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
WPIUtilJNI.java
1// TODO - RobotCore - Modified files
2
3// Copyright (c) FIRST and other WPILib contributors.
4// Open Source Software; you can modify and/or share it under the terms of
5// the WPILib BSD license file in the root directory of this project.
6
7package org.wpilib.util;
8
9import org.wpilib.hardware.hal.HALUtil;
10import org.wpilib.hardware.hal.PiNotifier.PiSignals;
11
12import robotCore.RobotCore;
13
14// import java.nio.ByteBuffer;
15
17public class WPIUtilJNI {
18 public static void waitForObject(int handle) throws InterruptedException {
19 // // System.out.println(String.format("WPIUtilJNI.waitForObject(%d)", handle));
20 // // throw new UnsupportedOperationException("Unimplemented method 'waitForObject'");
21 // if (handle > 0) {
22 // NotifierJNI.waitForIt(handle); // indicates notifier handle
23 // }
25 // TODO - Must verify
26 }
27
28 public static long now() {
29 // throw new UnsupportedOperationException("Unimplemented method 'now'");
30 return HALUtil.getMonotonicTime();
31 }
32
33 static long m_startTime = 0;
34
35 public static long getProgramStartTime() {
36 if (m_startTime == 0) {
37 m_startTime = HALUtil.getMonotonicTime();
38 }
39 // System.out.println(String.format("NetworkTablesJNI.getProgramStartTime: %d", m_startTime));
40 // throw new UnsupportedOperationException("Unimplemented method 'getProgramStartTime'");
41 return m_startTime; // TODO - Must verify
42 }
43
44 static private RobotCore m_robotCore;
45
46 public static void checkMsvcRuntime() {
47 m_robotCore = new RobotCore();
48
49 // throw new UnsupportedOperationException("Unimplemented method 'checkMsvcRuntime'");
50 // TODO - Must review
51 }
52
53 public static int makeEvent(boolean manualReset, boolean initialState) {
54 // System.out.println(String.format("WPIUtilJNI.makeEvent(%b, %b", manualReset, initialState));
55 // throw new UnsupportedOperationException("Unimplemented method 'makeEvent'");
56 return 0; // TODO - Must review
57 }
58
59 public static void destroyEvent(int eventHandle) {
60 throw new UnsupportedOperationException("Unimplemented method 'destroyEvent'");
61 // TODO - Must fix
62 }
63
64 public static void setEvent(int eventHandle) {
65 throw new UnsupportedOperationException("Unimplemented method 'setEvent'");
66 // TODO - Must fix
67 }
68
69 public static void resetEvent(int eventHandle) {
70 throw new UnsupportedOperationException("Unimplemented method 'resetEvent'");
71 // TODO - Must fix
72 }
73
74 public static int[] waitForObjects(int[] handles) throws InterruptedException {
75 // System.out.println(String.format("WPIUtilJNI.waitForObjects(%d)", handles.length));
76 // throw new UnsupportedOperationException("Unimplemented method 'waitForObjects'");
77 return null; // TODO - Must fix
78 }
79
80 public static boolean waitForObjectTimeout(int handle, double timeout) throws InterruptedException {
81 throw new UnsupportedOperationException("Unimplemented method 'waitForObjectTimeout'");
82 // return false; // TODO - Must fix
83 }
84
85 public static long getSystemTime() {
86 throw new UnsupportedOperationException("Unimplemented method 'getSystemTime'");
87 // return 0; // TODO - Must fix
88 }
89
90 // static int createAlert(String group, String id, String text, int level) {
91 // throw new UnsupportedOperationException("Unimplemented method 'createAlert'");
92 // // return 0; // TODO - Must fix
93 // }
94private static String levelName(int level) {
95 return switch (level) {
96 case AlertDataJNI.LEVEL_HIGH -> "HIGH";
97 case AlertDataJNI.LEVEL_MEDIUM -> "MEDIUM";
98 case AlertDataJNI.LEVEL_LOW -> "LOW";
99 default -> "LEVEL " + level;
100 };
101}
102
103private static final class AlertState {
104 final String group, id; final int level;
105 volatile String text; volatile boolean active;
106 AlertState(String g, String i, String t, int l) { group = g; id = i; text = t; level = l; }
107}
108private static final java.util.concurrent.ConcurrentHashMap<Integer, AlertState> s_alerts =
109 new java.util.concurrent.ConcurrentHashMap<>();
110private static final java.util.concurrent.atomic.AtomicInteger s_nextAlert = new java.util.concurrent.atomic.AtomicInteger();
111
112static int createAlert(String group, String id, String text, int level) {
113 int h = s_nextAlert.incrementAndGet(); // never 0: Alert treats 0 as "closed"
114 s_alerts.put(h, new AlertState(group, id, text, level));
115 return h;
116}
117static void destroyAlert(int h) { s_alerts.remove(h); }
118
119static void setAlertActive(int h, boolean active) {
120 AlertState a = s_alerts.get(h);
121 if (a == null) return;
122 if (active && !a.active) { // print on rising edge only
123 System.err.println("ALERT [" + levelName(a.level) + "] " + a.group + "/" + a.id + ": " + a.text);
124 }
125 a.active = active;
126}
127static boolean isAlertActive(int h) { AlertState a = s_alerts.get(h); return a != null && a.active; }
128static void setAlertText(int h, String t) { AlertState a = s_alerts.get(h); if (a != null) a.text = t; }
129static String getAlertText(int h) { AlertState a = s_alerts.get(h); return a == null ? "" : a.text; }
130
131 // static void setAlertActive(int alertHandle, boolean active) {
132 // throw new UnsupportedOperationException("Unimplemented method 'setAlertActive'");
133 // // TODO - Must fix
134 // }
135
136 // static boolean isAlertActive(int alertHandle) {
137 // throw new UnsupportedOperationException("Unimplemented method 'isAlertActive'");
138 // // return false; // TODO - Must fix
139 // }
140
141 // static void setAlertText(int alertHandle, String text) {
142 // throw new UnsupportedOperationException("Unimplemented method 'setAlertText'");
143 // // TODO - Must fix
144 // }
145
146 // static String getAlertText(int alertHandle) {
147 // throw new UnsupportedOperationException("Unimplemented method 'getAlertText'");
148 // // return null; // TODO - Must fix
149 // }
150
151 // static void destroyAlert(int alertHandle) {
152 // throw new UnsupportedOperationException("Unimplemented method 'destroyAlert'");
153 // // TODO - Must fix
154 // }
155
156
157
158
159// /** Checks if the MSVC runtime is valid. Throws a runtime exception if not. */
160// public static native void checkMsvcRuntime();
161
162// /**
163// * Write the given string to stderr.
164// *
165// * @param str String to write.
166// */
167// public static native void writeStderr(String str);
168
169// /**
170// * Enable mock time. The mock time is initially set to 0. This also sets program start time to 0.
171// */
172// public static native void enableMockTime();
173
174// /** Disable mock time. */
175// public static native void disableMockTime();
176
177// /**
178// * Set mock time.
179// *
180// * @param time The desired time in nanoseconds.
181// */
182// public static native void setMockTime(long time);
183
184// /**
185// * Returns the time.
186// *
187// * @return The time.
188// */
189// public static native long now();
190
191// /**
192// * Returns the timestamp at which the program started.
193// *
194// * @return The program start timestamp in nanoseconds.
195// */
196// public static native long getProgramStartTime();
197
198// /**
199// * Returns the system time.
200// *
201// * @return The system time.
202// */
203// public static native long getSystemTime();
204
205// static native int createAlert(String group, String id, String text, int level);
206
207// static native void destroyAlert(int alertHandle);
208
209// static native void setAlertActive(int alertHandle, boolean active);
210
211// static native boolean isAlertActive(int alertHandle);
212
213// static native void setAlertText(int alertHandle, String text);
214
215// static native String getAlertText(int alertHandle);
216
217// /**
218// * Creates an event. Events have binary state (signaled or not signaled) and may be either
219// * automatically reset or manually reset. Automatic-reset events go to non-signaled state when a
220// * WaitForObject is woken up by the event; manual-reset events require ResetEvent() to be called
221// * to set the event to non-signaled state; if ResetEvent() is not called, any waiter on that event
222// * will immediately wake when called.
223// *
224// * @param manualReset true for manual reset, false for automatic reset
225// * @param initialState true to make the event initially in signaled state
226// * @return Event handle
227// */
228// public static native int makeEvent(boolean manualReset, boolean initialState);
229
230// /**
231// * Destroys an event. Destruction wakes up any waiters.
232// *
233// * @param eventHandle event handle
234// */
235// public static native void destroyEvent(int eventHandle);
236
237// /**
238// * Sets an event to signaled state.
239// *
240// * @param eventHandle event handle
241// */
242// public static native void setEvent(int eventHandle);
243
244// /**
245// * Sets an event to non-signaled state.
246// *
247// * @param eventHandle event handle
248// */
249// public static native void resetEvent(int eventHandle);
250
251// /**
252// * Creates a semaphore. Semaphores keep an internal counter. Releasing the semaphore increases the
253// * count. A semaphore with a non-zero count is considered signaled. When a waiter wakes up it
254// * atomically decrements the count by 1. This is generally useful in a single-supplier,
255// * multiple-consumer scenario.
256// *
257// * @param initialCount initial value for the semaphore's internal counter
258// * @param maximumCount maximum value for the semaphore's internal counter
259// * @return Semaphore handle
260// */
261// public static native int makeSemaphore(int initialCount, int maximumCount);
262
263// /**
264// * Destroys a semaphore. Destruction wakes up any waiters.
265// *
266// * @param semHandle semaphore handle
267// */
268// public static native void destroySemaphore(int semHandle);
269
270// /**
271// * Releases N counts of a semaphore.
272// *
273// * @param semHandle semaphore handle
274// * @param releaseCount amount to add to semaphore's internal counter; must be positive
275// * @return True on successful release, false on failure (e.g. release count would exceed maximum
276// * value, or handle invalid)
277// */
278// public static native boolean releaseSemaphore(int semHandle, int releaseCount);
279
280// static native long allocateRawFrame();
281
282// static native void freeRawFrame(long frame);
283
284// static native long getRawFrameDataPtr(long frame);
285
286// static native void setRawFrameData(
287// long frame, ByteBuffer data, int size, int width, int height, int stride, int pixelFormat);
288
289// static native void setRawFrameInfo(
290// long frame, int size, int width, int height, int stride, int pixelFormat);
291
292// static native void setRawFrameTime(long frame, long timestamp, int timeSource);
293
294// /**
295// * Waits for a handle to be signaled.
296// *
297// * @param handle handle to wait on
298// * @throws InterruptedException on failure (e.g. object was destroyed)
299// */
300// public static native void waitForObject(int handle) throws InterruptedException;
301
302// /**
303// * Waits for a handle to be signaled, with timeout.
304// *
305// * @param handle handle to wait on
306// * @param timeout timeout in seconds
307// * @return True if timeout reached without handle being signaled
308// * @throws InterruptedException on failure (e.g. object was destroyed)
309// */
310// public static native boolean waitForObjectTimeout(int handle, double timeout)
311// throws InterruptedException;
312
313// /**
314// * Waits for one or more handles to be signaled.
315// *
316// * <p>Invalid handles are treated as signaled; the returned array will have the handle error bit
317// * set for any invalid handles.
318// *
319// * @param handles array of handles to wait on
320// * @return array of signaled handles
321// * @throws InterruptedException on failure (e.g. no objects were signaled)
322// */
323// public static native int[] waitForObjects(int[] handles) throws InterruptedException;
324
325// /**
326// * Waits for one or more handles to be signaled, with timeout.
327// *
328// * <p>Invalid handles are treated as signaled; the returned array will have the handle error bit
329// * set for any invalid handles.
330// *
331// * @param handles array of handles to wait on
332// * @param timeout timeout in seconds
333// * @return array of signaled handles; empty if timeout reached without any handle being signaled
334// * @throws InterruptedException on failure (e.g. no objects were signaled and no timeout)
335// */
336// public static native int[] waitForObjectsTimeout(int[] handles, double timeout)
337// throws InterruptedException;
338
339// /** Utility class. */
340// private WPIUtilJNI() {}
341}