2021-11-23 20:33:36 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
|
|
|
|
#include "frc/counter/UpDownCounter.h"
|
|
|
|
|
|
2024-09-20 17:43:39 -07:00
|
|
|
#include <memory>
|
2025-01-28 08:58:34 -08:00
|
|
|
#include <string>
|
2024-09-20 17:43:39 -07:00
|
|
|
|
2021-11-23 20:33:36 -08:00
|
|
|
#include <hal/Counter.h>
|
2025-02-07 12:37:23 -08:00
|
|
|
#include <hal/UsageReporting.h>
|
2025-01-28 08:58:34 -08:00
|
|
|
#include <wpi/StackTrace.h>
|
2021-11-23 20:33:36 -08:00
|
|
|
#include <wpi/sendable/SendableBuilder.h>
|
|
|
|
|
|
|
|
|
|
#include "frc/Errors.h"
|
|
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
2025-01-28 08:58:34 -08:00
|
|
|
UpDownCounter::UpDownCounter(int channel, EdgeConfiguration configuration)
|
|
|
|
|
: m_channel{channel} {
|
2021-11-23 20:33:36 -08:00
|
|
|
int32_t status = 0;
|
2025-01-28 08:58:34 -08:00
|
|
|
std::string stackTrace = wpi::GetStackTrace(1);
|
|
|
|
|
m_handle = HAL_InitializeCounter(
|
|
|
|
|
channel, configuration == EdgeConfiguration::kRisingEdge,
|
|
|
|
|
stackTrace.c_str(), &status);
|
|
|
|
|
FRC_CheckErrorStatus(status, "{}", channel);
|
2021-11-23 20:33:36 -08:00
|
|
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
2025-02-07 12:37:23 -08:00
|
|
|
HAL_ReportUsage("IO", channel, "UpDownCounter");
|
2025-01-28 08:58:34 -08:00
|
|
|
wpi::SendableRegistry::Add(this, "UpDown Counter", channel);
|
2021-11-23 20:33:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int UpDownCounter::GetCount() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
int val = HAL_GetCounter(m_handle, &status);
|
2025-01-28 08:58:34 -08:00
|
|
|
FRC_CheckErrorStatus(status, "{}", m_channel);
|
2021-11-23 20:33:36 -08:00
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpDownCounter::Reset() {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_ResetCounter(m_handle, &status);
|
2025-01-28 08:58:34 -08:00
|
|
|
FRC_CheckErrorStatus(status, "{}", m_channel);
|
2021-11-23 20:33:36 -08:00
|
|
|
}
|
|
|
|
|
|
2025-01-28 08:58:34 -08:00
|
|
|
void UpDownCounter::SetEdgeConfiguration(EdgeConfiguration configuration) {
|
2021-11-23 20:33:36 -08:00
|
|
|
int32_t status = 0;
|
2025-01-28 08:58:34 -08:00
|
|
|
bool rising = configuration == EdgeConfiguration::kRisingEdge;
|
|
|
|
|
HAL_SetCounterEdgeConfiguration(m_handle, rising, &status);
|
|
|
|
|
FRC_CheckErrorStatus(status, "{}", m_channel);
|
2021-11-23 20:33:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpDownCounter::InitSendable(wpi::SendableBuilder& builder) {
|
|
|
|
|
builder.SetSmartDashboardType("UpDown Counter");
|
2024-03-18 23:11:20 -07:00
|
|
|
builder.AddDoubleProperty("Count", [&] { return GetCount(); }, nullptr);
|
2021-11-23 20:33:36 -08:00
|
|
|
}
|