Merge branch '2022'

This commit is contained in:
Peter Johnson
2021-05-09 14:15:40 -07:00
765 changed files with 5914 additions and 13714 deletions

View File

@@ -187,6 +187,7 @@ if (WITH_GUI)
add_subdirectory(imgui)
add_subdirectory(wpigui)
add_subdirectory(glass)
add_subdirectory(outlineviewer)
endif()
if (WITH_CSCORE)

View File

@@ -10,8 +10,8 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import edu.wpi.cscore.VideoSource;
import edu.wpi.first.cameraserver.CameraServer;
import edu.wpi.first.cscore.VideoSource;
import edu.wpi.first.networktables.NetworkTableInstance;
import java.io.IOException;
import java.nio.file.Files;

View File

@@ -158,7 +158,7 @@ bool ReadConfig() {
void StartCamera(const CameraConfig& config) {
wpi::outs() << "Starting camera '" << config.name << "' on " << config.path
<< '\n';
auto camera = frc::CameraServer::GetInstance()->StartAutomaticCapture(
auto camera = frc::CameraServer::GetInstance().StartAutomaticCapture(
config.name, config.path);
camera.SetConfigJson(config.config);

View File

@@ -4,20 +4,20 @@
package edu.wpi.first.cameraserver;
import edu.wpi.cscore.AxisCamera;
import edu.wpi.cscore.CameraServerJNI;
import edu.wpi.cscore.CvSink;
import edu.wpi.cscore.CvSource;
import edu.wpi.cscore.MjpegServer;
import edu.wpi.cscore.UsbCamera;
import edu.wpi.cscore.VideoEvent;
import edu.wpi.cscore.VideoException;
import edu.wpi.cscore.VideoListener;
import edu.wpi.cscore.VideoMode;
import edu.wpi.cscore.VideoMode.PixelFormat;
import edu.wpi.cscore.VideoProperty;
import edu.wpi.cscore.VideoSink;
import edu.wpi.cscore.VideoSource;
import edu.wpi.first.cscore.AxisCamera;
import edu.wpi.first.cscore.CameraServerJNI;
import edu.wpi.first.cscore.CvSink;
import edu.wpi.first.cscore.CvSource;
import edu.wpi.first.cscore.MjpegServer;
import edu.wpi.first.cscore.UsbCamera;
import edu.wpi.first.cscore.VideoEvent;
import edu.wpi.first.cscore.VideoException;
import edu.wpi.first.cscore.VideoListener;
import edu.wpi.first.cscore.VideoMode;
import edu.wpi.first.cscore.VideoMode.PixelFormat;
import edu.wpi.first.cscore.VideoProperty;
import edu.wpi.first.cscore.VideoSink;
import edu.wpi.first.cscore.VideoSource;
import edu.wpi.first.networktables.EntryListenerFlags;
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableEntry;

View File

@@ -4,9 +4,9 @@
package edu.wpi.first.vision;
import edu.wpi.cscore.CvSink;
import edu.wpi.cscore.VideoSource;
import edu.wpi.first.cameraserver.CameraServerSharedStore;
import edu.wpi.first.cscore.CvSink;
import edu.wpi.first.cscore.VideoSource;
import org.opencv.core.Mat;
/**

View File

@@ -4,7 +4,7 @@
package edu.wpi.first.vision;
import edu.wpi.cscore.VideoSource;
import edu.wpi.first.cscore.VideoSource;
/**
* A vision thread is a special thread that runs a vision pipeline. It is a <i>daemon</i> thread; it

View File

@@ -13,7 +13,7 @@
* implements VisionRunner.Listener&lt;MyFindTotePipeline&gt; {
*
* // A USB camera connected to the roboRIO.
* private {@link edu.wpi.cscore.VideoSource VideoSource} usbCamera;
* private {@link edu.wpi.first.cscore.VideoSource VideoSource} usbCamera;
*
* // A vision pipeline. This could be handwritten or generated by GRIP.
* // This has to implement {@link edu.wpi.first.vision.VisionPipeline}.

View File

@@ -45,7 +45,7 @@ struct CameraServer::Impl {
std::vector<std::string> m_addresses;
};
CameraServer* CameraServer::GetInstance() {
CameraServer& CameraServer::GetInstance() {
struct Creator {
static void* call() { return new CameraServer{}; }
};
@@ -53,7 +53,7 @@ CameraServer* CameraServer::GetInstance() {
static void call(void* ptr) { delete static_cast<CameraServer*>(ptr); }
};
static wpi::ManagedStatic<CameraServer, Creator, Deleter> instance;
return &(*instance);
return *instance;
}
static wpi::StringRef MakeSourceValue(CS_Source source,

View File

@@ -32,7 +32,7 @@ class CameraServer {
/**
* Get the CameraServer instance.
*/
static CameraServer* GetInstance();
static CameraServer& GetInstance();
/**
* Start automatically capturing images to send to the dashboard.

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
import edu.wpi.first.wpiutil.RuntimeDetector;

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
/** A source that represents an Axis IP camera. */
public class AxisCamera extends HttpCamera {

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
import edu.wpi.first.wpiutil.RuntimeLoader;
import java.io.IOException;

View File

@@ -2,9 +2,9 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
import edu.wpi.cscore.raw.RawFrame;
import edu.wpi.first.cscore.raw.RawFrame;
import edu.wpi.first.wpiutil.RuntimeLoader;
import java.io.IOException;
import java.nio.ByteBuffer;

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
import org.opencv.core.Mat;

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
import org.opencv.core.Mat;

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
/** A source that represents a MJPEG-over-HTTP (IP) camera. */
public class HttpCamera extends VideoCamera {

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
public abstract class ImageSink extends VideoSink {
protected ImageSink(int handle) {

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
public abstract class ImageSource extends VideoSource {
protected ImageSource(int handle) {

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
/** A sink that acts as a MJPEG-over-HTTP network server. */
public class MjpegServer extends VideoSink {

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
/** A source that represents a USB camera. */
public class UsbCamera extends VideoCamera {

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
/** USB camera information. */
public class UsbCameraInfo {

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
/** A source that represents a video camera. */
public class VideoCamera extends VideoSource {

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
/** Video event. */
public class VideoEvent {

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
/** An exception raised by the camera server. */
public class VideoException extends RuntimeException {

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
import java.util.HashMap;
import java.util.Map;

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
/** Video mode. */
public class VideoMode {

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
/** A source or sink property. */
public class VideoProperty {

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
/**
* A source for video that provides a sequence of frames. Each frame may consist of multiple images

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
/**
* A source for video that provides a sequence of frames. Each frame may consist of multiple images

View File

@@ -2,9 +2,9 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore.raw;
package edu.wpi.first.cscore.raw;
import edu.wpi.cscore.CameraServerJNI;
import edu.wpi.first.cscore.CameraServerJNI;
import java.nio.ByteBuffer;
/**

View File

@@ -2,10 +2,10 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore.raw;
package edu.wpi.first.cscore.raw;
import edu.wpi.cscore.CameraServerJNI;
import edu.wpi.cscore.ImageSink;
import edu.wpi.first.cscore.CameraServerJNI;
import edu.wpi.first.cscore.ImageSink;
/**
* A sink for user code to accept video frames as raw bytes.

View File

@@ -2,11 +2,11 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore.raw;
package edu.wpi.first.cscore.raw;
import edu.wpi.cscore.CameraServerJNI;
import edu.wpi.cscore.ImageSource;
import edu.wpi.cscore.VideoMode;
import edu.wpi.first.cscore.CameraServerJNI;
import edu.wpi.first.cscore.ImageSource;
import edu.wpi.first.cscore.VideoMode;
/**
* A source for user code to provide video frames as raw bytes.

File diff suppressed because it is too large Load Diff

View File

@@ -293,32 +293,45 @@ void UsbCameraImpl::ProcessFrame(IMFSample* videoSample,
return;
}
bool lock2d = false;
BYTE* ptr = NULL;
LONG pitch = 0;
DWORD maxsize = 0, cursize = 0;
DWORD length = 0;
// "For 2-D buffers, the Lock2D method is more efficient than the Lock
// method" see IMFMediaBuffer::Lock method documentation:
// https://msdn.microsoft.com/en-us/library/windows/desktop/bb970366(v=vs.85).aspx
ComPtr<IMF2DBuffer> buffer2d;
DWORD memLength2d = 0;
if (true) {
buffer2d = buf.As<IMF2DBuffer>();
if (buffer2d) {
buffer2d->GetContiguousLength(&memLength2d);
if (SUCCEEDED(buffer2d->Lock2D(&ptr, &pitch))) {
lock2d = true;
// First try to access using Lock2DSize, then try Lock2D, then fallback
// https://docs.microsoft.com/en-us/windows/win32/api/mfobjects/nf-mfobjects-imfmediabuffer-lock
ComPtr<IMF2DBuffer> buffer2d = buf.As<IMF2DBuffer>();
if (buffer2d) {
BYTE* scanline0 = nullptr;
HRESULT result;
ComPtr<IMF2DBuffer2> buffer2d2 = buf.As<IMF2DBuffer2>();
if (buffer2d2) {
BYTE* datastart;
result = buffer2d2->Lock2DSize(MF2DBuffer_LockFlags_Read, &scanline0,
&pitch, &datastart, &length);
} else {
result = buffer2d->Lock2D(&scanline0, &pitch);
}
if (SUCCEEDED(result)) {
BOOL isContiguous;
if (pitch > 0 && SUCCEEDED(buffer2d->IsContiguousFormat(&isContiguous)) &&
isContiguous &&
(length || SUCCEEDED(buffer2d->GetContiguousLength(&length)))) {
// Use the buffer pointer.
ptr = scanline0;
} else {
// Release the buffer and fall back to Lock().
buffer2d->Unlock2D();
}
}
}
if (ptr == NULL) {
if (!SUCCEEDED(buf->Lock(&ptr, &maxsize, &cursize))) {
buffer2d = nullptr;
DWORD maxsize = 0;
if (!SUCCEEDED(buf->Lock(&ptr, &maxsize, &length))) {
return;
}
}
if (!ptr)
return;
cv::Mat tmpMat;
std::unique_ptr<Image> dest;
@@ -328,7 +341,7 @@ void UsbCameraImpl::ProcessFrame(IMFSample* videoSample,
case cs::VideoMode::PixelFormat::kMJPEG: {
// Special case
PutFrame(VideoMode::kMJPEG, mode.width, mode.height,
wpi::StringRef(reinterpret_cast<char*>(ptr), cursize),
wpi::StringRef(reinterpret_cast<char*>(ptr), length),
wpi::Now());
doFinalSet = false;
break;
@@ -360,10 +373,11 @@ void UsbCameraImpl::ProcessFrame(IMFSample* videoSample,
PutFrame(std::move(dest), wpi::Now());
}
if (lock2d)
if (buffer2d) {
buffer2d->Unlock2D();
else
} else {
buf->Unlock();
}
}
LRESULT UsbCameraImpl::PumpMain(HWND hwnd, UINT uiMsg, WPARAM wParam,

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
import org.junit.jupiter.api.Test;

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.cscore;
package edu.wpi.first.cscore;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;

Binary file not shown.

View File

@@ -741,30 +741,20 @@ void glass::DisplayNetworkTables(NetworkTablesModel* model,
ImGui::Columns();
}
void NetworkTablesView::Display() {
auto& storage = GetStorage();
auto pTreeView =
storage.GetBoolRef("tree", m_defaultFlags & NetworkTablesFlags_TreeView);
auto pShowConnections = storage.GetBoolRef(
"connections", m_defaultFlags & NetworkTablesFlags_ShowConnections);
auto pShowFlags = storage.GetBoolRef(
"flags", m_defaultFlags & NetworkTablesFlags_ShowFlags);
auto pShowTimestamp = storage.GetBoolRef(
"timestamp", m_defaultFlags & NetworkTablesFlags_ShowTimestamp);
auto pCreateNoncanonicalKeys = storage.GetBoolRef(
"createNonCanonical",
m_defaultFlags & NetworkTablesFlags_CreateNoncanonicalKeys);
if (ImGui::BeginPopupContextItem()) {
ImGui::MenuItem("Tree View", "", pTreeView);
ImGui::MenuItem("Show Connections", "", pShowConnections);
ImGui::MenuItem("Show Flags", "", pShowFlags);
ImGui::MenuItem("Show Timestamp", "", pShowTimestamp);
ImGui::Separator();
ImGui::MenuItem("Allow creation of non-canonical keys", "",
pCreateNoncanonicalKeys);
ImGui::EndPopup();
void NetworkTablesFlagsSettings::Update() {
if (!m_pTreeView) {
auto& storage = GetStorage();
m_pTreeView = storage.GetBoolRef(
"tree", m_defaultFlags & NetworkTablesFlags_TreeView);
m_pShowConnections = storage.GetBoolRef(
"connections", m_defaultFlags & NetworkTablesFlags_ShowConnections);
m_pShowFlags = storage.GetBoolRef(
"flags", m_defaultFlags & NetworkTablesFlags_ShowFlags);
m_pShowTimestamp = storage.GetBoolRef(
"timestamp", m_defaultFlags & NetworkTablesFlags_ShowTimestamp);
m_pCreateNoncanonicalKeys = storage.GetBoolRef(
"createNonCanonical",
m_defaultFlags & NetworkTablesFlags_CreateNoncanonicalKeys);
}
m_flags &=
@@ -772,11 +762,32 @@ void NetworkTablesView::Display() {
NetworkTablesFlags_ShowFlags | NetworkTablesFlags_ShowTimestamp |
NetworkTablesFlags_CreateNoncanonicalKeys);
m_flags |=
(*pTreeView ? NetworkTablesFlags_TreeView : 0) |
(*pShowConnections ? NetworkTablesFlags_ShowConnections : 0) |
(*pShowFlags ? NetworkTablesFlags_ShowFlags : 0) |
(*pShowTimestamp ? NetworkTablesFlags_ShowTimestamp : 0) |
(*pCreateNoncanonicalKeys ? NetworkTablesFlags_CreateNoncanonicalKeys
: 0);
DisplayNetworkTables(m_model, m_flags);
(*m_pTreeView ? NetworkTablesFlags_TreeView : 0) |
(*m_pShowConnections ? NetworkTablesFlags_ShowConnections : 0) |
(*m_pShowFlags ? NetworkTablesFlags_ShowFlags : 0) |
(*m_pShowTimestamp ? NetworkTablesFlags_ShowTimestamp : 0) |
(*m_pCreateNoncanonicalKeys ? NetworkTablesFlags_CreateNoncanonicalKeys
: 0);
}
void NetworkTablesFlagsSettings::DisplayMenu() {
if (!m_pTreeView) {
return;
}
ImGui::MenuItem("Tree View", "", m_pTreeView);
ImGui::MenuItem("Show Connections", "", m_pShowConnections);
ImGui::MenuItem("Show Flags", "", m_pShowFlags);
ImGui::MenuItem("Show Timestamp", "", m_pShowTimestamp);
ImGui::Separator();
ImGui::MenuItem("Allow creation of non-canonical keys", "",
m_pCreateNoncanonicalKeys);
}
void NetworkTablesView::Display() {
m_flags.Update();
if (ImGui::BeginPopupContextItem()) {
m_flags.DisplayMenu();
ImGui::EndPopup();
}
DisplayNetworkTables(m_model, m_flags.GetFlags());
}

View File

@@ -104,19 +104,39 @@ void DisplayNetworkTables(
NetworkTablesModel* model,
NetworkTablesFlags flags = NetworkTablesFlags_Default);
class NetworkTablesFlagsSettings {
public:
explicit NetworkTablesFlagsSettings(
NetworkTablesFlags defaultFlags = NetworkTablesFlags_Default)
: m_defaultFlags{defaultFlags}, m_flags{defaultFlags} {}
void Update();
void DisplayMenu();
NetworkTablesFlags GetFlags() const { return m_flags; }
private:
bool* m_pTreeView = nullptr;
bool* m_pShowConnections = nullptr;
bool* m_pShowFlags = nullptr;
bool* m_pShowTimestamp = nullptr;
bool* m_pCreateNoncanonicalKeys = nullptr;
NetworkTablesFlags m_defaultFlags; // NOLINT
NetworkTablesFlags m_flags; // NOLINT
};
class NetworkTablesView : public View {
public:
explicit NetworkTablesView(
NetworkTablesModel* model,
NetworkTablesFlags defaultFlags = NetworkTablesFlags_Default)
: m_model{model}, m_defaultFlags{defaultFlags}, m_flags{defaultFlags} {}
: m_model{model}, m_flags{defaultFlags} {}
void Display() override;
private:
NetworkTablesModel* m_model;
NetworkTablesFlags m_defaultFlags;
NetworkTablesFlags m_flags;
NetworkTablesFlagsSettings m_flags;
};
} // namespace glass

View File

@@ -28,7 +28,36 @@ public final class HAL extends JNIWrapper {
private static native void simPeriodicBeforeNative();
public static final List<Runnable> s_simPeriodicBefore = new ArrayList<>();
private static final List<Runnable> s_simPeriodicBefore = new ArrayList<>();
public static class SimPeriodicBeforeCallback implements AutoCloseable {
private SimPeriodicBeforeCallback(Runnable r) {
m_run = r;
}
@Override
public void close() {
synchronized (s_simPeriodicBefore) {
s_simPeriodicBefore.remove(m_run);
}
}
private final Runnable m_run;
}
/**
* Registers a callback to be run by IterativeRobotBase prior to the user's simulationPeriodic
* code.
*
* @param r runnable
* @return Callback object (must be retained for callback to stay active).
*/
public static SimPeriodicBeforeCallback registerSimPeriodicBeforeCallback(Runnable r) {
synchronized (s_simPeriodicBefore) {
s_simPeriodicBefore.add(r);
}
return new SimPeriodicBeforeCallback(r);
}
/**
* Runs SimPeriodicBefore callbacks. IterativeRobotBase calls this prior to the user's
@@ -45,7 +74,35 @@ public final class HAL extends JNIWrapper {
private static native void simPeriodicAfterNative();
public static final List<Runnable> s_simPeriodicAfter = new ArrayList<>();
private static final List<Runnable> s_simPeriodicAfter = new ArrayList<>();
public static class SimPeriodicAfterCallback implements AutoCloseable {
private SimPeriodicAfterCallback(Runnable r) {
m_run = r;
}
@Override
public void close() {
synchronized (s_simPeriodicAfter) {
s_simPeriodicAfter.remove(m_run);
}
}
private final Runnable m_run;
}
/**
* Registers a callback to be run by IterativeRobotBase after the user's simulationPeriodic code.
*
* @param r runnable
* @return Callback object (must be retained for callback to stay active).
*/
public static SimPeriodicAfterCallback registerSimPeriodicAfterCallback(Runnable r) {
synchronized (s_simPeriodicAfter) {
s_simPeriodicAfter.add(r);
}
return new SimPeriodicAfterCallback(r);
}
/**
* Runs SimPeriodicAfter callbacks. IterativeRobotBase calls this after the user's

View File

@@ -47,17 +47,11 @@ public class SimDeviceDataJNI extends JNIWrapper {
public static native int registerSimValueCreatedCallback(
int device, SimValueCallback callback, boolean initialNotify);
public static native int registerSimValueCreatedCallback2(
int device, SimValueCallback2 callback, boolean initialNotify);
public static native void cancelSimValueCreatedCallback(int uid);
public static native int registerSimValueChangedCallback(
int handle, SimValueCallback callback, boolean initialNotify);
public static native int registerSimValueChangedCallback2(
int handle, SimValueCallback2 callback, boolean initialNotify);
public static native void cancelSimValueChangedCallback(int uid);
/**
@@ -69,7 +63,7 @@ public class SimDeviceDataJNI extends JNIWrapper {
* @param initialNotify ignored (present for consistency)
*/
public static native int registerSimValueResetCallback(
int handle, SimValueCallback2 callback, boolean initialNotify);
int handle, SimValueCallback callback, boolean initialNotify);
public static native void cancelSimValueResetCallback(int uid);

View File

@@ -8,10 +8,10 @@ import edu.wpi.first.hal.HALValue;
@FunctionalInterface
public interface SimValueCallback {
void callback(String name, int handle, boolean readonly, HALValue value);
void callback(String name, int handle, int direction, HALValue value);
default void callbackNative(
String name, int handle, boolean readonly, int type, long value1, double value2) {
callback(name, handle, readonly, HALValue.fromNative(type, value1, value2));
String name, int handle, int direction, int type, long value1, double value2) {
callback(name, handle, direction, HALValue.fromNative(type, value1, value2));
}
}

View File

@@ -1,17 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.hal.simulation;
import edu.wpi.first.hal.HALValue;
@FunctionalInterface
public interface SimValueCallback2 {
void callback(String name, int handle, int direction, HALValue value);
default void callbackNative(
String name, int handle, int direction, int type, long value1, double value2) {
callback(name, handle, direction, HALValue.fromNative(type, value1, value2));
}
}

View File

@@ -4,7 +4,6 @@
package edu.wpi.first.hal.simulation;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.hal.JNIWrapper;
public class SimulatorJNI extends JNIWrapper {
@@ -29,48 +28,4 @@ public class SimulatorJNI extends JNIWrapper {
public static native void stepTimingAsync(long delta);
public static native void resetHandles();
public static class SimPeriodicBeforeCallback implements AutoCloseable {
private SimPeriodicBeforeCallback(Runnable r) {
m_run = r;
}
@Override
public void close() {
synchronized (HAL.s_simPeriodicBefore) {
HAL.s_simPeriodicBefore.remove(m_run);
}
}
private Runnable m_run;
}
public static SimPeriodicBeforeCallback registerSimPeriodicBeforeCallback(Runnable r) {
synchronized (HAL.s_simPeriodicBefore) {
HAL.s_simPeriodicBefore.add(r);
}
return new SimPeriodicBeforeCallback(r);
}
public static class SimPeriodicAfterCallback implements AutoCloseable {
private SimPeriodicAfterCallback(Runnable r) {
m_run = r;
}
@Override
public void close() {
synchronized (HAL.s_simPeriodicAfter) {
HAL.s_simPeriodicAfter.remove(m_run);
}
}
private Runnable m_run;
}
public static SimPeriodicAfterCallback registerSimPeriodicAfterCallback(Runnable r) {
synchronized (HAL.s_simPeriodicAfter) {
HAL.s_simPeriodicAfter.add(r);
}
return new SimPeriodicAfterCallback(r);
}
}

View File

@@ -11,6 +11,7 @@
#include "ConstantsInternal.h"
#include "DigitalInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/AddressableLEDTypes.h"
#include "hal/ChipObject.h"
@@ -142,8 +143,11 @@ void HAL_SetAddressableLEDLength(HAL_AddressableLEDHandle handle,
return;
}
if (length > HAL_kAddressableLEDMaxLength) {
if (length > HAL_kAddressableLEDMaxLength || length < 0) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "LED length must be less than or equal to " +
wpi::Twine(HAL_kAddressableLEDMaxLength) +
". " + wpi::Twine(length) + " was requested");
return;
}
@@ -173,8 +177,11 @@ void HAL_WriteAddressableLEDData(HAL_AddressableLEDHandle handle,
return;
}
if (length > led->stringLength) {
if (length > led->stringLength || length < 0) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "Data length must be less than or equal to " +
wpi::Twine(led->stringLength) + ". " +
wpi::Twine(length) + " was requested");
return;
}

View File

@@ -4,12 +4,14 @@
#include "hal/AnalogGyro.h"
#include <string>
#include <thread>
#include <wpi/raw_ostream.h>
#include "AnalogInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "hal/AnalogAccumulator.h"
#include "hal/AnalogInput.h"
#include "hal/handles/IndexedHandleResource.h"
@@ -21,6 +23,7 @@ struct AnalogGyro {
double voltsPerDegreePerSecond;
double offset;
int32_t center;
std::string previousAllocation;
};
} // namespace
@@ -55,36 +58,43 @@ static void Wait(double seconds) {
extern "C" {
HAL_GyroHandle HAL_InitializeAnalogGyro(HAL_AnalogInputHandle analogHandle,
const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
// Handle will be type checked by HAL_IsAccumulatorChannel
int16_t channel = getHandleIndex(analogHandle);
if (!HAL_IsAccumulatorChannel(analogHandle, status)) {
if (*status == 0) {
*status = HAL_INVALID_ACCUMULATOR_CHANNEL;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Gyro",
0, kNumAccumulators, channel);
}
return HAL_kInvalidHandle;
}
// handle known to be correct, so no need to type check
int16_t channel = getHandleIndex(analogHandle);
auto handle = analogGyroHandles->Allocate(channel, status);
HAL_GyroHandle handle;
auto gyro = analogGyroHandles->Allocate(channel, &handle, status);
if (*status != 0) {
if (gyro) {
hal::SetLastErrorPreviouslyAllocated(status, "Analog Gyro", channel,
gyro->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Gyro",
0, kNumAccumulators, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
// Initialize port structure
auto gyro = analogGyroHandles->Get(handle);
if (gyro == nullptr) { // would only error on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
gyro->handle = analogHandle;
gyro->voltsPerDegreePerSecond = 0;
gyro->offset = 0;
gyro->center = 0;
gyro->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}

View File

@@ -9,6 +9,7 @@
#include "AnalogInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/AnalogAccumulator.h"
#include "hal/handles/HandlesInternal.h"
@@ -21,8 +22,9 @@ using namespace hal;
extern "C" {
HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
int32_t* status) {
HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(
HAL_PortHandle portHandle, const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
initializeAnalog(status);
@@ -31,23 +33,28 @@ HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
}
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
if (channel == InvalidHandleIndex || channel >= kNumAnalogInputs) {
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Input",
0, kNumAnalogInputs, channel);
return HAL_kInvalidHandle;
}
HAL_AnalogInputHandle handle = analogInputHandles->Allocate(channel, status);
HAL_AnalogInputHandle handle;
auto analog_port = analogInputHandles->Allocate(channel, &handle, status);
if (*status != 0) {
if (analog_port) {
hal::SetLastErrorPreviouslyAllocated(status, "Analog Input", channel,
analog_port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Input",
0, kNumAnalogInputs, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
// Initialize port structure
auto analog_port = analogInputHandles->Get(handle);
if (analog_port == nullptr) { // would only error on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
analog_port->channel = static_cast<uint8_t>(channel);
if (HAL_IsAccumulatorChannel(handle, status)) {
analog_port->accumulator.reset(tAccumulator::create(channel, status));
@@ -59,6 +66,8 @@ HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
analogInputSystem->writeScanList(channel, channel, status);
HAL_SetAnalogAverageBits(handle, kDefaultAverageBits, status);
HAL_SetAnalogOversampleBits(handle, kDefaultOversampleBits, status);
analog_port->previousAllocation =
allocationLocation ? allocationLocation : "";
return handle;
}

View File

@@ -7,6 +7,7 @@
#include <stdint.h>
#include <memory>
#include <string>
#include <wpi/mutex.h>
@@ -31,6 +32,7 @@ extern bool analogSampleRateSet;
struct AnalogPort {
uint8_t channel;
std::unique_ptr<tAccumulator> accumulator;
std::string previousAllocation;
};
extern IndexedHandleResource<HAL_AnalogInputHandle, hal::AnalogPort,

View File

@@ -4,8 +4,11 @@
#include "hal/AnalogOutput.h"
#include <string>
#include "AnalogInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/Errors.h"
#include "hal/handles/HandlesInternal.h"
@@ -17,6 +20,7 @@ namespace {
struct AnalogOutput {
uint8_t channel;
std::string previousAllocation;
};
} // namespace
@@ -36,8 +40,9 @@ void InitializeAnalogOutput() {
extern "C" {
HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(HAL_PortHandle portHandle,
int32_t* status) {
HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(
HAL_PortHandle portHandle, const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
initializeAnalog(status);
@@ -46,25 +51,31 @@ HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(HAL_PortHandle portHandle,
}
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
if (channel == InvalidHandleIndex || channel >= kNumAnalogOutputs) {
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Output",
0, kNumAnalogOutputs, channel);
return HAL_kInvalidHandle;
}
HAL_AnalogOutputHandle handle =
analogOutputHandles->Allocate(channel, status);
HAL_AnalogOutputHandle handle;
auto port = analogOutputHandles->Allocate(channel, &handle, status);
if (*status != 0) {
if (port) {
hal::SetLastErrorPreviouslyAllocated(status, "Analog Output", channel,
port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status,
"Invalid Index for Analog Output", 0,
kNumAnalogOutputs, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = analogOutputHandles->Get(handle);
if (port == nullptr) { // would only error on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
port->channel = static_cast<uint8_t>(channel);
port->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}

View File

@@ -7,6 +7,7 @@
#include "AnalogInternal.h"
#include "DutyCycleInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/AnalogInput.h"
#include "hal/DutyCycle.h"
@@ -138,6 +139,11 @@ void HAL_SetAnalogTriggerLimitsDutyCycle(
if (lower < 0.0 || upper > 1.0) {
*status = PARAMETER_OUT_OF_RANGE;
auto lowerStr = std::to_string(lower);
auto upperStr = std::to_string(upper);
hal::SetLastError(
status, "Lower must be >= 0 and upper must be <=1. Requested lower " +
lowerStr + " Requested upper " + upperStr);
return;
}

View File

@@ -7,6 +7,7 @@
#include "ConstantsInternal.h"
#include "DigitalInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/HAL.h"
#include "hal/handles/LimitedHandleResource.h"
@@ -146,6 +147,9 @@ void HAL_SetCounterDownSource(HAL_CounterHandle counterHandle,
// TODO: wpi_setWPIErrorWithContext(ParameterOutOfRange, "Counter only
// supports DownSource in TwoPulse and ExternalDirection modes.");
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status,
"Counter only supports DownSource in TwoPulse and "
"ExternalDirection mode.");
return;
}
@@ -260,6 +264,11 @@ void HAL_SetCounterSamplesToAverage(HAL_CounterHandle counterHandle,
}
if (samplesToAverage < 1 || samplesToAverage > 127) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(
status,
"Samples to average must be between 1 and 127 inclusive. Requested " +
wpi::Twine(samplesToAverage));
return;
}
counter->counter->writeTimerConfig_AverageSize(samplesToAverage, status);
}

View File

@@ -11,6 +11,7 @@
#include "DigitalInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/cpp/fpga_clock.h"
#include "hal/handles/HandlesInternal.h"
@@ -38,7 +39,9 @@ void InitializeDIO() {
extern "C" {
HAL_DigitalHandle HAL_InitializeDIOPort(HAL_PortHandle portHandle,
HAL_Bool input, int32_t* status) {
HAL_Bool input,
const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
initializeDigital(status);
@@ -48,23 +51,28 @@ HAL_DigitalHandle HAL_InitializeDIOPort(HAL_PortHandle portHandle,
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex || channel >= kNumDigitalChannels) {
*status = PARAMETER_OUT_OF_RANGE;
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for DIO", 0,
kNumDigitalChannels, channel);
return HAL_kInvalidHandle;
}
auto handle =
digitalChannelHandles->Allocate(channel, HAL_HandleEnum::DIO, status);
HAL_DigitalHandle handle;
auto port = digitalChannelHandles->Allocate(channel, HAL_HandleEnum::DIO,
&handle, status);
if (*status != 0) {
if (port) {
hal::SetLastErrorPreviouslyAllocated(status, "PWM or DIO", channel,
port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for DIO", 0,
kNumDigitalChannels, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = digitalChannelHandles->Get(handle, HAL_HandleEnum::DIO);
if (port == nullptr) { // would only occur on thread issue.
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
port->channel = static_cast<uint8_t>(channel);
std::scoped_lock lock(digitalDIOMutex);
@@ -114,6 +122,7 @@ HAL_DigitalHandle HAL_InitializeDIOPort(HAL_PortHandle portHandle,
}
digitalSystem->writeOutputEnable(outputEnable, status);
port->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}
@@ -191,8 +200,8 @@ void HAL_SetDigitalPWMRate(double rate, int32_t* status) {
if (*status != 0) {
return;
}
uint16_t pwmPeriodPower = static_cast<uint16_t>(
std::log(1.0 / (16 * 1.0E-6 * rate)) / std::log(2.0) + 0.5);
uint16_t pwmPeriodPower =
std::lround(std::log(1.0 / (16 * 1.0E-6 * rate)) / std::log(2.0));
digitalSystem->writePWMPeriodPower(pwmPeriodPower, status);
}

View File

@@ -13,6 +13,7 @@
#include "AnalogInternal.h"
#include "DigitalInternal.h"
#include "EncoderInternal.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/AnalogAccumulator.h"
#include "hal/AnalogGyro.h"
@@ -528,6 +529,9 @@ void HAL_SetDMAExternalTrigger(HAL_DMAHandle handle,
if (!success) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status,
"Digital Source unabled to be mapped properly. Likely "
"invalid handle passed.");
return;
}

View File

@@ -5,6 +5,7 @@
#include "DigitalInternal.h"
#include <atomic>
#include <cmath>
#include <thread>
#include <FRC_NetworkCommunication/LoadOut.h>
@@ -108,10 +109,10 @@ void initializeDigital(int32_t* status) {
double loopTime = pwmSystem->readLoopTiming(status) /
(kSystemClockTicksPerMicrosecond * 1e3);
pwmSystem->writeConfig_Period(
static_cast<uint16_t>(kDefaultPwmPeriod / loopTime + 0.5), status);
uint16_t minHigh = static_cast<uint16_t>(
(kDefaultPwmCenter - kDefaultPwmStepsDown * loopTime) / loopTime + 0.5);
pwmSystem->writeConfig_Period(std::lround(kDefaultPwmPeriod / loopTime),
status);
uint16_t minHigh = std::lround(
(kDefaultPwmCenter - kDefaultPwmStepsDown * loopTime) / loopTime);
pwmSystem->writeConfig_MinHigh(minHigh, status);
// Ensure that PWM output values are set to OFF
for (uint8_t pwmIndex = 0; pwmIndex < kNumPWMChannels; pwmIndex++) {

View File

@@ -7,6 +7,7 @@
#include <stdint.h>
#include <memory>
#include <string>
#include <wpi/mutex.h>
@@ -70,6 +71,7 @@ struct DigitalPort {
int32_t centerPwm = 0;
int32_t deadbandMinPwm = 0;
int32_t minPwm = 0;
std::string previousAllocation;
};
extern DigitalHandleResource<HAL_DigitalHandle, DigitalPort,

View File

@@ -7,6 +7,7 @@
#include "EncoderInternal.h"
#include "FPGAEncoder.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/ChipObject.h"
#include "hal/Counter.h"
@@ -46,6 +47,9 @@ Encoder::Encoder(HAL_Handle digitalSourceHandleA,
}
default:
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "Encoding type " +
wpi::Twine(static_cast<int>(encodingType)) +
" invalid.");
return;
}
}
@@ -181,6 +185,10 @@ void Encoder::SetReverseDirection(bool reverseDirection, int32_t* status) {
void Encoder::SetSamplesToAverage(int32_t samplesToAverage, int32_t* status) {
if (samplesToAverage < 1 || samplesToAverage > 127) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(
status,
"Samples to average must be between 1 and 127 inclusive. Requested " +
wpi::Twine(samplesToAverage));
return;
}
if (m_counter) {

View File

@@ -8,6 +8,7 @@
#include "DigitalInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/handles/LimitedHandleResource.h"
@@ -193,6 +194,11 @@ void HAL_SetFPGAEncoderSamplesToAverage(HAL_FPGAEncoderHandle fpgaEncoderHandle,
}
if (samplesToAverage < 1 || samplesToAverage > 127) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(
status,
"Samples to average must be between 1 and 127 inclusive. Requested " +
wpi::Twine(samplesToAverage));
return;
}
encoder->encoder->writeTimerConfig_AverageSize(samplesToAverage, status);
}

View File

@@ -233,6 +233,8 @@ const char* HAL_GetErrorMessage(int32_t code) {
return HAL_CAN_BUFFER_OVERRUN_MESSAGE;
case HAL_LED_CHANNEL_ERROR:
return HAL_LED_CHANNEL_ERROR_MESSAGE;
case HAL_USE_LAST_ERROR:
return HAL_USE_LAST_ERROR_MESSAGE;
default:
return "Unknown error status";
}

View File

@@ -6,7 +6,15 @@
#include <stdint.h>
#include <wpi/Twine.h>
namespace hal {
void ReleaseFPGAInterrupt(int32_t interruptNumber);
void SetLastError(int32_t* status, const wpi::Twine& value);
void SetLastErrorIndexOutOfRange(int32_t* status, const wpi::Twine& message,
int32_t minimum, int32_t maximum,
int32_t channel);
void SetLastErrorPreviouslyAllocated(int32_t* status, const wpi::Twine& message,
int32_t channel,
const wpi::Twine& previousAllocation);
} // namespace hal

View File

@@ -67,11 +67,11 @@ void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status) {
return;
}
if ((i2CMXPDigitalHandle1 = HAL_InitializeDIOPort(
HAL_GetPort(24), false, status)) == HAL_kInvalidHandle) {
HAL_GetPort(24), false, nullptr, status)) == HAL_kInvalidHandle) {
return;
}
if ((i2CMXPDigitalHandle2 = HAL_InitializeDIOPort(
HAL_GetPort(25), false, status)) == HAL_kInvalidHandle) {
HAL_GetPort(25), false, nullptr, status)) == HAL_kInvalidHandle) {
HAL_FreeDIOPort(i2CMXPDigitalHandle1); // free the first port allocated
return;
}

View File

@@ -7,6 +7,7 @@
#include <wpi/mutex.h>
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/CANAPI.h"
#include "hal/Errors.h"
@@ -117,6 +118,7 @@ HAL_PDPHandle HAL_InitializePDP(int32_t module, int32_t* status) {
hal::init::CheckInit();
if (!HAL_CheckPDPModule(module)) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "Invalid pdp module " + wpi::Twine(module));
return HAL_kInvalidHandle;
}
@@ -192,6 +194,7 @@ double HAL_GetPDPChannelCurrent(HAL_PDPHandle handle, int32_t channel,
int32_t* status) {
if (!HAL_CheckPDPChannel(channel)) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "Invalid pdp channel " + wpi::Twine(channel));
return 0;
}

View File

@@ -12,6 +12,7 @@
#include "ConstantsInternal.h"
#include "DigitalInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/cpp/fpga_clock.h"
#include "hal/handles/HandlesInternal.h"
@@ -65,6 +66,7 @@ void InitializePWM() {}
extern "C" {
HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
initializeDigital(status);
@@ -75,7 +77,9 @@ HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex || channel >= kNumPWMChannels) {
*status = PARAMETER_OUT_OF_RANGE;
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for PWM", 0,
kNumPWMChannels, channel);
return HAL_kInvalidHandle;
}
@@ -87,19 +91,22 @@ HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
channel = remapMXPPWMChannel(channel) + 10; // remap MXP to proper channel
}
auto handle =
digitalChannelHandles->Allocate(channel, HAL_HandleEnum::PWM, status);
HAL_DigitalHandle handle;
auto port = digitalChannelHandles->Allocate(channel, HAL_HandleEnum::PWM,
&handle, status);
if (*status != 0) {
if (port) {
hal::SetLastErrorPreviouslyAllocated(status, "PWM or DIO", channel,
port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for PWM", 0,
kNumPWMChannels, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = digitalChannelHandles->Get(handle, HAL_HandleEnum::PWM);
if (port == nullptr) { // would only occur on thread issue.
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
port->channel = origChannel;
if (port->channel > tPWM::kNumHdrRegisters - 1) {
@@ -113,6 +120,8 @@ HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
// Defaults to allow an always valid config.
HAL_SetPWMConfig(handle, 2.0, 1.501, 1.5, 1.499, 1.0, status);
port->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}
void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
@@ -277,13 +286,13 @@ void HAL_SetPWMSpeed(HAL_DigitalHandle pwmPortHandle, double speed,
if (speed == 0.0) {
rawValue = GetCenterPwm(dPort);
} else if (speed > 0.0) {
rawValue = static_cast<int32_t>(
speed * static_cast<double>(GetPositiveScaleFactor(dPort)) +
static_cast<double>(GetMinPositivePwm(dPort)) + 0.5);
rawValue =
std::lround(speed * static_cast<double>(GetPositiveScaleFactor(dPort)) +
static_cast<double>(GetMinPositivePwm(dPort)));
} else {
rawValue = static_cast<int32_t>(
speed * static_cast<double>(GetNegativeScaleFactor(dPort)) +
static_cast<double>(GetMaxNegativePwm(dPort)) + 0.5);
rawValue =
std::lround(speed * static_cast<double>(GetNegativeScaleFactor(dPort)) +
static_cast<double>(GetMaxNegativePwm(dPort)));
}
if (!((rawValue >= GetMinNegativePwm(dPort)) &&

View File

@@ -4,8 +4,11 @@
#include "hal/Relay.h"
#include <string>
#include "DigitalInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/handles/IndexedHandleResource.h"
@@ -16,6 +19,7 @@ namespace {
struct Relay {
uint8_t channel;
bool fwd;
std::string previousAllocation;
};
} // namespace
@@ -38,6 +42,7 @@ void InitializeRelay() {
extern "C" {
HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
initializeDigital(status);
@@ -47,8 +52,10 @@ HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
}
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
if (channel == InvalidHandleIndex || channel >= kNumRelayChannels) {
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Relay", 0,
kNumRelayChannels, channel);
return HAL_kInvalidHandle;
}
@@ -56,18 +63,20 @@ HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
channel += kNumRelayHeaders; // add 4 to reverse channels
}
auto handle = relayHandles->Allocate(channel, status);
HAL_RelayHandle handle;
auto port = relayHandles->Allocate(channel, &handle, status);
if (*status != 0) {
if (port) {
hal::SetLastErrorPreviouslyAllocated(status, "Relay", channel,
port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Relay", 0,
kNumRelayChannels, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = relayHandles->Get(handle);
if (port == nullptr) { // would only occur on thread issue.
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
if (!fwd) {
// Subtract number of headers to put channel in range
channel -= kNumRelayHeaders;
@@ -78,6 +87,7 @@ HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
}
port->channel = static_cast<uint8_t>(channel);
port->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}

View File

@@ -18,6 +18,7 @@
#include "DigitalInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "hal/DIO.h"
#include "hal/HAL.h"
#include "hal/handles/HandlesInternal.h"
@@ -75,14 +76,14 @@ static void CommonSPIPortInit(int32_t* status) {
}
// MISO
if ((digitalHandles[3] = HAL_InitializeDIOPort(createPortHandleForSPI(29),
false, status)) ==
false, nullptr, status)) ==
HAL_kInvalidHandle) {
std::printf("Failed to allocate DIO 29 (MISO)\n");
return;
}
// MOSI
if ((digitalHandles[4] = HAL_InitializeDIOPort(createPortHandleForSPI(30),
false, status)) ==
false, nullptr, status)) ==
HAL_kInvalidHandle) {
std::printf("Failed to allocate DIO 30 (MOSI)\n");
HAL_FreeDIOPort(digitalHandles[3]); // free the first port allocated
@@ -103,6 +104,9 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
hal::init::CheckInit();
if (port < 0 || port >= kSpiMaxHandles) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "Serial port must be between 0 and " +
wpi::Twine(kSpiMaxHandles) + ". Requested " +
wpi::Twine(static_cast<int>(port)));
return;
}
@@ -133,7 +137,7 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
}
// CS1, Allocate
if ((digitalHandles[0] = HAL_InitializeDIOPort(createPortHandleForSPI(26),
false, status)) ==
false, nullptr, status)) ==
HAL_kInvalidHandle) {
std::printf("Failed to allocate DIO 26 (CS1)\n");
CommonSPIPortFree();
@@ -156,7 +160,7 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
}
// CS2, Allocate
if ((digitalHandles[1] = HAL_InitializeDIOPort(createPortHandleForSPI(27),
false, status)) ==
false, nullptr, status)) ==
HAL_kInvalidHandle) {
std::printf("Failed to allocate DIO 27 (CS2)\n");
CommonSPIPortFree();
@@ -179,7 +183,7 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
}
// CS3, Allocate
if ((digitalHandles[2] = HAL_InitializeDIOPort(createPortHandleForSPI(28),
false, status)) ==
false, nullptr, status)) ==
HAL_kInvalidHandle) {
std::printf("Failed to allocate DIO 28 (CS3)\n");
CommonSPIPortFree();
@@ -201,20 +205,20 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
return;
}
if ((digitalHandles[5] = HAL_InitializeDIOPort(createPortHandleForSPI(14),
false, status)) ==
false, nullptr, status)) ==
HAL_kInvalidHandle) {
wpi::outs() << "Failed to allocate DIO 14\n";
return;
}
if ((digitalHandles[6] = HAL_InitializeDIOPort(createPortHandleForSPI(15),
false, status)) ==
false, nullptr, status)) ==
HAL_kInvalidHandle) {
wpi::outs() << "Failed to allocate DIO 15\n";
HAL_FreeDIOPort(digitalHandles[5]); // free the first port allocated
return;
}
if ((digitalHandles[7] = HAL_InitializeDIOPort(createPortHandleForSPI(16),
false, status)) ==
false, nullptr, status)) ==
HAL_kInvalidHandle) {
wpi::outs() << "Failed to allocate DIO 16\n";
HAL_FreeDIOPort(digitalHandles[5]); // free the first port allocated
@@ -222,7 +226,7 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
return;
}
if ((digitalHandles[8] = HAL_InitializeDIOPort(createPortHandleForSPI(17),
false, status)) ==
false, nullptr, status)) ==
HAL_kInvalidHandle) {
wpi::outs() << "Failed to allocate DIO 17\n";
HAL_FreeDIOPort(digitalHandles[5]); // free the first port allocated
@@ -246,6 +250,8 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
break;
default:
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(
status, "Invalid SPI port " + wpi::Twine(static_cast<int>(port)));
break;
}
}
@@ -374,6 +380,9 @@ void HAL_SetSPIOpts(HAL_SPIPort port, HAL_Bool msbFirst,
void HAL_SetSPIChipSelectActiveHigh(HAL_SPIPort port, int32_t* status) {
if (port < 0 || port >= kSpiMaxHandles) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "Serial port must be between 0 and " +
wpi::Twine(kSpiMaxHandles) + ". Requested " +
wpi::Twine(static_cast<int>(port)));
return;
}
@@ -389,6 +398,9 @@ void HAL_SetSPIChipSelectActiveHigh(HAL_SPIPort port, int32_t* status) {
void HAL_SetSPIChipSelectActiveLow(HAL_SPIPort port, int32_t* status) {
if (port < 0 || port >= kSpiMaxHandles) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "Serial port must be between 0 and " +
wpi::Twine(kSpiMaxHandles) + ". Requested " +
wpi::Twine(static_cast<int>(port)));
return;
}
@@ -453,6 +465,9 @@ void HAL_SetSPIHandle(HAL_SPIPort port, int32_t handle) {
void HAL_InitSPIAuto(HAL_SPIPort port, int32_t bufferSize, int32_t* status) {
if (port < 0 || port >= kSpiMaxHandles) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "Serial port must be between 0 and " +
wpi::Twine(kSpiMaxHandles) + ". Requested " +
wpi::Twine(static_cast<int>(port)));
return;
}
@@ -484,6 +499,9 @@ void HAL_InitSPIAuto(HAL_SPIPort port, int32_t bufferSize, int32_t* status) {
void HAL_FreeSPIAuto(HAL_SPIPort port, int32_t* status) {
if (port < 0 || port >= kSpiMaxHandles) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "Serial port must be between 0 and " +
wpi::Twine(kSpiMaxHandles) + ". Requested " +
wpi::Twine(static_cast<int>(port)));
return;
}
@@ -586,11 +604,17 @@ void HAL_SetSPIAutoTransmitData(HAL_SPIPort port, const uint8_t* dataToSend,
int32_t* status) {
if (dataSize < 0 || dataSize > 32) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(
status, "Data size must be between 0 and 32 inclusive. Requested " +
wpi::Twine(dataSize));
return;
}
if (zeroSize < 0 || zeroSize > 127) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(
status, "Zero size must be between 0 and 127 inclusive. Requested " +
wpi::Twine(zeroSize));
return;
}

View File

@@ -19,6 +19,7 @@
#include <string>
#include <thread>
#include "HALInternal.h"
#include "hal/cpp/SerialHelper.h"
#include "hal/handles/HandlesInternal.h"
#include "hal/handles/IndexedHandleResource.h"
@@ -70,19 +71,14 @@ HAL_SerialPortHandle HAL_InitializeSerialPort(HAL_SerialPort port,
HAL_SerialPortHandle HAL_InitializeSerialPortDirect(HAL_SerialPort port,
const char* portName,
int32_t* status) {
auto handle = serialPortHandles->Allocate(static_cast<int16_t>(port), status);
HAL_SerialPortHandle handle;
auto serialPort =
serialPortHandles->Allocate(static_cast<int16_t>(port), &handle, status);
if (*status != 0) {
return HAL_kInvalidHandle;
}
auto serialPort = serialPortHandles->Get(handle);
if (serialPort == nullptr) {
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
serialPort->portId = open(portName, O_RDWR | O_NOCTTY);
if (serialPort->portId < 0) {
*status = errno;
@@ -188,6 +184,7 @@ void HAL_SetSerialBaudRate(HAL_SerialPortHandle handle, int32_t baud,
BAUDCASE(4000000)
default:
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "Invalid BaudRate: " + wpi::Twine(baud));
return;
}
int err = cfsetospeed(&port->tty, static_cast<speed_t>(port->baudRate));
@@ -230,6 +227,7 @@ void HAL_SetSerialDataBits(HAL_SerialPortHandle handle, int32_t bits,
break;
default:
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "Invalid data bits: " + wpi::Twine(bits));
return;
}
@@ -277,6 +275,7 @@ void HAL_SetSerialParity(HAL_SerialPortHandle handle, int32_t parity,
break;
default:
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "Invalid parity bits: " + wpi::Twine(parity));
return;
}
@@ -304,6 +303,7 @@ void HAL_SetSerialStopBits(HAL_SerialPortHandle handle, int32_t stopBits,
break;
default:
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "Invalid stop bits: " + wpi::Twine(stopBits));
return;
}
@@ -339,6 +339,7 @@ void HAL_SetSerialFlowControl(HAL_SerialPortHandle handle, int32_t flow,
break;
default:
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "Invalid fc bits: " + wpi::Twine(flow));
return;
}

View File

@@ -60,16 +60,12 @@ HAL_SolenoidHandle HAL_InitializeSolenoidPort(HAL_PortHandle portHandle,
return HAL_kInvalidHandle;
}
auto handle = solenoidHandles->Allocate(
module * kNumSolenoidChannels + channel, status);
HAL_SolenoidHandle handle;
auto solenoidPort = solenoidHandles->Allocate(
module * kNumSolenoidChannels + channel, &handle, status);
if (*status != 0) {
return HAL_kInvalidHandle;
}
auto solenoidPort = solenoidHandles->Get(handle);
if (solenoidPort == nullptr) { // would only occur on thread issues
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
solenoidPort->module = static_cast<uint8_t>(module);
solenoidPort->channel = static_cast<uint8_t>(channel);

View File

@@ -0,0 +1,61 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include <wpi/SmallString.h>
#include <wpi/Twine.h>
#include "hal/Errors.h"
#include "hal/HALBase.h"
namespace {
struct LastErrorStorage {
int32_t status;
wpi::SmallString<512> message;
};
} // namespace
static LastErrorStorage& GetThreadLastError() {
thread_local LastErrorStorage lastError;
return lastError;
}
namespace hal {
void SetLastError(int32_t* status, const wpi::Twine& value) {
LastErrorStorage& lastError = GetThreadLastError();
lastError.message.clear();
value.toVector(lastError.message);
lastError.status = *status;
*status = HAL_USE_LAST_ERROR;
}
void SetLastErrorIndexOutOfRange(int32_t* status, const wpi::Twine& message,
int32_t minimum, int32_t maximum,
int32_t requested) {
SetLastError(status, message + "\n Status: " + wpi::Twine(*status) +
"\n Minimum: " + wpi::Twine(minimum) +
" Maximum: " + wpi::Twine(maximum) +
" Reequested: " + wpi::Twine(requested));
}
void SetLastErrorPreviouslyAllocated(int32_t* status, const wpi::Twine& message,
int32_t channel,
const wpi::Twine& previousAllocation) {
hal::SetLastError(
status,
message + " " + wpi::Twine(channel) +
" previously allocated.\nLocation of the previous allocation:\n" +
previousAllocation + "\nLocation of the current allocation:");
}
} // namespace hal
extern "C" {
const char* HAL_GetLastError(int32_t* status) {
if (*status == HAL_USE_LAST_ERROR) {
LastErrorStorage& lastError = GetThreadLastError();
*status = lastError.status;
return lastError.message.c_str();
}
return HAL_GetErrorMessage(*status);
}
} // extern "C"

View File

@@ -6,6 +6,8 @@
#include <cassert>
#include <wpi/jni_util.h>
#include "HALUtil.h"
#include "edu_wpi_first_hal_AnalogGyroJNI.h"
#include "hal/AnalogGyro.h"
@@ -24,8 +26,9 @@ Java_edu_wpi_first_hal_AnalogGyroJNI_initializeAnalogGyro
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
HAL_GyroHandle handle =
HAL_InitializeAnalogGyro((HAL_AnalogInputHandle)id, &status);
auto stack = wpi::java::GetJavaStackTrace(env, "edu.wpi.first");
HAL_GyroHandle handle = HAL_InitializeAnalogGyro((HAL_AnalogInputHandle)id,
stack.c_str(), &status);
// Analog input does range checking, so we don't need to do so.
CheckStatusForceThrow(env, status);
return (jint)handle;

View File

@@ -6,6 +6,8 @@
#include <cassert>
#include <wpi/jni_util.h>
#include "HALUtil.h"
#include "edu_wpi_first_hal_AnalogJNI.h"
#include "hal/AnalogAccumulator.h"
@@ -29,9 +31,10 @@ Java_edu_wpi_first_hal_AnalogJNI_initializeAnalogInputPort
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
auto analog = HAL_InitializeAnalogInputPort((HAL_PortHandle)id, &status);
CheckStatusRange(env, status, 0, HAL_GetNumAnalogInputs(),
hal::getPortHandleChannel((HAL_PortHandle)id));
auto stack = wpi::java::GetJavaStackTrace(env, "edu.wpi.first");
auto analog =
HAL_InitializeAnalogInputPort((HAL_PortHandle)id, stack.c_str(), &status);
CheckStatusForceThrow(env, status);
return (jint)analog;
}
@@ -57,10 +60,10 @@ Java_edu_wpi_first_hal_AnalogJNI_initializeAnalogOutputPort
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
HAL_AnalogOutputHandle analog =
HAL_InitializeAnalogOutputPort((HAL_PortHandle)id, &status);
CheckStatusRange(env, status, 0, HAL_GetNumAnalogOutputs(),
hal::getPortHandleChannel((HAL_PortHandle)id));
auto stack = wpi::java::GetJavaStackTrace(env, "edu.wpi.first");
HAL_AnalogOutputHandle analog = HAL_InitializeAnalogOutputPort(
(HAL_PortHandle)id, stack.c_str(), &status);
CheckStatusForceThrow(env, status);
return (jlong)analog;
}

View File

@@ -119,12 +119,6 @@ Java_edu_wpi_first_hal_CounterJNI_setCounterDownSource
HAL_SetCounterDownSource((HAL_CounterHandle)id,
(HAL_Handle)digitalSourceHandle,
(HAL_AnalogTriggerType)analogTriggerType, &status);
if (status == PARAMETER_OUT_OF_RANGE) {
ThrowIllegalArgumentException(env,
"Counter only supports DownSource in "
"TwoPulse and ExternalDirection modes.");
return;
}
CheckStatus(env, status);
}
@@ -240,10 +234,6 @@ Java_edu_wpi_first_hal_CounterJNI_setCounterSamplesToAverage
{
int32_t status = 0;
HAL_SetCounterSamplesToAverage((HAL_CounterHandle)id, value, &status);
if (status == PARAMETER_OUT_OF_RANGE) {
ThrowBoundaryException(env, value, 1, 127);
return;
}
CheckStatus(env, status);
}

View File

@@ -6,6 +6,8 @@
#include <cassert>
#include <wpi/jni_util.h>
#include "HALUtil.h"
#include "edu_wpi_first_hal_DIOJNI.h"
#include "hal/DIO.h"
@@ -27,10 +29,10 @@ Java_edu_wpi_first_hal_DIOJNI_initializeDIOPort
(JNIEnv* env, jclass, jint id, jboolean input)
{
int32_t status = 0;
auto dio = HAL_InitializeDIOPort((HAL_PortHandle)id,
static_cast<uint8_t>(input), &status);
CheckStatusRange(env, status, 0, HAL_GetNumDigitalChannels(),
hal::getPortHandleChannel((HAL_PortHandle)id));
auto stack = wpi::java::GetJavaStackTrace(env, "edu.wpi.first");
auto dio = HAL_InitializeDIOPort(
(HAL_PortHandle)id, static_cast<uint8_t>(input), stack.c_str(), &status);
CheckStatusForceThrow(env, status);
return (jint)dio;
}

View File

@@ -267,10 +267,6 @@ Java_edu_wpi_first_hal_EncoderJNI_setEncoderSamplesToAverage
{
int32_t status = 0;
HAL_SetEncoderSamplesToAverage((HAL_EncoderHandle)id, value, &status);
if (status == PARAMETER_OUT_OF_RANGE) {
ThrowBoundaryException(env, value, 1, 127);
return;
}
CheckStatus(env, status);
}

View File

@@ -83,20 +83,18 @@ void ThrowUncleanStatusException(JNIEnv* env, wpi::StringRef msg,
env->Throw(static_cast<jthrowable>(exception));
}
void ThrowAllocationException(JNIEnv* env, int32_t minRange, int32_t maxRange,
int32_t requestedValue, int32_t status) {
const char* message = HAL_GetErrorMessage(status);
void ThrowAllocationException(JNIEnv* env, const char* lastError,
int32_t status) {
wpi::SmallString<1024> buf;
wpi::raw_svector_ostream oss(buf);
oss << " Code: " << status << ". " << message
<< ", Minimum Value: " << minRange << ", Maximum Value: " << maxRange
<< ", Requested Value: " << requestedValue;
env->ThrowNew(allocationExCls, buf.c_str());
oss << "Code: " << status << '\n' << lastError;
allocationExCls.Throw(env, buf.c_str());
}
void ThrowHalHandleException(JNIEnv* env, int32_t status) {
const char* message = HAL_GetErrorMessage(status);
const char* message = HAL_GetLastError(&status);
wpi::SmallString<1024> buf;
wpi::raw_svector_ostream oss(buf);
oss << " Code: " << status << ". " << message;
@@ -107,10 +105,11 @@ void ReportError(JNIEnv* env, int32_t status, bool doThrow) {
if (status == 0) {
return;
}
const char* message = HAL_GetLastError(&status);
if (status == HAL_HANDLE_ERROR) {
ThrowHalHandleException(env, status);
return;
}
const char* message = HAL_GetErrorMessage(status);
if (doThrow && status < 0) {
wpi::SmallString<1024> buf;
wpi::raw_svector_ostream oss(buf);
@@ -119,7 +118,11 @@ void ReportError(JNIEnv* env, int32_t status, bool doThrow) {
} else {
std::string func;
auto stack = GetJavaStackTrace(env, &func, "edu.wpi.first");
HAL_SendError(1, status, 0, message, func.c_str(), stack.c_str(), 1);
// Make a copy of message for safety, calling back into the HAL might
// invalidate the string.
wpi::SmallString<256> lastMessage{wpi::StringRef{message}};
HAL_SendError(1, status, 0, lastMessage.c_str(), func.c_str(),
stack.c_str(), 1);
}
}
@@ -128,17 +131,19 @@ void ThrowError(JNIEnv* env, int32_t status, int32_t minRange, int32_t maxRange,
if (status == 0) {
return;
}
const char* lastError = HAL_GetLastError(&status);
if (status == NO_AVAILABLE_RESOURCES || status == RESOURCE_IS_ALLOCATED ||
status == RESOURCE_OUT_OF_RANGE) {
ThrowAllocationException(env, minRange, maxRange, requestedValue, status);
ThrowAllocationException(env, lastError, status);
return;
}
if (status == HAL_HANDLE_ERROR) {
ThrowHalHandleException(env, status);
return;
}
const char* message = HAL_GetErrorMessage(status);
wpi::SmallString<1024> buf;
wpi::raw_svector_ostream oss(buf);
oss << " Code: " << status << ". " << message;
oss << " Code: " << status << ". " << lastError;
ThrowUncleanStatusException(env, buf.c_str(), status);
}

View File

@@ -6,6 +6,8 @@
#include <cassert>
#include <wpi/jni_util.h>
#include "HALUtil.h"
#include "edu_wpi_first_hal_PWMJNI.h"
#include "hal/DIO.h"
@@ -27,9 +29,9 @@ Java_edu_wpi_first_hal_PWMJNI_initializePWMPort
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
auto pwm = HAL_InitializePWMPort((HAL_PortHandle)id, &status);
CheckStatusRange(env, status, 0, HAL_GetNumPWMChannels(),
hal::getPortHandleChannel((HAL_PortHandle)id));
auto stack = wpi::java::GetJavaStackTrace(env, "edu.wpi.first");
auto pwm = HAL_InitializePWMPort((HAL_PortHandle)id, stack.c_str(), &status);
CheckStatusForceThrow(env, status);
return (jint)pwm;
}

View File

@@ -6,6 +6,8 @@
#include <cassert>
#include <wpi/jni_util.h>
#include "HALUtil.h"
#include "edu_wpi_first_hal_RelayJNI.h"
#include "hal/Ports.h"
@@ -26,10 +28,10 @@ Java_edu_wpi_first_hal_RelayJNI_initializeRelayPort
(JNIEnv* env, jclass, jint id, jboolean fwd)
{
int32_t status = 0;
auto stack = wpi::java::GetJavaStackTrace(env, "edu.wpi.first");
HAL_RelayHandle handle = HAL_InitializeRelayPort(
(HAL_PortHandle)id, static_cast<uint8_t>(fwd), &status);
CheckStatusRange(env, status, 0, HAL_GetNumRelayChannels(),
hal::getPortHandleChannel((HAL_PortHandle)id));
(HAL_PortHandle)id, static_cast<uint8_t>(fwd), stack.c_str(), &status);
CheckStatusForceThrow(env, status);
return (jint)handle;
}

View File

@@ -109,8 +109,6 @@ class DeviceCallbackStore {
class ValueCallbackStore {
public:
explicit ValueCallbackStore(bool dirCallback) : m_dirCallback{dirCallback} {}
void create(JNIEnv* env, jobject obj) { m_call = JGlobal<jobject>(env, obj); }
void performCallback(const char* name, HAL_SimValueHandle handle,
int32_t direction, const HAL_Value& value);
@@ -121,7 +119,6 @@ class ValueCallbackStore {
private:
wpi::java::JGlobal<jobject> m_call;
int32_t m_callbackId;
bool m_dirCallback;
};
} // namespace
@@ -181,17 +178,9 @@ void ValueCallbackStore::performCallback(const char* name,
}
auto [value1, value2] = ToValue12(value);
if (m_dirCallback) {
env->CallVoidMethod(m_call, simValueCallbackCallback,
MakeJString(env, name), static_cast<jint>(handle),
static_cast<jint>(direction),
static_cast<jint>(value.type), value1, value2);
} else {
env->CallVoidMethod(m_call, simValueCallbackCallback,
MakeJString(env, name), static_cast<jint>(handle),
static_cast<jboolean>(direction == HAL_SimValueOutput),
static_cast<jint>(value.type), value1, value2);
}
env->CallVoidMethod(m_call, simValueCallbackCallback, MakeJString(env, name),
static_cast<jint>(handle), static_cast<jint>(direction),
static_cast<jint>(value.type), value1, value2);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
@@ -266,12 +255,11 @@ using FreeValueCallbackFunc = void (*)(int32_t uid);
template <typename THandle>
static SIM_JniHandle AllocateValueCallback(
JNIEnv* env, THandle h, jobject callback, bool dirCallback,
jboolean initialNotify,
JNIEnv* env, THandle h, jobject callback, jboolean initialNotify,
int32_t (*createCallback)(THandle handle, void* param,
HALSIM_SimValueCallback callback,
HAL_Bool initialNotify)) {
auto callbackStore = std::make_shared<ValueCallbackStore>(dirCallback);
auto callbackStore = std::make_shared<ValueCallbackStore>();
auto handle = valueCallbackHandles->Allocate(callbackStore);
@@ -345,7 +333,7 @@ bool InitializeSimDeviceDataJNI(JNIEnv* env) {
}
simValueCallbackCallback = env->GetMethodID(
simValueCallbackCls, "callbackNative", "(Ljava/lang/String;IZIJD)V");
simValueCallbackCls, "callbackNative", "(Ljava/lang/String;IIIJD)V");
if (!simValueCallbackCallback) {
return false;
}
@@ -530,21 +518,7 @@ Java_edu_wpi_first_hal_simulation_SimDeviceDataJNI_registerSimValueCreatedCallba
(JNIEnv* env, jclass, jint device, jobject callback, jboolean initialNotify)
{
return AllocateValueCallback(env, static_cast<HAL_SimDeviceHandle>(device),
callback, false, initialNotify,
&HALSIM_RegisterSimValueCreatedCallback);
}
/*
* Class: edu_wpi_first_hal_simulation_SimDeviceDataJNI
* Method: registerSimValueCreatedCallback2
* Signature: (ILjava/lang/Object;Z)I
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_simulation_SimDeviceDataJNI_registerSimValueCreatedCallback2
(JNIEnv* env, jclass, jint device, jobject callback, jboolean initialNotify)
{
return AllocateValueCallback(env, static_cast<HAL_SimDeviceHandle>(device),
callback, true, initialNotify,
callback, initialNotify,
&HALSIM_RegisterSimValueCreatedCallback);
}
@@ -570,21 +544,7 @@ Java_edu_wpi_first_hal_simulation_SimDeviceDataJNI_registerSimValueChangedCallba
(JNIEnv* env, jclass, jint handle, jobject callback, jboolean initialNotify)
{
return AllocateValueCallback(env, static_cast<HAL_SimValueHandle>(handle),
callback, false, initialNotify,
&HALSIM_RegisterSimValueChangedCallback);
}
/*
* Class: edu_wpi_first_hal_simulation_SimDeviceDataJNI
* Method: registerSimValueChangedCallback2
* Signature: (ILjava/lang/Object;Z)I
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_simulation_SimDeviceDataJNI_registerSimValueChangedCallback2
(JNIEnv* env, jclass, jint handle, jobject callback, jboolean initialNotify)
{
return AllocateValueCallback(env, static_cast<HAL_SimValueHandle>(handle),
callback, true, initialNotify,
callback, initialNotify,
&HALSIM_RegisterSimValueChangedCallback);
}
@@ -610,7 +570,7 @@ Java_edu_wpi_first_hal_simulation_SimDeviceDataJNI_registerSimValueResetCallback
(JNIEnv* env, jclass, jint handle, jobject callback, jboolean initialNotify)
{
return AllocateValueCallback(env, static_cast<HAL_SimValueHandle>(handle),
callback, true, initialNotify,
callback, initialNotify,
&HALSIM_RegisterSimValueResetCallback);
}

View File

@@ -22,9 +22,12 @@ extern "C" {
* Initializes an analog gyro.
*
* @param handle handle to the analog port
* @param allocationLocation the location where the allocation is occuring
* (can be null)
* @return the initialized gyro handle
*/
HAL_GyroHandle HAL_InitializeAnalogGyro(HAL_AnalogInputHandle handle,
const char* allocationLocation,
int32_t* status);
/**

View File

@@ -22,10 +22,12 @@ extern "C" {
* Initializes the analog input port using the given port object.
*
* @param portHandle Handle to the port to initialize.
* @param allocationLocation the location where the allocation is occuring
* (can be null)
* @return the created analog input handle
*/
HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
int32_t* status);
HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(
HAL_PortHandle portHandle, const char* allocationLocation, int32_t* status);
/**
* Frees an analog input port.

View File

@@ -22,10 +22,12 @@ extern "C" {
* Initializes the analog output port using the given port object.
*
* @param handle handle to the port
* @param allocationLocation the location where the allocation is occuring
* (can be null)
* @return the created analog output handle
*/
HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(HAL_PortHandle portHandle,
int32_t* status);
HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(
HAL_PortHandle portHandle, const char* allocationLocation, int32_t* status);
/**
* Frees an analog output port.

View File

@@ -21,12 +21,16 @@ extern "C" {
/**
* Creates a new instance of a digital port.
*
* @param portHandle the port handle to create from
* @param input true for input, false for output
* @return the created digital handle
* @param portHandle the port handle to create from
* @param input true for input, false for output
* @param allocationLocation the location where the allocation is occuring
* (can be null)
* @return the created digital handle
*/
HAL_DigitalHandle HAL_InitializeDIOPort(HAL_PortHandle portHandle,
HAL_Bool input, int32_t* status);
HAL_Bool input,
const char* allocationLocation,
int32_t* status);
/**
* Checks if a DIO channel is valid.

View File

@@ -41,6 +41,8 @@
#define ERR_FRCSystem_NoDSConnection_MESSAGE \
"FRCSystem: No driver station connected"
#define HAL_SUCCESS 0
#define SAMPLE_RATE_TOO_HIGH 1001
#define SAMPLE_RATE_TOO_HIGH_MESSAGE \
"HAL: Analog module sample rate is too high"
@@ -125,6 +127,10 @@
#define HAL_SIM_NOT_SUPPORTED -1155
#define HAL_SIM_NOT_SUPPORTED_MESSAGE "HAL: Method not supported in sim"
#define HAL_USE_LAST_ERROR -1156
#define HAL_USE_LAST_ERROR_MESSAGE \
"HAL: Use HAL_GetLastError(status) to get last error"
#define HAL_CAN_BUFFER_OVERRUN -35007
#define HAL_CAN_BUFFER_OVERRUN_MESSAGE \
"HAL: CAN Output Buffer Full. Ensure a device is attached"

View File

@@ -22,6 +22,19 @@ HAL_ENUM(HAL_RuntimeType) { HAL_Athena, HAL_Mock };
extern "C" {
#endif
/**
* Gets the last error set on this thread, or the message for the status code.
*
* If passed HAL_USE_LAST_ERROR, the last error set on the thread will be
* returned.
*
* @param code the status code, set to the error status code if input is
* HAL_USE_LAST_ERROR
* @return the error message for the code. This does not need to be freed,
* but can be overwritten by another hal call on the same thread.
*/
const char* HAL_GetLastError(int32_t* status);
/**
* Gets the error message for a specific status code.
*

View File

@@ -22,9 +22,12 @@ extern "C" {
* Initializes a PWM port.
*
* @param portHandle the port to initialize
* @param allocationLocation the location where the allocation is occuring
* (can be null)
* @return the created pwm handle
*/
HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
const char* allocationLocation,
int32_t* status);
/**

View File

@@ -26,9 +26,12 @@ extern "C" {
*
* @param portHandle the port handle to initialize
* @param fwd true for the forward port, false for the reverse port
* @param allocationLocation the location where the allocation is occuring
* (can be null)
* @return the created relay handle
*/
HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
const char* allocationLocation,
int32_t* status);
/**

View File

@@ -22,7 +22,7 @@ namespace hal {
* allows a limited number of handles that are allocated by index.
* The enum value is separate, as 2 enum values are allowed per handle
* Because they are allocated by index, each individual index holds its own
* mutex, which reduces contention heavily.]
* mutex, which reduces contention heavily.
*
* @tparam THandle The Handle Type (Must be typedefed from HAL_Handle)
* @tparam TStruct The struct type held by this resource
@@ -38,7 +38,8 @@ class DigitalHandleResource : public HandleBase {
DigitalHandleResource(const DigitalHandleResource&) = delete;
DigitalHandleResource& operator=(const DigitalHandleResource&) = delete;
THandle Allocate(int16_t index, HAL_HandleEnum enumValue, int32_t* status);
std::shared_ptr<TStruct> Allocate(int16_t index, HAL_HandleEnum enumValue,
THandle* handle, int32_t* status);
int16_t GetIndex(THandle handle, HAL_HandleEnum enumValue) {
return getHandleTypedIndex(handle, enumValue, m_version);
}
@@ -52,21 +53,27 @@ class DigitalHandleResource : public HandleBase {
};
template <typename THandle, typename TStruct, int16_t size>
THandle DigitalHandleResource<THandle, TStruct, size>::Allocate(
int16_t index, HAL_HandleEnum enumValue, int32_t* status) {
std::shared_ptr<TStruct>
DigitalHandleResource<THandle, TStruct, size>::Allocate(
int16_t index, HAL_HandleEnum enumValue, THandle* handle, int32_t* status) {
// don't acquire the lock if we can fail early.
if (index < 0 || index >= size) {
*handle = HAL_kInvalidHandle;
*status = RESOURCE_OUT_OF_RANGE;
return HAL_kInvalidHandle;
return nullptr;
}
std::scoped_lock lock(m_handleMutexes[index]);
// check for allocation, otherwise allocate and return a valid handle
if (m_structures[index] != nullptr) {
*handle = HAL_kInvalidHandle;
*status = RESOURCE_IS_ALLOCATED;
return HAL_kInvalidHandle;
return m_structures[index];
}
m_structures[index] = std::make_shared<TStruct>();
return static_cast<THandle>(hal::createHandle(index, enumValue, m_version));
*handle =
static_cast<THandle>(hal::createHandle(index, enumValue, m_version));
*status = HAL_SUCCESS;
return m_structures[index];
}
template <typename THandle, typename TStruct, int16_t size>

View File

@@ -39,7 +39,8 @@ class IndexedHandleResource : public HandleBase {
IndexedHandleResource(const IndexedHandleResource&) = delete;
IndexedHandleResource& operator=(const IndexedHandleResource&) = delete;
THandle Allocate(int16_t index, int32_t* status);
std::shared_ptr<TStruct> Allocate(int16_t index, THandle* handle,
int32_t* status);
int16_t GetIndex(THandle handle) {
return getHandleTypedIndex(handle, enumValue, m_version);
}
@@ -54,21 +55,27 @@ class IndexedHandleResource : public HandleBase {
template <typename THandle, typename TStruct, int16_t size,
HAL_HandleEnum enumValue>
THandle IndexedHandleResource<THandle, TStruct, size, enumValue>::Allocate(
int16_t index, int32_t* status) {
std::shared_ptr<TStruct>
IndexedHandleResource<THandle, TStruct, size, enumValue>::Allocate(
int16_t index, THandle* handle, int32_t* status) {
// don't acquire the lock if we can fail early.
if (index < 0 || index >= size) {
*status = RESOURCE_OUT_OF_RANGE;
return HAL_kInvalidHandle;
*handle = HAL_kInvalidHandle;
return nullptr;
}
std::scoped_lock lock(m_handleMutexes[index]);
// check for allocation, otherwise allocate and return a valid handle
if (m_structures[index] != nullptr) {
*status = RESOURCE_IS_ALLOCATED;
return HAL_kInvalidHandle;
*handle = HAL_kInvalidHandle;
return m_structures[index];
}
m_structures[index] = std::make_shared<TStruct>();
return static_cast<THandle>(hal::createHandle(index, enumValue, m_version));
*handle =
static_cast<THandle>(hal::createHandle(index, enumValue, m_version));
*status = HAL_SUCCESS;
return m_structures[index];
}
template <typename THandle, typename TStruct, int16_t size,

View File

@@ -6,6 +6,7 @@
#include "DigitalInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/Errors.h"
#include "hal/handles/HandlesInternal.h"
@@ -110,8 +111,11 @@ void HAL_SetAddressableLEDLength(HAL_AddressableLEDHandle handle,
*status = HAL_HANDLE_ERROR;
return;
}
if (length > HAL_kAddressableLEDMaxLength) {
if (length > HAL_kAddressableLEDMaxLength || length < 0) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "LED length must be less than or equal to " +
wpi::Twine(HAL_kAddressableLEDMaxLength) +
". " + wpi::Twine(length) + " was requested");
return;
}
SimAddressableLEDData[led->index].length = length;
@@ -127,6 +131,10 @@ void HAL_WriteAddressableLEDData(HAL_AddressableLEDHandle handle,
}
if (length > SimAddressableLEDData[led->index].length) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status,
"Data length must be less than or equal to " +
wpi::Twine(SimAddressableLEDData[led->index].length) +
". " + wpi::Twine(length) + " was requested");
return;
}
SimAddressableLEDData[led->index].SetData(data, length);

View File

@@ -4,7 +4,10 @@
#include "hal/AnalogGyro.h"
#include <string>
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/AnalogAccumulator.h"
#include "hal/Errors.h"
@@ -15,6 +18,7 @@ namespace {
struct AnalogGyro {
HAL_AnalogInputHandle handle;
uint8_t index;
std::string previousAllocation;
};
} // namespace
@@ -34,36 +38,41 @@ void InitializeAnalogGyro() {
extern "C" {
HAL_GyroHandle HAL_InitializeAnalogGyro(HAL_AnalogInputHandle analogHandle,
const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
// Handle will be type checked by HAL_IsAccumulatorChannel
int16_t channel = getHandleIndex(analogHandle);
if (!HAL_IsAccumulatorChannel(analogHandle, status)) {
if (*status == 0) {
*status = HAL_INVALID_ACCUMULATOR_CHANNEL;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Gyro",
0, kNumAccumulators, channel);
}
return HAL_kInvalidHandle;
}
// handle known to be correct, so no need to type check
int16_t channel = getHandleIndex(analogHandle);
auto handle = analogGyroHandles->Allocate(channel, status);
HAL_GyroHandle handle;
auto gyro = analogGyroHandles->Allocate(channel, &handle, status);
if (*status != 0) {
if (gyro) {
hal::SetLastErrorPreviouslyAllocated(status, "Analog Gyro", channel,
gyro->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Gyro",
0, kNumAccumulators, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
// Initialize port structure
auto gyro = analogGyroHandles->Get(handle);
if (gyro == nullptr) { // would only error on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
gyro->handle = analogHandle;
gyro->index = channel;
SimAnalogGyroData[channel].initialized = true;
gyro->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}

View File

@@ -6,6 +6,7 @@
#include "AnalogInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/AnalogAccumulator.h"
#include "hal/handles/HandlesInternal.h"
@@ -18,28 +19,32 @@ void InitializeAnalogInput() {}
} // namespace hal::init
extern "C" {
HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
int32_t* status) {
HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(
HAL_PortHandle portHandle, const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
if (channel == InvalidHandleIndex || channel >= kNumAnalogInputs) {
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Input",
0, kNumAnalogInputs, channel);
return HAL_kInvalidHandle;
}
HAL_AnalogInputHandle handle = analogInputHandles->Allocate(channel, status);
HAL_AnalogInputHandle handle;
auto analog_port = analogInputHandles->Allocate(channel, &handle, status);
if (*status != 0) {
if (analog_port) {
hal::SetLastErrorPreviouslyAllocated(status, "Analog Input", channel,
analog_port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Input",
0, kNumAnalogInputs, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
// Initialize port structure
auto analog_port = analogInputHandles->Get(handle);
if (analog_port == nullptr) { // would only error on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
analog_port->channel = static_cast<uint8_t>(channel);
if (HAL_IsAccumulatorChannel(handle, status)) {
analog_port->isAccumulator = true;
@@ -51,6 +56,9 @@ HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
SimAnalogInData[channel].accumulatorInitialized = false;
SimAnalogInData[channel].simDevice = 0;
analog_port->previousAllocation =
allocationLocation ? allocationLocation : "";
return handle;
}
void HAL_FreeAnalogInputPort(HAL_AnalogInputHandle analogPortHandle) {

View File

@@ -6,6 +6,8 @@
#include <stdint.h>
#include <string>
#include "PortsInternal.h"
#include "hal/handles/HandlesInternal.h"
#include "hal/handles/IndexedHandleResource.h"
@@ -20,6 +22,7 @@ static constexpr uint32_t kAccumulatorChannels[] = {0, 1};
struct AnalogPort {
uint8_t channel;
bool isAccumulator;
std::string previousAllocation;
};
extern IndexedHandleResource<HAL_AnalogInputHandle, hal::AnalogPort,

View File

@@ -4,7 +4,10 @@
#include "hal/AnalogOutput.h"
#include <string>
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/Errors.h"
#include "hal/handles/HandlesInternal.h"
@@ -16,6 +19,7 @@ using namespace hal;
namespace {
struct AnalogOutput {
uint8_t channel;
std::string previousAllocation;
};
} // namespace
@@ -33,32 +37,39 @@ void InitializeAnalogOutput() {
} // namespace hal::init
extern "C" {
HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(HAL_PortHandle portHandle,
int32_t* status) {
HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(
HAL_PortHandle portHandle, const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
if (channel == InvalidHandleIndex || channel >= kNumAnalogOutputs) {
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Output",
0, kNumAnalogOutputs, channel);
return HAL_kInvalidHandle;
}
HAL_AnalogOutputHandle handle =
analogOutputHandles->Allocate(channel, status);
HAL_AnalogOutputHandle handle;
auto port = analogOutputHandles->Allocate(channel, &handle, status);
if (*status != 0) {
if (port) {
hal::SetLastErrorPreviouslyAllocated(status, "Analog Output", channel,
port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status,
"Invalid Index for Analog Output", 0,
kNumAnalogOutputs, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = analogOutputHandles->Get(handle);
if (port == nullptr) { // would only error on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
port->channel = static_cast<uint8_t>(channel);
// Initialize sim analog input
SimAnalogOutData[channel].initialized = true;
port->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}

View File

@@ -6,6 +6,7 @@
#include "DigitalInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/handles/HandlesInternal.h"
#include "hal/handles/LimitedHandleResource.h"
@@ -31,36 +32,41 @@ void InitializeDIO() {
extern "C" {
HAL_DigitalHandle HAL_InitializeDIOPort(HAL_PortHandle portHandle,
HAL_Bool input, int32_t* status) {
HAL_Bool input,
const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
if (*status != 0) {
return HAL_kInvalidHandle;
}
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for DIO", 0,
kNumDigitalChannels, channel);
return HAL_kInvalidHandle;
}
auto handle =
digitalChannelHandles->Allocate(channel, HAL_HandleEnum::DIO, status);
HAL_DigitalHandle handle;
auto port = digitalChannelHandles->Allocate(channel, HAL_HandleEnum::DIO,
&handle, status);
if (*status != 0) {
if (port) {
hal::SetLastErrorPreviouslyAllocated(status, "PWM or DIO", channel,
port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for DIO", 0,
kNumDigitalChannels, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = digitalChannelHandles->Get(handle, HAL_HandleEnum::DIO);
if (port == nullptr) { // would only occur on thread issue.
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
port->channel = static_cast<uint8_t>(channel);
SimDIOData[channel].initialized = true;
SimDIOData[channel].isInput = input;
SimDIOData[channel].simDevice = 0;
port->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}

View File

@@ -6,6 +6,8 @@
#include <stdint.h>
#include <string>
#include "PortsInternal.h"
#include "hal/AnalogTrigger.h"
#include "hal/handles/DigitalHandleResource.h"
@@ -56,6 +58,7 @@ struct DigitalPort {
int32_t centerPwm = 0;
int32_t deadbandMinPwm = 0;
int32_t minPwm = 0;
std::string previousAllocation;
};
extern DigitalHandleResource<HAL_DigitalHandle, DigitalPort,

View File

@@ -6,6 +6,7 @@
#include "CounterInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/Errors.h"
#include "hal/handles/HandlesInternal.h"
@@ -246,6 +247,7 @@ void HAL_SetEncoderMinRate(HAL_EncoderHandle encoderHandle, double minRate,
if (minRate == 0.0) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "minRate must not be 0");
return;
}
@@ -262,6 +264,7 @@ void HAL_SetEncoderDistancePerPulse(HAL_EncoderHandle encoderHandle,
if (distancePerPulse == 0.0) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "distancePerPulse must not be 0");
return;
}
encoder->distancePerPulse = distancePerPulse;

View File

@@ -250,6 +250,8 @@ const char* HAL_GetErrorMessage(int32_t code) {
return HAL_CAN_BUFFER_OVERRUN_MESSAGE;
case HAL_LED_CHANNEL_ERROR:
return HAL_LED_CHANNEL_ERROR_MESSAGE;
case HAL_USE_LAST_ERROR:
return HAL_USE_LAST_ERROR_MESSAGE;
default:
return "Unknown error status";
}

View File

@@ -0,0 +1,19 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <stdint.h>
#include <wpi/Twine.h>
namespace hal {
void SetLastError(int32_t* status, const wpi::Twine& value);
void SetLastErrorIndexOutOfRange(int32_t* status, const wpi::Twine& message,
int32_t minimum, int32_t maximum,
int32_t channel);
void SetLastErrorPreviouslyAllocated(int32_t* status, const wpi::Twine& message,
int32_t channel,
const wpi::Twine& previousAllocation);
} // namespace hal

View File

@@ -6,6 +6,7 @@
#include "CANAPIInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/CANAPI.h"
#include "hal/Errors.h"
@@ -27,6 +28,7 @@ extern "C" {
HAL_PDPHandle HAL_InitializePDP(int32_t module, int32_t* status) {
if (!HAL_CheckPDPModule(module)) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, "Invalid pdp module " + wpi::Twine(module));
return HAL_kInvalidHandle;
}
hal::init::CheckInit();

View File

@@ -7,6 +7,7 @@
#include "ConstantsInternal.h"
#include "DigitalInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/handles/HandlesInternal.h"
#include "mockdata/PWMDataInternal.h"
@@ -20,15 +21,15 @@ void InitializePWM() {}
extern "C" {
HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
if (*status != 0) {
return HAL_kInvalidHandle;
}
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for PWM", 0,
kNumPWMChannels, channel);
return HAL_kInvalidHandle;
}
@@ -40,19 +41,22 @@ HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
channel = remapMXPPWMChannel(channel) + 10; // remap MXP to proper channel
}
auto handle =
digitalChannelHandles->Allocate(channel, HAL_HandleEnum::PWM, status);
HAL_DigitalHandle handle;
auto port = digitalChannelHandles->Allocate(channel, HAL_HandleEnum::PWM,
&handle, status);
if (*status != 0) {
if (port) {
hal::SetLastErrorPreviouslyAllocated(status, "PWM or DIO", channel,
port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for PWM", 0,
kNumPWMChannels, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = digitalChannelHandles->Get(handle, HAL_HandleEnum::PWM);
if (port == nullptr) { // would only occur on thread issue.
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
port->channel = origChannel;
SimPWMData[origChannel].initialized = true;
@@ -60,6 +64,8 @@ HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
// Defaults to allow an always valid config.
HAL_SetPWMConfig(handle, 2.0, 1.501, 1.5, 1.499, 1.0, status);
port->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}
void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle, int32_t* status) {

View File

@@ -4,7 +4,10 @@
#include "hal/Relay.h"
#include <string>
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/handles/IndexedHandleResource.h"
#include "mockdata/RelayDataInternal.h"
@@ -15,6 +18,7 @@ namespace {
struct Relay {
uint8_t channel;
bool fwd;
std::string previousAllocation;
};
} // namespace
@@ -32,6 +36,7 @@ void InitializeRelay() {
extern "C" {
HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
if (*status != 0) {
@@ -39,8 +44,10 @@ HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
}
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
if (channel == InvalidHandleIndex || channel >= kNumRelayChannels) {
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Relay", 0,
kNumRelayChannels, channel);
return HAL_kInvalidHandle;
}
@@ -48,18 +55,20 @@ HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
channel += kNumRelayHeaders; // add 4 to reverse channels
}
auto handle = relayHandles->Allocate(channel, status);
HAL_RelayHandle handle;
auto port = relayHandles->Allocate(channel, &handle, status);
if (*status != 0) {
if (port) {
hal::SetLastErrorPreviouslyAllocated(status, "Relay", channel,
port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Relay", 0,
kNumRelayChannels, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = relayHandles->Get(handle);
if (port == nullptr) { // would only occur on thread issue.
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
if (!fwd) {
// Subtract number of headers to put channel in range
channel -= kNumRelayHeaders;
@@ -73,6 +82,7 @@ HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
}
port->channel = static_cast<uint8_t>(channel);
port->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}

View File

@@ -55,17 +55,13 @@ HAL_SolenoidHandle HAL_InitializeSolenoidPort(HAL_PortHandle portHandle,
return HAL_kInvalidHandle;
}
auto handle = solenoidHandles->Allocate(
module * kNumSolenoidChannels + channel, status);
HAL_SolenoidHandle handle;
auto solenoidPort = solenoidHandles->Allocate(
module * kNumSolenoidChannels + channel, &handle, status);
if (handle == HAL_kInvalidHandle) { // out of resources
*status = NO_AVAILABLE_RESOURCES;
return HAL_kInvalidHandle;
}
auto solenoidPort = solenoidHandles->Get(handle);
if (solenoidPort == nullptr) { // would only occur on thread issues
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
solenoidPort->module = static_cast<uint8_t>(module);
solenoidPort->channel = static_cast<uint8_t>(channel);

Some files were not shown because too many files have changed in this diff Show More