2020-12-26 14:12:05 -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.
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Counter.h"
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2018-09-24 00:08:25 -07:00
|
|
|
#include <utility>
|
|
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/Counter.h>
|
|
|
|
|
#include <hal/FRCUsageReporting.h>
|
2021-05-26 07:24:53 -07:00
|
|
|
#include <wpi/NullDeleter.h>
|
2021-06-13 16:38:05 -07:00
|
|
|
#include <wpi/sendable/SendableBuilder.h>
|
|
|
|
|
#include <wpi/sendable/SendableRegistry.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/AnalogTrigger.h"
|
|
|
|
|
#include "frc/DigitalInput.h"
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2015-06-29 02:43:44 -07:00
|
|
|
Counter::Counter(Mode mode) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2020-12-28 11:04:20 -08:00
|
|
|
m_counter = HAL_InitializeCounter(static_cast<HAL_Counter_Mode>(mode),
|
|
|
|
|
&m_index, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "InitializeCounter");
|
2014-06-13 17:45:10 -04:00
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
SetMaxPeriod(0.5_s);
|
2014-12-23 19:47:23 -05:00
|
|
|
|
2019-10-29 21:34:10 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Counter, m_index + 1, mode + 1);
|
2021-06-15 23:06:03 -07:00
|
|
|
wpi::SendableRegistry::AddLW(this, "Counter", m_index);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
Counter::Counter(int channel) : Counter(kTwoPulse) {
|
|
|
|
|
SetUpSource(channel);
|
2015-06-25 15:07:55 -04:00
|
|
|
ClearDownSource();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
Counter::Counter(DigitalSource* source) : Counter(kTwoPulse) {
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
SetUpSource(source);
|
2015-06-25 15:07:55 -04:00
|
|
|
ClearDownSource();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
Counter::Counter(std::shared_ptr<DigitalSource> source) : Counter(kTwoPulse) {
|
|
|
|
|
SetUpSource(source);
|
2015-06-25 15:07:55 -04:00
|
|
|
ClearDownSource();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
Counter::Counter(const AnalogTrigger& trigger) : Counter(kTwoPulse) {
|
2016-07-09 00:24:26 -07:00
|
|
|
SetUpSource(trigger.CreateOutput(AnalogTriggerType::kState));
|
2015-06-25 15:07:55 -04:00
|
|
|
ClearDownSource();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
Counter::Counter(EncodingType encodingType, DigitalSource* upSource,
|
|
|
|
|
DigitalSource* downSource, bool inverted)
|
2017-11-11 22:09:51 -08:00
|
|
|
: Counter(encodingType,
|
|
|
|
|
std::shared_ptr<DigitalSource>(upSource,
|
2021-05-26 07:24:53 -07:00
|
|
|
wpi::NullDeleter<DigitalSource>()),
|
2015-07-29 16:48:04 -04:00
|
|
|
std::shared_ptr<DigitalSource>(downSource,
|
2021-05-26 07:24:53 -07:00
|
|
|
wpi::NullDeleter<DigitalSource>()),
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
inverted) {}
|
|
|
|
|
|
|
|
|
|
Counter::Counter(EncodingType encodingType,
|
2015-07-29 16:48:04 -04:00
|
|
|
std::shared_ptr<DigitalSource> upSource,
|
|
|
|
|
std::shared_ptr<DigitalSource> downSource, bool inverted)
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
: Counter(kExternalDirection) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (encodingType != k1X && encodingType != k2X) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(err::ParameterOutOfRange, "{}",
|
|
|
|
|
"Counter only supports 1X and 2X quadrature decoding");
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
SetUpSource(upSource);
|
|
|
|
|
SetDownSource(downSource);
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
|
|
|
|
|
if (encodingType == k1X) {
|
|
|
|
|
SetUpSourceEdge(true, false);
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetCounterAverageSize(m_counter, 1, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
} else {
|
|
|
|
|
SetUpSourceEdge(true, true);
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetCounterAverageSize(m_counter, 2, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "Counter constructor");
|
2015-06-25 15:07:55 -04:00
|
|
|
SetDownSourceEdge(inverted, true);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Counter::~Counter() {
|
2021-04-18 20:35:29 -07:00
|
|
|
try {
|
|
|
|
|
SetUpdateWhenEmpty(true);
|
|
|
|
|
} catch (const RuntimeError& e) {
|
|
|
|
|
e.Report();
|
|
|
|
|
}
|
2014-06-13 17:45:10 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_FreeCounter(m_counter, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_ReportError(status, "{}", "Counter destructor");
|
2018-09-24 00:08:25 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
void Counter::SetUpSource(int channel) {
|
2015-07-29 16:48:04 -04:00
|
|
|
SetUpSource(std::make_shared<DigitalInput>(channel));
|
2021-06-15 23:06:03 -07:00
|
|
|
wpi::SendableRegistry::AddChild(this, m_upSource.get());
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
void Counter::SetUpSource(AnalogTrigger* analogTrigger,
|
2015-06-25 15:07:55 -04:00
|
|
|
AnalogTriggerType triggerType) {
|
2015-07-29 16:48:04 -04:00
|
|
|
SetUpSource(std::shared_ptr<AnalogTrigger>(analogTrigger,
|
2021-05-26 07:24:53 -07:00
|
|
|
wpi::NullDeleter<AnalogTrigger>()),
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
triggerType);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
void Counter::SetUpSource(std::shared_ptr<AnalogTrigger> analogTrigger,
|
2015-06-25 15:07:55 -04:00
|
|
|
AnalogTriggerType triggerType) {
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
SetUpSource(analogTrigger->CreateOutput(triggerType));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void Counter::SetUpSource(DigitalSource* source) {
|
2021-05-26 07:24:53 -07:00
|
|
|
SetUpSource(std::shared_ptr<DigitalSource>(
|
|
|
|
|
source, wpi::NullDeleter<DigitalSource>()));
|
2018-05-31 20:47:15 -07:00
|
|
|
}
|
|
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
void Counter::SetUpSource(std::shared_ptr<DigitalSource> source) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_upSource = source;
|
2021-04-18 20:35:29 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetCounterUpSource(m_counter, source->GetPortHandleForRouting(),
|
|
|
|
|
static_cast<HAL_AnalogTriggerType>(
|
|
|
|
|
source->GetAnalogTriggerTypeForRouting()),
|
|
|
|
|
&status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "SetUpSource");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
void Counter::SetUpSource(DigitalSource& source) {
|
2021-05-26 07:24:53 -07:00
|
|
|
SetUpSource(std::shared_ptr<DigitalSource>(
|
|
|
|
|
&source, wpi::NullDeleter<DigitalSource>()));
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetUpSourceEdge(bool risingEdge, bool fallingEdge) {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_upSource == nullptr) {
|
2021-04-18 20:35:29 -07:00
|
|
|
throw FRC_MakeError(
|
2021-05-23 19:33:33 -07:00
|
|
|
err::NullParameter, "{}",
|
2015-06-23 04:49:51 -07:00
|
|
|
"Must set non-nullptr UpSource before setting UpSourceEdge");
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetCounterUpSourceEdge(m_counter, risingEdge, fallingEdge, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "SetUpSourceEdge");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::ClearUpSource() {
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
m_upSource.reset();
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_ClearCounterUpSource(m_counter, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "ClearUpSource");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
void Counter::SetDownSource(int channel) {
|
2015-07-29 16:48:04 -04:00
|
|
|
SetDownSource(std::make_shared<DigitalInput>(channel));
|
2021-06-15 23:06:03 -07:00
|
|
|
wpi::SendableRegistry::AddChild(this, m_downSource.get());
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
void Counter::SetDownSource(AnalogTrigger* analogTrigger,
|
2015-06-25 15:07:55 -04:00
|
|
|
AnalogTriggerType triggerType) {
|
2021-05-26 07:24:53 -07:00
|
|
|
SetDownSource(std::shared_ptr<AnalogTrigger>(
|
|
|
|
|
analogTrigger, wpi::NullDeleter<AnalogTrigger>()),
|
2016-05-20 17:30:37 -07:00
|
|
|
triggerType);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
void Counter::SetDownSource(std::shared_ptr<AnalogTrigger> analogTrigger,
|
2015-06-25 15:07:55 -04:00
|
|
|
AnalogTriggerType triggerType) {
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
SetDownSource(analogTrigger->CreateOutput(triggerType));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void Counter::SetDownSource(DigitalSource* source) {
|
2021-05-26 07:24:53 -07:00
|
|
|
SetDownSource(std::shared_ptr<DigitalSource>(
|
|
|
|
|
source, wpi::NullDeleter<DigitalSource>()));
|
2018-05-31 20:47:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Counter::SetDownSource(DigitalSource& source) {
|
2021-05-26 07:24:53 -07:00
|
|
|
SetDownSource(std::shared_ptr<DigitalSource>(
|
|
|
|
|
&source, wpi::NullDeleter<DigitalSource>()));
|
2018-05-31 20:47:15 -07:00
|
|
|
}
|
|
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
void Counter::SetDownSource(std::shared_ptr<DigitalSource> source) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_downSource = source;
|
2021-04-18 20:35:29 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetCounterDownSource(m_counter, source->GetPortHandleForRouting(),
|
|
|
|
|
static_cast<HAL_AnalogTriggerType>(
|
|
|
|
|
source->GetAnalogTriggerTypeForRouting()),
|
|
|
|
|
&status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "SetDownSource");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetDownSourceEdge(bool risingEdge, bool fallingEdge) {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_downSource == nullptr) {
|
2021-04-18 20:35:29 -07:00
|
|
|
throw FRC_MakeError(
|
2021-05-23 19:33:33 -07:00
|
|
|
err::NullParameter, "{}",
|
2015-06-23 04:49:51 -07:00
|
|
|
"Must set non-nullptr DownSource before setting DownSourceEdge");
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetCounterDownSourceEdge(m_counter, risingEdge, fallingEdge, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "SetDownSourceEdge");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::ClearDownSource() {
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
m_downSource.reset();
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_ClearCounterDownSource(m_counter, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "ClearDownSource");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetUpDownCounterMode() {
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetCounterUpDownMode(m_counter, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "SetUpDownCounterMode");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetExternalDirectionMode() {
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetCounterExternalDirectionMode(m_counter, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "SetExternalDirectionMode");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetSemiPeriodMode(bool highSemiPeriod) {
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetCounterSemiPeriodMode(m_counter, highSemiPeriod, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetSemiPeriodMode to {}",
|
|
|
|
|
highSemiPeriod ? "true" : "false");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
void Counter::SetPulseLengthMode(double threshold) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetCounterPulseLengthMode(m_counter, threshold, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "SetPulseLengthMode");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void Counter::SetReverseDirection(bool reverseDirection) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2018-05-31 20:47:15 -07:00
|
|
|
HAL_SetCounterReverseDirection(m_counter, reverseDirection, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetReverseDirection to {}",
|
|
|
|
|
reverseDirection ? "true" : "false");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetSamplesToAverage(int samplesToAverage) {
|
|
|
|
|
if (samplesToAverage < 1 || samplesToAverage > 127) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(
|
|
|
|
|
err::ParameterOutOfRange,
|
|
|
|
|
"Average counter values must be between 1 and 127, {} out of range",
|
|
|
|
|
samplesToAverage);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetCounterSamplesToAverage(m_counter, samplesToAverage, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetSamplesToAverage to {}", samplesToAverage);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
int Counter::GetSamplesToAverage() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
int samples = HAL_GetCounterSamplesToAverage(m_counter, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "GetSamplesToAverage");
|
2018-05-31 20:47:15 -07:00
|
|
|
return samples;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int Counter::GetFPGAIndex() const {
|
|
|
|
|
return m_index;
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
int Counter::Get() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-09-06 00:01:45 -07:00
|
|
|
int value = HAL_GetCounter(m_counter, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "Get");
|
2015-06-25 15:07:55 -04:00
|
|
|
return value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::Reset() {
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_ResetCounter(m_counter, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "Reset");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
units::second_t Counter::GetPeriod() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
double value = HAL_GetCounterPeriod(m_counter, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "GetPeriod");
|
2021-05-28 22:06:59 -07:00
|
|
|
return units::second_t{value};
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
void Counter::SetMaxPeriod(units::second_t maxPeriod) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2021-10-25 08:58:12 -07:00
|
|
|
HAL_SetCounterMaxPeriod(m_counter, maxPeriod.value(), &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "SetMaxPeriod");
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Counter::SetUpdateWhenEmpty(bool enabled) {
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetCounterUpdateWhenEmpty(m_counter, enabled, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "SetUpdateWhenEmpty");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Counter::GetStopped() const {
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
bool value = HAL_GetCounterStopped(m_counter, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "GetStopped");
|
2015-06-25 15:07:55 -04:00
|
|
|
return value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Counter::GetDirection() const {
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
bool value = HAL_GetCounterDirection(m_counter, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "GetDirection");
|
2015-06-25 15:07:55 -04:00
|
|
|
return value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
void Counter::InitSendable(wpi::SendableBuilder& builder) {
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.SetSmartDashboardType("Counter");
|
2020-06-27 20:39:00 -07:00
|
|
|
builder.AddDoubleProperty(
|
2021-06-12 08:06:45 -07:00
|
|
|
"Value", [=] { return Get(); }, nullptr);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|