4

I'm working on a project with a Digispark ATTiny85, that performs keystrokes using the DigisparkKeyboard library (https://github.com/digistump/DigisparkArduinoIntegration/tree/master/libraries/DigisparkKeyboard). Besides the DigiKeyboard, I also import EEPROM.h.

My own code is only about 150 lines, I've cut down the size following a number of online guides, trying to avoid larger data types and so on. The Digispark is limited to ~6kB and the imported DigiKeyboard library takes up a little more that 5.4kB. In total the compiled project takes up about 7kB, which exceeds the boards capacity.

From the DigiKeyboard library I only use DigiKeyboard.sendKeyStroke() and DigiKeyboard.print() (with about only ten different characters). So I assume there is a lot of unused code I could remove, but I don't know how to approach this. I use Visual Studio Code and PlatformIO.

How could I reduce the size of the used DigisparkKeyboard library?

emma.makes
  • 105
  • 5
  • 1
    Are you printing numbers or only fixed messages? In the later case, does it help if you use `write()` instead of `print()`? – Edgar Bonet Jul 03 '22 at 09:51
  • Also, I have no experience with PlatformIO, but I guess you should be able to enable a “verbose” mode that shows you the full command used to call the compiler. Could you please check whether this command contains the option `-flto`? – Edgar Bonet Jul 03 '22 at 10:02
  • Thanks @EdgarBonet! The compiler flag `-O3 -flto` didn't change a byte for me and I couldn't find any documentation on `write()`. However, I switched to `.sendKeyStroke` for all write operations from `.print()`, which shaved off about 3kB! and the code fits the board! If you formulate an answer, I'll gladly accept it! – emma.makes Jul 03 '22 at 14:15
  • I just noticed that the big issue here wasn't the inclusion of `print()`, but rather one single instance of print, where I did: `DigiKeyboard.print("Time: "+String(round(runtime/1000)));`. So I believe the large code was due to the inclusion of `String()`. – emma.makes Jul 03 '22 at 15:07
  • 2
    Re “_If you formulate an answer, I'll gladly accept it!_”: Well, actually you found your solution yourself! In such a case, the recommended practice it to post and accept your own answer. – Edgar Bonet Jul 03 '22 at 16:13

2 Answers2

4

With input from @EdgarBonet, I looked into the functions used and it turned out that the String() function, I used once in my code, takes up about 3kB of space in the compiled program.

I ended up removing the line with String() and kept the imported DigiKeyboard library as it is.

emma.makes
  • 105
  • 5
1

The compiler/linker will remove unused functions/methods automatically, so I doubt you can save memory by removing unused functions/methods yourself.

However, you could possibly gain by:

  • Removing the calls of check functions/methods when you know the result will be always false, true, or always the same number, string or data type in general.
  • Removing (parts of) functions/methods which are related to physical or logical functionality you never use. Like e.g. if, case statements that will never apply).
  • Prevent using an entire class (e.g. String) (see second comment of Emma Makes below).
Michel Keijzers
  • 12,759
  • 7
  • 37
  • 56
  • 2
    Thank you for the feedback! I wasn't aware that unused functions and methods aren't included anyways! I was able to shave off about 3kB by switching from `.print()` to `.sendKeyStroke`! I'll keep the other suggestions in mind for future issues! – emma.makes Jul 03 '22 at 14:16
  • 1
    I would need to edit the comment, but it's too late to do so. The reduced size was not due to the change from `print()` to `sendKeyStroke()`, rather the fact that I didn't use `String()`, which I previously used in the `print()` function call. – emma.makes Jul 05 '22 at 16:03
  • 1
    That's also indeed a good way to reduce size, to preventing using a complete class (String in your case). I will add it to my answer – Michel Keijzers Jul 05 '22 at 19:04