Some general cleanups in the HAL (#188)

AnalogTrigger initialization checks are now in the correct order. Plus
frees no longer grab the structure if it is not needed.
This commit is contained in:
Thad House
2016-07-25 23:26:34 -07:00
committed by Peter Johnson
parent 8ac7e44f19
commit 866046edd1
6 changed files with 10 additions and 34 deletions

View File

@@ -32,26 +32,25 @@ extern "C" {
HAL_AnalogTriggerHandle HAL_InitializeAnalogTrigger(
HAL_AnalogInputHandle port_handle, int32_t* index, int32_t* status) {
if (port_handle == HAL_kInvalidHandle) {
*status = PARAMETER_OUT_OF_RANGE;
// ensure we are given a valid and active AnalogInput handle
auto analog_port = analogInputHandles.Get(port_handle);
if (analog_port == nullptr) {
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
HAL_AnalogInputHandle handle = analogTriggerHandles.Allocate();
HAL_AnalogTriggerHandle handle = analogTriggerHandles.Allocate();
if (handle == HAL_kInvalidHandle) {
*status = NO_AVAILABLE_RESOURCES;
return HAL_kInvalidHandle;
}
auto trigger = analogTriggerHandles.Get(handle);
trigger->analogHandle = port_handle;
auto analog_port = analogInputHandles.Get(trigger->analogHandle);
if (analog_port == nullptr) { // would only error on thread issue
if (trigger == nullptr) { // would only occur on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
trigger->analogHandle = port_handle;
trigger->index = static_cast<uint8_t>(getHandleIndex(handle));
*index = trigger->index;
// TODO: if (index == ~0ul) { CloneError(triggers); return; }
trigger->trigger.reset(tAnalogTrigger::create(trigger->index, status));
trigger->trigger->writeSourceSelect_Channel(analog_port->pin, status);
@@ -60,10 +59,6 @@ HAL_AnalogTriggerHandle HAL_InitializeAnalogTrigger(
void HAL_CleanAnalogTrigger(HAL_AnalogTriggerHandle analog_trigger_handle,
int32_t* status) {
auto trigger = analogTriggerHandles.Get(analog_trigger_handle);
if (trigger == nullptr) { // ignore status error
return;
}
analogTriggerHandles.Free(analog_trigger_handle);
// caller owns the analog input handle.
}