Add LPC1768
Dependencies: mbed-rtos mbed Xbus
Fork of MTi-1_example by
Revision 28:ae74baf7e5ab, committed 2015-05-20
- Comitter:
- Alex Young
- Date:
- Wed May 20 16:46:54 2015 +0200
- Parent:
- 27:eebe5fc884e3
- Child:
- 29:d9310e7b58b5
- Commit message:
- Make XbusMessage_format be smart about payload formatting.
Just as the xbus parser can convert from xbus payload to native structs
the xbus message formatter can convert from a native struct to a raw
xbus message payload.
Changed in this revision
| xbus/xbusmessage.c | Show annotated file Show diff for this revision Revisions of this file |
--- a/xbus/xbusmessage.c Wed May 20 16:46:14 2015 +0200
+++ b/xbus/xbusmessage.c Wed May 20 16:46:54 2015 +0200
@@ -17,6 +17,46 @@
#include "xbusdef.h"
#include "xbusutility.h"
+static uint16_t messageLength(struct XbusMessage const* message)
+{
+ switch (message->mid)
+ {
+ case XMID_SetOutputConfig:
+ return message->length * 2 * sizeof(uint16_t);
+
+ default:
+ return message->length;
+ }
+}
+
+static void formatOutputConfig(uint8_t* raw, struct XbusMessage const* message)
+{
+ struct OutputConfiguration* conf = message->data;
+ for (int i = 0; i < message->length; ++i)
+ {
+ raw = XbusUtility_writeU16(raw, conf->dtype);
+ raw = XbusUtility_writeU16(raw, conf->freq);
+ ++conf;
+ }
+}
+
+static void formatPayload(uint8_t* raw, struct XbusMessage const* message)
+{
+ switch (message->mid)
+ {
+ case XMID_SetOutputConfig:
+ formatOutputConfig(raw, message);
+ break;
+
+ default:
+ for (int i = 0; i < message->length; ++i)
+ {
+ *raw++ = ((uint8_t*)message->data)[i];
+ }
+ break;
+ }
+}
+
size_t XbusMessage_format(uint8_t* raw, struct XbusMessage const* message)
{
uint8_t* dptr = raw;
@@ -29,25 +69,26 @@
*dptr = message->mid;
checksum -= *dptr++;
- if (message->length < XBUS_EXTENDED_LENGTH)
+ uint16_t length = messageLength(message);
+
+ if (length < XBUS_EXTENDED_LENGTH)
{
- *dptr = message->length;
+ *dptr = length;
checksum -= *dptr++;
}
else
{
*dptr = XBUS_EXTENDED_LENGTH;
checksum -= *dptr++;
- *dptr = message->length >> 8;
+ *dptr = length >> 8;
checksum -= *dptr++;
- *dptr = message->length & 0xFF;
+ *dptr = length & 0xFF;
checksum -= *dptr++;
}
- uint8_t* sptr = message->data;
- for (int i = 0; i < message->length; ++i)
+ formatPayload(dptr, message);
+ for (int i = 0; i < length; ++i)
{
- *dptr = *sptr++;
checksum -= *dptr++;
}
*dptr++ = checksum;
David Khosravi
