Fixes for PR 2041 (Continuous time updates)
This commit is contained in:
parent
a16ff8cf69
commit
a796bc1f17
9 changed files with 77 additions and 23 deletions
|
@ -65,6 +65,8 @@ add_library(sim-base STATIC
|
|||
sim/timers.cpp
|
||||
sim/queue.h
|
||||
sim/queue.cpp
|
||||
sim/semphr.h
|
||||
sim/semphr.cpp
|
||||
# src/FreeRTOS
|
||||
sim/portmacro_cmsis.h
|
||||
sim/portmacro_cmsis.cpp
|
||||
|
|
|
@ -62,7 +62,7 @@ int WeatherCallback(uint16_t /*connHandle*/, uint16_t /*attrHandle*/, struct ble
|
|||
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() {
|
||||
|
|
|
@ -24,7 +24,7 @@ namespace Controllers {
|
|||
|
||||
class SimpleWeatherService {
|
||||
public:
|
||||
explicit SimpleWeatherService(const DateTime& dateTimeController);
|
||||
explicit SimpleWeatherService(DateTime& dateTimeController);
|
||||
|
||||
void Init();
|
||||
|
||||
|
@ -127,7 +127,7 @@ private:
|
|||
|
||||
uint16_t eventHandle {};
|
||||
|
||||
const Pinetime::Controllers::DateTime& dateTimeController;
|
||||
Pinetime::Controllers::DateTime& dateTimeController;
|
||||
|
||||
std::optional<CurrentWeather> currentWeather;
|
||||
std::optional<Forecast> forecast;
|
||||
|
|
1
sim/nrf_assert.h
Normal file
1
sim/nrf_assert.h
Normal file
|
@ -0,0 +1 @@
|
|||
#define ASSERT(expr) assert(expr)
|
|
@ -37,6 +37,7 @@ extern "C" {
|
|||
|
||||
typedef uint32_t TickType_t;
|
||||
#define portMAX_DELAY ( TickType_t ) 0xffffffffUL
|
||||
#define portNRF_RTC_MAXTICKS ((1U<<24)-1U)
|
||||
|
||||
typedef long BaseType_t;
|
||||
typedef unsigned long UBaseType_t;
|
||||
|
|
|
@ -5,43 +5,45 @@
|
|||
|
||||
QueueHandle_t xQueueCreate(const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize)
|
||||
{
|
||||
QueueHandle_t xQueue;
|
||||
Queue_t *xQueue = new Queue_t;
|
||||
if (uxItemSize != 1) {
|
||||
throw std::runtime_error("uxItemSize must be 1");
|
||||
}
|
||||
xQueue.queue.reserve(uxQueueLength);
|
||||
xQueue->queue.reserve(uxQueueLength);
|
||||
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);
|
||||
xQueue.queue.push_back(*reinterpret_cast<const uint8_t *const>(pvItemToQueue));
|
||||
Queue_t* pxQueue = ( Queue_t * ) xQueue;
|
||||
std::lock_guard<std::mutex> guard(pxQueue->mutex);
|
||||
pxQueue->queue.push_back(*reinterpret_cast<const uint8_t *const>(pvItemToQueue));
|
||||
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;
|
||||
*xHigherPriorityTaskWoken = pdFALSE;
|
||||
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) {
|
||||
return false;
|
||||
}
|
||||
SDL_Delay(25);
|
||||
xTicksToWait -= 25;
|
||||
}
|
||||
if (xQueue.queue.empty()) {
|
||||
if (pxQueue->queue.empty()) {
|
||||
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);
|
||||
*buf = xQueue.queue.at(0);
|
||||
xQueue.queue.erase(xQueue.queue.begin());
|
||||
*buf = pxQueue->queue.at(0);
|
||||
pxQueue->queue.erase(pxQueue->queue.begin());
|
||||
return true;
|
||||
}
|
||||
|
|
16
sim/queue.h
16
sim/queue.h
|
@ -8,15 +8,15 @@
|
|||
* returns an QueueHandle_t variable that can then be used as a parameter to
|
||||
* xQueueSend(), xQueueReceive(), etc.
|
||||
*/
|
||||
//typedef void * QueueHandle_t;
|
||||
struct QueueHandle_t {
|
||||
typedef void * QueueHandle_t;
|
||||
struct Queue_t {
|
||||
std::mutex mutex;
|
||||
std::vector<uint8_t> queue;
|
||||
QueueHandle_t() {}
|
||||
QueueHandle_t(const QueueHandle_t &o) {
|
||||
Queue_t() {}
|
||||
Queue_t(const Queue_t &o) {
|
||||
queue=o.queue;
|
||||
}
|
||||
QueueHandle_t &operator=(const QueueHandle_t &o) {
|
||||
Queue_t &operator=(const Queue_t &o) {
|
||||
queue=o.queue;
|
||||
return *this;
|
||||
}
|
||||
|
@ -24,6 +24,6 @@ struct QueueHandle_t {
|
|||
//using QueueHandle_t = std::vector<uint8_t>;
|
||||
|
||||
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 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 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 xQueueReceive(QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait );
|
||||
|
|
37
sim/semphr.cpp
Normal file
37
sim/semphr.cpp
Normal 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
11
sim/semphr.h
Normal 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);
|
Loading…
Reference in a new issue