Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for PR 2041 (Continuous time updates) #140

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sim/components/ble/SimpleWeatherService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions sim/components/ble/SimpleWeatherService.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Controllers {

class SimpleWeatherService {
public:
explicit SimpleWeatherService(const DateTime& dateTimeController);
explicit SimpleWeatherService(DateTime& dateTimeController);

void Init();

Expand Down Expand Up @@ -127,7 +127,7 @@ class SimpleWeatherService {

uint16_t eventHandle {};

const Pinetime::Controllers::DateTime& dateTimeController;
Pinetime::Controllers::DateTime& dateTimeController;

std::optional<CurrentWeather> currentWeather;
std::optional<Forecast> forecast;
Expand Down
1 change: 1 addition & 0 deletions sim/nrf_assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#define ASSERT(expr) assert(expr)
1 change: 1 addition & 0 deletions sim/portmacro_cmsis.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 14 additions & 12 deletions sim/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,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 changes: 8 additions & 8 deletions sim/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
* 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;
}
};
//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 changes: 37 additions & 0 deletions sim/semphr.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions sim/semphr.h
Original file line number Diff line number Diff line change
@@ -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);