diff --git a/src/esp32/esp32_queue.c b/src/j_esp32/j_esp32_queue.cpp similarity index 85% rename from src/esp32/esp32_queue.c rename to src/j_esp32/j_esp32_queue.cpp index a2324fe..b3b8852 100644 --- a/src/esp32/esp32_queue.c +++ b/src/j_esp32/j_esp32_queue.cpp @@ -1,10 +1,7 @@ +#if defined(ARDUINO_ARCH_ESP32) +#include "j_esp32_queue.h" -#if defined(_POSIX_THREADS) -#include "esp32_queue.h" - -#include "Arduino.h" - -void queue_init(queue_t *q, uint element_size, uint element_count) { +/*void queue_init(queue_t *q, uint element_size, uint element_count) { mutex_init(&q->core); q->cond = PTHREAD_COND_INITIALIZER; q->data = (uint8_t *)calloc(element_count + 1, element_size); @@ -12,24 +9,10 @@ void queue_init(queue_t *q, uint element_size, uint element_count) { q->element_size = (uint16_t)element_size; q->wptr = 0; q->rptr = 0; -} +}*/ void queue_free(queue_t *q) { free(q->data); } -static inline uint queue_get_level_unsafe(queue_t *q) { - int32_t rc = (int32_t)q->wptr - (int32_t)q->rptr; - if (rc < 0) { - rc += q->element_count + 1; - } - return (uint)rc; -} - -static inline uint queue_get_level(queue_t *q) { - mutex_enter_blocking(&q->core); - uint level = queue_get_level_unsafe(q); - mutex_exit(&q->core); - return level; -} static inline void *element_ptr(queue_t *q, uint index) { assert(index <= q->element_count); @@ -131,4 +114,5 @@ void queue_remove_blocking(queue_t *q, void *data) { void queue_peek_blocking(queue_t *q, void *data) { queue_peek_internal(q, data, true); } + #endif \ No newline at end of file diff --git a/src/esp32/esp32_queue.h b/src/j_esp32/j_esp32_queue.h similarity index 87% rename from src/esp32/esp32_queue.h rename to src/j_esp32/j_esp32_queue.h index 5ab3825..f2ff83d 100644 --- a/src/esp32/esp32_queue.h +++ b/src/j_esp32/j_esp32_queue.h @@ -1,17 +1,18 @@ -#if defined(_POSIX_THREADS) +// #if defined(ARDUINO_ARCH_ESP32) -#ifndef ESP32_QUEUE_H -#define ESP32_QUEUE_H +#ifndef J_ESP32_QUEUE_H +#define J_ESP32_QUEUE_H /* * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. * * SPDX-License-Identifier: BSD-3-Clause - * + * * Modified to run on an esp32. */ -#include "../jmutex.h" +#include + #include "Arduino.h" #include "pthread.h" @@ -19,10 +20,6 @@ #define PICO_QUEUE_MAX_LEVEL 0 #endif -#ifdef __cplusplus -extern "C" { -#endif - typedef struct { mutex_t core; pthread_cond_t cond; @@ -44,7 +41,15 @@ typedef struct { * \param element_count Maximum number of entries in the queue */ static inline void queue_init(queue_t *q, uint element_size, - uint element_count); + uint element_count) { + mutex_init(&q->core); + q->cond = PTHREAD_COND_INITIALIZER; + q->data = (uint8_t *)calloc(element_count + 1, element_size); + q->element_count = (uint16_t)element_count; + q->element_size = (uint16_t)element_size; + q->wptr = 0; + q->rptr = 0; +} /*! \brief Destroy the specified queue. * \ingroup queue @@ -64,7 +69,14 @@ void queue_free(queue_t *q); * This does not use the mutex, so may return incorrect results if the * mutex is not externally locked */ -static inline uint queue_get_level_unsafe(queue_t *q); +static inline uint queue_get_level_unsafe(queue_t *q) { + int32_t rc = (int32_t)q->wptr - (int32_t)q->rptr; + if (rc < 0) { + rc += q->element_count + 1; + } + return (uint)rc; +} + /*! \brief Check of level of the specified queue. * \ingroup queue @@ -72,7 +84,12 @@ static inline uint queue_get_level_unsafe(queue_t *q); * \param q Pointer to a queue_t structure, used as a handle * \return Number of entries in the queue */ -static inline uint queue_get_level(queue_t *q); +static inline uint queue_get_level(queue_t *q) { + mutex_enter_blocking(&q->core); + uint level = queue_get_level_unsafe(q); + mutex_exit(&q->core); + return level; +} #if PICO_QUEUE_MAX_LEVEL /*! \brief Returns the highest level reached by the specified queue since it was @@ -195,10 +212,6 @@ void queue_remove_blocking(queue_t *q, void *data); */ void queue_peek_blocking(queue_t *q, void *data); -#ifdef __cplusplus -} #endif - -#endif -#endif \ No newline at end of file +// #endif \ No newline at end of file diff --git a/src/jqueue.h b/src/jqueue.h index 279fcf4..dfab0f6 100644 --- a/src/jqueue.h +++ b/src/jqueue.h @@ -7,7 +7,7 @@ #elif defined(ARDUINO_ARCH_ESP32) -#include "esp32/esp32_queue.h" +#include "j_esp32/j_esp32_queue.h" #endif #endif \ No newline at end of file