kjj
Dependencies: mbed-rtos mbed Xbus
Fork of MTi-1_example by
Diff: main.cpp
- Revision:
- 25:01356fb59467
- Parent:
- 24:2cc49dc854e3
- Child:
- 26:665d3624f9ab
--- a/main.cpp Wed May 20 12:47:50 2015 +0200 +++ b/main.cpp Wed May 20 13:58:34 2015 +0200 @@ -1,15 +1,26 @@ #include "mbed.h" +#include "rtos.h" #include "xbusparser.h" #include "xbusmessage.h" +#define MEMORY_POOL_SIZE (4) +#define MAX_XBUS_DATA_SIZE (128) + static Serial pc(PA_2, PA_3); static Serial mt(PB_9, PB_8); static XbusParser* xbusParser; -static uint8_t rxBuffer[256]; + +MemoryPool<XbusMessage, MEMORY_POOL_SIZE> g_messagePool; +MemoryPool<uint8_t[MAX_XBUS_DATA_SIZE], MEMORY_POOL_SIZE> g_messageDataPool; -static void* allocateBuffer(size_t bufSize) +static void* allocateMessageData(size_t bufSize) { - return bufSize < sizeof(rxBuffer) ? rxBuffer : NULL; + return bufSize < MAX_XBUS_DATA_SIZE ? g_messageDataPool.alloc() : NULL; +} + +static void deallocateMessageData(void const* buffer) +{ + g_messageDataPool.free((uint8_t(*)[MAX_XBUS_DATA_SIZE])buffer); } static void mtLowLevelHandler(void) @@ -107,6 +118,10 @@ { pc.printf("Received Xbus message. MID=%X, length=%d\n", message->mid, message->length); } + if (message->data) + { + deallocateMessageData(message->data); + } } static void configureSerialPorts(void) @@ -123,7 +138,8 @@ int main(void) { XbusParserCallback xbusCallback = {}; - xbusCallback.allocateBuffer = allocateBuffer; + xbusCallback.allocateBuffer = allocateMessageData; + xbusCallback.deallocateBuffer = deallocateMessageData; xbusCallback.handleMessage = mtMessageHandler; xbusParser = XbusParser_create(&xbusCallback);