Hello everyone,
Recently I got the idea that I want to build a library. I want it to work as the Standard Library of a PLC programming environment, where we have commonly used functions, easily available.
Basically my goal is to replicate the PLC programming environment, so I can work in an environment that is common for me.
I created the files standard.cpp and standard.h - body and header files of my library. The compilation works just fine, and I suppose it would work if I booted it now.
However the namespace annoys me.
standard.h:
// ************************************************************
// Header
// ************************************************************
#ifndef standard_h
#define standard_h
#include "Arduino.h"
// ************************************************************
// Standard class
// ************************************************************
class Standard {
private:
public:
Standard();
void TON(bool IN, unsigned long PT, unsigned long ET, bool Q);
};
#endif
// ************************************************************
// ************************************************************
standard.cpp:
/*
INFORMATION:
Author: Christian Ritmann
Date created: 12th Oct. 2019
Last edited: ---
Location: Denmark
DESCRIPTION:
Makes a timer that does not interrupt program execution.
Upon bool true input on "IN", start a timer. Upon counting
to the value of the input time "PT" milliseconds, return
true on Q. Else return false. Output will stay true until input
"IN" is false. Resetting IN input before timer is done
will reset accumulated time. Outputs Elapsed Time on ET when
input is active.
This program is made as an attempt to replicate PLC TON-
function block (IEC 61131-3 library).
CHANGELOG:
12th Oct. 2019:
- Class created
*/
// ************************************************************
// Include libraries
// ************************************************************
#include "Arduino.h"
#include "standard.h"
// ************************************************************
// Standard class main file
// ************************************************************
// Constructor
Standard::Standard() {}
// Time On Delay
void Standard::TON(bool IN, unsigned long PT, unsigned long ET, bool Q) {
static unsigned long TimePrevious;
static bool StartFlag;
// STORE SYSTEM TIME ON INPUT
if(IN and !StartFlag) {
TimePrevious = millis();
StartFlag = true;
}
// RESET START FLAG, RETURN FALSE
if(!IN) {
StartFlag = false;
Q = false;
}
// CHECK IF ASSIGNED TIME HAS PASSED, ASSIGN RETURN VALUE TRUE
if(millis() - TimePrevious >= PT and IN) {
Q = true;
}
// RETURN ELAPSED TIME
if(IN) {
ET = millis() - TimePrevious;
}
// RETURN 0 IF INPUT IS FALSE
else {
ET = 0;
}
}
// ************************************************************
// ************************************************************
To call the Timer On Delay - TON() - I now have to define an instance of Standard, and call it by "Standard_Instance.TON()". Now the problem is:
My plan is to expand the standard library to maybe 20-30 functions. But I don't want to instantiate all 20-30 functions every time I need say a timer or a counter. I would like to be able to instantiate the timer function only, even if I need multiple instances at once.
Is there a solution to this problem?
Best regards
Christian
*EDIT
I see I got a little carried away with the post, and it ended up not really matching the title.
But what is described is what I mean. I just found it a little difficult to phrase since English isn't my first language, and programming language definetely isn't my first language
I edited the title to match the topic.