1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2025-12-13 05:53:06 +01:00

add new function getBoatDataValues to api for easier access

This commit is contained in:
wellenvogel
2021-12-03 17:00:51 +01:00
parent b4b08fa12d
commit 04d90447e8
7 changed files with 106 additions and 7 deletions

View File

@@ -3,6 +3,7 @@
#ifdef BOARD_TEST
#include "GwExampleTask.h"
#include "GwApi.h"
#include <vector>
/**
* an init function that ist being called before other initializations from the core
@@ -48,7 +49,11 @@ void exampleTask(GwApi *api){
//initialization goes here
//------
bool hasPosition=false;
bool hasPosition2=false;
LOG_DEBUG(GwLog::DEBUG,"example switch ist %s",exampleSwitch?"true":"false");
GwApi::StringList itemNames;
itemNames.push_back(F("Latitude"));
itemNames.push_back(F("Longitude"));
while(true){
delay(1000);
/*
@@ -89,6 +94,42 @@ void exampleTask(GwApi *api){
}
r->unref(); //delete the request
/** second example with string based functions to access boatData
uses STL vector and map - but does not need any request handling
this uses a reasonable amount of mmeory on the stack:
up to 2x the size of the list of names and 2x the size of the returned map
the default stack size of 2000 will not fit.
So be sure to use DECLARA_USERTASK_PARAM(taskFuntion,stackSize) and provide
a reasonable stack size (e.g. 4000 in this example).
Finally it only makes sense to use one of the versions - either with the request
or with the ValueMap approach.
The request access to boatData gives you more options on how to access the data
and ueses less ressources and runtime but this one is maybe easier to understand and implement
**/
//fetch the current values of the items that we have in itemNames
GwApi::ValueMap boatItems=api->getBoatDataValues(itemNames);
//get the values out of the map
//the returned item is a map iterator
auto longitude=boatItems.find(itemNames[1]);
auto latitude=boatItems.find(itemNames[0]);
//check if the iterators are valid (i.e. the values we requested have been found in boatData)
if (longitude != boatItems.end() && latitude != boatItems.end()){
//both values are there - so we have a valid position
if (! hasPosition2){
//access to the values via iterator->second (iterator->first would be the name)
if (exampleSwitch) LOG_DEBUG(GwLog::LOG,"(2)position availale lat=%f, lon=%f",
latitude->second,longitude->second);
hasPosition2=true;
}
}
else{
if (hasPosition2){
if (exampleSwitch) LOG_DEBUG(GwLog::LOG,"(2)position lost");
hasPosition2=false;
}
}
}
vTaskDelete(NULL);

View File

@@ -27,10 +27,11 @@ void exampleTask(GwApi *param);
void exampleInit(GwApi *param);
//make the task known to the core
//the task function should not return (unless you delete the task - see example code)
DECLARE_USERTASK(exampleTask)
//if your task needs more stack then the default 2000 bytes, replace the DECLARE_USERTASK
//with: DECLARE_USERTASK_PARAM(exampleTask,2500);
//this will give you 2500 bytes of stack
//we create our task with a stack size of 4000 bytes
DECLARE_USERTASK_PARAM(exampleTask,4000)
//if your task is happy with the default 2000 bytes of stack, replace the DECLARE_USERTASK_PARAM
//with: DECLARE_USERTASK(exampleTask);
//let the core call an init function before the
//N2K Stuff and the communication is set up