feito

Dependencies:   BufferedSerial

Committer:
josefg25
Date:
Thu May 20 16:18:59 2021 +0000
Revision:
9:d0ef39e209b7
Parent:
0:c25c4b67b6a1
SRA_update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yaaqobhpt 0:c25c4b67b6a1 1 #include "MessageBuilder.h"
yaaqobhpt 0:c25c4b67b6a1 2 #include "mbed.h"
yaaqobhpt 0:c25c4b67b6a1 3
yaaqobhpt 0:c25c4b67b6a1 4 MessageBuilder::MessageBuilder() {
yaaqobhpt 0:c25c4b67b6a1 5 reset();
yaaqobhpt 0:c25c4b67b6a1 6 }
yaaqobhpt 0:c25c4b67b6a1 7
yaaqobhpt 0:c25c4b67b6a1 8 MessageBuilder::~MessageBuilder() {
yaaqobhpt 0:c25c4b67b6a1 9 // TODO Auto-generated destructor stub
yaaqobhpt 0:c25c4b67b6a1 10 }
yaaqobhpt 0:c25c4b67b6a1 11
yaaqobhpt 0:c25c4b67b6a1 12 char MessageBuilder::add(const void* data, size_t len) {
yaaqobhpt 0:c25c4b67b6a1 13 if (available() >= len) {
yaaqobhpt 0:c25c4b67b6a1 14 memcpy(_pointer, data, len);
yaaqobhpt 0:c25c4b67b6a1 15 _pointer += len;
yaaqobhpt 0:c25c4b67b6a1 16 return 0;
yaaqobhpt 0:c25c4b67b6a1 17 } else {
yaaqobhpt 0:c25c4b67b6a1 18 return 1;
yaaqobhpt 0:c25c4b67b6a1 19 }
yaaqobhpt 0:c25c4b67b6a1 20 }
yaaqobhpt 0:c25c4b67b6a1 21
yaaqobhpt 0:c25c4b67b6a1 22 void MessageBuilder::reset() {
yaaqobhpt 0:c25c4b67b6a1 23 message[0] = 0x06;
yaaqobhpt 0:c25c4b67b6a1 24 message[1] = 0x85;
yaaqobhpt 0:c25c4b67b6a1 25 _pointer = &message[2];
yaaqobhpt 0:c25c4b67b6a1 26 }
yaaqobhpt 0:c25c4b67b6a1 27
yaaqobhpt 0:c25c4b67b6a1 28 // Note: if message size grow beyond 32 bytes, return "size_t" insted, because it
yaaqobhpt 0:c25c4b67b6a1 29 // is the most appropriate type for "sizeof" operator. Now, unsgined char is used
yaaqobhpt 0:c25c4b67b6a1 30 // for memory economy.
yaaqobhpt 0:c25c4b67b6a1 31 unsigned char MessageBuilder::available() {
yaaqobhpt 0:c25c4b67b6a1 32 return &message[max_len - 1] - _pointer + 1;
yaaqobhpt 0:c25c4b67b6a1 33 }
yaaqobhpt 0:c25c4b67b6a1 34
yaaqobhpt 0:c25c4b67b6a1 35 unsigned char MessageBuilder::length() {
yaaqobhpt 0:c25c4b67b6a1 36 return _pointer - &message[0];
yaaqobhpt 0:c25c4b67b6a1 37 }
yaaqobhpt 0:c25c4b67b6a1 38
yaaqobhpt 0:c25c4b67b6a1 39 char MessageBuilder::add(float data) {
yaaqobhpt 0:c25c4b67b6a1 40 if (available() >= sizeof(data)) {
yaaqobhpt 0:c25c4b67b6a1 41 memcpy(_pointer, &data, sizeof(data));
yaaqobhpt 0:c25c4b67b6a1 42 _pointer += sizeof(data);
yaaqobhpt 0:c25c4b67b6a1 43 return 0;
yaaqobhpt 0:c25c4b67b6a1 44 } else {
yaaqobhpt 0:c25c4b67b6a1 45 return 1;
yaaqobhpt 0:c25c4b67b6a1 46 }
yaaqobhpt 0:c25c4b67b6a1 47 }
yaaqobhpt 0:c25c4b67b6a1 48
yaaqobhpt 0:c25c4b67b6a1 49 char MessageBuilder::add(int data) {
yaaqobhpt 0:c25c4b67b6a1 50 if (available() >= sizeof(data)) {
yaaqobhpt 0:c25c4b67b6a1 51 memcpy(_pointer, &data, sizeof(data));
yaaqobhpt 0:c25c4b67b6a1 52 _pointer += sizeof(data);
yaaqobhpt 0:c25c4b67b6a1 53 return 0;
yaaqobhpt 0:c25c4b67b6a1 54 } else {
yaaqobhpt 0:c25c4b67b6a1 55 return 1;
yaaqobhpt 0:c25c4b67b6a1 56 }
yaaqobhpt 0:c25c4b67b6a1 57 }
yaaqobhpt 0:c25c4b67b6a1 58
yaaqobhpt 0:c25c4b67b6a1 59 char MessageBuilder::add(char data) {
yaaqobhpt 0:c25c4b67b6a1 60 if (available() >= sizeof(data)) {
yaaqobhpt 0:c25c4b67b6a1 61 memcpy(_pointer, &data, sizeof(data));
yaaqobhpt 0:c25c4b67b6a1 62 _pointer += sizeof(data);
yaaqobhpt 0:c25c4b67b6a1 63 return 0;
yaaqobhpt 0:c25c4b67b6a1 64 } else {
yaaqobhpt 0:c25c4b67b6a1 65 return 1;
yaaqobhpt 0:c25c4b67b6a1 66 }
yaaqobhpt 0:c25c4b67b6a1 67 }
yaaqobhpt 0:c25c4b67b6a1 68
yaaqobhpt 0:c25c4b67b6a1 69 char MessageBuilder::add(unsigned int data) {
yaaqobhpt 0:c25c4b67b6a1 70 if (available() >= sizeof(data)) {
yaaqobhpt 0:c25c4b67b6a1 71 memcpy(_pointer, &data, sizeof(data));
yaaqobhpt 0:c25c4b67b6a1 72 _pointer += sizeof(data);
yaaqobhpt 0:c25c4b67b6a1 73 return 0;
yaaqobhpt 0:c25c4b67b6a1 74 } else {
yaaqobhpt 0:c25c4b67b6a1 75 return 1;
yaaqobhpt 0:c25c4b67b6a1 76 }
yaaqobhpt 0:c25c4b67b6a1 77 }
yaaqobhpt 0:c25c4b67b6a1 78