Serial pc(PA_2, PA_3);
//some declarations and functions omitted for brevity
int main(){ LIS3DH_config(); while(1) { //pc.printf("here\r"); //do accelerometer stuff LIS3DH_read_3axis(); LIS3DH_tilt_angle(); /* 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! */ SensorData message; /* Create a stream that will write to our buffer. */ pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); /* Fill in the tilt angle from p (p is a tilt angle) */ message.tilt_angle = p; /* Now we are ready to encode the message! */ status = pb_encode(&stream, SensorData_fields, &message); message_length = stream.bytes_written; pc.printf("%s\r", buffer); //} /* Then just check for any errors.. */ if (!status) { pc.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
} wait(.1); }}K.i...-[.
.&p...-[.
..m...-[.
..m...-[.
3vW...-[.
.QQ...-[.
..Q...-[.
..f...-[.
".h...-[.
WlY...-[.
.(k...-[.
#include "mbed.h"#include <stdio.h>#include <pb_encode.h>#include <pb_decode.h>#include "KV1KV2Comms.pb.h"#include <string> Serial pc(PA_2, PA_3);Serial device(PA_9, PA_10); // tx, rx
uint8_t buffer[128];size_t message_length;bool status;int i = 0;
int main() {
/* Allocate space for the decoded message. */ SensorData message; char c; while(1) { if(device.readable()) { /* But because we are lazy, we will just decode it immediately. */ int i = 0;
while ((c = device.getc()) != '\r'){ buffer[i] = c; //pc.printf("%c", c); if(++i > sizeof(buffer)) break; }
if (c == '\r'){
for(int y = 0; y < sizeof(buffer); y++){ pc.printf("%02x ", buffer[y]); } pc.printf("\r"); /* 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, SensorData_fields, &message); /* Check for errors... */ if (!status) { pc.printf("Decoding failed: %s\n", PB_GET_ERROR(&stream)); return 1; } /* Print the data contained in the message. */ pc.printf("Your lucky number was %f!\n", message.tilt_angle); }
} }} for(int y = 0; y < sizeof(buffer); y++){ pc.printf("%02x ", buffer[y]); }5b 1d 5b 1d 1d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
5b 1d 5b 1d 1d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
5b 1d 5b 1d 1d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
5b 1d 5b 1d 1d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
/* Now we are ready to decode the message. */ status = pb_decode(&stream, SensorData_fields, &message); /* Check for errors... */ if (!status) { pc.printf("Decoding failed: %s\n", PB_GET_ERROR(&stream)); return 1; } /* Print the data contained in the message. */ pc.printf("Your lucky number was %f!\n", message.tilt_angle);Your lucky number was 0.000000!
SensorData message; /* Create a stream that will write to our buffer. */ pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); /* Fill in the tilt angle from p */ message.tilt_angle = p;
//debug so we know what the data is before being encoded pc.printf("message contains %f\n", message.tilt_angle); /* Now we are ready to encode the message! */ status = pb_encode(&stream, SensorData_fields, &message); message_length = stream.bytes_written;
//print out buffer to serial in hex pc.printf("%X\r", buffer);message contains 1.234718200001D8message contains 1.252614200001D8message contains 1.159460200001D8message contains 1.179667200001D8message contains 1.251465200001D8message contains 1.291126200001D8message contains 1.332516200001D8message contains 1.294451200001D8
SensorData message; /* Create a stream that will write to our buffer. */ pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); /* Fill in the tilt angle from p */ message.tilt_angle = p; pc.printf("message contains %f\n", message.tilt_angle); /* Now we are ready to encode the message! */ status = pb_encode(&stream, SensorData_fields, &message); message_length = stream.bytes_written; //pc.printf("%u\r", status); //if (pb_encode(&stream, SensorData_fields, &message)) { //encode message //new test code: // message_length = stream.bytes_written; for (int i = 0; i < sizeof(buffer); i++) { pc.printf("%X", buffer[i]); } pc.printf("\r");message contains 1.112535D8C678E3F101F2D5B1D0830D9B48040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000message contains 1.132095D7BE8903F101F2D5B1D0830D9B48040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000message contains 1.165736DDA36953F101F2D5B1D0830D9B48040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000message contains 1.139276DCED3913F101F2D5B1D0830D9B48040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000