Arduboy Classes and Header files?

Alright I have my final solution, We have the sketch folder and a src subfolder with PROPER .cpp and .h files.

src - - |
          public.h // Standard for all my projects, store some public vars and stuff to avoid globals

Also looks like I cant open .cpp or .h files through the IDE, sublime it is then ¯\_(ツ)_/¯

I would keep all files in the same folder. I makes it easier for people to install it the IDE Sketchbook. People not used to programming or the Arduino environment struggle with this. It’s best to keep it as simple as possible.

You can if you keep them in one folder, along with the .ino file(s).

Thanks for the tip! Real useful.

im getting an issue where the compiler is throwing an error because of a syntax error does not exist in my public.h file and that one of the variables is already referenced? Am I not supposed to use #include on every file to import public.h?

sketch\TicTacToe.ino.cpp.o (symbol from plugin): In function `arduboy':

(.text+0x0): multiple definition of `arduPressed'

Pharap can you help with my earlier post?

As MLXXXp mentioned, that’s because the IDE has some batty restriction where it won’t open files that are in folders, it will only open files that are on the same level as the .ino.

Go for it. Most people only use the IDE for compiling and tend to use an external editor.
(If you want to know what other people are/have been using, see this thread.)

You can tell the IDE that you’re using an external editor by going File > Preferences > Settings Tab > Use external editor (checkbox)

That’s a linker error.
Most likely you’ve ended up with two definitions of the same function, either because you’ve written two definitions or you’ve got a function that isn’t marked inline that should be marked inline (probably arduPressed).

I can’t tell the exact problem without seeing the code.

Here is the code for Public.h
// Public.h

#pragma once

const int fps = 60;
int arduPressed[] = {0, 0, 0, 0, 0, 0};

I import it throught sprites.cpp and TicTacToe.ino using this

#include "Public.h"

Ah, I know what the problem is.

You can’t define variables in a header.
You can ‘declare’ them (in which case they must be marked extern and then defined elsewhere), but you can’t define them.

In your case you can put extern int arduPressed[]; in Public.h and then put int arduPressed[] = {}; in Public.cpp.

It’s generally better to avoid global variables if you can though, or at least only use them in your main .ino.

Referencing global variables is considered bad style because it’s an opportunity for bugs to creep in, but there’s actually a memory reason for it: accessing globals increases code size (partly because any changes to global variables means the variable has to be immediately updated because there’s no way of knowing what else might try to access it).

I’m guessing from the name you’re trying to track which buttons have been pressed and which haven’t, so you know when a button has only just changed?
In which case you needn’t bother since Arduboy2 does that for you with the justPressed function (you do have to remember to call pollButtons though).

1 Like

God everything is so convoluted.
C++ is like the cranky old man of programming languages


oh, I dont have to use a list anymore with ard2? How would I do that exactly?

It’s because it’s from a time when computer resources were incredibly limited (1985) and was inspired by a language from an even earlier time (1972).

(The history of programming languages and computers is long and complicated. Without the earlier languages, things like Python, Java and C# would be a lot different from how they currently are.)

It’s far too late to undo existing rules, so all they can do is add new features.

Call pollButtons() after the usual if(!arduboy.nextFrame()) return; line, and then you’re free to use justPressed as you would pressed and it only returns true on the first time that the button is pressed until the button is released again.

I was just being funny. I know the reason it is the way it is.

Can I PollButtons in a update function of a class or do I need to pass it as an arg?

If in doubt, check the source or the docs.

It’s void pollButtons(); and bool justPressed(uint8_t button);.

For some reason the Public.h file just broke when I downloaded it from the github repo I set up and added a selector class

Here is my code:

// Public.h
// Pygasm 2018

#pragma once

extern const int fps;
extern int counter;

// Public.cpp
// Pygasm 2018

#include "Public.h"

const int fps = 60;
int counter = 0;

It raises this error after I set up the selector class

sketch\Public.cpp:7:12: warning: 'counter' initialized and declared 'extern'

 extern int counter = 0;


In file included from sketch\Sprites.cpp:6:0:

Public.h:6: error: two or more data types in declaration of 'fps'

 extern const int fps;


exit status 1
two or more data types in declaration of 'fps'

and removing the const fixed it somehow. I GUESS.

Im just going to leave the public.h file empty and remove it if I dont need it in the end.

Strange, I can’t seem to reproduce it by copying that code.

Are you sure you didn’t have extern on the front of counter in Public.cpp?
It’s also possible the IDE got desynced if you were using an external editor and didn’t have the external editor flag set,
I’ve had that happen a few times before.

To be honest if you can find a way to not use globals, that would save you the hassle.

I usually create a Game class with a setup and loop function, create a global instance of that, forward the regular setup and loop to the member functions of Game and shove all my would be globals into Game as member variables.
Then the globals are either made public, given accessors, or passed to the functions that need them.

It makes the class slightly monolithic,
but I find it somewhat better than having to deal with global variables.

Yeah, i was thinking that I should take the approach that hollow seeker and my python projects did

I removed it and tried working on getting the selector working but there were too many issues with the weirdness of C++ (C++ hates tuples, simple index search, and “non-class objects”). I dunno what to do now accept retry later.

C++ doesn’t have tuples…

Well, technically the C++ stdlib does and in a newer version of C++ (It’s either C++17 or C++20, not sure which) they’ll be adding tuple unpacking, but the Arduino compiler is limited to C++11 and doesn’t have the C++ stdlib, mostly due to memory constraints.

Not sure what you mean by ‘index search’, but if you mean just looping through an array until you find a matching item, that works fine.

I’m not sure what you mean by ‘non class objects’.

It appears to me that it does. At least, I don’t get an error when I add:

#include <stdlib.h>

Was that supposed to be a joke?

No, why? What did you mean?