mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user