Revising my question about decoding

34 views
Skip to first unread message

Prince Montano

unread,
Dec 26, 2023, 9:57:05 PM12/26/23
to nanopb
Hello,

It seems that I have a massive gap in my understanding and was asking the wrong question as per my last thread post.

With the earlier nanopb generated ServoTarget.pb.h,  earlier Mr. Petteri mentioned

"If you are going to allocate constant amount of storage for the repeated
field, you can just use field option "max_count = 20" and nanopb will handle
the array automatically. No need to use callbacks."

I'm assuming that this field option is actually  message_actuation_ServoTarget_size. Or do I have to put that in the header myself?

Also by stating that callbacks do not have to be used, does it mean that I could just get away with doing the lines below:

// Decoding time
pb_istream_t input_stream = pb_istream_from_buffer(reinterpret_cast<const pb_byte_t*>(&pb_packets[0]), rx_buf_len - rx_buf_idx);

// Instance of ServoTargets to store the decoded messages
message_actuation_ServoTargets msg = message_actuation_ServoTargets_init_zero;
// Decode the ServoTargets message
if (!pb_decode(&stream, message_actuation_ServoTargets_fields, &msg)) {
    // Decoding failed
    return 1;
}

and access each entry as msg.targets[i] iteratively?

Thank you very much in advance for the help! Advanced Happy New year 
ServoTarget.pb.h

Petteri Aimonen

unread,
Dec 27, 2023, 3:14:02 AM12/27/23
to nan...@googlegroups.com
Hi,

At its simplest, you'd do something like this in .proto:

message ServoTargets {
repeated ServoTarget target = 1 [(nanopb).max_count = 20];
}

and then you get in .pb.h:

struct ServoTargets {
pb_size_t targets_count;
ServoTarget targets[20];
}

and can use them like any C array.

--
Petteri
Reply all
Reply to author
Forward
0 new messages