Fix callback for parsing string

This commit is contained in:
2015-11-07 21:49:51 +00:00
parent 841cbde3da
commit 015ea873ab
7 changed files with 57 additions and 4 deletions

View File

@@ -24,4 +24,7 @@ nanopb_%: $(NANOPB_DIR)/proto/%.proto
mv -f $@.c src
run:
platformio run
platformio run
test:
g++ src/pb_common.c src/pb_decode.c src/pb_encode.c src/tm1638.pb.c test.cpp -o test -Isrc

2
nanopb

Submodule nanopb updated: ef422656a5...7d4c8807af

View File

@@ -22,4 +22,4 @@ platform = atmelavr
framework = arduino
board = uno
targets = upload
upload_port = COM3
upload_port = COM4

View File

@@ -85,7 +85,7 @@ message SetDisplayToNumber {
}
message SetDisplayToString {
required string string = 1 [(nanopb).max_size = 40];
required string string = 1;
required int32 dots = 2;
required int32 pos = 3;
required bool has_font = 4;

BIN
test.buffer Normal file

Binary file not shown.

50
test.cpp Normal file
View File

@@ -0,0 +1,50 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
extern "C" {
#include "pb_decode.h"
#include "pb_encode.h"
#include "tm1638.pb.h"
}
bool callbackFont(pb_istream_t *stream, const pb_field_t *field, void **arg) {
uint8_t buffer[256] = {0};
int length = stream->bytes_left;
if (length > sizeof(buffer) - 1) {
return false;
}
if (!pb_read(stream, buffer, length)) {
return false;
}
memset(*arg, 0, sizeof(buffer));
strncpy((char*) *arg, (char*) buffer, length);
return true;
}
int main() {
FILE *fp = fopen("test.buffer", "r");
int packet_size = fgetc(fp);
uint8_t in_buffer[256];
int bytes_read = fread((char*) in_buffer, 1, packet_size, fp);
if (bytes_read != packet_size) {
printf("length problem");
return 1;
}
pb_istream_t istream = pb_istream_from_buffer(in_buffer, bytes_read);
_tm1638_Command command;
command.setDisplayToString.string.funcs.decode = &callbackFont;
if (!pb_decode(&istream, tm1638_Command_fields, &command)) {
printf("decode problem");
return 1;
}
tm1638_SetDisplayToString setDisplayToString = command.setDisplayToString;
char dots = setDisplayToString.dots;
char pos = setDisplayToString.dots;
printf("%s\n", setDisplayToString.string.arg);
return 0;
}

BIN
test.exe Normal file

Binary file not shown.