Arduboy Classes and Header files?

Unrelated question, What does ! do in if statements?
ex: if (!arduboy.nextFrame()) {return;}

! is the ‘not’ operator (formally ‘logical negation’), it inverts a bool to be its opposite.
I.e. !false == true and !true == false

So an alternative would be to do:

void setup(void)
{
  if(arduboy.nextFrame())
  {
    // Do stuff
  }
}

But then the whole function would be inside the brackets.
It’s easier to just invert the logic and exit early.
(And easier to read since you don’t have to track the brace to find out when the if ends.)

Another question:
Could I structure this: player = (player == Cell::Nought) ? Cell::Cross : Cell::Nought;
But inside an arduboy.print() function and instead it checks for the winner and returns a string. Think of a shorter version of this

if (winner == Cell::Cross) {
  arduboy.print(F("Crosses"));
  arduboy.setCursor(5, 45);
  arduboy.print(F("Wins!"));
} else if (winner == Cell::Nought) {
  arduboy.print(F("Noughts"));
  arduboy.setCursor(5, 45);
  arduboy.print(F("Wins!"));
} else {
  arduboy.print(F("Cat's"));
  arduboy.setCursor(5, 45);
  arduboy.print(F("Game!"));
}

Depends why you want to make it shorter.
Are you aiming to save progmem or make it more readable?

If you’re trying to make it more readable then that’s probably the best you can do (either that or use a switch).

If you’re trying to make it use less progmem then there’s something you might be able to do but it’s not as pretty.

cant i do something like this?
arduboy.print((winner == Cell::Cross) ? "Crosses" /*elif thing?*/ (winner == Cell::Nought) : "Noughts" : "Draw!")

If you wrap the strings in F then yes, but it’s not as easy to read and not guaranteed to save progmem.

arduboy.print((winner == Cell::Cross) ? F("Crosses") : (winner == Cell::Nought) ? F("Noughts") : F("Draw!"));

Well when its turned into hex, having three functions and a shortened if should be cheaper than than 9 functions and 3 ifs

Not necessarily, it might result in the exact same compiled code.
It depends on what optimisations the compiler decides to use.

Doing

arduboy.print((winner == Cell::Empty) ? F("Game!") : F("Wins!"));

Might result in a saving though since the compiler doesn’t usually attempt to check for duplicate strings.

Saving progmem is very much about getting into the compiler’s “head” so to speak - knowing which optimisations it is and isn’t allowed to do and spotting the ones that you can do manually that it can’t do either because it doesn’t know how (which in GCC’s case isn’t many) or isn’t legally allowed to do because it changes the program’s semantics (these are the ones humans are best at doing).

I released a new version, I ended up shaving 500 bytes of progmem off and adding a hex.

I double checked the sizes.
Using the old system results in 11452 bytes of progmem:

if (winner == Cell::Cross) {
  arduboy.print(F("Crosses"));
  arduboy.setCursor(5, 45);
  arduboy.print(F("Wins!"));
} else if (winner == Cell::Nought) {
  arduboy.print(F("Noughts"));
  arduboy.setCursor(5, 45);
  arduboy.print(F("Wins!"));
} else {
  arduboy.print(F("Cat's"));
  arduboy.setCursor(5, 45);
  arduboy.print(F("Game!"));
}

While using the current system results in 11412 bytes of progmem:

arduboy.print((winner == Cell::Cross) ? F("Crosses") : (winner == Cell::Nought) ? F("Noughts") : F("Draw!"));
arduboy.setCursor(5, 45);
arduboy.print((winner == Cell::Empty) ? F(" ") : F("Wins!"));

However this results in 11410 bytes of progmem:

if (winner == Cell::Cross)
  arduboy.print(F("Crosses"));
else if (winner == Cell::Nought)
  arduboy.print(F("Noughts"));
else
  arduboy.print(F("Cat's"));

arduboy.setCursor(5, 45);
arduboy.print((winner == Cell::Empty) ? F(" ") : F("Wins!"));

Which is what I mean when I say it’s sometimes hard to judge.
Also it seems you left out the “Game” part of “Cat’s Game”, was that intentional?

If it wasn’t then you should add it back in (which will increase progmem by a few bytes).
If it was then you can save 10 more bytes by turning

arduboy.print((winner == Cell::Empty) ? F(" ") : F("Wins!"));

into:

if(winner != Cell::Empty)
{
	arduboy.setCursor(5, 45);
	arduboy.print(F("Wins!"));
}

You can avoid the brackets and make it somewhat more readable:
Instead of

if (!arduboy.nextFrame()) {return;}

use

if (arduboy.nextFrame() == false) {return;}

However, personally I read the first as “If not time for the next frame, return”, so it’s more readable to me than the second form.

Readability is in the eye of the beholder.
The majority of programmers would argue that == false is less readable than !.
Examples here and here.

Yes. I added a “However…” while you were typing your post.

1 Like

If we’re mentioning alternatives for the sake of it, then it makes sense to mention that not is also a viable alternative since it’s one of the alternative operators.
I.e.

if(not arduboy.nextFrame()) { return; }

Is also valid.

Though both == false and not are rarely used.

1 Like

I like how the other method look more than the quoted and the two bytes gained seem a bit meaningless.

1 Like

Yeah, sometimes you get cases like that where savings are unexpected. When looking for savings it pays to try several different approaches before settling on one.

Ive shared my boilerplate code for arduboy games (at least for me) along with my next project. Take a look if you want.

Also, unrelated question, but how to other people have different color arduboys (with the circuit board green/red/blue)?

The colour units were available from the Kickstarter.

Other people have modified their Arduboys by doing stuff like colouring the circuit board with a pen (eried) or lining the case with a sweet wrapper (KeyboardCamper).

(And then obviously the new special edition units are black with gold buttons.)


I also have boilerplate that’s been steadily evolving,
but I’ve only got round to releasing one game because I keep trying to do too many things at once.

A Sharpie seems…risky to say the least, but possible, Im just afraid i will end up with either a bad pen job or a dead arduboy.

Another question.
Say I have this spritesheet (Its very simple).

flappePlayer

How would I draw a specific sprite out of this sheet with the background of the sprite filled? (I remember seeing some function that did this but i cant remember it)