Switches HAL to fixed length signed integers, and adds our own HAL_Bool Type (#155)

* Switches HAL to fixed length signed integers, and adds our own HAL_Bool type

* Replaces HAL Floats with Doubles

Doubles are just as fast as floats with optimizations turned on, so
switches to all doubles. All made doubles for consistency.

* Prepends HAL/ to HAL include files. Also fixes some range errors
This commit is contained in:
Thad House
2016-07-12 10:45:14 -07:00
committed by Peter Johnson
parent 4a98e68815
commit b51e85ae26
89 changed files with 900 additions and 795 deletions

View File

@@ -75,7 +75,7 @@ void HAL_FreeAnalogInputPort(HAL_AnalogInputHandle analog_port_handle) {
*
* @return Analog module is valid and present
*/
bool HAL_CheckAnalogModule(uint8_t module) { return module == 1; }
HAL_Bool HAL_CheckAnalogModule(int32_t module) { return module == 1; }
/**
* Check that the analog output channel number is value.
@@ -84,9 +84,8 @@ bool HAL_CheckAnalogModule(uint8_t module) { return module == 1; }
*
* @return Analog channel is valid
*/
bool HAL_CheckAnalogInputChannel(uint32_t pin) {
if (pin < kNumAnalogInputs) return true;
return false;
HAL_Bool HAL_CheckAnalogInputChannel(int32_t pin) {
return (pin < kNumAnalogInputs) && (pin >= 0);
}
/**
@@ -104,7 +103,7 @@ void HAL_SetAnalogSampleRate(double samplesPerSecond, int32_t* status) {
// Compute the convert rate
uint32_t ticksPerSample =
static_cast<uint32_t>(static_cast<float>(kTimebase) / samplesPerSecond);
static_cast<uint32_t>(static_cast<double>(kTimebase) / samplesPerSecond);
uint32_t ticksPerConversion =
ticksPerSample / getAnalogNumChannelsToActivate(status);
// ticksPerConversion must be at least 80
@@ -132,11 +131,11 @@ void HAL_SetAnalogSampleRate(double samplesPerSecond, int32_t* status) {
*
* @return Sample rate.
*/
float HAL_GetAnalogSampleRate(int32_t* status) {
double HAL_GetAnalogSampleRate(int32_t* status) {
uint32_t ticksPerConversion = analogInputSystem->readLoopTiming(status);
uint32_t ticksPerSample =
ticksPerConversion * getAnalogNumActiveChannels(status);
return static_cast<float>(kTimebase) / static_cast<float>(ticksPerSample);
return static_cast<double>(kTimebase) / static_cast<double>(ticksPerSample);
}
/**
@@ -150,13 +149,14 @@ float HAL_GetAnalogSampleRate(int32_t* status) {
* @param bits Number of bits to average.
*/
void HAL_SetAnalogAverageBits(HAL_AnalogInputHandle analog_port_handle,
uint32_t bits, int32_t* status) {
int32_t bits, int32_t* status) {
auto port = analogInputHandles.Get(analog_port_handle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
analogInputSystem->writeAverageBits(port->pin, bits, status);
analogInputSystem->writeAverageBits(port->pin, static_cast<uint8_t>(bits),
status);
}
/**
@@ -168,14 +168,14 @@ void HAL_SetAnalogAverageBits(HAL_AnalogInputHandle analog_port_handle,
* @param analog_port_pointer Pointer to the analog port to use.
* @return Bits to average.
*/
uint32_t HAL_GetAnalogAverageBits(HAL_AnalogInputHandle analog_port_handle,
int32_t* status) {
int32_t HAL_GetAnalogAverageBits(HAL_AnalogInputHandle analog_port_handle,
int32_t* status) {
auto port = analogInputHandles.Get(analog_port_handle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return kDefaultAverageBits;
}
uint32_t result = analogInputSystem->readAverageBits(port->pin, status);
uint8_t result = analogInputSystem->readAverageBits(port->pin, status);
return result;
}
@@ -191,13 +191,14 @@ uint32_t HAL_GetAnalogAverageBits(HAL_AnalogInputHandle analog_port_handle,
* @param bits Number of bits to oversample.
*/
void HAL_SetAnalogOversampleBits(HAL_AnalogInputHandle analog_port_handle,
uint32_t bits, int32_t* status) {
int32_t bits, int32_t* status) {
auto port = analogInputHandles.Get(analog_port_handle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
analogInputSystem->writeOversampleBits(port->pin, bits, status);
analogInputSystem->writeOversampleBits(port->pin, static_cast<uint8_t>(bits),
status);
}
/**
@@ -210,14 +211,14 @@ void HAL_SetAnalogOversampleBits(HAL_AnalogInputHandle analog_port_handle,
* @param analog_port_pointer Pointer to the analog port to use.
* @return Bits to oversample.
*/
uint32_t HAL_GetAnalogOversampleBits(HAL_AnalogInputHandle analog_port_handle,
int32_t* status) {
int32_t HAL_GetAnalogOversampleBits(HAL_AnalogInputHandle analog_port_handle,
int32_t* status) {
auto port = analogInputHandles.Get(analog_port_handle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return kDefaultOversampleBits;
}
uint32_t result = analogInputSystem->readOversampleBits(port->pin, status);
uint8_t result = analogInputSystem->readOversampleBits(port->pin, status);
return result;
}
@@ -231,7 +232,7 @@ uint32_t HAL_GetAnalogOversampleBits(HAL_AnalogInputHandle analog_port_handle,
* @param analog_port_pointer Pointer to the analog port to use.
* @return A sample straight from the channel on this module.
*/
int16_t HAL_GetAnalogValue(HAL_AnalogInputHandle analog_port_handle,
int32_t HAL_GetAnalogValue(HAL_AnalogInputHandle analog_port_handle,
int32_t* status) {
auto port = analogInputHandles.Get(analog_port_handle);
if (port == nullptr) {
@@ -289,12 +290,12 @@ int32_t HAL_GetAnalogAverageValue(HAL_AnalogInputHandle analog_port_handle,
* @param analog_port_pointer Pointer to the analog port to use.
* @return A scaled sample straight from the channel on this module.
*/
float HAL_GetAnalogVoltage(HAL_AnalogInputHandle analog_port_handle,
int32_t* status) {
int16_t value = HAL_GetAnalogValue(analog_port_handle, status);
uint32_t LSBWeight = HAL_GetAnalogLSBWeight(analog_port_handle, status);
double HAL_GetAnalogVoltage(HAL_AnalogInputHandle analog_port_handle,
int32_t* status) {
int32_t value = HAL_GetAnalogValue(analog_port_handle, status);
int32_t LSBWeight = HAL_GetAnalogLSBWeight(analog_port_handle, status);
int32_t offset = HAL_GetAnalogOffset(analog_port_handle, status);
float voltage = LSBWeight * 1.0e-9 * value - offset * 1.0e-9;
double voltage = LSBWeight * 1.0e-9 * value - offset * 1.0e-9;
return voltage;
}
@@ -311,15 +312,15 @@ float HAL_GetAnalogVoltage(HAL_AnalogInputHandle analog_port_handle,
* @return A scaled sample from the output of the oversample and average engine
* for the channel.
*/
float HAL_GetAnalogAverageVoltage(HAL_AnalogInputHandle analog_port_handle,
int32_t* status) {
double HAL_GetAnalogAverageVoltage(HAL_AnalogInputHandle analog_port_handle,
int32_t* status) {
int32_t value = HAL_GetAnalogAverageValue(analog_port_handle, status);
uint32_t LSBWeight = HAL_GetAnalogLSBWeight(analog_port_handle, status);
int32_t LSBWeight = HAL_GetAnalogLSBWeight(analog_port_handle, status);
int32_t offset = HAL_GetAnalogOffset(analog_port_handle, status);
uint32_t oversampleBits =
int32_t oversampleBits =
HAL_GetAnalogOversampleBits(analog_port_handle, status);
float voltage =
LSBWeight * 1.0e-9 * value / static_cast<float>(1 << oversampleBits) -
double voltage =
LSBWeight * 1.0e-9 * value / static_cast<double>(1 << oversampleBits) -
offset * 1.0e-9;
return voltage;
}
@@ -346,9 +347,10 @@ int32_t HAL_GetAnalogVoltsToValue(HAL_AnalogInputHandle analog_port_handle,
voltage = 0.0;
*status = VOLTAGE_OUT_OF_RANGE;
}
uint32_t LSBWeight = HAL_GetAnalogLSBWeight(analog_port_handle, status);
int32_t LSBWeight = HAL_GetAnalogLSBWeight(analog_port_handle, status);
int32_t offset = HAL_GetAnalogOffset(analog_port_handle, status);
int32_t value = (int32_t)((voltage + offset * 1.0e-9) / (LSBWeight * 1.0e-9));
int32_t value =
static_cast<int32_t>((voltage + offset * 1.0e-9) / (LSBWeight * 1.0e-9));
return value;
}
@@ -362,8 +364,8 @@ int32_t HAL_GetAnalogVoltsToValue(HAL_AnalogInputHandle analog_port_handle,
* @param analog_port_pointer Pointer to the analog port to use.
* @return Least significant bit weight.
*/
uint32_t HAL_GetAnalogLSBWeight(HAL_AnalogInputHandle analog_port_handle,
int32_t* status) {
int32_t HAL_GetAnalogLSBWeight(HAL_AnalogInputHandle analog_port_handle,
int32_t* status) {
auto port = analogInputHandles.Get(analog_port_handle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;