mirror of
https://github.com/google/pebble.git
synced 2025-06-26 10:56:17 +00:00
Import of the watch repository from Pebble
This commit is contained in:
commit
3b92768480
10334 changed files with 2564465 additions and 0 deletions
22
third_party/nanopb/examples/simple/Makefile
vendored
Normal file
22
third_party/nanopb/examples/simple/Makefile
vendored
Normal file
|
@ -0,0 +1,22 @@
|
|||
# Include the nanopb provided Makefile rules
|
||||
include ../../extra/nanopb.mk
|
||||
|
||||
# Compiler flags to enable all warnings & debug info
|
||||
CFLAGS = -Wall -Werror -g -O0
|
||||
CFLAGS += "-I$(NANOPB_DIR)"
|
||||
|
||||
# C source code files that are required
|
||||
CSRC = simple.c # The main program
|
||||
CSRC += simple.pb.c # The compiled protocol definition
|
||||
CSRC += $(NANOPB_DIR)/pb_encode.c # The nanopb encoder
|
||||
CSRC += $(NANOPB_DIR)/pb_decode.c # The nanopb decoder
|
||||
CSRC += $(NANOPB_DIR)/pb_common.c # The nanopb common parts
|
||||
|
||||
# Build rule for the main program
|
||||
simple: $(CSRC)
|
||||
$(CC) $(CFLAGS) -osimple $(CSRC)
|
||||
|
||||
# Build rule for the protocol
|
||||
simple.pb.c: simple.proto
|
||||
$(PROTOC) $(PROTOC_OPTS) --nanopb_out=. simple.proto
|
||||
|
29
third_party/nanopb/examples/simple/README.txt
vendored
Normal file
29
third_party/nanopb/examples/simple/README.txt
vendored
Normal file
|
@ -0,0 +1,29 @@
|
|||
Nanopb example "simple"
|
||||
=======================
|
||||
|
||||
This example demonstrates the very basic use of nanopb. It encodes and
|
||||
decodes a simple message.
|
||||
|
||||
The code uses four different API functions:
|
||||
|
||||
* pb_ostream_from_buffer() to declare the output buffer that is to be used
|
||||
* pb_encode() to encode a message
|
||||
* pb_istream_from_buffer() to declare the input buffer that is to be used
|
||||
* pb_decode() to decode a message
|
||||
|
||||
Example usage
|
||||
-------------
|
||||
|
||||
On Linux, simply type "make" to build the example. After that, you can
|
||||
run it with the command: ./simple
|
||||
|
||||
On other platforms, you first have to compile the protocol definition using
|
||||
the following command::
|
||||
|
||||
../../generator-bin/protoc --nanopb_out=. simple.proto
|
||||
|
||||
After that, add the following five files to your project and compile:
|
||||
|
||||
simple.c simple.pb.c pb_encode.c pb_decode.c pb_common.c
|
||||
|
||||
|
71
third_party/nanopb/examples/simple/simple.c
vendored
Normal file
71
third_party/nanopb/examples/simple/simple.c
vendored
Normal file
|
@ -0,0 +1,71 @@
|
|||
#include <stdio.h>
|
||||
#include <pb_encode.h>
|
||||
#include <pb_decode.h>
|
||||
#include "simple.pb.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
/* This is the buffer where we will store our message. */
|
||||
uint8_t buffer[128];
|
||||
size_t message_length;
|
||||
bool status;
|
||||
|
||||
/* Encode our message */
|
||||
{
|
||||
/* Allocate space on the stack to store the message data.
|
||||
*
|
||||
* Nanopb generates simple struct definitions for all the messages.
|
||||
* - check out the contents of simple.pb.h!
|
||||
* It is a good idea to always initialize your structures
|
||||
* so that you do not have garbage data from RAM in there.
|
||||
*/
|
||||
SimpleMessage message = SimpleMessage_init_zero;
|
||||
|
||||
/* Create a stream that will write to our buffer. */
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
||||
|
||||
/* Fill in the lucky number */
|
||||
message.lucky_number = 13;
|
||||
|
||||
/* Now we are ready to encode the message! */
|
||||
status = pb_encode(&stream, SimpleMessage_fields, &message);
|
||||
message_length = stream.bytes_written;
|
||||
|
||||
/* Then just check for any errors.. */
|
||||
if (!status)
|
||||
{
|
||||
printf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Now we could transmit the message over network, store it in a file or
|
||||
* wrap it to a pigeon's leg.
|
||||
*/
|
||||
|
||||
/* But because we are lazy, we will just decode it immediately. */
|
||||
|
||||
{
|
||||
/* Allocate space for the decoded message. */
|
||||
SimpleMessage message = SimpleMessage_init_zero;
|
||||
|
||||
/* Create a stream that reads from the buffer. */
|
||||
pb_istream_t stream = pb_istream_from_buffer(buffer, message_length);
|
||||
|
||||
/* Now we are ready to decode the message. */
|
||||
status = pb_decode(&stream, SimpleMessage_fields, &message);
|
||||
|
||||
/* Check for errors... */
|
||||
if (!status)
|
||||
{
|
||||
printf("Decoding failed: %s\n", PB_GET_ERROR(&stream));
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Print the data contained in the message. */
|
||||
printf("Your lucky number was %d!\n", (int)message.lucky_number);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
9
third_party/nanopb/examples/simple/simple.proto
vendored
Normal file
9
third_party/nanopb/examples/simple/simple.proto
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
// A very simple protocol definition, consisting of only
|
||||
// one message.
|
||||
|
||||
syntax = "proto2";
|
||||
|
||||
message SimpleMessage {
|
||||
required int32 lucky_number = 1;
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue