mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-29 02:21:44 +00:00
[ntcore] Use int for options instead of double
Periodic time is stored in milliseconds.
This commit is contained in:
@@ -12,7 +12,7 @@ nt::PubSubOptions::PubSubOptions(std::span<const PubSubOption> options) {
|
||||
for (auto&& option : options) {
|
||||
switch (option.type) {
|
||||
case NT_PUBSUB_PERIODIC:
|
||||
periodic = option.value;
|
||||
periodicMs = option.value;
|
||||
break;
|
||||
case NT_PUBSUB_SENDALL:
|
||||
sendAll = option.value != 0;
|
||||
|
||||
@@ -16,7 +16,9 @@ class PubSubOptions {
|
||||
PubSubOptions() = default;
|
||||
explicit PubSubOptions(std::span<const PubSubOption> options);
|
||||
|
||||
double periodic = 0.1;
|
||||
static constexpr unsigned int kDefaultPeriodicMs = 100;
|
||||
|
||||
unsigned int periodicMs = kDefaultPeriodicMs;
|
||||
size_t pollStorageSize = 1;
|
||||
bool sendAll = false;
|
||||
bool topicsOnly = false;
|
||||
|
||||
@@ -118,10 +118,10 @@ JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved) {
|
||||
//
|
||||
|
||||
static std::span<const nt::PubSubOption> FromJavaPubSubOptions(
|
||||
JNIEnv* env, jintArray optionTypes, jdoubleArray optionValues,
|
||||
JNIEnv* env, jintArray optionTypes, jintArray optionValues,
|
||||
wpi::SmallVectorImpl<nt::PubSubOption>& arr) {
|
||||
JIntArrayRef types{env, optionTypes};
|
||||
JDoubleArrayRef values{env, optionValues};
|
||||
JIntArrayRef values{env, optionValues};
|
||||
if (types.size() != values.size()) {
|
||||
return {};
|
||||
}
|
||||
@@ -720,12 +720,12 @@ Java_edu_wpi_first_networktables_NetworkTablesJNI_setTopicProperties
|
||||
/*
|
||||
* Class: edu_wpi_first_networktables_NetworkTablesJNI
|
||||
* Method: subscribe
|
||||
* Signature: (IILjava/lang/String;[I[D)I
|
||||
* Signature: (IILjava/lang/String;[I[I)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_networktables_NetworkTablesJNI_subscribe
|
||||
(JNIEnv* env, jclass, jint topic, jint type, jstring typeStr,
|
||||
jintArray optionTypes, jdoubleArray optionValues)
|
||||
jintArray optionTypes, jintArray optionValues)
|
||||
{
|
||||
wpi::SmallVector<nt::PubSubOption, 4> options;
|
||||
return nt::Subscribe(
|
||||
@@ -748,12 +748,12 @@ Java_edu_wpi_first_networktables_NetworkTablesJNI_unsubscribe
|
||||
/*
|
||||
* Class: edu_wpi_first_networktables_NetworkTablesJNI
|
||||
* Method: publish
|
||||
* Signature: (IILjava/lang/String;[I[D)I
|
||||
* Signature: (IILjava/lang/String;[I[I)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_networktables_NetworkTablesJNI_publish
|
||||
(JNIEnv* env, jclass, jint topic, jint type, jstring typeStr,
|
||||
jintArray optionTypes, jdoubleArray optionValues)
|
||||
jintArray optionTypes, jintArray optionValues)
|
||||
{
|
||||
wpi::SmallVector<nt::PubSubOption, 4> options;
|
||||
return nt::Publish(
|
||||
@@ -764,12 +764,12 @@ Java_edu_wpi_first_networktables_NetworkTablesJNI_publish
|
||||
/*
|
||||
* Class: edu_wpi_first_networktables_NetworkTablesJNI
|
||||
* Method: publishEx
|
||||
* Signature: (IILjava/lang/String;Ljava/lang/String;[I[D)I
|
||||
* Signature: (IILjava/lang/String;Ljava/lang/String;[I[I)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_networktables_NetworkTablesJNI_publishEx
|
||||
(JNIEnv* env, jclass, jint topic, jint type, jstring typeStr,
|
||||
jstring properties, jintArray optionTypes, jdoubleArray optionValues)
|
||||
jstring properties, jintArray optionTypes, jintArray optionValues)
|
||||
{
|
||||
wpi::json j;
|
||||
try {
|
||||
@@ -804,12 +804,12 @@ Java_edu_wpi_first_networktables_NetworkTablesJNI_unpublish
|
||||
/*
|
||||
* Class: edu_wpi_first_networktables_NetworkTablesJNI
|
||||
* Method: getEntry
|
||||
* Signature: (IILjava/lang/String;[I[D)I
|
||||
* Signature: (IILjava/lang/String;[I[I)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_networktables_NetworkTablesJNI_getEntry__IILjava_lang_String_2_3I_3D
|
||||
Java_edu_wpi_first_networktables_NetworkTablesJNI_getEntry__IILjava_lang_String_2_3I_3I
|
||||
(JNIEnv* env, jclass, jint topic, jint type, jstring typeStr,
|
||||
jintArray optionTypes, jdoubleArray optionValues)
|
||||
jintArray optionTypes, jintArray optionValues)
|
||||
{
|
||||
wpi::SmallVector<nt::PubSubOption, 4> options;
|
||||
return nt::GetEntry(
|
||||
@@ -856,12 +856,12 @@ Java_edu_wpi_first_networktables_NetworkTablesJNI_getTopicFromHandle
|
||||
/*
|
||||
* Class: edu_wpi_first_networktables_NetworkTablesJNI
|
||||
* Method: subscribeMultiple
|
||||
* Signature: (I[Ljava/lang/Object;[I[D)I
|
||||
* Signature: (I[Ljava/lang/Object;[I[I)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_networktables_NetworkTablesJNI_subscribeMultiple
|
||||
(JNIEnv* env, jclass, jint inst, jobjectArray prefixes, jintArray optionTypes,
|
||||
jdoubleArray optionValues)
|
||||
jintArray optionValues)
|
||||
{
|
||||
if (!prefixes) {
|
||||
nullPointerEx.Throw(env, "prefixes cannot be null");
|
||||
|
||||
@@ -293,7 +293,7 @@ void CImpl::Publish(NT_Publisher pubHandle, NT_Topic topicHandle,
|
||||
}
|
||||
publisher->handle = pubHandle;
|
||||
publisher->options = options;
|
||||
publisher->periodMs = std::lround(options.periodic * 100) * 10;
|
||||
publisher->periodMs = std::lround(options.periodicMs / 10.0) * 10;
|
||||
if (publisher->periodMs < kMinPeriodMs) {
|
||||
publisher->periodMs = kMinPeriodMs;
|
||||
}
|
||||
|
||||
@@ -367,7 +367,7 @@ struct SubscriberData {
|
||||
topicNames{topicNames.begin(), topicNames.end()},
|
||||
subuid{subuid},
|
||||
options{options},
|
||||
periodMs(std::lround(options.periodic * 100) * 10) {
|
||||
periodMs(std::lround(options.periodicMs / 10.0) * 10) {
|
||||
if (periodMs < kMinPeriodMs) {
|
||||
periodMs = kMinPeriodMs;
|
||||
}
|
||||
@@ -377,7 +377,7 @@ struct SubscriberData {
|
||||
const PubSubOptions& options_) {
|
||||
topicNames = {topicNames_.begin(), topicNames_.end()};
|
||||
options = options_;
|
||||
periodMs = std::lround(options_.periodic * 100) * 10;
|
||||
periodMs = std::lround(options_.periodicMs / 10.0) * 10;
|
||||
if (periodMs < kMinPeriodMs) {
|
||||
periodMs = kMinPeriodMs;
|
||||
}
|
||||
@@ -465,7 +465,8 @@ struct Writer : public mpack_writer_t {
|
||||
|
||||
static void WriteOptions(mpack_writer_t& w, const PubSubOptions& options) {
|
||||
int size = (options.sendAll ? 1 : 0) + (options.topicsOnly ? 1 : 0) +
|
||||
(options.periodic != 0.1 ? 1 : 0) + (options.prefixMatch ? 1 : 0);
|
||||
(options.periodicMs != PubSubOptions::kDefaultPeriodicMs ? 1 : 0) +
|
||||
(options.prefixMatch ? 1 : 0);
|
||||
mpack_start_map(&w, size);
|
||||
if (options.sendAll) {
|
||||
mpack_write_str(&w, "all");
|
||||
@@ -475,9 +476,9 @@ static void WriteOptions(mpack_writer_t& w, const PubSubOptions& options) {
|
||||
mpack_write_str(&w, "topicsonly");
|
||||
mpack_write_bool(&w, true);
|
||||
}
|
||||
if (options.periodic != 0.1) {
|
||||
if (options.periodicMs != PubSubOptions::kDefaultPeriodicMs) {
|
||||
mpack_write_str(&w, "periodic");
|
||||
mpack_write_float(&w, options.periodic);
|
||||
mpack_write_float(&w, options.periodicMs / 1000.0);
|
||||
}
|
||||
if (options.prefixMatch) {
|
||||
mpack_write_str(&w, "prefix");
|
||||
|
||||
@@ -238,10 +238,12 @@ static void WireDecodeTextImpl(std::string_view in, T& out,
|
||||
// periodic
|
||||
auto periodicIt = joptions->find("periodic");
|
||||
if (periodicIt != joptions->end()) {
|
||||
if (!GetNumber(periodicIt->second, &options.periodic)) {
|
||||
double val;
|
||||
if (!GetNumber(periodicIt->second, &val)) {
|
||||
error = "periodic value must be a number";
|
||||
goto err;
|
||||
}
|
||||
options.periodicMs = val * 1000;
|
||||
}
|
||||
|
||||
// send all changes
|
||||
|
||||
@@ -99,12 +99,12 @@ static void WireEncodeSubscribeImpl(wpi::raw_ostream& os, int64_t subuid,
|
||||
os << "\"prefix\":true";
|
||||
first = false;
|
||||
}
|
||||
if (options.periodic != 0.1) {
|
||||
if (options.periodicMs != PubSubOptions::kDefaultPeriodicMs) {
|
||||
if (!first) {
|
||||
os << ',';
|
||||
}
|
||||
os << "\"periodic\":";
|
||||
s.dump_float(options.periodic);
|
||||
s.dump_float(options.periodicMs / 1000.0);
|
||||
}
|
||||
os << "},\"topics\":";
|
||||
EncodePrefixes(os, topicNames, s);
|
||||
|
||||
@@ -330,7 +330,7 @@ void CImpl::Publish(NT_Publisher pubHandle, NT_Topic topicHandle,
|
||||
}
|
||||
publisher->handle = pubHandle;
|
||||
publisher->options = options;
|
||||
publisher->periodMs = std::lround(options.periodic * 100) * 10;
|
||||
publisher->periodMs = std::lround(options.periodicMs / 10.0) * 10;
|
||||
if (publisher->periodMs < 10) {
|
||||
publisher->periodMs = 10;
|
||||
}
|
||||
|
||||
@@ -288,10 +288,11 @@ struct NT_PubSubOption {
|
||||
|
||||
/**
|
||||
* Option value. 1 (true) or 0 (false) for immediate and logging options,
|
||||
* time between updates, in seconds, for periodic option. For local/remote
|
||||
* option, 1=local only, 2=remote only, 0 or 3=both local and remote.
|
||||
* time between updates, in milliseconds, for periodic option. For
|
||||
* local/remote option, 1=local only, 2=remote only, 0 or 3=both local and
|
||||
* remote.
|
||||
*/
|
||||
double value;
|
||||
unsigned int value;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -262,7 +262,7 @@ class Event {
|
||||
/** NetworkTables publish/subscribe option. */
|
||||
class PubSubOption {
|
||||
public:
|
||||
constexpr PubSubOption(NT_PubSubOptionType type, double value)
|
||||
constexpr PubSubOption(NT_PubSubOptionType type, unsigned int value)
|
||||
: type{type}, value{value} {}
|
||||
|
||||
/**
|
||||
@@ -276,7 +276,8 @@ class PubSubOption {
|
||||
* @return option
|
||||
*/
|
||||
static constexpr PubSubOption Periodic(double time) {
|
||||
return PubSubOption{NT_PUBSUB_PERIODIC, time};
|
||||
return PubSubOption{NT_PUBSUB_PERIODIC,
|
||||
static_cast<unsigned int>(time * 1000)};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -287,7 +288,7 @@ class PubSubOption {
|
||||
* @return option
|
||||
*/
|
||||
static constexpr PubSubOption SendAll(bool enabled) {
|
||||
return PubSubOption{NT_PUBSUB_SENDALL, enabled ? 1.0 : 0.0};
|
||||
return PubSubOption{NT_PUBSUB_SENDALL, enabled ? 1u : 0u};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -298,7 +299,7 @@ class PubSubOption {
|
||||
* @return option
|
||||
*/
|
||||
static constexpr PubSubOption TopicsOnly(bool enabled) {
|
||||
return PubSubOption{NT_PUBSUB_TOPICSONLY, enabled ? 1.0 : 0.0};
|
||||
return PubSubOption{NT_PUBSUB_TOPICSONLY, enabled ? 1u : 0u};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -309,7 +310,7 @@ class PubSubOption {
|
||||
* @return option
|
||||
*/
|
||||
static constexpr PubSubOption KeepDuplicates(bool enabled) {
|
||||
return PubSubOption{NT_PUBSUB_KEEPDUPLICATES, enabled ? 1.0 : 0.0};
|
||||
return PubSubOption{NT_PUBSUB_KEEPDUPLICATES, enabled ? 1u : 0u};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -320,8 +321,8 @@ class PubSubOption {
|
||||
* @param depth number of entries to save for polling.
|
||||
* @return option
|
||||
*/
|
||||
static constexpr PubSubOption PollStorage(int depth) {
|
||||
return PubSubOption{NT_PUBSUB_POLLSTORAGE, static_cast<double>(depth)};
|
||||
static constexpr PubSubOption PollStorage(unsigned int depth) {
|
||||
return PubSubOption{NT_PUBSUB_POLLSTORAGE, depth};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -332,7 +333,7 @@ class PubSubOption {
|
||||
* @return option
|
||||
*/
|
||||
static constexpr PubSubOption LocalOnly() {
|
||||
return PubSubOption{NT_PUBSUB_LOCALREMOTE, 1.0};
|
||||
return PubSubOption{NT_PUBSUB_LOCALREMOTE, 1u};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -343,7 +344,7 @@ class PubSubOption {
|
||||
* @return option
|
||||
*/
|
||||
static constexpr PubSubOption RemoteOnly() {
|
||||
return PubSubOption{NT_PUBSUB_LOCALREMOTE, 2.0};
|
||||
return PubSubOption{NT_PUBSUB_LOCALREMOTE, 2u};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -354,11 +355,11 @@ class PubSubOption {
|
||||
* @return option
|
||||
*/
|
||||
static constexpr PubSubOption AllUpdates() {
|
||||
return PubSubOption{NT_PUBSUB_LOCALREMOTE, 0.0};
|
||||
return PubSubOption{NT_PUBSUB_LOCALREMOTE, 0u};
|
||||
}
|
||||
|
||||
NT_PubSubOptionType type;
|
||||
double value;
|
||||
unsigned int value;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user