![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
sra-romi
Dependencies: BufferedSerial Matrix
MessageBuilder.cpp@1:dc87724abce8, 2019-04-11 (annotated)
- Committer:
- fabiofaria
- Date:
- Thu Apr 11 09:51:28 2019 +0000
- Revision:
- 1:dc87724abce8
Initial commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fabiofaria | 1:dc87724abce8 | 1 | #include "MessageBuilder.h" |
fabiofaria | 1:dc87724abce8 | 2 | #include "mbed.h" |
fabiofaria | 1:dc87724abce8 | 3 | |
fabiofaria | 1:dc87724abce8 | 4 | MessageBuilder::MessageBuilder() { |
fabiofaria | 1:dc87724abce8 | 5 | reset(); |
fabiofaria | 1:dc87724abce8 | 6 | } |
fabiofaria | 1:dc87724abce8 | 7 | |
fabiofaria | 1:dc87724abce8 | 8 | MessageBuilder::~MessageBuilder() { |
fabiofaria | 1:dc87724abce8 | 9 | // TODO Auto-generated destructor stub |
fabiofaria | 1:dc87724abce8 | 10 | } |
fabiofaria | 1:dc87724abce8 | 11 | |
fabiofaria | 1:dc87724abce8 | 12 | char MessageBuilder::add(const void* data, size_t len) { |
fabiofaria | 1:dc87724abce8 | 13 | if (available() >= len) { |
fabiofaria | 1:dc87724abce8 | 14 | memcpy(_pointer, data, len); |
fabiofaria | 1:dc87724abce8 | 15 | _pointer += len; |
fabiofaria | 1:dc87724abce8 | 16 | return 0; |
fabiofaria | 1:dc87724abce8 | 17 | } else { |
fabiofaria | 1:dc87724abce8 | 18 | return 1; |
fabiofaria | 1:dc87724abce8 | 19 | } |
fabiofaria | 1:dc87724abce8 | 20 | } |
fabiofaria | 1:dc87724abce8 | 21 | |
fabiofaria | 1:dc87724abce8 | 22 | void MessageBuilder::reset() { |
fabiofaria | 1:dc87724abce8 | 23 | message[0] = 0x06; |
fabiofaria | 1:dc87724abce8 | 24 | message[1] = 0x85; |
fabiofaria | 1:dc87724abce8 | 25 | _pointer = &message[2]; |
fabiofaria | 1:dc87724abce8 | 26 | } |
fabiofaria | 1:dc87724abce8 | 27 | |
fabiofaria | 1:dc87724abce8 | 28 | // Note: if message size grow beyond 32 bytes, return "size_t" insted, because it |
fabiofaria | 1:dc87724abce8 | 29 | // is the most appropriate type for "sizeof" operator. Now, unsgined char is used |
fabiofaria | 1:dc87724abce8 | 30 | // for memory economy. |
fabiofaria | 1:dc87724abce8 | 31 | unsigned char MessageBuilder::available() { |
fabiofaria | 1:dc87724abce8 | 32 | return &message[max_len - 1] - _pointer + 1; |
fabiofaria | 1:dc87724abce8 | 33 | } |
fabiofaria | 1:dc87724abce8 | 34 | |
fabiofaria | 1:dc87724abce8 | 35 | unsigned char MessageBuilder::length() { |
fabiofaria | 1:dc87724abce8 | 36 | return _pointer - &message[0]; |
fabiofaria | 1:dc87724abce8 | 37 | } |
fabiofaria | 1:dc87724abce8 | 38 | |
fabiofaria | 1:dc87724abce8 | 39 | char MessageBuilder::add(float data) { |
fabiofaria | 1:dc87724abce8 | 40 | if (available() >= sizeof(data)) { |
fabiofaria | 1:dc87724abce8 | 41 | memcpy(_pointer, &data, sizeof(data)); |
fabiofaria | 1:dc87724abce8 | 42 | _pointer += sizeof(data); |
fabiofaria | 1:dc87724abce8 | 43 | return 0; |
fabiofaria | 1:dc87724abce8 | 44 | } else { |
fabiofaria | 1:dc87724abce8 | 45 | return 1; |
fabiofaria | 1:dc87724abce8 | 46 | } |
fabiofaria | 1:dc87724abce8 | 47 | } |
fabiofaria | 1:dc87724abce8 | 48 | |
fabiofaria | 1:dc87724abce8 | 49 | char MessageBuilder::add(int data) { |
fabiofaria | 1:dc87724abce8 | 50 | if (available() >= sizeof(data)) { |
fabiofaria | 1:dc87724abce8 | 51 | memcpy(_pointer, &data, sizeof(data)); |
fabiofaria | 1:dc87724abce8 | 52 | _pointer += sizeof(data); |
fabiofaria | 1:dc87724abce8 | 53 | return 0; |
fabiofaria | 1:dc87724abce8 | 54 | } else { |
fabiofaria | 1:dc87724abce8 | 55 | return 1; |
fabiofaria | 1:dc87724abce8 | 56 | } |
fabiofaria | 1:dc87724abce8 | 57 | } |
fabiofaria | 1:dc87724abce8 | 58 | |
fabiofaria | 1:dc87724abce8 | 59 | char MessageBuilder::add(char data) { |
fabiofaria | 1:dc87724abce8 | 60 | if (available() >= sizeof(data)) { |
fabiofaria | 1:dc87724abce8 | 61 | memcpy(_pointer, &data, sizeof(data)); |
fabiofaria | 1:dc87724abce8 | 62 | _pointer += sizeof(data); |
fabiofaria | 1:dc87724abce8 | 63 | return 0; |
fabiofaria | 1:dc87724abce8 | 64 | } else { |
fabiofaria | 1:dc87724abce8 | 65 | return 1; |
fabiofaria | 1:dc87724abce8 | 66 | } |
fabiofaria | 1:dc87724abce8 | 67 | } |
fabiofaria | 1:dc87724abce8 | 68 | |
fabiofaria | 1:dc87724abce8 | 69 | char MessageBuilder::add(unsigned int data) { |
fabiofaria | 1:dc87724abce8 | 70 | if (available() >= sizeof(data)) { |
fabiofaria | 1:dc87724abce8 | 71 | memcpy(_pointer, &data, sizeof(data)); |
fabiofaria | 1:dc87724abce8 | 72 | _pointer += sizeof(data); |
fabiofaria | 1:dc87724abce8 | 73 | return 0; |
fabiofaria | 1:dc87724abce8 | 74 | } else { |
fabiofaria | 1:dc87724abce8 | 75 | return 1; |
fabiofaria | 1:dc87724abce8 | 76 | } |
fabiofaria | 1:dc87724abce8 | 77 | } |