Store values in library

I've made a library in the past and want to make another more advanced one. I was thinking a tick tack toe library would be neat where the user would make a call Serial.print(tick.value(1)); which the one would accept numbers 1 to 9 which would save the value in the library and the library would respond back the X | 0 | 0 format of tic tac toe with the move you made and the move that it countered.

I haven't been able to find a library that stored values though, the ones i've seen all pass them through agruments. I remember from a c tutorial on youtube by Bucky that programmers usually use private and public methods where private saves the state of the public.

Would I use a static int for the numbers that I want saved in the library?

In the future if I use statics is there a way to release them when no longer needed so that you can store them for a while but can recycle them if needed?

By "library", do you mean "class", perhaps?

A class can have member variables, which can store values at runtime. All of the libraries classes that I've seen have member variables.

If you create a class and have a 'public' member variable, it's accessible in your code.

ie

myClass.value = 123;

if(myClass.value == 123)
   // Do something

Perhaps I'm misunderstanding you.

I’ll give an example of what I am thinking. Below is a CPP file for the library rectangle. I don’t remember where I found this library.

#include "Arduino.h"
#include "rectangle_t.h"
unsigned long SERIAL_BAUD_RATE    = 9600UL;
rectangle_t::rectangle_t(unsigned int const width, unsigned int const height)
: _width(width)
        , _height(height)
    {   }
int rectangle_t::area() const            { return _width * _height; }

void rectangle_t::setWidth(int width)    { _width  = width;  }
void rectangle_t::setHeight(int height)  { _height = height; }

and below this is the H file

#ifndef rectangle_t_h
#define rectangle_t_h


class rectangle_t
{
    unsigned int    _width;
    unsigned int    _height;
 
public:
    // constructor using initializer list, see:
    // <https://isocpp.org/wiki/faq/ctors#init-lists>

    rectangle_t(unsigned int const width, unsigned int const height);

    int area() const;

    void setWidth(int width);
    void setHeight(int height);

};

#endif

So in this library you make a call to rectangle_t(8,7); and it returns 56. What I want to do is made a fuction like rectangel.setWidth(8); and it stores the width in the library, this way a few minutes later you could enter rectangel.setHeight(9); and it will store the value of height. Then a few minutes later you could call rectangel.findAreaOfInfromationStored() and it will return the information you are requesting, without having to declare all a bunch of ints in the part about void setup each time you want to use the library.

Also, if possible, which I am not sure if it is, I was thinking it would also be nice if I call rectangel.clearmemory() which ideally the space of the ints that it stored to be cleared so something else can use them.

Thomas, it seems like you understand the concept of classes pretty well. (Unless that's an example you copied from a textbook without understanding.)

What is stopping you from doing what you originally planned?

The example of rectangle.clearMemory() is a little contrived. For a small object like rectangle, you would create and destroy the whole object if you wanted to recover the memory. For example, make it a local variable in a function then it will be removed from memory (and all values lost) after the function ends. For a bigger class that needs to manage its own memory - maybe it needs to store a buffer with a lot of data - then malloc() and free() can be used inside the class. A better idea would be to have the variable-size memory composed of classes too and then the main class can create and destroy the data elements as it needs.

@MorganS, that was a copied/borrowed example.

without having to declare all a bunch of ints in the part about void setup each time you want to use the library.

I hope you don't mean that you want to store runtime variables in the library itself, for use in the future?

What I want to do is made a fuction like rectangel.setWidth(8); and it stores the width in the library, this way a few minutes later you could enter rectangel.setHeight(9); and it will store the value of height. Then a few minutes later you could call rectangel.findAreaOfInfromationStored() and it will return the information you are requesting

If I understand you correctly, this is already done in that Rectangle class. (Not a 'library', but a class. A 'library' is a file or group of files.) You call 'setWidth()' and the class stores the width. You call 'setHeight()' and the class stores the height. You call 'area()' and it returns the calculated area.

So you create a rectangle_t object:-rectangle_t myRect(12,24); Then you can change the width and/or height:-

myRect.setWidth(10);
myRect.setHeight(20);

Then if you want to know the calculated area:-int myRectArea = myRect.area(); You could also, (if you were writing or modifying that class), create public member functions that could access the class's private '_width' and '_height' member variables. You'd use them like this:-

int myRectWidth = myRect.getWidth();
int myRectHeight = myRect.getHeight();

Is this what you mean? If not, you'll need to explain what you want a bit more clearly.

Especially these from your opening post:-

Serial.print(tick.value(1)); which the one would accept numbers 1 to 9 which would save the value in the library

So you want to call the 'tick' class's 'value()' function with a value from 1 to 9. I understand that bit.

the library would respond back the X | 0 | 0 format of tic tac toe with the move you made and the move that it countered

Can you give an example of exactly what the 'value()' function needs to return?

Thomas, it seems like you understand the concept of classes pretty well. (Unless that’s an example you copied from a textbook without understanding.)

Totally copied the library from somewhere. I think it may have been from buckys tutorial, but it may have been posted on the form. Actually, I think it was on the arduino.cc how to make a library page.

I hope you don’t mean that you want to store runtime variables in the library itself, for use in the future?

I think that may be what I’m thinking of doing. Otherwise how would I be able to call the code in a different void section? I don’t want to have to declare the variables as globals to do this, I’d rather the library do that automatically. Or at least that was what I was thinking. Maybe that is a bad idea for some reason. If so please explain why.

Is this what you mean? If not, you’ll need to explain what you want a bit more clearly.

this is more along the lines of what I was thinking

#include <rectangle_t.h>

void setup() {
  // initialize digital pin 13 as an output.
 Serial.begin(9600);
}

// the loop function runs over and over again forever
void loop() {
  StoreRectData();
  FiveMinutesLaterInDifferentLoopReadDataStored();
}
void StoreRectData()
{ 
  rectangle_t myRect(12,24);
}
void  FiveMinutesLaterInDifferentLoopReadDataStored()
{
  Serial.println(myRect.area());
}

So you want to call the ‘tick’ class’s ‘value()’ function with a value from 1 to 9. I understand that bit.

correct. Tic tack toe has 9 places you can put an x. the 1 to 9 identifies where you are putting the next x. Then that information goes into the class and is stored there with the other information that was stored previously and figures out where to put it’s 0 for its turn.

Can you give an example of exactly what the ‘value()’ function needs to return?

Sure. three lines of something like this 1 X | X | X /* the | represents the lines drawn in tic tax toe the X represents your moves, the 0 represents the moves decided by the class and if neither X or O is occupying the space then the space is available for the either the player or the class to use next turn*/

line 1 |X|0 //1|2 |3 // player responses with one of these numbers
line 2 ||
//4|5 |6
line 3 ||X //7|8|9

calls would be made like this

void loop(){
players turn(); // player chooses which space, info is stored in library to be retrieved later
player 2 turn();
}

void players turn(){
// chooseX(1); // data is stored in library
// if entered, print chart
}
void player 2 turn(){
// choose0(2) // data is stored in library
//if entered, print chart containg data stored from players turn() and player2 turn()

If you want your object to be global, that is accessible from inside many functions, then you need to declare the instance in global scope, that is outside any function, conventionally at the top of the sketch.

Serial is a good example. It is declared globally, in the supporting Arduino code that you don't normally see. Then the setup() function can set the baud rate and every other function that calls Serial uses that same object with the same setting already stored.

So are library H files delared in glabal scope?

After you mentioned Serial.begin() which is a good example, I found the Serial reference in C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino Below is the snipplet of code that I found relevant to Serial.

#include "HardwareSerial.h"

uint16_t makeWord(uint16_t w);
uint16_t makeWord(byte h, byte l);

#define word(...) makeWord(__VA_ARGS__)

unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);

void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
void noTone(uint8_t _pin);

// WMath prototypes
long random(long);
long random(long, long);
void randomSeed(unsigned int);
long map(long, long, long, long, long);

#endif

#include "pins_arduino.h"

#endif

So can I declare a global variable in the H file of the library that will be stored automatically within the global scoop when I use the library?

You can #include a .h file anywhere but it is extremely rare to put one inside a single function's scope. So yes, in practise, a .h file will create global-scope identifiers.