Arduboy Classes and Header files?


(Scott) #32

Nope. Only if you don’t give it an extension. If you name a file something.cpp or something.h the IDE won’t add .ino on the end.


(Pharap) #33

Woops, my mistake.

In that case I have no clue.


I’m sure other subdir names work.
Otherwise that’s a really weird feature.
(Then again, so is -fpermissive. If you can call it a ‘feature’.)


(Simon) #34

Yes … it appears to have been fixed … I have updated my original post.


(Pharap) #35

Ah, that makes sense.


I’m sure the “add .ino to end of files” thing also used to be an issue. Sadly there’s a lot of issues to fish through.

This comment seems to suggest it used to be an issue:

Likewise a comment here mentions it:

https://forum.arduino.cc/index.php?PHPSESSID=n6fs080c4a08osbf8kqhaan5f2&topic=421205.msg2901602#msg2901602


(Alex) #36

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

TicTacToe
|
|
TicTacToe.ino
|
|
src - - |
          |
          sprites.h
          sprites.cpp
          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 ¯\_(ツ)_/¯


(Scott) #37

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).


(Alex) #38

Thanks for the tip! Real useful.


(Alex) #39

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'

(Alex) #40

Pharap can you help with my earlier post?


(Pharap) #41

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.


(Alex) #42

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"


(Pharap) #43

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).


(Alex) #44

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

“YOU CANT DECLARE VARIABLES IN A HEADER, GET OFF MY COMPILER!”

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


(Pharap) #45

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.


(Alex) #46

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?


(Pharap) #47

If in doubt, check the source or the docs.

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


(Alex) #48

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.


(Pharap) #49

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.


(Alex) #50

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


(Alex) #51

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.