Register / Login | Desktop view |
#include <Arduino.h>
#include <REPTestOS.h>
REPTestOS OS; //create now Operating System object
void setup()
{
OS.bootOS();
}
void loop()
{
OS.runOS();
}
#include "REPTestOS.h"
#include "Util.h"
//global variable definitions
Util util;
PnumaControl pnuma1(9, 10);
REPTestOS::REPTestOS(){}; //constructor
REPTestOS::~REPTestOS(){}; //destructor
void REPTestOS::bootOS() //boot operating system function. To run in startup
{
Wire.begin(); //Start wire
setupMenu(); //Initialize menu system
Serial.begin(9600); // initialize serial communication at 9600 bits per second:
Serial.println("Booting"); //small serial indicator during boot phase
//Start remaining items
}
void REPTestOS::runOS() //constantly runs in loop
{
taskManager.runLoop(); //handler for menu system task management
}
/*----------------------------------------------------------
------------------------------------------------------------
----------------- Menu System Handlers ---------------------
------------------------------------------------------------
----------------------------------------------------------*/
void CALLBACK_FUNCTION cycleFrequency(int id)
{
pnuma1.cyclesPerSecond = int(menuTestSettingsCyclesPerSecond.getAsFloatingPointValue()); //update pnuma1 with value
}
void CALLBACK_FUNCTION pullControl(int id)
{
pnuma1.setMode(menuTestSettingsPush.getBoolean(), menuTestSettingsPull.getBoolean()); //update pnuma1 with mode
}
void CALLBACK_FUNCTION pushControl(int id)
{
pnuma1.setMode(menuTestSettingsPush.getBoolean(), menuTestSettingsPull.getBoolean()); //update pnuma1 with mode
}
void CALLBACK_FUNCTION stopTest(int id)
{
pnuma1.running = false;
}
void CALLBACK_FUNCTION startTest(int id)
{
pnuma1.running = true;
}
PnumaControl::PnumaControl(int pushIn, int pullIn)
{
pushPin = pushIn; //constructor requires push and pull actuator pin #'s, set to private
pullPin = pullIn;
}
PnumaControl::~PnumaControl()
{
}
void PnumaControl::setup()
{
pinMode(pushPin, OUTPUT);
pinMode(pullPin, OUTPUT);
digitalWrite(pushPin, LOW);
digitalWrite(pullPin, LOW);
}
void PnumaControl::control()
{
if (running)
{
baseClock = millis(); //update clock
unsigned long timeDelta = baseClock - lastActuation; //find time difference between last actuation
int freq = 1000 / cyclesPerSecond; //compare to desired frequency
if (timeDelta > freq)
{
setNextCycle(); //update state based off of mode
actuate(); //fire updated actuation state
lastActuation = millis(); //reset timer
}
}
};
void CALLBACK_FUNCTION cycleFrequency(int id)
{
auto cycles = int(menuTestSettingsCyclesPerSecond.getAsFloatingPointValue()); //update pnuma1 with value
pnuma1.onCycleFrequencyChanged(cycles);
}
If I define a global instance of PnumaControl, within REPTestOS, I have access to it within the callback functions which I've moved to REPTestOS
class Abc {
private:
int a;
int b;
int c;
public:
void onAChanged(int val) {
a = val
}
};
// forward declare Abc here, so it can be used anywhere
extern Abc myGlobalAbc;
// declare it global here
Abc myGlobalAbc;
#include <abc.h>
// use myGlobalAbc here
davetcc wrote:Instead of your control method that currently needs to do the timing loop, why not create a taskManager event or task that does this instead. TcMenu is built on top of taskManager, so it's there for you to use by default.
davetcc wrote:If I understand this correctly, you want to access your global instance in different places, this is a bit beyond to scope of this forum to be honest, it's more a C++ question. But hey let's give it a go here:
If I had class Abc that I wanted to access in various places.
Abc.h
class Abc { private: int a; int b; int c; public: void onAChanged(int val) { a = val } }; // forward declare Abc here, so it can be used anywhere extern Abc myGlobalAbc;
abc.cpp
// declare it global here Abc myGlobalAbc;
main.cpp
#include <abc.h> // use myGlobalAbc here
davetcc wrote:Also, are you confusing classes with libraries, or are you putting each class in it's own Arduino / PIO library?
davetcc wrote:At the moment you can't move the callbacks out of main when using the designer, it will just put them back into the main file again on the next pass, thinking that they are missing. Has tcMenu designer detected your main class with that name?
That said, as I understand it, globals are bad practice and should be avoided
davetcc wrote:Don’t forget that even the boards that sound huge has less available memory than you may expect after RTOS has started.
I did notice that all the required support tcmenu needs takes up a substantial amount of room. I'm currently at 29400bytes Flash, and 1065 Ram
I was under the impression that the compiler would look at all variables, functions and objects, and when they are called in say, the setup() function,