1
0
Fork 0

Fixes for PR 2041 (Continuous time updates)

This commit is contained in:
mark9064 2024-03-30 18:56:54 +00:00 committed by NeroBurner
parent a16ff8cf69
commit a796bc1f17
9 changed files with 77 additions and 23 deletions

View file

@ -65,6 +65,8 @@ add_library(sim-base STATIC
sim/timers.cpp sim/timers.cpp
sim/queue.h sim/queue.h
sim/queue.cpp sim/queue.cpp
sim/semphr.h
sim/semphr.cpp
# src/FreeRTOS # src/FreeRTOS
sim/portmacro_cmsis.h sim/portmacro_cmsis.h
sim/portmacro_cmsis.cpp sim/portmacro_cmsis.cpp

View file

@ -62,7 +62,7 @@ int WeatherCallback(uint16_t /*connHandle*/, uint16_t /*attrHandle*/, struct ble
return static_cast<Pinetime::Controllers::SimpleWeatherService*>(arg)->OnCommand(ctxt); return static_cast<Pinetime::Controllers::SimpleWeatherService*>(arg)->OnCommand(ctxt);
} }
SimpleWeatherService::SimpleWeatherService(const DateTime& dateTimeController) : dateTimeController(dateTimeController) { SimpleWeatherService::SimpleWeatherService(DateTime& dateTimeController) : dateTimeController(dateTimeController) {
} }
void SimpleWeatherService::Init() { void SimpleWeatherService::Init() {

View file

@ -24,7 +24,7 @@ namespace Controllers {
class SimpleWeatherService { class SimpleWeatherService {
public: public:
explicit SimpleWeatherService(const DateTime& dateTimeController); explicit SimpleWeatherService(DateTime& dateTimeController);
void Init(); void Init();
@ -127,7 +127,7 @@ private:
uint16_t eventHandle {}; uint16_t eventHandle {};
const Pinetime::Controllers::DateTime& dateTimeController; Pinetime::Controllers::DateTime& dateTimeController;
std::optional<CurrentWeather> currentWeather; std::optional<CurrentWeather> currentWeather;
std::optional<Forecast> forecast; std::optional<Forecast> forecast;

1
sim/nrf_assert.h Normal file
View file

@ -0,0 +1 @@
#define ASSERT(expr) assert(expr)

View file

@ -37,6 +37,7 @@ extern "C" {
typedef uint32_t TickType_t; typedef uint32_t TickType_t;
#define portMAX_DELAY ( TickType_t ) 0xffffffffUL #define portMAX_DELAY ( TickType_t ) 0xffffffffUL
#define portNRF_RTC_MAXTICKS ((1U<<24)-1U)
typedef long BaseType_t; typedef long BaseType_t;
typedef unsigned long UBaseType_t; typedef unsigned long UBaseType_t;

View file

@ -5,43 +5,45 @@
QueueHandle_t xQueueCreate(const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize) QueueHandle_t xQueueCreate(const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize)
{ {
QueueHandle_t xQueue; Queue_t *xQueue = new Queue_t;
if (uxItemSize != 1) { if (uxItemSize != 1) {
throw std::runtime_error("uxItemSize must be 1"); throw std::runtime_error("uxItemSize must be 1");
} }
xQueue.queue.reserve(uxQueueLength); xQueue->queue.reserve(uxQueueLength);
return xQueue; return xQueue;
} }
BaseType_t xQueueSend(QueueHandle_t &xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait) BaseType_t xQueueSend(QueueHandle_t xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait)
{ {
std::lock_guard<std::mutex> guard(xQueue.mutex); Queue_t* pxQueue = ( Queue_t * ) xQueue;
xQueue.queue.push_back(*reinterpret_cast<const uint8_t *const>(pvItemToQueue)); std::lock_guard<std::mutex> guard(pxQueue->mutex);
pxQueue->queue.push_back(*reinterpret_cast<const uint8_t *const>(pvItemToQueue));
return true; return true;
} }
BaseType_t xQueueSendFromISR(QueueHandle_t &xQueue, const void * const pvItemToQueue, BaseType_t *xHigherPriorityTaskWoken) BaseType_t xQueueSendFromISR(QueueHandle_t xQueue, const void * const pvItemToQueue, BaseType_t *xHigherPriorityTaskWoken)
{ {
TickType_t xTicksToWait = 0; TickType_t xTicksToWait = 0;
*xHigherPriorityTaskWoken = pdFALSE; *xHigherPriorityTaskWoken = pdFALSE;
return xQueueSend(xQueue, pvItemToQueue, 0.0); return xQueueSend(xQueue, pvItemToQueue, 0.0);
} }
BaseType_t xQueueReceive(QueueHandle_t &xQueue, void * const pvBuffer, TickType_t xTicksToWait) BaseType_t xQueueReceive(QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait)
{ {
while (xQueue.queue.empty()) { Queue_t* pxQueue = ( Queue_t * ) xQueue;
while (pxQueue->queue.empty()) {
if (xTicksToWait <= 25) { if (xTicksToWait <= 25) {
return false; return false;
} }
SDL_Delay(25); SDL_Delay(25);
xTicksToWait -= 25; xTicksToWait -= 25;
} }
if (xQueue.queue.empty()) { if (pxQueue->queue.empty()) {
return false; return false;
} }
std::lock_guard<std::mutex> guard(xQueue.mutex); std::lock_guard<std::mutex> guard(pxQueue->mutex);
uint8_t *buf = reinterpret_cast<uint8_t * const>(pvBuffer); uint8_t *buf = reinterpret_cast<uint8_t * const>(pvBuffer);
*buf = xQueue.queue.at(0); *buf = pxQueue->queue.at(0);
xQueue.queue.erase(xQueue.queue.begin()); pxQueue->queue.erase(pxQueue->queue.begin());
return true; return true;
} }

View file

@ -8,15 +8,15 @@
* returns an QueueHandle_t variable that can then be used as a parameter to * returns an QueueHandle_t variable that can then be used as a parameter to
* xQueueSend(), xQueueReceive(), etc. * xQueueSend(), xQueueReceive(), etc.
*/ */
//typedef void * QueueHandle_t; typedef void * QueueHandle_t;
struct QueueHandle_t { struct Queue_t {
std::mutex mutex; std::mutex mutex;
std::vector<uint8_t> queue; std::vector<uint8_t> queue;
QueueHandle_t() {} Queue_t() {}
QueueHandle_t(const QueueHandle_t &o) { Queue_t(const Queue_t &o) {
queue=o.queue; queue=o.queue;
} }
QueueHandle_t &operator=(const QueueHandle_t &o) { Queue_t &operator=(const Queue_t &o) {
queue=o.queue; queue=o.queue;
return *this; return *this;
} }
@ -24,6 +24,6 @@ struct QueueHandle_t {
//using QueueHandle_t = std::vector<uint8_t>; //using QueueHandle_t = std::vector<uint8_t>;
QueueHandle_t xQueueCreate(const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize); QueueHandle_t xQueueCreate(const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize);
BaseType_t xQueueSend(QueueHandle_t &xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait); BaseType_t xQueueSend(QueueHandle_t xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait);
BaseType_t xQueueSendFromISR(QueueHandle_t &xQueue, const void * const pvItemToQueue, BaseType_t *xHigherPriorityTaskWoken); BaseType_t xQueueSendFromISR(QueueHandle_t xQueue, const void * const pvItemToQueue, BaseType_t *xHigherPriorityTaskWoken);
BaseType_t xQueueReceive(QueueHandle_t &xQueue, void * const pvBuffer, TickType_t xTicksToWait ); BaseType_t xQueueReceive(QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait );

37
sim/semphr.cpp Normal file
View file

@ -0,0 +1,37 @@
#include "semphr.h"
#include <SDL2/SDL.h>
#include <stdexcept>
QueueHandle_t xSemaphoreCreateMutex() {
SemaphoreHandle_t xSemaphore = xQueueCreate(1, 1);
Queue_t *pxQueue = (Queue_t *)xSemaphore;
pxQueue->queue.push_back(0);
return xSemaphore;
};
BaseType_t xSemaphoreTake(SemaphoreHandle_t xSemaphore,
TickType_t xTicksToWait) {
Queue_t *pxQueue = (Queue_t *)xSemaphore;
while (!pxQueue->queue.empty()) {
if (xTicksToWait <= 25) {
return false;
}
SDL_Delay(25);
xTicksToWait -= 25;
}
std::lock_guard<std::mutex> guard(pxQueue->mutex);
if (!pxQueue->queue.empty()) {
return false;
}
pxQueue->queue.push_back(0);
return true;
}
BaseType_t xSemaphoreGive(SemaphoreHandle_t xSemaphore) {
Queue_t *pxQueue = (Queue_t *)xSemaphore;
std::lock_guard<std::mutex> guard(pxQueue->mutex);
if (pxQueue->queue.size() != 1) {
throw std::runtime_error("Mutex released without being held");
}
pxQueue->queue.pop_back();
return true;
}

11
sim/semphr.h Normal file
View file

@ -0,0 +1,11 @@
#pragma once
#include "FreeRTOS.h"
#include "queue.h"
typedef QueueHandle_t SemaphoreHandle_t;
QueueHandle_t xSemaphoreCreateMutex();
BaseType_t xSemaphoreTake( SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait);
BaseType_t xSemaphoreGive( SemaphoreHandle_t xSemaphore);