Fix callback for parsing string
This commit is contained in:
5
Makefile
5
Makefile
@@ -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
2
nanopb
Submodule nanopb updated: ef422656a5...7d4c8807af
@@ -22,4 +22,4 @@ platform = atmelavr
|
||||
framework = arduino
|
||||
board = uno
|
||||
targets = upload
|
||||
upload_port = COM3
|
||||
upload_port = COM4
|
||||
@@ -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
BIN
test.buffer
Normal file
Binary file not shown.
50
test.cpp
Normal file
50
test.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user