mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-05 03:21:42 +00:00
Some general HAL cleanups (#153)
This commit is contained in:
committed by
Peter Johnson
parent
aa9c2b2c92
commit
ea6876e81f
@@ -150,12 +150,3 @@ void HAL_NetworkCommunicationObserveUserProgramTest();
|
|||||||
uint32_t HAL_Report(uint8_t resource, uint8_t instanceNumber,
|
uint32_t HAL_Report(uint8_t resource, uint8_t instanceNumber,
|
||||||
uint8_t context = 0, const char* feature = nullptr);
|
uint8_t context = 0, const char* feature = nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: HACKS for now...
|
|
||||||
extern "C" {
|
|
||||||
|
|
||||||
void NumericArrayResize();
|
|
||||||
void RTSetCleanupProc();
|
|
||||||
void EDVR_CreateReference();
|
|
||||||
void Occur();
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,48 +0,0 @@
|
|||||||
/*----------------------------------------------------------------------------*/
|
|
||||||
/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
|
|
||||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
||||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
||||||
/* the project. */
|
|
||||||
/*----------------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "../Errors.h"
|
|
||||||
#include "HAL/cpp/priority_mutex.h"
|
|
||||||
|
|
||||||
// TODO: Replace this with something appropriate to avoid conflicts with
|
|
||||||
// wpilibC++ Resource class (which performs an essentially identical function).
|
|
||||||
namespace hal {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The Resource class is a convenient way to track allocated resources.
|
|
||||||
* It tracks them as indicies in the range [0 .. elements - 1].
|
|
||||||
* E.g. the library uses this to track hardware channel allocation.
|
|
||||||
*
|
|
||||||
* The Resource class does not allocate the hardware channels or other
|
|
||||||
* resources; it just tracks which indices were marked in use by
|
|
||||||
* Allocate and not yet freed by Free.
|
|
||||||
*/
|
|
||||||
class Resource {
|
|
||||||
public:
|
|
||||||
Resource(const Resource&) = delete;
|
|
||||||
Resource& operator=(const Resource&) = delete;
|
|
||||||
explicit Resource(uint32_t size);
|
|
||||||
virtual ~Resource() = default;
|
|
||||||
static void CreateResourceObject(Resource** r, uint32_t elements);
|
|
||||||
uint32_t Allocate(const char* resourceDesc);
|
|
||||||
uint32_t Allocate(uint32_t index, const char* resourceDesc);
|
|
||||||
void Free(uint32_t index);
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<bool> m_isAllocated;
|
|
||||||
priority_recursive_mutex m_allocateLock;
|
|
||||||
|
|
||||||
static priority_recursive_mutex m_createLock;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace hal
|
|
||||||
@@ -236,23 +236,15 @@ int16_t HAL_GetAnalogValue(HAL_AnalogInputHandle analog_port_handle,
|
|||||||
*status = HAL_HANDLE_ERROR;
|
*status = HAL_HANDLE_ERROR;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int16_t value;
|
|
||||||
if (!HAL_CheckAnalogInputChannel(port->pin)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
tAI::tReadSelect readSelect;
|
tAI::tReadSelect readSelect;
|
||||||
readSelect.Channel = port->pin;
|
readSelect.Channel = port->pin;
|
||||||
readSelect.Averaged = false;
|
readSelect.Averaged = false;
|
||||||
|
|
||||||
{
|
std::lock_guard<priority_recursive_mutex> sync(analogRegisterWindowMutex);
|
||||||
std::lock_guard<priority_recursive_mutex> sync(analogRegisterWindowMutex);
|
analogInputSystem->writeReadSelect(readSelect, status);
|
||||||
analogInputSystem->writeReadSelect(readSelect, status);
|
analogInputSystem->strobeLatchOutput(status);
|
||||||
analogInputSystem->strobeLatchOutput(status);
|
return static_cast<int16_t>(analogInputSystem->readOutput(status));
|
||||||
value = (int16_t)analogInputSystem->readOutput(status);
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -276,23 +268,14 @@ int32_t HAL_GetAnalogAverageValue(HAL_AnalogInputHandle analog_port_handle,
|
|||||||
*status = HAL_HANDLE_ERROR;
|
*status = HAL_HANDLE_ERROR;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int32_t value;
|
|
||||||
if (!HAL_CheckAnalogInputChannel(port->pin)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
tAI::tReadSelect readSelect;
|
tAI::tReadSelect readSelect;
|
||||||
readSelect.Channel = port->pin;
|
readSelect.Channel = port->pin;
|
||||||
readSelect.Averaged = true;
|
readSelect.Averaged = true;
|
||||||
|
|
||||||
{
|
std::lock_guard<priority_recursive_mutex> sync(analogRegisterWindowMutex);
|
||||||
std::lock_guard<priority_recursive_mutex> sync(analogRegisterWindowMutex);
|
analogInputSystem->writeReadSelect(readSelect, status);
|
||||||
analogInputSystem->writeReadSelect(readSelect, status);
|
analogInputSystem->strobeLatchOutput(status);
|
||||||
analogInputSystem->strobeLatchOutput(status);
|
return static_cast<int32_t>(analogInputSystem->readOutput(status));
|
||||||
value = (int32_t)analogInputSystem->readOutput(status);
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -109,10 +109,9 @@ uint32_t remapMXPPWMChannel(uint32_t pin) {
|
|||||||
* If it's an analog trigger, determine the module from the high order routing
|
* If it's an analog trigger, determine the module from the high order routing
|
||||||
* channel else do normal digital input remapping based on pin number (MXP)
|
* channel else do normal digital input remapping based on pin number (MXP)
|
||||||
*/
|
*/
|
||||||
extern "C++" bool remapDigitalSource(HAL_Handle digitalSourceHandle,
|
bool remapDigitalSource(HAL_Handle digitalSourceHandle,
|
||||||
HAL_AnalogTriggerType analogTriggerType,
|
HAL_AnalogTriggerType analogTriggerType, uint32_t& pin,
|
||||||
uint32_t& pin, uint8_t& module,
|
uint8_t& module, bool& analogTrigger) {
|
||||||
bool& analogTrigger) {
|
|
||||||
if (isHandleType(digitalSourceHandle, HAL_HandleEnum::AnalogTrigger)) {
|
if (isHandleType(digitalSourceHandle, HAL_HandleEnum::AnalogTrigger)) {
|
||||||
// If handle passed, index is not negative
|
// If handle passed, index is not negative
|
||||||
uint32_t index = getHandleIndex(digitalSourceHandle);
|
uint32_t index = getHandleIndex(digitalSourceHandle);
|
||||||
|
|||||||
@@ -374,6 +374,7 @@ uint32_t HAL_Report(uint8_t resource, uint8_t instanceNumber, uint8_t context,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: HACKS
|
// TODO: HACKS
|
||||||
|
// No need for header definitions, as we should not run from user code.
|
||||||
void NumericArrayResize() {}
|
void NumericArrayResize() {}
|
||||||
void RTSetCleanupProc() {}
|
void RTSetCleanupProc() {}
|
||||||
void EDVR_CreateReference() {}
|
void EDVR_CreateReference() {}
|
||||||
|
|||||||
@@ -1,105 +0,0 @@
|
|||||||
/*----------------------------------------------------------------------------*/
|
|
||||||
/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
|
|
||||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
||||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
||||||
/* the project. */
|
|
||||||
/*----------------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
#include "HAL/cpp/Resource.h"
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#include "HAL/Errors.h"
|
|
||||||
#include "HAL/cpp/priority_mutex.h"
|
|
||||||
|
|
||||||
namespace hal {
|
|
||||||
|
|
||||||
priority_recursive_mutex Resource::m_createLock;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Allocate storage for a new instance of Resource.
|
|
||||||
* Allocate a bool array of values that will get initialized to indicate that no
|
|
||||||
* resources have been allocated yet. The indicies of the resources are [0 ..
|
|
||||||
* elements - 1].
|
|
||||||
*/
|
|
||||||
Resource::Resource(uint32_t elements) {
|
|
||||||
std::lock_guard<priority_recursive_mutex> sync(m_createLock);
|
|
||||||
m_isAllocated = std::vector<bool>(elements, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Factory method to create a Resource allocation-tracker *if* needed.
|
|
||||||
*
|
|
||||||
* @param r -- address of the caller's Resource pointer. If *r == nullptr, this
|
|
||||||
* will construct a Resource and make *r point to it. If *r != nullptr, i.e.
|
|
||||||
* the caller already has a Resource instance, this won't do anything.
|
|
||||||
* @param elements -- the number of elements for this Resource allocator to
|
|
||||||
* track, that is, it will allocate resource numbers in the range
|
|
||||||
* [0 .. elements - 1].
|
|
||||||
*/
|
|
||||||
/*static*/ void Resource::CreateResourceObject(Resource** r,
|
|
||||||
uint32_t elements) {
|
|
||||||
std::lock_guard<priority_recursive_mutex> sync(m_createLock);
|
|
||||||
if (*r == nullptr) {
|
|
||||||
*r = new Resource(elements);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Allocate a resource.
|
|
||||||
* When a resource is requested, mark it allocated. In this case, a free
|
|
||||||
* resource value
|
|
||||||
* within the range is located and returned after it is marked allocated.
|
|
||||||
*/
|
|
||||||
uint32_t Resource::Allocate(const char* resourceDesc) {
|
|
||||||
std::lock_guard<priority_recursive_mutex> sync(m_allocateLock);
|
|
||||||
for (uint32_t i = 0; i < m_isAllocated.size(); i++) {
|
|
||||||
if (!m_isAllocated[i]) {
|
|
||||||
m_isAllocated[i] = true;
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// TODO: wpi_setWPIErrorWithContext(NoAvailableResources, resourceDesc);
|
|
||||||
return ~0ul;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Allocate a specific resource value.
|
|
||||||
* The user requests a specific resource value, i.e. channel number and it is
|
|
||||||
* verified unallocated, then returned.
|
|
||||||
*/
|
|
||||||
uint32_t Resource::Allocate(uint32_t index, const char* resourceDesc) {
|
|
||||||
std::lock_guard<priority_recursive_mutex> sync(m_allocateLock);
|
|
||||||
if (index >= m_isAllocated.size()) {
|
|
||||||
// TODO: wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, resourceDesc);
|
|
||||||
return ~0ul;
|
|
||||||
}
|
|
||||||
if (m_isAllocated[index]) {
|
|
||||||
// TODO: wpi_setWPIErrorWithContext(ResourceAlreadyAllocated, resourceDesc);
|
|
||||||
return ~0ul;
|
|
||||||
}
|
|
||||||
m_isAllocated[index] = true;
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Free an allocated resource.
|
|
||||||
* After a resource is no longer needed, for example a destructor is called for
|
|
||||||
* a channel assignment class, Free will release the resource value so it can
|
|
||||||
* be reused somewhere else in the program.
|
|
||||||
*/
|
|
||||||
void Resource::Free(uint32_t index) {
|
|
||||||
std::lock_guard<priority_recursive_mutex> sync(m_allocateLock);
|
|
||||||
if (index == ~0ul) return;
|
|
||||||
if (index >= m_isAllocated.size()) {
|
|
||||||
// TODO: wpi_setWPIError(NotAllocated);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!m_isAllocated[index]) {
|
|
||||||
// TODO: wpi_setWPIError(NotAllocated);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
m_isAllocated[index] = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace hal
|
|
||||||
@@ -23,8 +23,7 @@ Java_edu_wpi_first_wpilibj_hal_CompressorJNI_initializeCompressor(
|
|||||||
int32_t status = 0;
|
int32_t status = 0;
|
||||||
auto handle = HAL_InitializeCompressor(module, &status);
|
auto handle = HAL_InitializeCompressor(module, &status);
|
||||||
if (status == PARAMETER_OUT_OF_RANGE) {
|
if (status == PARAMETER_OUT_OF_RANGE) {
|
||||||
//TODO: Move 63 to a constant (Thad will do)
|
ThrowBoundaryException(env, module, 0, HAL_GetNumPCMModules());
|
||||||
ThrowBoundaryException(env, module, 0, 63);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (jint)handle;
|
return (jint)handle;
|
||||||
|
|||||||
Reference in New Issue
Block a user