From 01c3e3af2394f863323b846fa304ff7e0a30e9df Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Tue, 16 Jan 2018 23:43:07 +0100 Subject: eos support --- code/esp32/components/eos/msgq.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 code/esp32/components/eos/msgq.c (limited to 'code/esp32/components/eos/msgq.c') diff --git a/code/esp32/components/eos/msgq.c b/code/esp32/components/eos/msgq.c new file mode 100644 index 0000000..ec89efe --- /dev/null +++ b/code/esp32/components/eos/msgq.c @@ -0,0 +1,39 @@ +#include +#include + +#include "eos.h" +#include "msgq.h" + +#define EOS_MSGQ_IDX_MASK(IDX, SIZE) ((IDX) & ((SIZE) - 1)) + +void eos_msgq_init(EOSMsgQ *msgq, EOSMsgItem *array, uint8_t size) { + msgq->idx_r = 0; + msgq->idx_w = 0; + msgq->array = array; + msgq->size = size; +} + +int eos_msgq_push(EOSMsgQ *msgq, unsigned char cmd, unsigned char *buffer, uint16_t len) { + if (msgq->idx_w - msgq->idx_r == msgq->size) return EOS_ERR_Q_FULL; + + uint8_t idx = EOS_MSGQ_IDX_MASK(msgq->idx_w, msgq->size); + msgq->array[idx].cmd = cmd; + memcpy(msgq->array[idx].buffer, buffer, len); + msgq->array[idx].len = len; + msgq->idx_w++; + return EOS_OK; +} + +void eos_msgq_pop(EOSMsgQ *msgq, unsigned char *cmd, unsigned char **buffer, uint16_t *len) { + if (msgq->idx_r == msgq->idx_w) { + *cmd = 0; + *buffer = NULL; + } else { + uint8_t idx = EOS_MSGQ_IDX_MASK(msgq->idx_r, msgq->size); + *cmd = msgq->array[idx].cmd; + *buffer = msgq->array[idx].buffer; + *len = msgq->array[idx].len; + msgq->idx_r++; + } +} + -- cgit v1.2.3