Merge pull request #36 from girtsf/various-cleanups

Various cleanups
pull/37/head
Kevin Hester 2020-03-16 09:00:42 -07:00 zatwierdzone przez GitHub
commit 52dbc4e15d
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
8 zmienionych plików z 51 dodań i 38 usunięć

6
.clang-format 100644
Wyświetl plik

@ -0,0 +1,6 @@
Language: Cpp
IndentWidth: 4
ColumnLimit: 130
PointerAlignment: Right
BreakBeforeBraces: Linux
AllowShortFunctionsOnASingleLine: Inline

Wyświetl plik

@ -126,8 +126,7 @@ void CustomRF95::handleInterrupt()
// parsing was successful, queue for our recipient // parsing was successful, queue for our recipient
mp->has_payload = true; mp->has_payload = true;
int res = rxDest.enqueueFromISR(mp, &higherPriWoken); // NOWAIT - fixme, if queue is full, delete older messages assert(rxDest.enqueueFromISR(mp, &higherPriWoken)); // NOWAIT - fixme, if queue is full, delete older messages
assert(res == pdTRUE);
} }
clearRxBuf(); // This message accepted and cleared clearRxBuf(); // This message accepted and cleared
@ -185,4 +184,4 @@ void CustomRF95::startSend(MeshPacket *txp)
int res = RH_RF95::send(radiobuf, numbytes); int res = RH_RF95::send(radiobuf, numbytes);
assert(res); assert(res);
} }

Wyświetl plik

@ -66,16 +66,14 @@ public:
/// Return a buffer for use by others /// Return a buffer for use by others
void release(T *p) void release(T *p)
{ {
int res = dead.enqueue(p, 0); assert(dead.enqueue(p, 0));
assert(res == pdTRUE);
assert(p >= buf && (p - buf) < maxElements); // sanity check to make sure a programmer didn't free something that didn't come from this pool assert(p >= buf && (p - buf) < maxElements); // sanity check to make sure a programmer didn't free something that didn't come from this pool
} }
/// Return a buffer from an ISR, if higherPriWoken is set to true you have some work to do ;-) /// Return a buffer from an ISR, if higherPriWoken is set to true you have some work to do ;-)
void releaseFromISR(T *p, BaseType_t *higherPriWoken) void releaseFromISR(T *p, BaseType_t *higherPriWoken)
{ {
int res = dead.enqueueFromISR(p, higherPriWoken); assert(dead.enqueueFromISR(p, higherPriWoken));
assert(res == pdTRUE);
assert(p >= buf && (p - buf) < maxElements); // sanity check to make sure a programmer didn't free something that didn't come from this pool assert(p >= buf && (p - buf) < maxElements); // sanity check to make sure a programmer didn't free something that didn't come from this pool
} }
}; };

Wyświetl plik

@ -173,7 +173,7 @@ void MeshService::handleFromRadio(MeshPacket *mp)
if (d) if (d)
releaseToPool(d); releaseToPool(d);
} }
assert(toPhoneQueue.enqueue(mp, 0) == pdTRUE); // FIXME, instead of failing for full queue, delete the oldest mssages assert(toPhoneQueue.enqueue(mp, 0)); // FIXME, instead of failing for full queue, delete the oldest mssages
if (mp->payload.want_response) if (mp->payload.want_response)
sendNetworkPing(mp->from); sendNetworkPing(mp->from);

Wyświetl plik

@ -1,23 +1,25 @@
#include "PeriodicTask.h" #include "PeriodicTask.h"
#include "Periodic.h" #include "Periodic.h"
PeriodicTask::PeriodicTask(uint32_t initialPeriod) : period(initialPeriod) PeriodicTask::PeriodicTask(uint32_t initialPeriod) : period(initialPeriod) {}
{
}
/// call this from loop /// call this from loop
void PeriodicTask::loop() void PeriodicTask::loop()
{ {
uint32_t now = millis();
if (period && (now - lastMsec) >= period)
{ {
meshtastic::LockGuard lg(&lock);
uint32_t now = millis();
if (!period || (now - lastMsec) < period) {
return;
}
lastMsec = now; lastMsec = now;
doTask();
} }
// Release the lock in case the task wants to change the period.
doTask();
} }
void Periodic::doTask() void Periodic::doTask()
{ {
uint32_t p = callback(); uint32_t p = callback();
setPeriod(p); setPeriod(p);
} }

Wyświetl plik

@ -1,11 +1,12 @@
#pragma once #pragma once
#include <Arduino.h> #include <cstdint>
#include "configuration.h"
#include "lock.h"
/** /**
* A base class for tasks that want their doTask() method invoked periodically * A base class for tasks that want their doTask() method invoked periodically
* *
* FIXME: currently just syntatic sugar for polling in loop (you must call .loop), but eventually * FIXME: currently just syntatic sugar for polling in loop (you must call .loop), but eventually
* generalize with the freertos scheduler so we can save lots of power by having everything either in * generalize with the freertos scheduler so we can save lots of power by having everything either in
* something like this or triggered off of an irq. * something like this or triggered off of an irq.
@ -15,9 +16,10 @@ class PeriodicTask
uint32_t lastMsec = 0; uint32_t lastMsec = 0;
uint32_t period = 1; // call soon after creation uint32_t period = 1; // call soon after creation
public: // Protects the above variables.
uint32_t periodMsec; meshtastic::Lock lock;
public:
virtual ~PeriodicTask() {} virtual ~PeriodicTask() {}
PeriodicTask(uint32_t initialPeriod = 1); PeriodicTask(uint32_t initialPeriod = 1);
@ -26,8 +28,12 @@ public:
virtual void loop(); virtual void loop();
/// Set a new period in msecs (can be called from doTask or elsewhere and the scheduler will cope) /// Set a new period in msecs (can be called from doTask or elsewhere and the scheduler will cope)
void setPeriod(uint32_t p) { period = p; } void setPeriod(uint32_t p)
{
meshtastic::LockGuard lg(&lock);
period = p;
}
protected: protected:
virtual void doTask() = 0; virtual void doTask() = 0;
}; };

Wyświetl plik

@ -18,7 +18,7 @@ public:
{ {
T *p; T *p;
return this->dequeue(&p, maxWait) == pdTRUE ? p : NULL; return this->dequeue(&p, maxWait) ? p : nullptr;
} }
// returns a ptr or null if the queue was empty // returns a ptr or null if the queue was empty
@ -26,6 +26,6 @@ public:
{ {
T *p; T *p;
return this->dequeueFromISR(&p, higherPriWoken) == pdTRUE ? p : NULL; return this->dequeueFromISR(&p, higherPriWoken) ? p : nullptr;
} }
}; };

Wyświetl plik

@ -1,18 +1,22 @@
#pragma once #pragma once
#include <Arduino.h> #include <cassert>
#include <assert.h> #include <type_traits>
#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>
/** /**
* A wrapper for freertos queues. Note: each element object must be quite small, so T should be only * A wrapper for freertos queues. Note: each element object should be small
* pointer types or ints * and POD (Plain Old Data type) as elements are memcpied by value.
*/ */
template <class T> template <class T>
class TypedQueue class TypedQueue
{ {
static_assert(std::is_pod<T>::value, "T must be pod");
QueueHandle_t h; QueueHandle_t h;
public: public:
TypedQueue(int maxElements) TypedQueue(int maxElements)
{ {
h = xQueueCreate(maxElements, sizeof(T)); h = xQueueCreate(maxElements, sizeof(T));
@ -34,24 +38,22 @@ public:
return uxQueueMessagesWaiting(h) == 0; return uxQueueMessagesWaiting(h) == 0;
} }
// pdTRUE for success else failure bool enqueue(T x, TickType_t maxWait = portMAX_DELAY)
BaseType_t enqueue(T x, TickType_t maxWait = portMAX_DELAY)
{ {
return xQueueSendToBack(h, &x, maxWait); return xQueueSendToBack(h, &x, maxWait) == pdTRUE;
} }
BaseType_t enqueueFromISR(T x, BaseType_t *higherPriWoken) bool enqueueFromISR(T x, BaseType_t *higherPriWoken)
{ {
return xQueueSendToBackFromISR(h, &x, higherPriWoken); return xQueueSendToBackFromISR(h, &x, higherPriWoken) == pdTRUE;
} }
// pdTRUE for success else failure bool dequeue(T *p, TickType_t maxWait = portMAX_DELAY)
BaseType_t dequeue(T *p, TickType_t maxWait = portMAX_DELAY)
{ {
return xQueueReceive(h, p, maxWait); return xQueueReceive(h, p, maxWait) == pdTRUE;
} }
BaseType_t dequeueFromISR(T *p, BaseType_t *higherPriWoken) bool dequeueFromISR(T *p, BaseType_t *higherPriWoken)
{ {
return xQueueReceiveFromISR(h, p, higherPriWoken); return xQueueReceiveFromISR(h, p, higherPriWoken);
} }