mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[hal] Digital IO SystemCore implementation (#7621)
This commit is contained in:
@@ -34,29 +34,90 @@ int32_t SmartIo::InitializeMode(SmartIoMode mode) {
|
||||
|
||||
modePublisher = inst.GetIntegerTopic(subTableString + "type").Publish();
|
||||
getSubscriber =
|
||||
inst.GetIntegerTopic(subTableString + "valget").Subscribe(0.0, options);
|
||||
inst.GetIntegerTopic(subTableString + "valget").Subscribe(0, options);
|
||||
frequencySubscriber =
|
||||
inst.GetIntegerTopic(subTableString + "freqget").Subscribe(0, options);
|
||||
setPublisher =
|
||||
inst.GetIntegerTopic(subTableString + "valset").Publish(options);
|
||||
periodPublisher =
|
||||
inst.GetIntegerTopic(subTableString + "periodset").Publish(options);
|
||||
|
||||
currentMode = mode;
|
||||
switch (mode) {
|
||||
case SmartIoMode::PWMOutput:
|
||||
modePublisher.Set(4);
|
||||
setPublisher =
|
||||
inst.GetIntegerTopic(subTableString + "valset").Publish(options);
|
||||
// These need to set a 0 output
|
||||
case SmartIoMode::DigitalOutput:
|
||||
case SmartIoMode::PwmOutput:
|
||||
setPublisher.Set(0);
|
||||
return 0;
|
||||
break;
|
||||
|
||||
// These don't need to set any value
|
||||
case SmartIoMode::DigitalInput:
|
||||
case SmartIoMode::AnalogInput:
|
||||
case SmartIoMode::PwmInput:
|
||||
case SmartIoMode::SingleCounterRising:
|
||||
case SmartIoMode::SingleCounterFalling:
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
return INCOMPATIBLE_STATE;
|
||||
}
|
||||
|
||||
modePublisher.Set(static_cast<int>(mode));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t SmartIo::SetPwmMicroseconds(uint16_t microseconds) {
|
||||
if (currentMode != SmartIoMode::PWMOutput) {
|
||||
int32_t SmartIo::SwitchDioDirection(bool input) {
|
||||
if (currentMode != SmartIoMode::DigitalInput &&
|
||||
currentMode != SmartIoMode::DigitalOutput) {
|
||||
return INCOMPATIBLE_STATE;
|
||||
}
|
||||
|
||||
// TODO(thad) add support for always on signal
|
||||
modePublisher.Set(input ? 0 : 1);
|
||||
currentMode = input ? SmartIoMode::DigitalInput : SmartIoMode::DigitalOutput;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t SmartIo::SetDigitalOutput(bool value) {
|
||||
if (currentMode != SmartIoMode::DigitalInput &&
|
||||
currentMode != SmartIoMode::DigitalOutput) {
|
||||
return INCOMPATIBLE_STATE;
|
||||
}
|
||||
setPublisher.Set(value ? 255.0 : 0.0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t SmartIo::GetDigitalInput(bool* value) {
|
||||
if (currentMode != SmartIoMode::DigitalInput &&
|
||||
currentMode != SmartIoMode::DigitalOutput) {
|
||||
return INCOMPATIBLE_STATE;
|
||||
}
|
||||
*value = getSubscriber.Get() != 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t SmartIo::SetPwmOutputPeriod(PwmOutputPeriod period) {
|
||||
if (currentMode != SmartIoMode::PwmOutput) {
|
||||
return INCOMPATIBLE_STATE;
|
||||
}
|
||||
|
||||
switch (period) {
|
||||
case PwmOutputPeriod::k20ms:
|
||||
case PwmOutputPeriod::k10ms:
|
||||
case PwmOutputPeriod::k5ms:
|
||||
case PwmOutputPeriod::k2ms:
|
||||
periodPublisher.Set(static_cast<int>(period));
|
||||
return 0;
|
||||
|
||||
default:
|
||||
return PARAMETER_OUT_OF_RANGE;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t SmartIo::SetPwmMicroseconds(uint16_t microseconds) {
|
||||
if (currentMode != SmartIoMode::PwmOutput) {
|
||||
return INCOMPATIBLE_STATE;
|
||||
}
|
||||
|
||||
if (microseconds > 4095) {
|
||||
microseconds = 4095;
|
||||
@@ -68,7 +129,7 @@ int32_t SmartIo::SetPwmMicroseconds(uint16_t microseconds) {
|
||||
}
|
||||
|
||||
int32_t SmartIo::GetPwmMicroseconds(uint16_t* microseconds) {
|
||||
if (currentMode != SmartIoMode::PWMOutput) {
|
||||
if (currentMode != SmartIoMode::PwmOutput) {
|
||||
return INCOMPATIBLE_STATE;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user