2016-05-26 22:14:25 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 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/AnalogOutput.h"
|
|
|
|
|
|
|
|
|
|
#include "AnalogInternal.h"
|
2016-06-05 15:23:58 -07:00
|
|
|
#include "HAL/Errors.h"
|
|
|
|
|
#include "handles/HandlesInternal.h"
|
2016-05-26 22:14:25 -07:00
|
|
|
|
|
|
|
|
using namespace hal;
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize the analog output port using the given port object.
|
|
|
|
|
*/
|
2016-06-05 15:23:58 -07:00
|
|
|
void* initializeAnalogOutputPort(HalPortHandle port_handle, int32_t* status) {
|
2016-05-26 22:14:25 -07:00
|
|
|
initializeAnalog(status);
|
2016-06-05 15:23:58 -07:00
|
|
|
|
|
|
|
|
if (*status != 0) return nullptr;
|
|
|
|
|
|
|
|
|
|
int16_t pin = getPortHandlePin(port_handle);
|
|
|
|
|
if (pin == HAL_HANDLE_INVALID_TYPE) {
|
|
|
|
|
*status = PARAMETER_OUT_OF_RANGE;
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2016-05-26 22:14:25 -07:00
|
|
|
|
|
|
|
|
// Initialize port structure
|
|
|
|
|
AnalogPort* analog_port = new AnalogPort();
|
2016-06-05 15:23:58 -07:00
|
|
|
analog_port->pin = (uint8_t)pin;
|
2016-05-26 22:14:25 -07:00
|
|
|
analog_port->accumulator = nullptr;
|
|
|
|
|
return analog_port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void freeAnalogOutputPort(void* analog_port_pointer) {
|
|
|
|
|
AnalogPort* port = (AnalogPort*)analog_port_pointer;
|
|
|
|
|
if (!port) return;
|
|
|
|
|
delete port->accumulator;
|
|
|
|
|
delete port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check that the analog output channel number is value.
|
|
|
|
|
* Verify that the analog channel number is one of the legal channel numbers.
|
|
|
|
|
* Channel numbers are 0-based.
|
|
|
|
|
*
|
|
|
|
|
* @return Analog channel is valid
|
|
|
|
|
*/
|
|
|
|
|
bool checkAnalogOutputChannel(uint32_t pin) {
|
|
|
|
|
if (pin < kAnalogOutputPins) return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setAnalogOutput(void* analog_port_pointer, double voltage,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
AnalogPort* port = (AnalogPort*)analog_port_pointer;
|
|
|
|
|
|
|
|
|
|
uint16_t rawValue = (uint16_t)(voltage / 5.0 * 0x1000);
|
|
|
|
|
|
|
|
|
|
if (voltage < 0.0)
|
|
|
|
|
rawValue = 0;
|
|
|
|
|
else if (voltage > 5.0)
|
|
|
|
|
rawValue = 0x1000;
|
|
|
|
|
|
2016-06-05 15:23:58 -07:00
|
|
|
analogOutputSystem->writeMXP(port->pin, rawValue, status);
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double getAnalogOutput(void* analog_port_pointer, int32_t* status) {
|
|
|
|
|
AnalogPort* port = (AnalogPort*)analog_port_pointer;
|
|
|
|
|
|
2016-06-05 15:23:58 -07:00
|
|
|
uint16_t rawValue = analogOutputSystem->readMXP(port->pin, status);
|
2016-05-26 22:14:25 -07:00
|
|
|
|
|
|
|
|
return rawValue * 5.0 / 0x1000;
|
|
|
|
|
}
|
|
|
|
|
}
|