<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://bloggingadeadhorse.com/arduboyWiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Filmote</id>
	<title>Arduboy Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://bloggingadeadhorse.com/arduboyWiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Filmote"/>
	<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php/Special:Contributions/Filmote"/>
	<updated>2026-10-23T05:09:05Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.1</generator>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Developing_Games&amp;diff=634</id>
		<title>Developing Games</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Developing_Games&amp;diff=634"/>
		<updated>2024-08-25T03:46:43Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* Utility Libraries */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Developing games for the Arduboy is an exciting endeavor that blends creativity with technical skills. The Arduboy is a compact, credit card-sized gaming platform based on the Arduino microcontroller, making it accessible for both beginners and experienced developers. Its simplicity and portability offer a unique challenge due to its limited resources, such as a monochrome 128x64 pixel display and minimal memory. However, these constraints inspire innovation and efficient coding practices.&lt;br /&gt;
&lt;br /&gt;
Standard libraries for sound and graphics play a significant role in the development process. The Arduboy2 library provides essential functions for managing the display, handling input, and maintaining game states. It facilitates tasks such as drawing shapes, rendering text, and controlling frame rates, allowing developers to concentrate on game mechanics. For audio, the ArduboyTones library enables the addition of sound effects and music, enhancing the game&#039;s atmosphere. Using these libraries, developers can create functional and engaging games that work well within the Arduboy&#039;s hardware limitations.&lt;br /&gt;
&lt;br /&gt;
== Prerequisites for Developing Games ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Arduino Development IDEs ==&lt;br /&gt;
Arduino development environments provide a range of tools for creating, testing, and deploying projects to the Arduboy. The primary environment is the Arduino Integrated Development Environment (IDE), known for its user-friendly interface that simplifies writing, compiling, and uploading code. It supports various Arduino models and includes a library manager for easy access to numerous libraries. Additionally, Visual Studio Code (VSCode) can be used for Arduino development, offering advanced features like integrated debugging and a customizable interface. These environments cater to both beginners and advanced users, offering tools that range from straightforward to professional-grade.&lt;br /&gt;
&lt;br /&gt;
=== Visual Studio Code ===&lt;br /&gt;
&lt;br /&gt;
The [https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.vscode-arduino Arduino plugin] for Visual Studio Code is an easy way to develop and build Arduboy games with a full-featured IDE.&lt;br /&gt;
&lt;br /&gt;
1. Download and install [https://code.visualstudio.com/ Visual Studio Code].&lt;br /&gt;
&lt;br /&gt;
2. Install the &amp;quot;Arduino&amp;quot; extension developed by Microsoft.&lt;br /&gt;
&lt;br /&gt;
3. Configure the extension to use the bundled version of Arduino CLI.&lt;br /&gt;
&lt;br /&gt;
4. Add the following URL to the &amp;quot;Additional Urls&amp;quot; setting for the Arduino extension: &amp;lt;pre&amp;gt;https://raw.githubusercontent.com/MrBlinky/Arduboy-homemade-package/master/package_arduboy_homemade_index.json&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
5. Open the Arduino Board Manager and click &amp;quot;Refresh Package Indexes&amp;quot;. Install the Arduboy homemade package by Mr.Blinky.&lt;br /&gt;
&lt;br /&gt;
6. Add a file &amp;quot;.vscode/arduino.json&amp;quot; to your project to define how to build your project. For example, to build a game for the Arduboy FX, you might have:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;configuration&amp;quot;: &amp;quot;core=arduboy-core,boot=cathy3k&amp;quot;,&lt;br /&gt;
    &amp;quot;board&amp;quot;: &amp;quot;arduboy-homemade:avr:arduboy-fx&amp;quot;,&lt;br /&gt;
    &amp;quot;sketch&amp;quot;: &amp;quot;my_game.ino&amp;quot;,&lt;br /&gt;
    &amp;quot;output&amp;quot;: &amp;quot;build/&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. You can now build your project with the &amp;quot;Arduino: Verify&amp;quot; command. The command will create a &amp;quot;build&amp;quot; directory inside your project directory, and generate .hex and .elf files there.&lt;br /&gt;
&lt;br /&gt;
== Common Libraries ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An Arduboy library is a collection of pre-written code designed to simplify and standardize the development of games and applications for the Arduboy platform. These libraries provide a range of functionalities, such as handling graphics, sound, input, and other core features of the device. For example, the Arduboy2 library includes functions for drawing shapes, rendering text, managing game states, and handling user input, while the ArduboyTones library offers tools for generating sound effects and music. By utilizing these libraries, developers can streamline their coding process, focus on the creative aspects of game design, and ensure their applications run efficiently on the limited hardware of the Arduboy.&lt;br /&gt;
&lt;br /&gt;
=== Base Libraries ===&lt;br /&gt;
&lt;br /&gt;
==== [[Arduboy2]] ====&lt;br /&gt;
The basic Arduboy library, often referred to as Arduboy2, is an essential toolkit for developing games on the Arduboy platform. This library provides a comprehensive set of functions that manage the device&#039;s core features, making it easier for developers to create games and applications. It includes capabilities for drawing graphics on the 128x64 pixel monochrome display, such as drawing lines, circles, and rectangles, as well as rendering text. The library also handles input from the device&#039;s buttons, allowing developers to implement game controls efficiently. Additionally, Arduboy2 offers functions for managing frame rates and game states, ensuring smooth game play experiences. By using the Arduboy2 library, developers can focus more on the creative aspects of game development while leveraging standardized, efficient code for fundamental tasks.&lt;br /&gt;
&lt;br /&gt;
Additionally, the library provides basic [[Collision Detection]] functions.&lt;br /&gt;
&lt;br /&gt;
=== Graphic Libraries ===&lt;br /&gt;
&lt;br /&gt;
==== [[Sprites / SpritesB Library|Sprites]] / SpritesB ====&lt;br /&gt;
Shipped as part of the [[Arduboy2]] library, the Sprites library provides functions for rendering both masked and unmasked bitmaps and tilesheets. When working with masked bitmaps, the library supports various formats including standalone masks (allowing them to be used with multiple bitmaps) or interlaced within the bitmap image themselves (for best performance). The library can only render images from PROGMEM and provides no FX chip support.&lt;br /&gt;
&lt;br /&gt;
The SpritesB library is functionally identical to the standard Sprites library but the code has been optimised for size at the expense of speed.&lt;br /&gt;
&lt;br /&gt;
==== [[ArduboyG Tutorial|ArduboyG]] ====&lt;br /&gt;
The ArduboyG library extends the basic Arduboy2 library with advanced functions for handling sprites, animations, and complex graphical operations, including support for 3 and 4 color greyscale. The library allows images to be rendered from PROGMEM or directly from the FX chip. Images can include transparent sections and must be a multiple of 8 bits high.  These are converted using a python script provided with the library.&lt;br /&gt;
&lt;br /&gt;
==== [[ArdBitmap]] ====&lt;br /&gt;
The ArdBitmap library works with compressed and uncompressed bitmaps and provides horizontal and vertical mirroring and real-time image resizing (downscaling).  It can only render images from PROGMEM and provides no FX chip support.&lt;br /&gt;
&lt;br /&gt;
The original forum post can be found [https://community.arduboy.com/t/ardbitmap-bitmap-library-tools/2631/6 here] and the library and documentation can be found [https://github.com/igvina/ArdBitmap here]&lt;br /&gt;
&lt;br /&gt;
=== Sound Libraries ===&lt;br /&gt;
&lt;br /&gt;
=== Utility Libraries ===&lt;br /&gt;
&lt;br /&gt;
==== [[Fixed Point]] ====&lt;br /&gt;
The Arduboy is based on a simple processor which does not handle floating point calculations natively.  As a result, any attempt to use floating point numbers and arithmetic results in a excess code and RAM use as well as poor performance.  Fixed Point numbers use a different approach and removes many of the issues found in floating point numbers.  This library, developed by Pharap, implements fixed point numbers that have varying scale and precision.&lt;br /&gt;
&lt;br /&gt;
==== [[Fonts]] ====&lt;br /&gt;
There are a number of fonts available for the Arduboy ranging from small (4x4 pixels) to large (5x7) pixels.&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Developing_Games&amp;diff=633</id>
		<title>Developing Games</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Developing_Games&amp;diff=633"/>
		<updated>2024-08-25T03:46:24Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* Utility Libraries */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Developing games for the Arduboy is an exciting endeavor that blends creativity with technical skills. The Arduboy is a compact, credit card-sized gaming platform based on the Arduino microcontroller, making it accessible for both beginners and experienced developers. Its simplicity and portability offer a unique challenge due to its limited resources, such as a monochrome 128x64 pixel display and minimal memory. However, these constraints inspire innovation and efficient coding practices.&lt;br /&gt;
&lt;br /&gt;
Standard libraries for sound and graphics play a significant role in the development process. The Arduboy2 library provides essential functions for managing the display, handling input, and maintaining game states. It facilitates tasks such as drawing shapes, rendering text, and controlling frame rates, allowing developers to concentrate on game mechanics. For audio, the ArduboyTones library enables the addition of sound effects and music, enhancing the game&#039;s atmosphere. Using these libraries, developers can create functional and engaging games that work well within the Arduboy&#039;s hardware limitations.&lt;br /&gt;
&lt;br /&gt;
== Prerequisites for Developing Games ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Arduino Development IDEs ==&lt;br /&gt;
Arduino development environments provide a range of tools for creating, testing, and deploying projects to the Arduboy. The primary environment is the Arduino Integrated Development Environment (IDE), known for its user-friendly interface that simplifies writing, compiling, and uploading code. It supports various Arduino models and includes a library manager for easy access to numerous libraries. Additionally, Visual Studio Code (VSCode) can be used for Arduino development, offering advanced features like integrated debugging and a customizable interface. These environments cater to both beginners and advanced users, offering tools that range from straightforward to professional-grade.&lt;br /&gt;
&lt;br /&gt;
=== Visual Studio Code ===&lt;br /&gt;
&lt;br /&gt;
The [https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.vscode-arduino Arduino plugin] for Visual Studio Code is an easy way to develop and build Arduboy games with a full-featured IDE.&lt;br /&gt;
&lt;br /&gt;
1. Download and install [https://code.visualstudio.com/ Visual Studio Code].&lt;br /&gt;
&lt;br /&gt;
2. Install the &amp;quot;Arduino&amp;quot; extension developed by Microsoft.&lt;br /&gt;
&lt;br /&gt;
3. Configure the extension to use the bundled version of Arduino CLI.&lt;br /&gt;
&lt;br /&gt;
4. Add the following URL to the &amp;quot;Additional Urls&amp;quot; setting for the Arduino extension: &amp;lt;pre&amp;gt;https://raw.githubusercontent.com/MrBlinky/Arduboy-homemade-package/master/package_arduboy_homemade_index.json&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
5. Open the Arduino Board Manager and click &amp;quot;Refresh Package Indexes&amp;quot;. Install the Arduboy homemade package by Mr.Blinky.&lt;br /&gt;
&lt;br /&gt;
6. Add a file &amp;quot;.vscode/arduino.json&amp;quot; to your project to define how to build your project. For example, to build a game for the Arduboy FX, you might have:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;configuration&amp;quot;: &amp;quot;core=arduboy-core,boot=cathy3k&amp;quot;,&lt;br /&gt;
    &amp;quot;board&amp;quot;: &amp;quot;arduboy-homemade:avr:arduboy-fx&amp;quot;,&lt;br /&gt;
    &amp;quot;sketch&amp;quot;: &amp;quot;my_game.ino&amp;quot;,&lt;br /&gt;
    &amp;quot;output&amp;quot;: &amp;quot;build/&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. You can now build your project with the &amp;quot;Arduino: Verify&amp;quot; command. The command will create a &amp;quot;build&amp;quot; directory inside your project directory, and generate .hex and .elf files there.&lt;br /&gt;
&lt;br /&gt;
== Common Libraries ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An Arduboy library is a collection of pre-written code designed to simplify and standardize the development of games and applications for the Arduboy platform. These libraries provide a range of functionalities, such as handling graphics, sound, input, and other core features of the device. For example, the Arduboy2 library includes functions for drawing shapes, rendering text, managing game states, and handling user input, while the ArduboyTones library offers tools for generating sound effects and music. By utilizing these libraries, developers can streamline their coding process, focus on the creative aspects of game design, and ensure their applications run efficiently on the limited hardware of the Arduboy.&lt;br /&gt;
&lt;br /&gt;
=== Base Libraries ===&lt;br /&gt;
&lt;br /&gt;
==== [[Arduboy2]] ====&lt;br /&gt;
The basic Arduboy library, often referred to as Arduboy2, is an essential toolkit for developing games on the Arduboy platform. This library provides a comprehensive set of functions that manage the device&#039;s core features, making it easier for developers to create games and applications. It includes capabilities for drawing graphics on the 128x64 pixel monochrome display, such as drawing lines, circles, and rectangles, as well as rendering text. The library also handles input from the device&#039;s buttons, allowing developers to implement game controls efficiently. Additionally, Arduboy2 offers functions for managing frame rates and game states, ensuring smooth game play experiences. By using the Arduboy2 library, developers can focus more on the creative aspects of game development while leveraging standardized, efficient code for fundamental tasks.&lt;br /&gt;
&lt;br /&gt;
Additionally, the library provides basic [[Collision Detection]] functions.&lt;br /&gt;
&lt;br /&gt;
=== Graphic Libraries ===&lt;br /&gt;
&lt;br /&gt;
==== [[Sprites / SpritesB Library|Sprites]] / SpritesB ====&lt;br /&gt;
Shipped as part of the [[Arduboy2]] library, the Sprites library provides functions for rendering both masked and unmasked bitmaps and tilesheets. When working with masked bitmaps, the library supports various formats including standalone masks (allowing them to be used with multiple bitmaps) or interlaced within the bitmap image themselves (for best performance). The library can only render images from PROGMEM and provides no FX chip support.&lt;br /&gt;
&lt;br /&gt;
The SpritesB library is functionally identical to the standard Sprites library but the code has been optimised for size at the expense of speed.&lt;br /&gt;
&lt;br /&gt;
==== [[ArduboyG Tutorial|ArduboyG]] ====&lt;br /&gt;
The ArduboyG library extends the basic Arduboy2 library with advanced functions for handling sprites, animations, and complex graphical operations, including support for 3 and 4 color greyscale. The library allows images to be rendered from PROGMEM or directly from the FX chip. Images can include transparent sections and must be a multiple of 8 bits high.  These are converted using a python script provided with the library.&lt;br /&gt;
&lt;br /&gt;
==== [[ArdBitmap]] ====&lt;br /&gt;
The ArdBitmap library works with compressed and uncompressed bitmaps and provides horizontal and vertical mirroring and real-time image resizing (downscaling).  It can only render images from PROGMEM and provides no FX chip support.&lt;br /&gt;
&lt;br /&gt;
The original forum post can be found [https://community.arduboy.com/t/ardbitmap-bitmap-library-tools/2631/6 here] and the library and documentation can be found [https://github.com/igvina/ArdBitmap here]&lt;br /&gt;
&lt;br /&gt;
=== Sound Libraries ===&lt;br /&gt;
&lt;br /&gt;
=== Utility Libraries ===&lt;br /&gt;
&lt;br /&gt;
==== [[Fixed Point]] ====&lt;br /&gt;
The Arduboy is based on a simple processor which does not handle floating point calculations natively.  As a result, any attempt to use floating point numbers and arithmetic results in a excess code and RAM use as well as poor performance.  Fixed Point numbers use a different approach and removes many of the issues found in floating point numbers.  This library, developed by Pharap, implements fixed point numbers that have varying scale and precision.&lt;br /&gt;
&lt;br /&gt;
[[Fonts]]&lt;br /&gt;
&lt;br /&gt;
There are a number of fonts available for the Arduboy ranging from small (4x4 pixels) to large (5x7) pixels.&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fonts&amp;diff=632</id>
		<title>Fonts</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fonts&amp;diff=632"/>
		<updated>2024-08-25T03:44:50Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Default Font ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Arduboy2 Class contains a 5 x 7 pixel font. Additional details can be found in the repo [https://mlxxxp.github.io/documents/Arduino/libraries/Arduboy2/Doxygen/html/classPrint.html here]&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;src/fonts/Font3x5.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2 arduboy;&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
  arduboy.boot();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    arduboy.setCursor(0, 0);&lt;br /&gt;
    arduboy.print(F(&amp;quot;ABCDEFGHIJKLMNOPQRSTU\nabcdefghijklmnopqrstu\n1234567890./?!@#$&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;[[File:DefaultFont2.png|frameless]]&lt;br /&gt;
&lt;br /&gt;
== TinyFont ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
TinyFont is a 4x4 font which conforms to small &#039;&#039;ASCII&#039;&#039; from 32-127.&lt;br /&gt;
&lt;br /&gt;
It stores 4 letters in 8 bytes and uses masks to draw the correct letter which makes the sprites 224 bytes in size.&lt;br /&gt;
&lt;br /&gt;
Additional details can be found [https://community.arduboy.com/t/tinyfont-for-arduboy/3136 here] and the repo [https://github.com/yinkou/Arduboy-TinyFont here]&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;Tinyfont.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2 arduboy;&lt;br /&gt;
Tinyfont tinyfont = Tinyfont(arduboy.sBuffer, Arduboy2::width(), Arduboy2::height());&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
    arduboy.begin();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    tinyfont.setCursor(1, 0);&lt;br /&gt;
    tinyfont.print(F(&amp;quot;THE QUICK BROWN FOX JUMPS\nOVER THE LAZY DOG.&amp;quot;));&lt;br /&gt;
    tinyfont.setCursor(1, 11);&lt;br /&gt;
    tinyfont.print(F(&amp;quot;The quick brown fox jumps\nover the lazy dog.&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:TnyFont.png|frameless]]&lt;br /&gt;
&lt;br /&gt;
== Font3x5 ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Font3x5 is a simple library that provides a 3 x 5 pixel font which has the upper- and lower-case alphabet, numbers and the exclamation mark and the period.  You can easily add extra characters if you like.&lt;br /&gt;
&lt;br /&gt;
There is a #define that allows you to remove the lower case letters.  Doing so strips out 108 bytes.  Additional details can be found [https://community.arduboy.com/t/3x5-font-for-those-with-good-eyesight/4920 here] and the repo [https://github.com/filmote/Font3x5 here]&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;src/fonts/Font3x5.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2Base arduboy;&lt;br /&gt;
Font3x5 font3x5 = Font3x5();&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
   arduboy.begin();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    font3x5.setCursor(12, 12);&lt;br /&gt;
    font3x5.print(F(&amp;quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\nabcdefghijklmnopqrstuvwxyz&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\n0123456789&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\n!.&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also pass a line height to the constructor, as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;Font3x5 font3x5 = Font3x5( 11 );&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two images below show the standard line height - 8 - and 11.&lt;br /&gt;
&lt;br /&gt;
[[File:Font3x5 00.png|frameless]]  [[File:Font3x5 01.png|frameless]]&lt;br /&gt;
&lt;br /&gt;
== Font4x6 ==&lt;br /&gt;
&lt;br /&gt;
Font4x6 is a simple library that provides a 4 x 6 pixel font which has the upper- and lower-case alphabet, numbers and the exclamation mark and the period.  You can easily add extra characters if you like.&lt;br /&gt;
&lt;br /&gt;
There is a #define that allows you to remove the lower case letters.  Doing so strips out 108 bytes. Additional details can be found [https://community.arduboy.com/t/4x6-font-for-the-arduboy/5589 here] and the repo [https://github.com/filmote/Font4x6 here]&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;src/fonts/Font4x6.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2Base arduboy;&lt;br /&gt;
Font4x6 font4x6 = Font4x6();&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
&lt;br /&gt;
    arduboy.boot();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    font4x6.setCursor(12, 12);&lt;br /&gt;
    font4x6.print(F(&amp;quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&amp;quot;));&lt;br /&gt;
    font4x6.print(F(&amp;quot;\nabcdefghijklmnopqrstuvwxyz&amp;quot;));&lt;br /&gt;
    font4x6.print(F(&amp;quot;\n0123456789&amp;quot;));&lt;br /&gt;
    font4x6.print(F(&amp;quot;\n!.&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also pass a line height to the constructor, as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;Font4x6 font4x6 = Font4x6( 11 );&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Font4x6.png|frameless]]&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=File:DefaultFont2.png&amp;diff=631</id>
		<title>File:DefaultFont2.png</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=File:DefaultFont2.png&amp;diff=631"/>
		<updated>2024-08-25T03:44:09Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;DefaultFont2&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=File:DefaultFont.png&amp;diff=630</id>
		<title>File:DefaultFont.png</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=File:DefaultFont.png&amp;diff=630"/>
		<updated>2024-08-25T03:42:50Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;DefaultFont&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fonts&amp;diff=629</id>
		<title>Fonts</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fonts&amp;diff=629"/>
		<updated>2024-08-25T03:40:56Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Default Font ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Arduboy2 Class contains a 5 x 7 pixel font.&lt;br /&gt;
&lt;br /&gt;
Additional details can be found in the repo [https://mlxxxp.github.io/documents/Arduino/libraries/Arduboy2/Doxygen/html/classPrint.html here]&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;src/fonts/Font3x5.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2 arduboy;&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
  arduboy.boot();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    arduboy.setCursor(0, 0);&lt;br /&gt;
    arduboy.print(F(&amp;quot;ABCDEFGHIJKLMNOPQRSTU\nabcdefghijklmnopqrstu\n1234567890./?!@#$&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== TinyFont ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
TinyFont is a 4x4 font which conforms to small &#039;&#039;ASCII&#039;&#039; from 32-127.&lt;br /&gt;
&lt;br /&gt;
It stores 4 letters in 8 bytes and uses masks to draw the correct letter which makes the sprites 224 bytes in size.&lt;br /&gt;
&lt;br /&gt;
Additional details can be found [https://community.arduboy.com/t/tinyfont-for-arduboy/3136 here] and the repo [https://github.com/yinkou/Arduboy-TinyFont here]&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;Tinyfont.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2 arduboy;&lt;br /&gt;
Tinyfont tinyfont = Tinyfont(arduboy.sBuffer, Arduboy2::width(), Arduboy2::height());&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
    arduboy.begin();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    tinyfont.setCursor(1, 0);&lt;br /&gt;
    tinyfont.print(F(&amp;quot;THE QUICK BROWN FOX JUMPS\nOVER THE LAZY DOG.&amp;quot;));&lt;br /&gt;
    tinyfont.setCursor(1, 11);&lt;br /&gt;
    tinyfont.print(F(&amp;quot;The quick brown fox jumps\nover the lazy dog.&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:TnyFont.png|frameless]]&lt;br /&gt;
&lt;br /&gt;
== Font3x5 ==&lt;br /&gt;
Font3x5 is a simple library that provides a 3 x 5 pixel font which has the upper- and lower-case alphabet, numbers and the exclamation mark and the period.  You can easily add extra characters if you like.&lt;br /&gt;
&lt;br /&gt;
There is a #define that allows you to remove the lower case letters.  Doing so strips out 108 bytes.  Additional details can be found [https://community.arduboy.com/t/3x5-font-for-those-with-good-eyesight/4920 here] and the repo [https://github.com/filmote/Font3x5 here]&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;src/fonts/Font3x5.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2Base arduboy;&lt;br /&gt;
Font3x5 font3x5 = Font3x5();&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
   arduboy.begin();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    font3x5.setCursor(12, 12);&lt;br /&gt;
    font3x5.print(F(&amp;quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\nabcdefghijklmnopqrstuvwxyz&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\n0123456789&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\n!.&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also pass a line height to the constructor, as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;Font3x5 font3x5 = Font3x5( 11 );&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two images below show the standard line height - 8 - and 11.&lt;br /&gt;
&lt;br /&gt;
[[File:Font3x5 00.png|frameless]]  [[File:Font3x5 01.png|frameless]]&lt;br /&gt;
&lt;br /&gt;
== Font4x6 ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Font4x6 is a simple library that provides a 4 x 6 pixel font which has the upper- and lower-case alphabet, numbers and the exclamation mark and the period.  You can easily add extra characters if you like.&lt;br /&gt;
&lt;br /&gt;
There is a #define that allows you to remove the lower case letters.  Doing so strips out 108 bytes. Additional details can be found [https://community.arduboy.com/t/4x6-font-for-the-arduboy/5589 here] and the repo [https://github.com/filmote/Font4x6 here]&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;src/fonts/Font4x6.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2Base arduboy;&lt;br /&gt;
Font4x6 font4x6 = Font4x6();&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
&lt;br /&gt;
    arduboy.boot();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    font4x6.setCursor(12, 12);&lt;br /&gt;
    font4x6.print(F(&amp;quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&amp;quot;));&lt;br /&gt;
    font4x6.print(F(&amp;quot;\nabcdefghijklmnopqrstuvwxyz&amp;quot;));&lt;br /&gt;
    font4x6.print(F(&amp;quot;\n0123456789&amp;quot;));&lt;br /&gt;
    font4x6.print(F(&amp;quot;\n!.&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also pass a line height to the constructor, as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;Font4x6 font4x6 = Font4x6( 11 );&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Font4x6.png|frameless]]&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fonts&amp;diff=628</id>
		<title>Fonts</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fonts&amp;diff=628"/>
		<updated>2024-08-25T03:40:42Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Default Font ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Arduboy2 Class contains a 5 x 7 pixel font.&lt;br /&gt;
&lt;br /&gt;
Additional details can be found in the repo [https://mlxxxp.github.io/documents/Arduino/libraries/Arduboy2/Doxygen/html/classPrint.html here]&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;src/fonts/Font3x5.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2 arduboy;&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
  arduboy.boot();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    arduboy.setCursor(0, 0);&lt;br /&gt;
    arduboy.print(F(&amp;quot;ABCDEFGHIJKLMNOPQRSTU\nabcdefghijklmnopqrstu\n1234567890./?!@#$&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== TinyFont ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
TinyFont is a 4x4 font which conforms to small &#039;&#039;ASCII&#039;&#039; from 32-127.&lt;br /&gt;
&lt;br /&gt;
It stores 4 letters in 8 bytes and uses masks to draw the correct letter which makes the sprites 224 bytes in size.&lt;br /&gt;
&lt;br /&gt;
Additional details can be found [https://community.arduboy.com/t/tinyfont-for-arduboy/3136 here] and the repo [https://github.com/yinkou/Arduboy-TinyFont here]&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;Tinyfont.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2 arduboy;&lt;br /&gt;
Tinyfont tinyfont = Tinyfont(arduboy.sBuffer, Arduboy2::width(), Arduboy2::height());&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
    arduboy.begin();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    tinyfont.setCursor(1, 0);&lt;br /&gt;
    tinyfont.print(F(&amp;quot;THE QUICK BROWN FOX JUMPS\nOVER THE LAZY DOG.&amp;quot;));&lt;br /&gt;
    tinyfont.setCursor(1, 11);&lt;br /&gt;
    tinyfont.print(F(&amp;quot;The quick brown fox jumps\nover the lazy dog.&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:TnyFont.png|frameless]]&lt;br /&gt;
&lt;br /&gt;
== Font3x5 ==&lt;br /&gt;
Font3x5 is a simple library that provides a 3 x 5 pixel font which has the upper- and lower-case alphabet, numbers and the exclamation mark and the period.  You can easily add extra characters if you like.&lt;br /&gt;
&lt;br /&gt;
There is a #define that allows you to remove the lower case letters.  Doing so strips out 108 bytes.  Additional details can be found [https://community.arduboy.com/t/3x5-font-for-those-with-good-eyesight/4920 here] and the repo [https://github.com/filmote/Font3x5 here]&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;src/fonts/Font3x5.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2Base arduboy;&lt;br /&gt;
Font3x5 font3x5 = Font3x5();&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
   arduboy.begin();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    font3x5.setCursor(12, 12);&lt;br /&gt;
    font3x5.print(F(&amp;quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\nabcdefghijklmnopqrstuvwxyz&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\n0123456789&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\n!.&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also pass a line height to the constructor, as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;Font3x5 font3x5 = Font3x5( 11 );&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two images below show the standard line height - 8 - and 11.&lt;br /&gt;
&lt;br /&gt;
[[File:Font3x5 00.png|frameless]]  [[File:Font3x5 01.png|frameless]]&lt;br /&gt;
&lt;br /&gt;
== Font4x6 ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Font4x6 is a simple library that provides a 4 x 6 pixel font which has the upper- and lower-case alphabet, numbers and the exclamation mark and the period.  You can easily add extra characters if you like.&lt;br /&gt;
&lt;br /&gt;
There is a #define that allows you to remove the lower case letters.  Doing so strips out 108 bytes. Additional details can be found [https://community.arduboy.com/t/4x6-font-for-the-arduboy/5589 here] and the repo [https://github.com/filmote/Font4x6 here]&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;src/fonts/Font4x6.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2Base arduboy;&lt;br /&gt;
Font4x6 font4x6 = Font4x6();&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
&lt;br /&gt;
    arduboy.boot();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    font4x6.setCursor(12, 12);&lt;br /&gt;
    font4x6.print(F(&amp;quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&amp;quot;));&lt;br /&gt;
    font4x6.print(F(&amp;quot;\nabcdefghijklmnopqrstuvwxyz&amp;quot;));&lt;br /&gt;
    font4x6.print(F(&amp;quot;\n0123456789&amp;quot;));&lt;br /&gt;
    font4x6.print(F(&amp;quot;\n!.&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also pass a line height to the constructor, as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;Font4x6 font4x6 = Font4x6( 11 );&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Font4x6.png|frameless]]&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fonts&amp;diff=627</id>
		<title>Fonts</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fonts&amp;diff=627"/>
		<updated>2024-08-25T03:31:51Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== TinyFont ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
TinyFont is a 4x4 font which conforms to small &#039;&#039;ASCII&#039;&#039; from 32-127.&lt;br /&gt;
&lt;br /&gt;
It stores 4 letters in 8 bytes and uses masks to draw the correct letter which makes the sprites 224 bytes in size.&lt;br /&gt;
&lt;br /&gt;
Additional details can be found [https://community.arduboy.com/t/tinyfont-for-arduboy/3136 here] and the repo [https://github.com/yinkou/Arduboy-TinyFont here]&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;Tinyfont.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2 arduboy;&lt;br /&gt;
Tinyfont tinyfont = Tinyfont(arduboy.sBuffer, Arduboy2::width(), Arduboy2::height());&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
    arduboy.begin();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    tinyfont.setCursor(1, 0);&lt;br /&gt;
    tinyfont.print(F(&amp;quot;THE QUICK BROWN FOX JUMPS\nOVER THE LAZY DOG.&amp;quot;));&lt;br /&gt;
    tinyfont.setCursor(1, 11);&lt;br /&gt;
    tinyfont.print(F(&amp;quot;The quick brown fox jumps\nover the lazy dog.&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:TnyFont.png|frameless]]&lt;br /&gt;
&lt;br /&gt;
== Font3x5 ==&lt;br /&gt;
Font3x5 is a simple library that provides a 3 x 5 pixel font which has the upper- and lower-case alphabet, numbers and the exclamation mark and the period.  You can easily add extra characters if you like.&lt;br /&gt;
&lt;br /&gt;
There is a #define that allows you to remove the lower case letters.  Doing so strips out 108 bytes.  Additional details can be found [https://community.arduboy.com/t/3x5-font-for-those-with-good-eyesight/4920 here] and the repo [https://github.com/filmote/Font3x5 here]&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;src/fonts/Font3x5.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2Base arduboy;&lt;br /&gt;
Font3x5 font3x5 = Font3x5();&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
   arduboy.begin();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    font3x5.setCursor(12, 12);&lt;br /&gt;
    font3x5.print(F(&amp;quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\nabcdefghijklmnopqrstuvwxyz&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\n0123456789&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\n!.&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also pass a line height to the constructor, as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;Font3x5 font3x5 = Font3x5( 11 );&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two images below show the standard line height - 8 - and 11.&lt;br /&gt;
&lt;br /&gt;
[[File:Font3x5 00.png|frameless]]  [[File:Font3x5 01.png|frameless]]&lt;br /&gt;
&lt;br /&gt;
== Font4x6 ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Font4x6 is a simple library that provides a 4 x 6 pixel font which has the upper- and lower-case alphabet, numbers and the exclamation mark and the period.  You can easily add extra characters if you like.&lt;br /&gt;
&lt;br /&gt;
There is a #define that allows you to remove the lower case letters.  Doing so strips out 108 bytes. Additional details can be found [https://community.arduboy.com/t/4x6-font-for-the-arduboy/5589 here] and the repo [https://github.com/filmote/Font4x6 here]&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;src/fonts/Font4x6.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2Base arduboy;&lt;br /&gt;
Font4x6 font4x6 = Font4x6();&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
&lt;br /&gt;
    arduboy.boot();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    font4x6.setCursor(12, 12);&lt;br /&gt;
    font4x6.print(F(&amp;quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&amp;quot;));&lt;br /&gt;
    font4x6.print(F(&amp;quot;\nabcdefghijklmnopqrstuvwxyz&amp;quot;));&lt;br /&gt;
    font4x6.print(F(&amp;quot;\n0123456789&amp;quot;));&lt;br /&gt;
    font4x6.print(F(&amp;quot;\n!.&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also pass a line height to the constructor, as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;Font4x6 font4x6 = Font4x6( 11 );&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Font4x6.png|frameless]]&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fonts&amp;diff=626</id>
		<title>Fonts</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fonts&amp;diff=626"/>
		<updated>2024-08-25T03:30:26Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== TinyFont ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
TinyFont is a 4x4 font which conforms to small &#039;&#039;ASCII&#039;&#039; from 32-127.&lt;br /&gt;
&lt;br /&gt;
It stores 4 letters in 8 bytes and uses masks to draw the correct letter which makes the sprites 224 bytes in size.&lt;br /&gt;
&lt;br /&gt;
Additional details can be found [https://community.arduboy.com/t/tinyfont-for-arduboy/3136 here] and the repo [https://github.com/yinkou/Arduboy-TinyFont here]&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;Tinyfont.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2 arduboy;&lt;br /&gt;
Tinyfont tinyfont = Tinyfont(arduboy.sBuffer, Arduboy2::width(), Arduboy2::height());&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
&lt;br /&gt;
  arduboy.begin();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
  arduboy.clear();&lt;br /&gt;
&lt;br /&gt;
  tinyfont.setCursor(1, 0);&lt;br /&gt;
  tinyfont.print(&amp;quot;THE QUICK BROWN FOX JUMPS\nOVER THE LAZY DOG.&amp;quot;);&lt;br /&gt;
  tinyfont.setCursor(1, 11);&lt;br /&gt;
  tinyfont.print(&amp;quot;The quick brown fox jumps\nover the lazy dog.&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
  arduboy.display();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:TnyFont.png|frameless]]&lt;br /&gt;
&lt;br /&gt;
== Font3x5 ==&lt;br /&gt;
Font3x5 is a simple library that provides a 3 x 5 pixel font which has the upper- and lower-case alphabet, numbers and the exclamation mark and the period.  You can easily add extra characters if you like.&lt;br /&gt;
&lt;br /&gt;
There is a #define that allows you to remove the lower case letters.  Doing so strips out 108 bytes.  Additional details can be found [https://community.arduboy.com/t/3x5-font-for-those-with-good-eyesight/4920 here] and the repo [https://github.com/filmote/Font3x5 here]&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;src/fonts/Font3x5.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2Base arduboy;&lt;br /&gt;
Font3x5 font3x5 = Font3x5();&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
&lt;br /&gt;
   arduboy.boot();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    font3x5.setCursor(12, 12);&lt;br /&gt;
    font3x5.print(F(&amp;quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\nabcdefghijklmnopqrstuvwxyz&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\n0123456789&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\n!.&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also pass a line height to the constructor, as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;Font3x5 font3x5 = Font3x5( 11 );&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two images below show the standard line height - 8 - and 11.&lt;br /&gt;
&lt;br /&gt;
[[File:Font3x5 00.png|frameless]]  [[File:Font3x5 01.png|frameless]]&lt;br /&gt;
&lt;br /&gt;
== Font4x6 ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Font4x6 is a simple library that provides a 4 x 6 pixel font which has the upper- and lower-case alphabet, numbers and the exclamation mark and the period.  You can easily add extra characters if you like.&lt;br /&gt;
&lt;br /&gt;
There is a #define that allows you to remove the lower case letters.  Doing so strips out 108 bytes. Additional details can be found [https://community.arduboy.com/t/4x6-font-for-the-arduboy/5589 here] and the repo [https://github.com/filmote/Font4x6 here]&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;src/fonts/Font4x6.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2Base arduboy;&lt;br /&gt;
Font4x6 font4x6 = Font4x6();&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
&lt;br /&gt;
    arduboy.boot();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    font4x6.setCursor(12, 12);&lt;br /&gt;
    font4x6.print(F(&amp;quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&amp;quot;));&lt;br /&gt;
    font4x6.print(F(&amp;quot;\nabcdefghijklmnopqrstuvwxyz&amp;quot;));&lt;br /&gt;
    font4x6.print(F(&amp;quot;\n0123456789&amp;quot;));&lt;br /&gt;
    font4x6.print(F(&amp;quot;\n!.&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also pass a line height to the constructor, as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;Font4x6 font4x6 = Font4x6( 11 );&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Font4x6.png|frameless]]&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=File:TnyFont.png&amp;diff=625</id>
		<title>File:TnyFont.png</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=File:TnyFont.png&amp;diff=625"/>
		<updated>2024-08-25T03:24:59Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;TnyFont&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fonts&amp;diff=624</id>
		<title>Fonts</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fonts&amp;diff=624"/>
		<updated>2024-08-25T03:22:25Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Font3x5 ==&lt;br /&gt;
Font3x5 is a simple library that provides a 3 x 5 pixel font which has the upper- and lower-case alphabet, numbers and the exclamation mark and the period.  You can easily add extra characters if you like.&lt;br /&gt;
&lt;br /&gt;
There is a #define that allows you to remove the lower case letters.  Doing so strips out 108 bytes.&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;src/fonts/Font3x5.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2Base arduboy;&lt;br /&gt;
Font3x5 font3x5 = Font3x5();&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
&lt;br /&gt;
   arduboy.boot();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    font3x5.setCursor(12, 12);&lt;br /&gt;
    font3x5.print(F(&amp;quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\nabcdefghijklmnopqrstuvwxyz&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\n0123456789&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\n!.&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also pass a line height to the constructor, as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;Font3x5 font3x5 = Font3x5( 11 );&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two images below show the standard line height - 8 - and 11.&lt;br /&gt;
&lt;br /&gt;
[[File:Font3x5 00.png|frameless]]  [[File:Font3x5 01.png|frameless]]&lt;br /&gt;
&lt;br /&gt;
== Font4x6 ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Font4x6 is a simple library that provides a 4 x 6 pixel font which has the upper- and lower-case alphabet, numbers and the exclamation mark and the period.  You can easily add extra characters if you like.&lt;br /&gt;
&lt;br /&gt;
There is a #define that allows you to remove the lower case letters.  Doing so strips out 108 bytes.&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;src/fonts/Font4x6.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2Base arduboy;&lt;br /&gt;
Font4x6 font4x6 = Font4x6();&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
&lt;br /&gt;
    arduboy.boot();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    font4x6.setCursor(12, 12);&lt;br /&gt;
    font4x6.print(F(&amp;quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&amp;quot;));&lt;br /&gt;
    font4x6.print(F(&amp;quot;\nabcdefghijklmnopqrstuvwxyz&amp;quot;));&lt;br /&gt;
    font4x6.print(F(&amp;quot;\n0123456789&amp;quot;));&lt;br /&gt;
    font4x6.print(F(&amp;quot;\n!.&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also pass a line height to the constructor, as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;Font4x6 font4x6 = Font4x6( 11 );&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Font4x6.png|frameless]]&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=File:Font4x6.png&amp;diff=623</id>
		<title>File:Font4x6.png</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=File:Font4x6.png&amp;diff=623"/>
		<updated>2024-08-25T02:27:57Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Font4x6&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=File:Font3x5_01.png&amp;diff=622</id>
		<title>File:Font3x5 01.png</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=File:Font3x5_01.png&amp;diff=622"/>
		<updated>2024-08-25T02:20:40Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Font3x5 01&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=File:Font3x5_00.png&amp;diff=621</id>
		<title>File:Font3x5 00.png</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=File:Font3x5_00.png&amp;diff=621"/>
		<updated>2024-08-25T02:20:13Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Font3x5 00&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fonts&amp;diff=620</id>
		<title>Fonts</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fonts&amp;diff=620"/>
		<updated>2024-08-25T02:18:20Z</updated>

		<summary type="html">&lt;p&gt;Filmote: Created page with &amp;quot;=== Font3x5 === Font3x5 is a simple library that provides a 3 x 5 pixel font which has the upper- and lower-case alphabet, numbers and the exclamation mark and the period.  You can easily add extra characters if you like.  There is a #define that allows you to remove the lower case letters.  Doing so strips out 108 bytes.  A sample program is shown below:  &amp;lt;pre&amp;gt; #include &amp;lt;Arduboy2.h&amp;gt; #include &amp;quot;src/fonts/Font3x5.h&amp;quot;  Arduboy2Base arduboy; Font3x5 font3x5 = Font3x5();  void...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=== Font3x5 ===&lt;br /&gt;
Font3x5 is a simple library that provides a 3 x 5 pixel font which has the upper- and lower-case alphabet, numbers and the exclamation mark and the period.  You can easily add extra characters if you like.&lt;br /&gt;
&lt;br /&gt;
There is a #define that allows you to remove the lower case letters.  Doing so strips out 108 bytes.&lt;br /&gt;
&lt;br /&gt;
A sample program is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;Arduboy2.h&amp;gt;&lt;br /&gt;
#include &amp;quot;src/fonts/Font3x5.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Arduboy2Base arduboy;&lt;br /&gt;
Font3x5 font3x5 = Font3x5();&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
&lt;br /&gt;
   arduboy.boot();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
    if (!(arduboy.nextFrame())) return;&lt;br /&gt;
&lt;br /&gt;
    arduboy.clear();&lt;br /&gt;
    font3x5.setCursor(12, 12);&lt;br /&gt;
    font3x5.print(F(&amp;quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\nabcdefghijklmnopqrstuvwxyz&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\n0123456789&amp;quot;));&lt;br /&gt;
    font3x5.print(F(&amp;quot;\n!.&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    arduboy.display();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=619</id>
		<title>Fixed Point</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=619"/>
		<updated>2024-08-17T12:02:41Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
Most of us are familiar with the integer data types used by the Arduboy environment.  They come in various lengths and can be both signed and unsigned. We take care to choose appropriate data types that consider both the range of our data as well as the memory required to store them. Operations involving integer values are incredibly efficient and for many sketches are all that are required.&lt;br /&gt;
&lt;br /&gt;
Sometimes however, integer values are simply not flexible enough. Imagine trying to calculate the movement of an accelerating rocket or calculate the trajectory of a rocket or a missile in an Arduboy game. To make these calculations, you need numbers with both an integer and fractional component. The typical way to represent such numbers on a computer is through floating points.&lt;br /&gt;
&lt;br /&gt;
C++ includes two floating point data types of different precision – the float and the double. On most platforms the float is 4 bytes and the double is 8 bytes, however on the Arduino (and thus the Arduboy) they are actually same precision (4 bytes) and are therefore interchangeable. These data types can store numbers in the range of -3.4028235 x 1038 to +3. 4028235 x 1038 (for comparison there is estimated to be 7.5 x 1018 grains of sand in the world) but at a precision of only 6 or 7 decimal digits.&lt;br /&gt;
&lt;br /&gt;
For the Arduboy, in contrast to integer operations, floating point calculations are internally quite complicated and thus incredibly processor intensive. Including numerous calculations to reposition the player and enemies within the main loop of a program might prevent the game from performing well or worse yet even being playable.&lt;br /&gt;
&lt;br /&gt;
However, there is an alternative to &#039;&#039;&#039;floating point&#039;&#039;&#039; arithmetic. This alternative is &#039;&#039;&#039;fixed point&#039;&#039;&#039; arithmetic. Fixed point numbers behave more similarly to the integer values you are familiar with. As such, the processing power required to manipulate fixed point numbers is more similar to integer calculations than to floating point calculations. &lt;br /&gt;
&lt;br /&gt;
There is however a tradeoff for the benefit that fixed point data types bring. The tradeoff is that you must explicitly nominate the size  of both the integer and fractional portion of the variable – much in the same way you nominate the available range when choosing integer data types. In addition to the obvious data range restrictions of the integer component, the resolution (or ‘granularity’)  of the fractional component must also be chosen.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 8 bit fixed point type with a 4 bit integer part and a 4 bit fractional part would permit an integer range of 0 to 15 and a fractional range of 0/16 to 15/16. This equates to a range of 0 to 15.9375 (15/16 is equivalent to 0.9375).&lt;br /&gt;
&lt;br /&gt;
Note that the fractional part can only go up in increments of 1/16 (i.e. 0.0625), hence that particular format cannot accurately represent numbers at a lower resolution, for example the value 0.05 could not be accurately represented, it would have to be rounded to 0.0625. However, by using larger types or by increasing the number of bits afforded to the fractional part at the expense of the integer part you can balance the format to suit your needs.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 16 bit type with 8 bits of integer part and 8 bits of fractional part could represent all the positions on the Arduboy’s screen, whilst providing a reasonable range of fractional steps in between the precise pixel locations. This would mean having much smoother movement than using regular integers, but in a way that’s cheaper than floating points in terms of both code use and processing power.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Fixed Points in Action&#039;&#039;&#039; ==&lt;br /&gt;
In the examples below, I am using @Pharap’s [https://community.arduboy.com/t/a-fixed-point-arithmetic-library/4129 FixedPoints] library which defines a number of signed and unsigned data types which are listed at the end of this article. It also includes overrides for the common arithmetic and comparison operators (+, -, / , *, &amp;lt;, &amp;gt;, etc) as well as some handy functions like &amp;lt;code&amp;gt;ceilFixed()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;floorFixed()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;roundFixed()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Firstly, some simple assignments and calculations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar = 7.0;&lt;br /&gt;
myVar = myVar + 3;&lt;br /&gt;
&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 4);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
static_cast&amp;lt;float&amp;gt;(myVar) : 10.0000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Please note that an extra ‘level of precision’ argument is being used in the print code here. Without supplying this extra argument, the printed results suffer from rounding errors made by the &amp;lt;code&amp;gt;Serial.print()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Serial.println()&amp;lt;/code&amp;gt; functions to simplify the code for printing floats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Simple. Now let’s introduce a decimal place.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar = 3.7;&lt;br /&gt;
Serial.print(“myVar.getInteger() : “);&lt;br /&gt;
Serial.println(myVar.getInteger());&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 8);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myVar.getInteger() : 3&lt;br /&gt;
static_cast&amp;lt;float&amp;gt;(myVar) :  3.69921875&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But hang on, we set the variable to 3.7 and it printed out 3.69921875. Why so? As mentioned earlier, both &#039;&#039;&#039;floating point&#039;&#039;&#039; and &#039;&#039;&#039;fixed point&#039;&#039;&#039; numbers introduce a degree of inaccuracy when storing numbers. With Fixed Point numbers, the precision (and by extension the accuracy) is specified by the programmer.&lt;br /&gt;
&lt;br /&gt;
In the sample code above, the variable chosen was a two-byte incarnation and it  can store a number roughly between -128 and +128. The integer component is stored in 7 bits which allows a range of 0 to 127 and the fractional part is stored in 8 bits and has a range between 0 / 256 to 255 / 256. The largest fractional portion (255 / 256) converted to decimal is 0.9961 hence the data range cannot quite store +128, but it can store up to 127.9961.&lt;br /&gt;
&lt;br /&gt;
If you could view the bits in the fractional part of the variable (which you can through &amp;lt;code&amp;gt;myVar.getFraction()&amp;lt;/code&amp;gt;), you would see that it is stored as 179. As it turns out 179 / 256 is equal to 0.69921875.&lt;br /&gt;
&lt;br /&gt;
The inaccuracy introduced by a relatively small (two-byte) &#039;&#039;&#039;fixed point&#039;&#039;&#039; is irrelevant if all you are trying to do is simulate some realistic movements of characters in a game. If you have a need to calculate more accurate values, you can simply increase the size of the &#039;&#039;&#039;fixed point&#039;&#039;&#039; variable to either an &amp;lt;code&amp;gt;SQ15x16&amp;lt;/code&amp;gt; or an &amp;lt;code&amp;gt;SQ31x32&amp;lt;/code&amp;gt; (though be aware that some operations do not work on &amp;lt;code&amp;gt;SQ31x32&amp;lt;/code&amp;gt; due to presently unavoidable restrictions). The code below illustrates the accuracy of each data type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar7x8 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar7x8), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar7x8 = myVar7x8 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
Serial.println();&lt;br /&gt;
 &lt;br /&gt;
SQ15x16 myVar15x16 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar15x16), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar15x16 = myVar15x16 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
Serial.println();&lt;br /&gt;
 &lt;br /&gt;
SQ31x32 myVar31x32 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar31x32), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar31x32 = myVar31x32 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
Serial.println();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 4.00000000 4.09765625 4.19531250 4.29296875 4.39062500 4.48828125 4.58593750 4.68359375 4.78125000 4.87890625 4.97656250&lt;br /&gt;
 4.00000000 4.09999084 4.19998168 4.29997253 4.39996337 4.49995422 4.59994506 4.69993591 4.79992675 4.89991760 4.99990844 &lt;br /&gt;
 4.00000000 4.09999990 4.19999980 4.30000019 4.40000009 4.50000000 4.59999990 4.69999980 4.80000019 4.90000009 5.00000000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One thing to note with the output is the compounding effect that adding two inaccurate numbers results in. This can be seen easily if we repeat the same exercise on the smallest, one-byte data type as shown below. The fractional part of an &amp;lt;code&amp;gt;SQ3x4&amp;lt;/code&amp;gt; number is stored in 1/16th increments or 0.0625 in decimal.&lt;br /&gt;
&lt;br /&gt;
When we add the first increment of 0.1  we immediately introduce an inaccuracy as the number gets rounded down to the nearest 1/16th of a number. As we repeat the addition, the error increases. The following code shows that.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ3x4 myVar3x4 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar3x4), 4);&lt;br /&gt;
  Serial.print(“ “);&lt;br /&gt;
  myVar3x4 = myVar3x4 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 4.0000 4.0625 4.1250 4.1875 4.2500 4.3125 4.3750 4.4375 4.5000 4.5625 4.6250&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choosing larger &#039;&#039;&#039;fixed point&#039;&#039;&#039; data types increases the range of the number and accuracy of the fractional component at the expense of memory.&lt;br /&gt;
&lt;br /&gt;
== Why would I bother with Fixed Points at all? ==&lt;br /&gt;
&lt;br /&gt;
Imagine you are building a sideways-scrolling game where aliens (or cars, planes or other moving objects) are moving towards you at various speeds. With each pass through the program’s loop you could decrement the x coordinate of each alien but this would result in all aliens moving at the same speed. You could try to move aliens every second or third time through the loop by keeping some sort of counter but this can be error prone and&lt;br /&gt;
&lt;br /&gt;
may result in the aliens moving slowly. To really allow flexibility in the aliens’ movement, you could store the x and y in fixed point variables and increment these by a decimal value allowing the aliens to move at similar but slightly different speeds.&lt;br /&gt;
&lt;br /&gt;
Consider the Alien class below. Each alien is described with an x and y coordinate and takes care of its own movement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Alien {&lt;br /&gt;
 &lt;br /&gt;
   public:&lt;br /&gt;
 &lt;br /&gt;
     Alien(SQ7x8 x, SQ7x8 y, SQ7x8 incX, SQ7x8 incY, const uint8_t *bitmap) {&lt;br /&gt;
&lt;br /&gt;
       _x = x;&lt;br /&gt;
       _y = y;&lt;br /&gt;
       _incX = incX;&lt;br /&gt;
       _incY = incY;&lt;br /&gt;
       _bitmap = bitmap; &lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 getX() const { return _x; }&lt;br /&gt;
     SQ7x8 getY() const { return _y; }&lt;br /&gt;
 &lt;br /&gt;
     void setX(const SQ7x8 value) { _x = value; }&lt;br /&gt;
     void setY(const SQ7x8 value) { _y = value; }&lt;br /&gt;
 &lt;br /&gt;
     inline void Alien::move(uint8_t frame) {&lt;br /&gt;
 &lt;br /&gt;
       _x = _x - _incX;&lt;br /&gt;
       _y = _y - _incY;&lt;br /&gt;
       Sprites::drawOverwrite(_x.getInteger(), _y.getInteger(), _bitmap, frame);&lt;br /&gt;
   } &lt;br /&gt;
 &lt;br /&gt;
   private:&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 _x;&lt;br /&gt;
     SQ7x8 _y;&lt;br /&gt;
     SQ7x8 _incX;&lt;br /&gt;
     SQ7x8 _incY;&lt;br /&gt;
     const uint8_t *_bitmap;&lt;br /&gt;
   &lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And our main .ino file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Alien alien1 = {127, 16, (SQ7x8)0.9, (SQ7x8)1.34, alienBitmap };&lt;br /&gt;
Alien alien2 = {127, 32, (SQ7x8)1.7, (SQ7x8)1.76, alienBitmap };&lt;br /&gt;
Alien alien3 = {127, 48, (SQ7x8)1.5, (SQ7x8)2.13, alienBitmap }; &lt;br /&gt;
 &lt;br /&gt;
void loop() {&lt;br /&gt;
  alien1.move(frame);&lt;br /&gt;
  alien2.move(frame);&lt;br /&gt;
  alien3.move(frame);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With fixed point data types, we can have our aliens move at slightly different speeds without the overhead of using floating points.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Common Type Aliases&#039;&#039;&#039; ==&lt;br /&gt;
@Pharap’s library includes the following data types. However the library is implemented with templates and it is a relatively simple matter to declare your own type aliases.&lt;br /&gt;
&lt;br /&gt;
The predeclared aliases (which can be  used as an example of how to declare such an alias) can be found in &#039;&#039;&#039;src\ FixedPointsCommon\SFixedCommon.h and src\ FixedPointsCommon\UfixedCommon.h.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Unsigned Data Type Aliases:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|-&lt;br /&gt;
|UQ4x4&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 15&lt;br /&gt;
|1 / 16th&lt;br /&gt;
|0 to 16&lt;br /&gt;
|-&lt;br /&gt;
|UQ8x8&lt;br /&gt;
|16 bit (2 bytes)&lt;br /&gt;
|0 to 255&lt;br /&gt;
|1 / 256th&lt;br /&gt;
|0 to 256&lt;br /&gt;
|-&lt;br /&gt;
|UQ16x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|0 to 65,355&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|0 to 65,356&lt;br /&gt;
|-&lt;br /&gt;
|UQ32x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|0 to 4,294,967,295&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|0 to 4,294,967,296&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x7&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 128th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x15&lt;br /&gt;
|16 bit (2 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 32,768th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x31&lt;br /&gt;
|32 bit (4 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 2,147,483,648th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x63&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1/ 9,223,372,036,854,775,808th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Signed Data Type Aliases:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|-&lt;br /&gt;
|SQ3x4&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;15 to +15&lt;br /&gt;
|1 / 16th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;16 to +16&lt;br /&gt;
|-&lt;br /&gt;
|SQ7x8&lt;br /&gt;
|16 bit (2 bytes)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;127 to +127&lt;br /&gt;
|1 / 256th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;128 to +128&lt;br /&gt;
|-&lt;br /&gt;
|SQ15x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;32,767 to +32,767&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;32,768 to +32,768&lt;br /&gt;
|-&lt;br /&gt;
|SQ31x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2,147,483,647 to +2,147,483,647&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2,147,483,648 to +2,147,483,648&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x6&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;1 to +1&lt;br /&gt;
|1 / 64th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x14&lt;br /&gt;
|16 bit (2 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;1 to +1&lt;br /&gt;
|1 / 16,384th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x30&lt;br /&gt;
|32 bit (4 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;1 to +1&lt;br /&gt;
|1 / 1,073,741,824th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x62&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;1 to +1&lt;br /&gt;
|1/ 4,611,686,018,427,387,904th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2 to +2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt; Lower bound is inclusive, upper bound is exclusive&lt;br /&gt;
&lt;br /&gt;
FixedPoints can be found through the Arduino IDE library manager. If you have not used the library manager before see here: &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;https://arduboy.com/download-and-learn-arduino/&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Debugging_Code&amp;diff=618</id>
		<title>Debugging Code</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Debugging_Code&amp;diff=618"/>
		<updated>2024-08-17T02:49:24Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* But how do I turn sections on and off? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Most modern programming languages, such as C# or Java, and their respective development environments provide extensive tools for profiling and debugging code.  As such, developers take for granted the ability to set break points or alter variable values and code on the fly.  The Arduboy with its limited resources provides none of these facilities and debugging is often performed by logging status information to the console.&lt;br /&gt;
&lt;br /&gt;
Therefore developing on the Arduino and Arduboy is like stepping back in time for some developers - forcing them to re-think how to debug applications.&lt;br /&gt;
&lt;br /&gt;
== Debugging Code is Valuable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Due to the limited memory in an Arduino environment, most developers will add temporary logging within a troublesome procedure and then strip it out when the issue is resolved. Over the life of a project, the same logging code may be added and removed from a single procedure a number of times!&lt;br /&gt;
&lt;br /&gt;
I think logging code is valuable and should remain in the application at all times and be turned on or off at the flick of a switch.&lt;br /&gt;
&lt;br /&gt;
Consider the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
001  #define DEBUG&lt;br /&gt;
002&lt;br /&gt;
003  #ifdef DEBUG&lt;br /&gt;
004    #define DEBUG_print(x)        Serial.print(x)&lt;br /&gt;
005    #define DEBUG_printDec(x)     Serial.print(x, DEC)&lt;br /&gt;
006    #define DEBUG_println(x)      Serial.println(x)&lt;br /&gt;
007  #else&lt;br /&gt;
008    #define DEBUG_print(x)&lt;br /&gt;
009    #define DEBUG_printDec(x)&lt;br /&gt;
010    #define DEBUG_println(x)&lt;br /&gt;
011  #endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It uses C / C++ prototypes to define an identifier &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; (line 001) that indicates whether the application is in debug  mode or not. Lines 004 – 006 define function prototypes that will log a string or decimal value to the serial port using a syntax similar to the &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;println()&amp;lt;/code&amp;gt; functions.&lt;br /&gt;
&lt;br /&gt;
If the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier is not defined, the prototype functions are declared again but this time with no body (lines 008 – 010). This has the result of effectively dropping the function at compile time – resulting in a smaller compilation unit. Rather than deleting the &amp;lt;code&amp;gt;&#039;#define DEBUG&amp;lt;/code&amp;gt; line, I simply prefix the identifier with a few characters to change its value.&lt;br /&gt;
&lt;br /&gt;
Now that we have defined both the identifier and the prototype functions, we can use them in the following way. To log a simple message or variable value, use the prototype function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DEBUG_print(“Player X position : “);&lt;br /&gt;
DEBUG_println(x);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more complex logging, conditional include the code if the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier is defined:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
   for (byte x = 0; x &amp;lt; card_count; x++) {&lt;br /&gt;
     Serial.println(card[x]);&lt;br /&gt;
   }&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== But how do I turn sections on and off? ==&lt;br /&gt;
&lt;br /&gt;
As a program grows and more debugging code is added, you will invariably run into two problems – firstly, you have so much information being logged that you cannot see the forest for the trees and, secondly, that the actual logging code is taking up a significant portion of the minimal memory available.&lt;br /&gt;
&lt;br /&gt;
The following technique allows you to enable and disable logging overall and at the procedural level.&lt;br /&gt;
&lt;br /&gt;
In the previous example, a single identifier named &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; was used to turn on or off all debugging code. Identifier definitions can be nested in the Arduboy environment to create a hierarchy of debugging flags as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_A&lt;br /&gt;
#endif&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_B&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This definition provides an overall debugging identifier and individual identifiers for two procedures, &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; &amp;amp; &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt;. Overall debugging can be enabled or disabled by changing the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier to something else. Subsequent definitions of the identifiers &amp;lt;code&amp;gt;DEBUG_PROC_A&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;DEBUG_PROC_B&amp;lt;/code&amp;gt; will be skipped as they are conditioned on the presence of identifier &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Debugging of individual procedures can be controlled by altering their identifiers to include or exclude them.&lt;br /&gt;
&lt;br /&gt;
The sample application below shows these flags in action:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
   #define DEBUG_PROC_A&lt;br /&gt;
#endif&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
   #define DEBUG_PROC_B&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
  #ifdef DEBUG&lt;br /&gt;
     Serial.println(“Loop(): Start”);&lt;br /&gt;
  #endif&lt;br /&gt;
&lt;br /&gt;
  proc_A();&lt;br /&gt;
  proc_B(12, 14);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void proc_A() {&lt;br /&gt;
  #ifdef DEBUG_PROC_A&lt;br /&gt;
     Serial.println(“proc_A(): Start”);&lt;br /&gt;
  #endif &lt;br /&gt;
  ... actual code.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void proc_B(uint8_t param1, uint8_t param2) {&lt;br /&gt;
  #ifdef DEBUG_PROC_B&lt;br /&gt;
     Serial.print(“proc_B(): Start);&lt;br /&gt;
  #endif&lt;br /&gt;
  ... actual code.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This technique can be extended to provide granular logging within a procedure itself. The example below shows how the level of logging could be refined in &amp;lt;code&amp;gt;Procedure B&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
 &lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_A&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_B&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG_PROC_B&lt;br /&gt;
  #define DEBUG_PROC_B_CALL&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG_PROC_B&lt;br /&gt;
  #define DEBUG_PROC_B_PARAMS&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then, within the procedure itself, the new logging levels are incorporated to provide more information as the logging level is increased.&lt;br /&gt;
&lt;br /&gt;
In the example below, if the &amp;lt;code&amp;gt;DEBUG_ PROC_B_CALL&amp;lt;/code&amp;gt; identifier is defined without &amp;lt;code&amp;gt;DEBUG_PROC_B_PARAMS&amp;lt;/code&amp;gt; identifier, the call to the procedure will only log the&lt;br /&gt;
text &amp;lt;code&amp;gt;proc_B(): Start&amp;lt;/code&amp;gt;. Including identifier &amp;lt;code&amp;gt;DEBUG_PROC_B_PARAMS&amp;lt;/code&amp;gt; will also log out the parameter values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void proc_B(uint8_t param1, uint8_t param2) {&lt;br /&gt;
&lt;br /&gt;
  #ifdef DEBUG_PROC_B_CALL&lt;br /&gt;
    Serial.println(“proc_B(): Start”);&lt;br /&gt;
  #endif&lt;br /&gt;
  #ifdef DEBUG_PROC_B_PARAMS&lt;br /&gt;
    Serial.print(&amp;quot;&amp;gt; param1 = &amp;quot;);&lt;br /&gt;
    Serial.println(param1);&lt;br /&gt;
    Serial.println(&amp;quot;&amp;gt; param2 = &amp;quot;);&lt;br /&gt;
    Serial.println(param2);&lt;br /&gt;
  #endif&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Almost there .. ==&lt;br /&gt;
&lt;br /&gt;
Previously we defined a number of prototype functions to allow us to conditionally include a &amp;lt;code&amp;gt;Serial.print()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;Serial.println()&amp;lt;/code&amp;gt; in our code.&lt;br /&gt;
These prototypes can be expanded to include additional information, such as the function (procedure) name, file name and line number, as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_println(x)        Serial.println(x)&lt;br /&gt;
  #define DEBUG_printDec(x)       Serial.println(x, DEC)&lt;br /&gt;
&lt;br /&gt;
  #define DEBUG_println(str)&lt;br /&gt;
    Serial.print(__FILE__);&lt;br /&gt;
    Serial.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
    Serial.print(__FUNCTION__);&lt;br /&gt;
    Serial.print(&amp;quot;: &amp;quot;);&lt;br /&gt;
    Serial.print(__LINE__);&lt;br /&gt;
    Serial.print(&amp;quot; (&amp;quot;);&lt;br /&gt;
    Serial.print(millis());&lt;br /&gt;
    Serial.print(&amp;quot;) &amp;quot;);&lt;br /&gt;
    Serial.println(str);&lt;br /&gt;
  #endif&lt;br /&gt;
&lt;br /&gt;
#else&lt;br /&gt;
  #define DEBUG_print(x)&lt;br /&gt;
  #define DEBUG_printDec(x)&lt;br /&gt;
  #define DEBUG_println(x)&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Last - but certainly not least – my final tip. When developing and testing logic games, it is often desirable to have the pseudo-random number generator produce the same series of numbers each time thus allowing you to test a sequence of events again and again until your code performs correctly.&lt;br /&gt;
&lt;br /&gt;
I use the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier to condition the seeding of the number sequence – if I am debugging the code, I want the seed to be a constant otherwise I seed the generator with another random number (in this case the value read from an analogue port with a floating pin).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
    int seed = 1;&lt;br /&gt;
#else&lt;br /&gt;
    int seed = analogRead(3);&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
randomSeed(seed);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Debugging_Code&amp;diff=617</id>
		<title>Debugging Code</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Debugging_Code&amp;diff=617"/>
		<updated>2024-08-17T02:48:54Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* But how do I turn sections on and off? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Most modern programming languages, such as C# or Java, and their respective development environments provide extensive tools for profiling and debugging code.  As such, developers take for granted the ability to set break points or alter variable values and code on the fly.  The Arduboy with its limited resources provides none of these facilities and debugging is often performed by logging status information to the console.&lt;br /&gt;
&lt;br /&gt;
Therefore developing on the Arduino and Arduboy is like stepping back in time for some developers - forcing them to re-think how to debug applications.&lt;br /&gt;
&lt;br /&gt;
== Debugging Code is Valuable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Due to the limited memory in an Arduino environment, most developers will add temporary logging within a troublesome procedure and then strip it out when the issue is resolved. Over the life of a project, the same logging code may be added and removed from a single procedure a number of times!&lt;br /&gt;
&lt;br /&gt;
I think logging code is valuable and should remain in the application at all times and be turned on or off at the flick of a switch.&lt;br /&gt;
&lt;br /&gt;
Consider the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
001  #define DEBUG&lt;br /&gt;
002&lt;br /&gt;
003  #ifdef DEBUG&lt;br /&gt;
004    #define DEBUG_print(x)        Serial.print(x)&lt;br /&gt;
005    #define DEBUG_printDec(x)     Serial.print(x, DEC)&lt;br /&gt;
006    #define DEBUG_println(x)      Serial.println(x)&lt;br /&gt;
007  #else&lt;br /&gt;
008    #define DEBUG_print(x)&lt;br /&gt;
009    #define DEBUG_printDec(x)&lt;br /&gt;
010    #define DEBUG_println(x)&lt;br /&gt;
011  #endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It uses C / C++ prototypes to define an identifier &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; (line 001) that indicates whether the application is in debug  mode or not. Lines 004 – 006 define function prototypes that will log a string or decimal value to the serial port using a syntax similar to the &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;println()&amp;lt;/code&amp;gt; functions.&lt;br /&gt;
&lt;br /&gt;
If the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier is not defined, the prototype functions are declared again but this time with no body (lines 008 – 010). This has the result of effectively dropping the function at compile time – resulting in a smaller compilation unit. Rather than deleting the &amp;lt;code&amp;gt;&#039;#define DEBUG&amp;lt;/code&amp;gt; line, I simply prefix the identifier with a few characters to change its value.&lt;br /&gt;
&lt;br /&gt;
Now that we have defined both the identifier and the prototype functions, we can use them in the following way. To log a simple message or variable value, use the prototype function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DEBUG_print(“Player X position : “);&lt;br /&gt;
DEBUG_println(x);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more complex logging, conditional include the code if the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier is defined:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
   for (byte x = 0; x &amp;lt; card_count; x++) {&lt;br /&gt;
     Serial.println(card[x]);&lt;br /&gt;
   }&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== But how do I turn sections on and off? ==&lt;br /&gt;
&lt;br /&gt;
As a program grows and more debugging code is added, you will invariably run into two problems – firstly, you have so much information being logged that you cannot see the forest for the trees and, secondly, that the actual logging code is taking up a significant portion of the minimal memory available.&lt;br /&gt;
&lt;br /&gt;
The following technique allows you to enable and disable logging overall and at the procedural level.&lt;br /&gt;
&lt;br /&gt;
In the previous example, a single identifier named &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; was used to turn on or off all debugging code. Identifier definitions can be nested in the Arduboy environment to create a hierarchy of debugging flags as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_A&lt;br /&gt;
#endif&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_B&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This definition provides an overall debugging identifier and individual identifiers for two procedures, &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; &amp;amp; &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt;. Overall debugging can be enabled or disabled by changing the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier to something else. Subsequent definitions of the identifiers &amp;lt;code&amp;gt;DEBUG_PROC_A&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;DEBUG_PROC_B&amp;lt;/code&amp;gt; will be skipped as they are conditioned on the presence of identifier &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Debugging of individual procedures can be controlled by altering their identifiers to include or exclude them.&lt;br /&gt;
&lt;br /&gt;
The sample application below shows these flags in action:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
   #define DEBUG_PROC_A&lt;br /&gt;
#endif&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
   #define DEBUG_PROC_B&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
  #ifdef DEBUG&lt;br /&gt;
     Serial.println(“Loop(): Start”);&lt;br /&gt;
  #endif&lt;br /&gt;
&lt;br /&gt;
  proc_A();&lt;br /&gt;
  proc_B(12, 14);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void proc_A() {&lt;br /&gt;
  #ifdef DEBUG_PROC_A&lt;br /&gt;
     Serial.println(“proc_A(): Start”);&lt;br /&gt;
  #endif &lt;br /&gt;
  ... actual code.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void proc_B(uint8_t param1, uint8_t param2) {&lt;br /&gt;
  #ifdef DEBUG_PROC_B&lt;br /&gt;
     Serial.print(“proc_B(): Start);&lt;br /&gt;
  #endif&lt;br /&gt;
  ... actual code.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This technique can be extended to provide granular logging within a procedure itself. The example below shows how the level of logging could be refined in &amp;lt;code&amp;gt;Procedure B&amp;lt;code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
 &lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_A&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_B&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG_PROC_B&lt;br /&gt;
  #define DEBUG_PROC_B_CALL&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG_PROC_B&lt;br /&gt;
  #define DEBUG_PROC_B_PARAMS&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then, within the procedure itself, the new logging levels are incorporated to provide more information as the logging level is increased.&lt;br /&gt;
&lt;br /&gt;
In the example below, if the &amp;lt;code&amp;gt;DEBUG_ PROC_B_CALL&amp;lt;/code&amp;gt; identifier is defined without &amp;lt;code&amp;gt;DEBUG_PROC_B_PARAMS&amp;lt;/code&amp;gt; identifier, the call to the procedure will only log the&lt;br /&gt;
text &amp;lt;code&amp;gt;proc_B(): Start&amp;lt;/code&amp;gt;. Including identifier &amp;lt;code&amp;gt;DEBUG_PROC_B_PARAMS&amp;lt;/code&amp;gt; will also log out the parameter values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void proc_B(uint8_t param1, uint8_t param2) {&lt;br /&gt;
&lt;br /&gt;
  #ifdef DEBUG_PROC_B_CALL&lt;br /&gt;
    Serial.println(“proc_B(): Start”);&lt;br /&gt;
  #endif&lt;br /&gt;
  #ifdef DEBUG_PROC_B_PARAMS&lt;br /&gt;
    Serial.print(&amp;quot;&amp;gt; param1 = &amp;quot;);&lt;br /&gt;
    Serial.println(param1);&lt;br /&gt;
    Serial.println(&amp;quot;&amp;gt; param2 = &amp;quot;);&lt;br /&gt;
    Serial.println(param2);&lt;br /&gt;
  #endif&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Almost there .. ==&lt;br /&gt;
&lt;br /&gt;
Previously we defined a number of prototype functions to allow us to conditionally include a &amp;lt;code&amp;gt;Serial.print()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;Serial.println()&amp;lt;/code&amp;gt; in our code.&lt;br /&gt;
These prototypes can be expanded to include additional information, such as the function (procedure) name, file name and line number, as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_println(x)        Serial.println(x)&lt;br /&gt;
  #define DEBUG_printDec(x)       Serial.println(x, DEC)&lt;br /&gt;
&lt;br /&gt;
  #define DEBUG_println(str)&lt;br /&gt;
    Serial.print(__FILE__);&lt;br /&gt;
    Serial.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
    Serial.print(__FUNCTION__);&lt;br /&gt;
    Serial.print(&amp;quot;: &amp;quot;);&lt;br /&gt;
    Serial.print(__LINE__);&lt;br /&gt;
    Serial.print(&amp;quot; (&amp;quot;);&lt;br /&gt;
    Serial.print(millis());&lt;br /&gt;
    Serial.print(&amp;quot;) &amp;quot;);&lt;br /&gt;
    Serial.println(str);&lt;br /&gt;
  #endif&lt;br /&gt;
&lt;br /&gt;
#else&lt;br /&gt;
  #define DEBUG_print(x)&lt;br /&gt;
  #define DEBUG_printDec(x)&lt;br /&gt;
  #define DEBUG_println(x)&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Last - but certainly not least – my final tip. When developing and testing logic games, it is often desirable to have the pseudo-random number generator produce the same series of numbers each time thus allowing you to test a sequence of events again and again until your code performs correctly.&lt;br /&gt;
&lt;br /&gt;
I use the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier to condition the seeding of the number sequence – if I am debugging the code, I want the seed to be a constant otherwise I seed the generator with another random number (in this case the value read from an analogue port with a floating pin).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
    int seed = 1;&lt;br /&gt;
#else&lt;br /&gt;
    int seed = analogRead(3);&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
randomSeed(seed);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Debugging_Code&amp;diff=616</id>
		<title>Debugging Code</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Debugging_Code&amp;diff=616"/>
		<updated>2024-08-17T02:48:34Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* Debugging Code is Valuable */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Most modern programming languages, such as C# or Java, and their respective development environments provide extensive tools for profiling and debugging code.  As such, developers take for granted the ability to set break points or alter variable values and code on the fly.  The Arduboy with its limited resources provides none of these facilities and debugging is often performed by logging status information to the console.&lt;br /&gt;
&lt;br /&gt;
Therefore developing on the Arduino and Arduboy is like stepping back in time for some developers - forcing them to re-think how to debug applications.&lt;br /&gt;
&lt;br /&gt;
== Debugging Code is Valuable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Due to the limited memory in an Arduino environment, most developers will add temporary logging within a troublesome procedure and then strip it out when the issue is resolved. Over the life of a project, the same logging code may be added and removed from a single procedure a number of times!&lt;br /&gt;
&lt;br /&gt;
I think logging code is valuable and should remain in the application at all times and be turned on or off at the flick of a switch.&lt;br /&gt;
&lt;br /&gt;
Consider the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
001  #define DEBUG&lt;br /&gt;
002&lt;br /&gt;
003  #ifdef DEBUG&lt;br /&gt;
004    #define DEBUG_print(x)        Serial.print(x)&lt;br /&gt;
005    #define DEBUG_printDec(x)     Serial.print(x, DEC)&lt;br /&gt;
006    #define DEBUG_println(x)      Serial.println(x)&lt;br /&gt;
007  #else&lt;br /&gt;
008    #define DEBUG_print(x)&lt;br /&gt;
009    #define DEBUG_printDec(x)&lt;br /&gt;
010    #define DEBUG_println(x)&lt;br /&gt;
011  #endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It uses C / C++ prototypes to define an identifier &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; (line 001) that indicates whether the application is in debug  mode or not. Lines 004 – 006 define function prototypes that will log a string or decimal value to the serial port using a syntax similar to the &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;println()&amp;lt;/code&amp;gt; functions.&lt;br /&gt;
&lt;br /&gt;
If the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier is not defined, the prototype functions are declared again but this time with no body (lines 008 – 010). This has the result of effectively dropping the function at compile time – resulting in a smaller compilation unit. Rather than deleting the &amp;lt;code&amp;gt;&#039;#define DEBUG&amp;lt;/code&amp;gt; line, I simply prefix the identifier with a few characters to change its value.&lt;br /&gt;
&lt;br /&gt;
Now that we have defined both the identifier and the prototype functions, we can use them in the following way. To log a simple message or variable value, use the prototype function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DEBUG_print(“Player X position : “);&lt;br /&gt;
DEBUG_println(x);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more complex logging, conditional include the code if the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier is defined:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
   for (byte x = 0; x &amp;lt; card_count; x++) {&lt;br /&gt;
     Serial.println(card[x]);&lt;br /&gt;
   }&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== But how do I turn sections on and off? ==&lt;br /&gt;
&lt;br /&gt;
As a program grows and more debugging code is added, you will invariably run into two problems – firstly, you have so much information being logged that you cannot see the forest for the trees and, secondly, that the actual logging code is taking up a significant portion of the minimal memory available.&lt;br /&gt;
&lt;br /&gt;
The following technique allows you to enable and disable logging overall and at the procedural level.&lt;br /&gt;
&lt;br /&gt;
In the previous example, a single identifier named &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; was used to turn on or off all debugging code. Identifier definitions can be nested in the Arduboy environment to create a hierarchy of debugging flags as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_A&lt;br /&gt;
#endif&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_B&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This definition provides an overall debugging identifier and individual identifiers for two procedures, &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; &amp;amp; &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt;. Overall debugging can be enabled or disabled by changing the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier to something else. Subsequent definitions of the identifiers &amp;lt;code&amp;gt;DEBUG_PROC_A&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;DEBUG_PROC_B&amp;lt;/code&amp;gt; will be skipped as they are conditioned on the presence of identifier &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Debugging of individual procedures can be controlled by altering their identifiers to include or exclude them.&lt;br /&gt;
&lt;br /&gt;
The sample application below shows these flags in action:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
   #define DEBUG_PROC_A&lt;br /&gt;
#endif&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
   #define DEBUG_PROC_B&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
  #ifdef DEBUG&lt;br /&gt;
     Serial.println(“Loop(): Start”);&lt;br /&gt;
  #endif&lt;br /&gt;
&lt;br /&gt;
  proc_A();&lt;br /&gt;
  proc_B(12, 14);&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void proc_A() {&lt;br /&gt;
  #ifdef DEBUG_PROC_A&lt;br /&gt;
     Serial.println(“proc_A(): Start”);&lt;br /&gt;
  #endif &lt;br /&gt;
  ... actual code.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void proc_B(uint8_t param1, uint8_t param2) {&lt;br /&gt;
  #ifdef DEBUG_PROC_B&lt;br /&gt;
     Serial.print(“proc_B(): Start);&lt;br /&gt;
  #endif&lt;br /&gt;
  ... actual code.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This technique can be extended to provide granular logging within a procedure itself. The example below shows how the level of logging could be refined in &amp;lt;code&amp;gt;Procedure B&amp;lt;code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
 &lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_A&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_B&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG_PROC_B&lt;br /&gt;
  #define DEBUG_PROC_B_CALL&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG_PROC_B&lt;br /&gt;
  #define DEBUG_PROC_B_PARAMS&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then, within the procedure itself, the new logging levels are incorporated to provide more information as the logging level is increased.&lt;br /&gt;
&lt;br /&gt;
In the example below, if the &amp;lt;code&amp;gt;DEBUG_ PROC_B_CALL&amp;lt;/code&amp;gt; identifier is defined without &amp;lt;code&amp;gt;DEBUG_PROC_B_PARAMS&amp;lt;/code&amp;gt; identifier, the call to the procedure will only log the&lt;br /&gt;
text &amp;lt;code&amp;gt;proc_B(): Start&amp;lt;/code&amp;gt;. Including identifier &amp;lt;code&amp;gt;DEBUG_PROC_B_PARAMS&amp;lt;/code&amp;gt; will also log out the parameter values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void proc_B(uint8_t param1, uint8_t param2) {&lt;br /&gt;
&lt;br /&gt;
  #ifdef DEBUG_PROC_B_CALL&lt;br /&gt;
    Serial.println(“proc_B(): Start”);&lt;br /&gt;
  #endif&lt;br /&gt;
  #ifdef DEBUG_PROC_B_PARAMS&lt;br /&gt;
    Serial.print(&amp;quot;&amp;gt; param1 = &amp;quot;);&lt;br /&gt;
    Serial.println(param1);&lt;br /&gt;
    Serial.println(&amp;quot;&amp;gt; param2 = &amp;quot;);&lt;br /&gt;
    Serial.println(param2);&lt;br /&gt;
  #endif&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Almost there .. ==&lt;br /&gt;
&lt;br /&gt;
Previously we defined a number of prototype functions to allow us to conditionally include a &amp;lt;code&amp;gt;Serial.print()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;Serial.println()&amp;lt;/code&amp;gt; in our code.&lt;br /&gt;
These prototypes can be expanded to include additional information, such as the function (procedure) name, file name and line number, as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_println(x)        Serial.println(x)&lt;br /&gt;
  #define DEBUG_printDec(x)       Serial.println(x, DEC)&lt;br /&gt;
&lt;br /&gt;
  #define DEBUG_println(str)&lt;br /&gt;
    Serial.print(__FILE__);&lt;br /&gt;
    Serial.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
    Serial.print(__FUNCTION__);&lt;br /&gt;
    Serial.print(&amp;quot;: &amp;quot;);&lt;br /&gt;
    Serial.print(__LINE__);&lt;br /&gt;
    Serial.print(&amp;quot; (&amp;quot;);&lt;br /&gt;
    Serial.print(millis());&lt;br /&gt;
    Serial.print(&amp;quot;) &amp;quot;);&lt;br /&gt;
    Serial.println(str);&lt;br /&gt;
  #endif&lt;br /&gt;
&lt;br /&gt;
#else&lt;br /&gt;
  #define DEBUG_print(x)&lt;br /&gt;
  #define DEBUG_printDec(x)&lt;br /&gt;
  #define DEBUG_println(x)&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Last - but certainly not least – my final tip. When developing and testing logic games, it is often desirable to have the pseudo-random number generator produce the same series of numbers each time thus allowing you to test a sequence of events again and again until your code performs correctly.&lt;br /&gt;
&lt;br /&gt;
I use the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier to condition the seeding of the number sequence – if I am debugging the code, I want the seed to be a constant otherwise I seed the generator with another random number (in this case the value read from an analogue port with a floating pin).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
    int seed = 1;&lt;br /&gt;
#else&lt;br /&gt;
    int seed = analogRead(3);&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
randomSeed(seed);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Debugging_Code&amp;diff=615</id>
		<title>Debugging Code</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Debugging_Code&amp;diff=615"/>
		<updated>2024-08-17T02:48:09Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* But how do I turn sections on and off? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Most modern programming languages, such as C# or Java, and their respective development environments provide extensive tools for profiling and debugging code.  As such, developers take for granted the ability to set break points or alter variable values and code on the fly.  The Arduboy with its limited resources provides none of these facilities and debugging is often performed by logging status information to the console.&lt;br /&gt;
&lt;br /&gt;
Therefore developing on the Arduino and Arduboy is like stepping back in time for some developers - forcing them to re-think how to debug applications.&lt;br /&gt;
&lt;br /&gt;
== Debugging Code is Valuable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Due to the limited memory in an Arduino environment, most developers will add temporary logging within a troublesome procedure and then strip it out when the issue is resolved. Over the life of a project, the same logging code may be added and removed from a single procedure a number of times!&lt;br /&gt;
&lt;br /&gt;
I think logging code is valuable and should remain in the application at all times and be turned on or off at the flick of a switch.&lt;br /&gt;
&lt;br /&gt;
Consider the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
001  #define DEBUG&lt;br /&gt;
002&lt;br /&gt;
003  #ifdef DEBUG&lt;br /&gt;
004    #define DEBUG_print(x)        Serial.print(x)&lt;br /&gt;
005    #define DEBUG_printDec(x)     Serial.print(x, DEC)&lt;br /&gt;
006    #define DEBUG_println(x)      Serial.println(x)&lt;br /&gt;
007  #else&lt;br /&gt;
008    #define DEBUG_print(x)&lt;br /&gt;
009    #define DEBUG_printDec(x)&lt;br /&gt;
010    #define DEBUG_println(x)&lt;br /&gt;
011  #endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It uses C / C++ prototypes to define an identifier &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; (line 001) that indicates whether the application is in debug  mode or not. Lines 004 – 006 define function prototypes that will log a string or decimal value to the serial port using a syntax similar to the &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;println()&amp;lt;/code&amp;gt; functions.&lt;br /&gt;
&lt;br /&gt;
If the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier is not defined, the prototype functions are declared again but this time with no body (lines 08 – 10). This has the result of effectively dropping the function at compile time – resulting in a smaller compilation unit. Rather than deleting the &amp;lt;code&amp;gt;&#039;#define DEBUG&amp;lt;/code&amp;gt; line, I simply prefix the identifier with a few characters to change its value.&lt;br /&gt;
&lt;br /&gt;
Now that we have defined both the identifier and the prototype functions, we can use them in the following way. To log a simple message or variable value, use the prototype function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DEBUG_print(“Player X position : “);&lt;br /&gt;
DEBUG_println(x);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more complex logging, conditional include the code if the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier is defined:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
   for (byte x = 0; x &amp;lt; card_count; x++) {&lt;br /&gt;
     Serial.println(card[x]);&lt;br /&gt;
   }&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== But how do I turn sections on and off? ==&lt;br /&gt;
&lt;br /&gt;
As a program grows and more debugging code is added, you will invariably run into two problems – firstly, you have so much information being logged that you cannot see the forest for the trees and, secondly, that the actual logging code is taking up a significant portion of the minimal memory available.&lt;br /&gt;
&lt;br /&gt;
The following technique allows you to enable and disable logging overall and at the procedural level.&lt;br /&gt;
&lt;br /&gt;
In the previous example, a single identifier named &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; was used to turn on or off all debugging code. Identifier definitions can be nested in the Arduboy environment to create a hierarchy of debugging flags as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_A&lt;br /&gt;
#endif&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_B&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This definition provides an overall debugging identifier and individual identifiers for two procedures, &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; &amp;amp; &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt;. Overall debugging can be enabled or disabled by changing the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier to something else. Subsequent definitions of the identifiers &amp;lt;code&amp;gt;DEBUG_PROC_A&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;DEBUG_PROC_B&amp;lt;/code&amp;gt; will be skipped as they are conditioned on the presence of identifier &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Debugging of individual procedures can be controlled by altering their identifiers to include or exclude them.&lt;br /&gt;
&lt;br /&gt;
The sample application below shows these flags in action:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
   #define DEBUG_PROC_A&lt;br /&gt;
#endif&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
   #define DEBUG_PROC_B&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
  #ifdef DEBUG&lt;br /&gt;
     Serial.println(“Loop(): Start”);&lt;br /&gt;
  #endif&lt;br /&gt;
&lt;br /&gt;
  proc_A();&lt;br /&gt;
  proc_B(12, 14);&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void proc_A() {&lt;br /&gt;
  #ifdef DEBUG_PROC_A&lt;br /&gt;
     Serial.println(“proc_A(): Start”);&lt;br /&gt;
  #endif &lt;br /&gt;
  ... actual code.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void proc_B(uint8_t param1, uint8_t param2) {&lt;br /&gt;
  #ifdef DEBUG_PROC_B&lt;br /&gt;
     Serial.print(“proc_B(): Start);&lt;br /&gt;
  #endif&lt;br /&gt;
  ... actual code.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This technique can be extended to provide granular logging within a procedure itself. The example below shows how the level of logging could be refined in &amp;lt;code&amp;gt;Procedure B&amp;lt;code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
 &lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_A&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_B&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG_PROC_B&lt;br /&gt;
  #define DEBUG_PROC_B_CALL&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG_PROC_B&lt;br /&gt;
  #define DEBUG_PROC_B_PARAMS&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then, within the procedure itself, the new logging levels are incorporated to provide more information as the logging level is increased.&lt;br /&gt;
&lt;br /&gt;
In the example below, if the &amp;lt;code&amp;gt;DEBUG_ PROC_B_CALL&amp;lt;/code&amp;gt; identifier is defined without &amp;lt;code&amp;gt;DEBUG_PROC_B_PARAMS&amp;lt;/code&amp;gt; identifier, the call to the procedure will only log the&lt;br /&gt;
text &amp;lt;code&amp;gt;proc_B(): Start&amp;lt;/code&amp;gt;. Including identifier &amp;lt;code&amp;gt;DEBUG_PROC_B_PARAMS&amp;lt;/code&amp;gt; will also log out the parameter values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void proc_B(uint8_t param1, uint8_t param2) {&lt;br /&gt;
&lt;br /&gt;
  #ifdef DEBUG_PROC_B_CALL&lt;br /&gt;
    Serial.println(“proc_B(): Start”);&lt;br /&gt;
  #endif&lt;br /&gt;
  #ifdef DEBUG_PROC_B_PARAMS&lt;br /&gt;
    Serial.print(&amp;quot;&amp;gt; param1 = &amp;quot;);&lt;br /&gt;
    Serial.println(param1);&lt;br /&gt;
    Serial.println(&amp;quot;&amp;gt; param2 = &amp;quot;);&lt;br /&gt;
    Serial.println(param2);&lt;br /&gt;
  #endif&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Almost there .. ==&lt;br /&gt;
&lt;br /&gt;
Previously we defined a number of prototype functions to allow us to conditionally include a &amp;lt;code&amp;gt;Serial.print()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;Serial.println()&amp;lt;/code&amp;gt; in our code.&lt;br /&gt;
These prototypes can be expanded to include additional information, such as the function (procedure) name, file name and line number, as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_println(x)        Serial.println(x)&lt;br /&gt;
  #define DEBUG_printDec(x)       Serial.println(x, DEC)&lt;br /&gt;
&lt;br /&gt;
  #define DEBUG_println(str)&lt;br /&gt;
    Serial.print(__FILE__);&lt;br /&gt;
    Serial.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
    Serial.print(__FUNCTION__);&lt;br /&gt;
    Serial.print(&amp;quot;: &amp;quot;);&lt;br /&gt;
    Serial.print(__LINE__);&lt;br /&gt;
    Serial.print(&amp;quot; (&amp;quot;);&lt;br /&gt;
    Serial.print(millis());&lt;br /&gt;
    Serial.print(&amp;quot;) &amp;quot;);&lt;br /&gt;
    Serial.println(str);&lt;br /&gt;
  #endif&lt;br /&gt;
&lt;br /&gt;
#else&lt;br /&gt;
  #define DEBUG_print(x)&lt;br /&gt;
  #define DEBUG_printDec(x)&lt;br /&gt;
  #define DEBUG_println(x)&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Last - but certainly not least – my final tip. When developing and testing logic games, it is often desirable to have the pseudo-random number generator produce the same series of numbers each time thus allowing you to test a sequence of events again and again until your code performs correctly.&lt;br /&gt;
&lt;br /&gt;
I use the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier to condition the seeding of the number sequence – if I am debugging the code, I want the seed to be a constant otherwise I seed the generator with another random number (in this case the value read from an analogue port with a floating pin).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
    int seed = 1;&lt;br /&gt;
#else&lt;br /&gt;
    int seed = analogRead(3);&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
randomSeed(seed);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Debugging_Code&amp;diff=614</id>
		<title>Debugging Code</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Debugging_Code&amp;diff=614"/>
		<updated>2024-08-17T02:47:25Z</updated>

		<summary type="html">&lt;p&gt;Filmote: Created page with &amp;quot;Most modern programming languages, such as C# or Java, and their respective development environments provide extensive tools for profiling and debugging code.  As such, developers take for granted the ability to set break points or alter variable values and code on the fly.  The Arduboy with its limited resources provides none of these facilities and debugging is often performed by logging status information to the console.  Therefore developing on the Arduino and Ardubo...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Most modern programming languages, such as C# or Java, and their respective development environments provide extensive tools for profiling and debugging code.  As such, developers take for granted the ability to set break points or alter variable values and code on the fly.  The Arduboy with its limited resources provides none of these facilities and debugging is often performed by logging status information to the console.&lt;br /&gt;
&lt;br /&gt;
Therefore developing on the Arduino and Arduboy is like stepping back in time for some developers - forcing them to re-think how to debug applications.&lt;br /&gt;
&lt;br /&gt;
== Debugging Code is Valuable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Due to the limited memory in an Arduino environment, most developers will add temporary logging within a troublesome procedure and then strip it out when the issue is resolved. Over the life of a project, the same logging code may be added and removed from a single procedure a number of times!&lt;br /&gt;
&lt;br /&gt;
I think logging code is valuable and should remain in the application at all times and be turned on or off at the flick of a switch.&lt;br /&gt;
&lt;br /&gt;
Consider the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
001  #define DEBUG&lt;br /&gt;
002&lt;br /&gt;
003  #ifdef DEBUG&lt;br /&gt;
004    #define DEBUG_print(x)        Serial.print(x)&lt;br /&gt;
005    #define DEBUG_printDec(x)     Serial.print(x, DEC)&lt;br /&gt;
006    #define DEBUG_println(x)      Serial.println(x)&lt;br /&gt;
007  #else&lt;br /&gt;
008    #define DEBUG_print(x)&lt;br /&gt;
009    #define DEBUG_printDec(x)&lt;br /&gt;
010    #define DEBUG_println(x)&lt;br /&gt;
011  #endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It uses C / C++ prototypes to define an identifier &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; (line 001) that indicates whether the application is in debug  mode or not. Lines 004 – 006 define function prototypes that will log a string or decimal value to the serial port using a syntax similar to the &amp;lt;code&amp;gt;print()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;println()&amp;lt;/code&amp;gt; functions.&lt;br /&gt;
&lt;br /&gt;
If the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier is not defined, the prototype functions are declared again but this time with no body (lines 08 – 10). This has the result of effectively dropping the function at compile time – resulting in a smaller compilation unit. Rather than deleting the &amp;lt;code&amp;gt;&#039;#define DEBUG&amp;lt;/code&amp;gt; line, I simply prefix the identifier with a few characters to change its value.&lt;br /&gt;
&lt;br /&gt;
Now that we have defined both the identifier and the prototype functions, we can use them in the following way. To log a simple message or variable value, use the prototype function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DEBUG_print(“Player X position : “);&lt;br /&gt;
DEBUG_println(x);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more complex logging, conditional include the code if the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier is defined:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
   for (byte x = 0; x &amp;lt; card_count; x++) {&lt;br /&gt;
     Serial.println(card[x]);&lt;br /&gt;
   }&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== But how do I turn sections on and off? ==&lt;br /&gt;
&lt;br /&gt;
As a program grows and more debugging code is added, you will invariably run into two problems – firstly, you have so much information being logged that you cannot see the forest for the trees and, secondly, that the actual logging code is taking up a significant portion of the minimal memory available.&lt;br /&gt;
&lt;br /&gt;
The following technique allows you to enable and disable logging overall and at the procedural level.&lt;br /&gt;
&lt;br /&gt;
In the previous example, a single identifier named &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; was used to turn on or off all debugging code. Identifier definitions can be nested in the Arduboy environment to create a hierarchy of debugging flags as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_A&lt;br /&gt;
#endif&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_B&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This definition provides an overall debugging identifier and individual identifiers for two procedures, &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; &amp;amp; &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt;. Overall debugging can be enabled or disabled by changing the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier to something else. Subsequent definitions of the identifiers &amp;lt;code&amp;gt;DEBUG_PROC_A&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;DEBUG_PROC_B&amp;lt;/code&amp;gt; will be skipped as they are conditioned on the presence of identifier &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Debugging of individual procedures can be controlled by altering their identifiers to include or exclude them.&lt;br /&gt;
&lt;br /&gt;
The sample application below shows these flags in action:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
   #define DEBUG_PROC_A&lt;br /&gt;
#endif&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
   #define DEBUG_PROC_B&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
&lt;br /&gt;
  #ifdef DEBUG&lt;br /&gt;
     Serial.println(“Loop(): Start”);&lt;br /&gt;
  #endif&lt;br /&gt;
&lt;br /&gt;
  proc_A();&lt;br /&gt;
  proc_B(12, 14);&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void proc_A() {&lt;br /&gt;
&lt;br /&gt;
  #ifdef DEBUG_PROC_A&lt;br /&gt;
     Serial.println(“proc_A(): Start”);&lt;br /&gt;
  #endif &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void proc_B(uint8_t param1, uint8_t param2) {&lt;br /&gt;
&lt;br /&gt;
  #ifdef DEBUG_PROC_B&lt;br /&gt;
     Serial.print(“proc_B(): Start);&lt;br /&gt;
  #endif&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This technique can be extended to provide granular logging within a procedure itself. The example below shows how the level of logging could be refined in &amp;lt;code&amp;gt;Procedure B&amp;lt;code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
 &lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_A&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_PROC_B&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG_PROC_B&lt;br /&gt;
  #define DEBUG_PROC_B_CALL&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG_PROC_B&lt;br /&gt;
  #define DEBUG_PROC_B_PARAMS&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then, within the procedure itself, the new logging levels are incorporated to provide more information as the logging level is increased.&lt;br /&gt;
&lt;br /&gt;
In the example below, if the &amp;lt;code&amp;gt;DEBUG_ PROC_B_CALL&amp;lt;/code&amp;gt; identifier is defined without &amp;lt;code&amp;gt;DEBUG_PROC_B_PARAMS&amp;lt;/code&amp;gt; identifier, the call to the procedure will only log the&lt;br /&gt;
text &amp;lt;code&amp;gt;proc_B(): Start&amp;lt;/code&amp;gt;. Including identifier &amp;lt;code&amp;gt;DEBUG_PROC_B_PARAMS&amp;lt;/code&amp;gt; will also log out the parameter values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void proc_B(uint8_t param1, uint8_t param2) {&lt;br /&gt;
&lt;br /&gt;
  #ifdef DEBUG_PROC_B_CALL&lt;br /&gt;
    Serial.println(“proc_B(): Start”);&lt;br /&gt;
  #endif&lt;br /&gt;
  #ifdef DEBUG_PROC_B_PARAMS&lt;br /&gt;
    Serial.print(&amp;quot;&amp;gt; param1 = &amp;quot;);&lt;br /&gt;
    Serial.println(param1);&lt;br /&gt;
    Serial.println(&amp;quot;&amp;gt; param2 = &amp;quot;);&lt;br /&gt;
    Serial.println(param2);&lt;br /&gt;
  #endif&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Almost there .. ==&lt;br /&gt;
&lt;br /&gt;
Previously we defined a number of prototype functions to allow us to conditionally include a &amp;lt;code&amp;gt;Serial.print()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;Serial.println()&amp;lt;/code&amp;gt; in our code.&lt;br /&gt;
These prototypes can be expanded to include additional information, such as the function (procedure) name, file name and line number, as shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define DEBUG&lt;br /&gt;
&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
  #define DEBUG_println(x)        Serial.println(x)&lt;br /&gt;
  #define DEBUG_printDec(x)       Serial.println(x, DEC)&lt;br /&gt;
&lt;br /&gt;
  #define DEBUG_println(str)&lt;br /&gt;
    Serial.print(__FILE__);&lt;br /&gt;
    Serial.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
    Serial.print(__FUNCTION__);&lt;br /&gt;
    Serial.print(&amp;quot;: &amp;quot;);&lt;br /&gt;
    Serial.print(__LINE__);&lt;br /&gt;
    Serial.print(&amp;quot; (&amp;quot;);&lt;br /&gt;
    Serial.print(millis());&lt;br /&gt;
    Serial.print(&amp;quot;) &amp;quot;);&lt;br /&gt;
    Serial.println(str);&lt;br /&gt;
  #endif&lt;br /&gt;
&lt;br /&gt;
#else&lt;br /&gt;
  #define DEBUG_print(x)&lt;br /&gt;
  #define DEBUG_printDec(x)&lt;br /&gt;
  #define DEBUG_println(x)&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Last - but certainly not least – my final tip. When developing and testing logic games, it is often desirable to have the pseudo-random number generator produce the same series of numbers each time thus allowing you to test a sequence of events again and again until your code performs correctly.&lt;br /&gt;
&lt;br /&gt;
I use the &amp;lt;code&amp;gt;DEBUG&amp;lt;/code&amp;gt; identifier to condition the seeding of the number sequence – if I am debugging the code, I want the seed to be a constant otherwise I seed the generator with another random number (in this case the value read from an analogue port with a floating pin).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#ifdef DEBUG&lt;br /&gt;
    int seed = 1;&lt;br /&gt;
#else&lt;br /&gt;
    int seed = analogRead(3);&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
randomSeed(seed);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Main_Page&amp;diff=613</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Main_Page&amp;diff=613"/>
		<updated>2024-08-17T02:15:38Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* Tutorials */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Getting started ==&lt;br /&gt;
&lt;br /&gt;
* [[What is the Arduboy|What is the Arduboy?]]&lt;br /&gt;
** [[What is the Arduboy#Arduboy Variants|Arduboy Variants]]&lt;br /&gt;
* [[Installing Games]]&lt;br /&gt;
* [[Developing Games]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Don&#039;t know where to start at all? See the official [https://www.arduboy.com/quick-start Quickstart guide] for a simplified overview. &lt;br /&gt;
&lt;br /&gt;
There is also a [https://community.arduboy.com/t/quick-start-guide/2790 Legacy Quickstart guide] that might be of some use (made for older Arduboy variants but still mostly relevant)&lt;br /&gt;
&lt;br /&gt;
== Community ==&lt;br /&gt;
&lt;br /&gt;
Come chat with the community and show off your games at https://community.arduboy.com/! The bulk of the Arduboy community exists on the forums; there isn&#039;t an official Discord/etc.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Main Arduboy website:&#039;&#039;&#039; https://www.arduboy.com/&lt;br /&gt;
* &#039;&#039;&#039;Community website:&#039;&#039;&#039; https://community.arduboy.com/&lt;br /&gt;
&lt;br /&gt;
== Tools ==&lt;br /&gt;
* [[Ardens]] - Debugger, profiler, and emulator&lt;br /&gt;
* [[Arduboy Toolset]] - Various tools for uploading, converting, etc&lt;br /&gt;
* [[Cart Builder]] - Home for the official flashcart + all games made available for it&lt;br /&gt;
* [[Arduboy-Python-Utilities]] - Command line python tools for uploading, converting, etc&lt;br /&gt;
&lt;br /&gt;
== Tutorials ==&lt;br /&gt;
* Miscellaneous&lt;br /&gt;
** [[Reducing PROGMEM and RAM Usage]]&lt;br /&gt;
** [[Scoped Enumerations]]&lt;br /&gt;
** [[Modulus Function]]&lt;br /&gt;
** [[Debugging Code]]&lt;br /&gt;
* State Machines&lt;br /&gt;
** [[State%20Machines|Introduction]]&lt;br /&gt;
** [[State Machines - Organising Variables|Organising Variables]]&lt;br /&gt;
* High Scores&lt;br /&gt;
** [[High Score - Getting Started|Getting Started]]&lt;br /&gt;
** [[High Score - High Scores and Slots|High Scores and Slots]]&lt;br /&gt;
** [[High Score - Capturing The Players Initials|Capturing The Players Initials]]&lt;br /&gt;
** [[High Score - Creating a New Entry in the Correct Location|Creating a New Entry in the Correct Location]]&lt;br /&gt;
** [[High Score - Writing it to the EEPROM|Writing it to the EEPROM]]&lt;br /&gt;
* Frogger&lt;br /&gt;
** [[Frogger Getting Started|Getting Started]]&lt;br /&gt;
** [[Frogger - Implementing the Cars|Implementing the Cars]]&lt;br /&gt;
** [[Frogger - Mixing up the Car Movements|Mixing up the Car Movements]]&lt;br /&gt;
** [[Frogger - Adding the Frog|Adding the Player&#039;s Frog]]&lt;br /&gt;
** [[Frogger - Collision Detection and Explosion Graphics|Collision Detection and Explosion Graphics]]&lt;br /&gt;
** [[Frogger - Adding Logs and Turtles|Adding Logs and Turtles]]&lt;br /&gt;
** [[Frogger - Turtle Submerge Logic|Turtle Submerge Logic]]&lt;br /&gt;
** [[Frogger - Adding Home Logic|Adding Home Logic]]&lt;br /&gt;
** [[Frogger - Adding Score Logic|Adding Score Logic]]&lt;br /&gt;
** [[Frogger - Adding a Title and Game Over Screen|Adding a Title and Game Over Screen]]&lt;br /&gt;
* [[ArduboyG Tutorial]]&lt;br /&gt;
** [[ArduboyG Tutorial#History of Greyscale on the Arduboy|History of the Greyscale on the Arduboy]]&lt;br /&gt;
** [[ArduboyG Tutorial#Getting Started|Getting Started]]&lt;br /&gt;
** [[ArduboyG Tutorial#Creating Graphics|Creating Graphics]]&lt;br /&gt;
** [[ArduboyG Tutorial#Displaying the Graphics|Displaying the Graphics]]&lt;br /&gt;
** [[ArduboyG Tutorial#Hints and Tricks|Hints and Tricks]]&lt;br /&gt;
&lt;br /&gt;
== Documentation ==&lt;br /&gt;
* [[Flashcart Format]]&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Modulus_Function&amp;diff=612</id>
		<title>Modulus Function</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Modulus_Function&amp;diff=612"/>
		<updated>2024-08-17T01:30:05Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A modulus (modulo, mod) is simply the remainder when dividing one number by another. When we divide 7 by 2 the result is 3 with 1 remainder.&lt;br /&gt;
&lt;br /&gt;
In C / C++ the operand &amp;lt;code&amp;gt;%&amp;lt;/code&amp;gt; returns the modulus between two numbers, for example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Serial.println(7 % 2);          &amp;gt;&amp;gt;   Outputs ‘1’.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
One ‘gotcha’ with the modulus operation is that it returns the modulus using the same sign as the dividend.&lt;br /&gt;
&lt;br /&gt;
The sign of the divisor is irrelevant. The examples below shows this in action:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Serial.println(7 % 2);          &amp;gt;&amp;gt;   Outputs ‘1’.&lt;br /&gt;
Serial.println(-7 % 2);         &amp;gt;&amp;gt;   Outputs ‘-1’.&lt;br /&gt;
Serial.println(7 % -2);         &amp;gt;&amp;gt;   Outputs ‘1’.&lt;br /&gt;
Serial.println(-7 % -2);        &amp;gt;&amp;gt;   Outputs ‘-1’.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Consider the function below. It tests to see if a number is odd by checking the modulus is one. Of course, this will fail if the &amp;lt;code&amp;gt;numberToTest&amp;lt;/code&amp;gt; is negative.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bool isAnOddNumber(int numberToTest) {&lt;br /&gt;
  return numberToTest % 2 == 1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Changing the function to test for a non-zero number - a one or a negative one – accounts for both positive or negative inputs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bool isAnOddNumber(int numberToTest) {&lt;br /&gt;
  return numberToTest % 2 != 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Modulus_Function&amp;diff=611</id>
		<title>Modulus Function</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Modulus_Function&amp;diff=611"/>
		<updated>2024-08-17T01:29:37Z</updated>

		<summary type="html">&lt;p&gt;Filmote: Created page with &amp;quot;A modulus (modulo, mod) is simply the remainder when dividing one number by another. When we divide 7 by 2 the result is 3 with 1 remainder.  In C / C++ the operand &amp;lt;code&amp;gt;%&amp;lt;/code&amp;gt; returns the modulus between two numbers, for example:  &amp;lt;pre&amp;gt; Serial.println(7 % 2);          &amp;gt;&amp;gt;   Outputs ‘1’. &amp;lt;/pre&amp;gt;   One ‘gotcha’ with the modulus operation is that it returns the modulus using the same sign as the dividend.  The sign of the divisor is irrelevant. The examples below...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A modulus (modulo, mod) is simply the remainder when dividing one number by another. When we divide 7 by 2 the result is 3 with 1 remainder.&lt;br /&gt;
&lt;br /&gt;
In C / C++ the operand &amp;lt;code&amp;gt;%&amp;lt;/code&amp;gt; returns the modulus between two numbers, for example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Serial.println(7 % 2);          &amp;gt;&amp;gt;   Outputs ‘1’.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
One ‘gotcha’ with the modulus operation is that it returns the modulus using the same sign as the dividend.&lt;br /&gt;
&lt;br /&gt;
The sign of the divisor is irrelevant. The examples below shows this in action:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Serial.println(7 % 2);          &amp;gt;&amp;gt;   Outputs ‘1’.&lt;br /&gt;
Serial.println(-7 % 2);         &amp;gt;&amp;gt;   Outputs ‘-1’.&lt;br /&gt;
Serial.println(7 % -2);         &amp;gt;&amp;gt;   Outputs ‘1’.&lt;br /&gt;
Serial.println(-7 % -2);        &amp;gt;&amp;gt;   Outputs ‘-1’.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Consider the function below. It tests to see if a number is odd by checking the modulus is one. Of course, this will fail if the &#039;&#039;&#039;numberToTest&#039;&#039;&#039; is negative.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bool isAnOddNumber(int numberToTest) {&lt;br /&gt;
  return numberToTest % 2 == 1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Changing the function to test for a non-zero number - a one or a negative one – accounts for both positive or negative inputs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bool isAnOddNumber(int numberToTest) {&lt;br /&gt;
  return numberToTest % 2 != 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Main_Page&amp;diff=610</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Main_Page&amp;diff=610"/>
		<updated>2024-08-17T01:26:04Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* Tutorials */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Getting started ==&lt;br /&gt;
&lt;br /&gt;
* [[What is the Arduboy|What is the Arduboy?]]&lt;br /&gt;
** [[What is the Arduboy#Arduboy Variants|Arduboy Variants]]&lt;br /&gt;
* [[Installing Games]]&lt;br /&gt;
* [[Developing Games]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Don&#039;t know where to start at all? See the official [https://www.arduboy.com/quick-start Quickstart guide] for a simplified overview. &lt;br /&gt;
&lt;br /&gt;
There is also a [https://community.arduboy.com/t/quick-start-guide/2790 Legacy Quickstart guide] that might be of some use (made for older Arduboy variants but still mostly relevant)&lt;br /&gt;
&lt;br /&gt;
== Community ==&lt;br /&gt;
&lt;br /&gt;
Come chat with the community and show off your games at https://community.arduboy.com/! The bulk of the Arduboy community exists on the forums; there isn&#039;t an official Discord/etc.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Main Arduboy website:&#039;&#039;&#039; https://www.arduboy.com/&lt;br /&gt;
* &#039;&#039;&#039;Community website:&#039;&#039;&#039; https://community.arduboy.com/&lt;br /&gt;
&lt;br /&gt;
== Tools ==&lt;br /&gt;
* [[Ardens]] - Debugger, profiler, and emulator&lt;br /&gt;
* [[Arduboy Toolset]] - Various tools for uploading, converting, etc&lt;br /&gt;
* [[Cart Builder]] - Home for the official flashcart + all games made available for it&lt;br /&gt;
* [[Arduboy-Python-Utilities]] - Command line python tools for uploading, converting, etc&lt;br /&gt;
&lt;br /&gt;
== Tutorials ==&lt;br /&gt;
* Miscellaneous&lt;br /&gt;
** [[Reducing PROGMEM and RAM Usage]]&lt;br /&gt;
** [[Scoped Enumerations]]&lt;br /&gt;
** [[Modulus Function]]&lt;br /&gt;
* State Machines&lt;br /&gt;
** [[State%20Machines|Introduction]]&lt;br /&gt;
** [[State Machines - Organising Variables|Organising Variables]]&lt;br /&gt;
* High Scores&lt;br /&gt;
** [[High Score - Getting Started|Getting Started]]&lt;br /&gt;
** [[High Score - High Scores and Slots|High Scores and Slots]]&lt;br /&gt;
** [[High Score - Capturing The Players Initials|Capturing The Players Initials]]&lt;br /&gt;
** [[High Score - Creating a New Entry in the Correct Location|Creating a New Entry in the Correct Location]]&lt;br /&gt;
** [[High Score - Writing it to the EEPROM|Writing it to the EEPROM]]&lt;br /&gt;
* Frogger&lt;br /&gt;
** [[Frogger Getting Started|Getting Started]]&lt;br /&gt;
** [[Frogger - Implementing the Cars|Implementing the Cars]]&lt;br /&gt;
** [[Frogger - Mixing up the Car Movements|Mixing up the Car Movements]]&lt;br /&gt;
** [[Frogger - Adding the Frog|Adding the Player&#039;s Frog]]&lt;br /&gt;
** [[Frogger - Collision Detection and Explosion Graphics|Collision Detection and Explosion Graphics]]&lt;br /&gt;
** [[Frogger - Adding Logs and Turtles|Adding Logs and Turtles]]&lt;br /&gt;
** [[Frogger - Turtle Submerge Logic|Turtle Submerge Logic]]&lt;br /&gt;
** [[Frogger - Adding Home Logic|Adding Home Logic]]&lt;br /&gt;
** [[Frogger - Adding Score Logic|Adding Score Logic]]&lt;br /&gt;
** [[Frogger - Adding a Title and Game Over Screen|Adding a Title and Game Over Screen]]&lt;br /&gt;
* [[ArduboyG Tutorial]]&lt;br /&gt;
** [[ArduboyG Tutorial#History of Greyscale on the Arduboy|History of the Greyscale on the Arduboy]]&lt;br /&gt;
** [[ArduboyG Tutorial#Getting Started|Getting Started]]&lt;br /&gt;
** [[ArduboyG Tutorial#Creating Graphics|Creating Graphics]]&lt;br /&gt;
** [[ArduboyG Tutorial#Displaying the Graphics|Displaying the Graphics]]&lt;br /&gt;
** [[ArduboyG Tutorial#Hints and Tricks|Hints and Tricks]]&lt;br /&gt;
&lt;br /&gt;
== Documentation ==&lt;br /&gt;
* [[Flashcart Format]]&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Scoped_Enumerations&amp;diff=609</id>
		<title>Scoped Enumerations</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Scoped_Enumerations&amp;diff=609"/>
		<updated>2024-08-17T01:24:45Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== &#039;&#039;&#039;What are Scoped Enumerations?&#039;&#039;&#039; ==&lt;br /&gt;
Almost all programming languages provide a concept of an enumeration – a set of related constant values. In most languages, enumerations simply resolve to integer values and can be cast back and forth as needed.&lt;br /&gt;
&lt;br /&gt;
In C++ this casting is done implicitly and it is this capability of swapping from enumeration elements to the underlying value that can lead to problems. A simple enumeration that describes the suits in a deck of cards is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum {&lt;br /&gt;
  Spades,&lt;br /&gt;
  Clubs,&lt;br /&gt;
  Diamonds,&lt;br /&gt;
  Hearts&lt;br /&gt;
};&lt;br /&gt;
 &lt;br /&gt;
int suitInPlay = Hearts;&lt;br /&gt;
suitInPlay++;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A couple of things to note about this code is that it is not ‘type safe’ due to the variable &amp;lt;code&amp;gt;suitInPlay&amp;lt;/code&amp;gt; being declared as a simple integer and therefore being able to accept any valid int value rather than only those nominated&lt;br /&gt;
&lt;br /&gt;
in the enumeration. The increment is also problematic as it takes a valid declaration of the &amp;lt;code&amp;gt;suitInPlay&amp;lt;/code&amp;gt; (Hearts which in turn has an underlying value of three) and then increments it to a value outside of the enumeration.&lt;br /&gt;
&lt;br /&gt;
C++ 11 extends the basic enumeration type with scoped enumerations that overcomes this problem. The following code shows a scoped enumeration definition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum class Suits {&lt;br /&gt;
  Spades,&lt;br /&gt;
  Clubs,&lt;br /&gt;
  Diamonds,&lt;br /&gt;
  Hearts&lt;br /&gt;
};&lt;br /&gt;
 &lt;br /&gt;
Suits suitInPlay = Suits::Clubs;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Attempting to increment the variable or convert it to a numeric will fail. The declaration does not imply an integral data type - byte, integer or long - and the developer should not make any assumptions as to the type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum class Suits : uint8_t {&lt;br /&gt;
  Spades,&lt;br /&gt;
  Clubs,&lt;br /&gt;
  Diamonds,&lt;br /&gt;
  Hearts&lt;br /&gt;
};&lt;br /&gt;
 &lt;br /&gt;
Suits suitInPlay = Clubs;&lt;br /&gt;
Serial.print(“suitInPlay = “);&lt;br /&gt;
Serial.println((uint8_t)suitInPlay);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
suitInPlay = 3;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, attempting to assign a numeric value to a variable of type &amp;lt;code&amp;gt;Suits&amp;lt;/code&amp;gt; will fail. Likewise attempting to assign an element of the &amp;lt;code&amp;gt;Suits&amp;lt;/code&amp;gt; enumeration to an integer variable will also fail.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Overloading Operators&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One major benefit of using a class enumerator is the ability to override the standard operators like ++, --, &amp;lt;, &amp;gt; and == to suit your needs.&lt;br /&gt;
&lt;br /&gt;
In my 1943 game, the enemy boats have gun turrets that rotate to point towards the player as they pass each other. To record the direction of the turret, I have created a scoped class called &amp;lt;code&amp;gt;Direction&amp;lt;/code&amp;gt; as shown below. As you would expect, the enumeration lists the various directions from &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;NorthEast&amp;lt;/code&amp;gt; in order. In my code, the actual values assigned to these elements are irrelevant and the default value of &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; has been assigned to &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;NorthEast&amp;lt;/code&amp;gt; and so forth up with &amp;lt;code&amp;gt;NorthWest&amp;lt;/code&amp;gt; having a value of &amp;lt;code&amp;gt;7&amp;lt;/code&amp;gt;. I have an additional direction, called &amp;lt;code&amp;gt;Unknown&amp;lt;/code&amp;gt;, which is assigned the value of &amp;lt;code&amp;gt;8&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum class Direction : uint8_t {&lt;br /&gt;
  North,&lt;br /&gt;
  NorthEast,&lt;br /&gt;
  East,&lt;br /&gt;
  SouthEast,&lt;br /&gt;
  South,&lt;br /&gt;
  SouthWest,&lt;br /&gt;
  West,&lt;br /&gt;
  NorthWest,&lt;br /&gt;
  Unknown&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Values in an enumeration can be compared together using the standard equality and inequality comparisons. However, the introduction of the &amp;lt;code&amp;gt;Unknown&amp;lt;/code&amp;gt; element in the &amp;lt;code&amp;gt;Direction&amp;lt;/code&amp;gt; enumeration allows me to illustrate how we can override the standard operation.&lt;br /&gt;
&lt;br /&gt;
When comparing two directions together, the comparison should return false if either or both arguments are equal to &amp;lt;code&amp;gt;Direction::Unknown&amp;lt;/code&amp;gt;. The following operator override demonstrates this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator==(const Direction lhs, const Direction rhs) {&lt;br /&gt;
  return ((uint8_t)lhs == (uint8_t)Direction::Unknown || (uint8_t)rhs == (uint8_t)Direction::Unknown&lt;br /&gt;
  ? false&lt;br /&gt;
  : (uint8_t)lhs == (uint8_t)rhs);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Putting this operator to work reveals the following results:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Direction a = Direction::East;&lt;br /&gt;
Direction b = Direction::East;&lt;br /&gt;
 &lt;br /&gt;
if (a == b) { Serial.println(“1 a is equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::North;&lt;br /&gt;
 &lt;br /&gt;
if (!(a == b)) { Serial.println(“2 a is not equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
a = Direction::Unknown;&lt;br /&gt;
b = Direction::Unknown;&lt;br /&gt;
 &lt;br /&gt;
if (!(a == b)) { Serial.println(“3 a is not equal to b”); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1 a is equal to b&lt;br /&gt;
2 a is not equal to b&lt;br /&gt;
3 a is not equal to b&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that in the last test, the comparison between the two Unknown directions returned false due to the rule created in the equality comparison.&lt;br /&gt;
&lt;br /&gt;
Hang on, what’s with the &amp;lt;code&amp;gt;(!(a == b))&amp;lt;/code&amp;gt; syntax? What about using the standard ‘not equals’ syntax?  As we have overridden the equality operator we need to override the inequality operator as well. The inequality operator shown below simply calls the modified equality operator and returns the NOT of it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator!=(const Direction lhs, const Direction rhs)  {&lt;br /&gt;
  return ! (lhs == rhs);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this new operator override in place, we can use the standard notation shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::North;&lt;br /&gt;
 &lt;br /&gt;
if (a != b) { Serial.println(“2 a is not equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
a = Direction::Unknown;&lt;br /&gt;
b = Direction::Unknown;&lt;br /&gt;
 &lt;br /&gt;
if (a != b) { Serial.println(“3 a is not equal to b”); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1 a is equal to b&lt;br /&gt;
2 a is not equal to b&lt;br /&gt;
3 a is not equal to b&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Getting more complex .. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
So now we can compare directions, what about comparing values?&lt;br /&gt;
&lt;br /&gt;
Assume the boat is at the bottom of the screen entering from the right and leaving towards the left. If the player is stationary and located at the top-centre of the screen then to shoot towards the player the gun will need to be pointed towards the &amp;lt;code&amp;gt;North-West&amp;lt;/code&amp;gt; initially and will swing through &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt; and then towards the &amp;lt;code&amp;gt;North-East&amp;lt;/code&amp;gt; as the boat leaves the screen. In my code, I was hoping to compare the current direction of the turret to the player’s position and simply add or subtract one to swing it around as required. Although the enumeration details an order to the directions where &amp;lt;code&amp;gt;South&amp;lt;/code&amp;gt; is greater than &amp;lt;code&amp;gt;SouthEast&amp;lt;/code&amp;gt; (in a clockwise view of the world), it does not consider that &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt; is greater than &amp;lt;code&amp;gt;NorthWest&amp;lt;/code&amp;gt;, ie it has no concept of the circular nature of a compass.&lt;br /&gt;
&lt;br /&gt;
The following operators override the standard ‘less than’ and ‘greater than’ operators. Before calculating a return value, the operator determines whether it needs to consider the resetting of values at &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt; by subtracting the integer values from each other. Where the comparison must cater for &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt;, adding &amp;lt;code&amp;gt;8&amp;lt;/code&amp;gt; (a full circle) to the either the right or left hand side argument (for ‘less than’ or ‘greater than’ respectively) ensures that the comparison caters for this appropriately.&lt;br /&gt;
 						&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator&amp;lt;(const Direction lhs, const Direction rhs)  {&lt;br /&gt;
    return (abs((uint8_t)lhs - (uint8_t)rhs) &amp;lt; 4 &lt;br /&gt;
            ? (uint8_t)lhs - (uint8_t)rhs &lt;br /&gt;
            : (uint8_t)lhs - (8 + (uint8_t)rhs)) &amp;lt; 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
inline bool operator&amp;gt;(const Direction  lhs, const Direction  rhs)  {&lt;br /&gt;
    return (abs((uint8_t)lhs - (uint8_t)rhs) &amp;lt; 4 &lt;br /&gt;
            ? (uint8_t)lhs - (uint8_t)rhs &lt;br /&gt;
            : (8 + (uint8_t)lhs) - (uint8_t)rhs) &amp;gt; 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Putting these two operators through their paces reveals the correct results shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Direction a = Direction::East;&lt;br /&gt;
Direction b = Direction::South;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“1 East is less than South”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“2 South is greater than East”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::North;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“3 NorthWest is less than North”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“4 North is greater than NorthWest”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::NorthEast;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“5 NorthWest is less than NorthEast”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“6 NorthEast is greater than NorthWest”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::North;&lt;br /&gt;
b = Direction::NorthEast;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“7 North is less than NorthEast”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“8 NorthEast is greater than North”); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1 East is less than South&lt;br /&gt;
2 South is greater than East&lt;br /&gt;
3 NorthWest is less than North&lt;br /&gt;
4 North is greater than NorthWest&lt;br /&gt;
5 NorthWest is less than NorthEast&lt;br /&gt;
6 NorthEast is greater than NorthWest&lt;br /&gt;
7 North is less than NorthEast&lt;br /&gt;
8 NorthEast is greater than North&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now that we have the ‘equality’, ‘greater than’ and ‘less than’ operators in place, we can add the following two operators to complete the comparisons.&lt;br /&gt;
&lt;br /&gt;
Note they perform the logical &amp;lt;code&amp;gt;NOT&amp;lt;/code&amp;gt; of those operators previously defined.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator&amp;lt;=(const Direction lhs, const Direction rhs) { return !(lhs &amp;gt; rhs); }&lt;br /&gt;
inline bool operator&amp;gt;=(const Direction lhs, const Direction rhs) { return !(lhs &amp;lt; rhs); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== And just plain confusing .. ==&lt;br /&gt;
&lt;br /&gt;
Not really confusing but overriding the standard increment and decrement operators reveal an interesting compiler trick that you just have to take for granted actually works. The operator below describes a pre-increment operator.&lt;br /&gt;
&lt;br /&gt;
If the supplied direction is NorthWest&lt;br /&gt;
it returns North otherwise it returns the direction adjacent to the supplied on in the enumeration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction &amp;amp;operator++( Direction &amp;amp;c ) {&lt;br /&gt;
  c = ( c == Direction::NorthWest )&lt;br /&gt;
  ? Direction::North&lt;br /&gt;
  : static_cast&amp;lt;Direction&amp;gt;( static_cast&amp;lt;uint8_t&amp;gt;(c) + 1 );&lt;br /&gt;
  return c;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A post-increment operator includes an anonymous second parameter of type integer. This parameter is not used in the code and is simply there to differentiate between the pre- and post- increment functions.&lt;br /&gt;
&lt;br /&gt;
The compiler must recognise this method signature and assume it is a post-increment function. The implementation of this operator simply calls the pre-incrementer and returns its value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction operator++( Direction &amp;amp;c, int ) {&lt;br /&gt;
  Direction result = c;&lt;br /&gt;
  ++c;&lt;br /&gt;
  return result;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The decrement functions are nearly identical to the increment operators and are shown below for completeness.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction &amp;amp;operator--( Direction &amp;amp; c ) {&lt;br /&gt;
  c = ( c == Direction::North )&lt;br /&gt;
  ? Direction::NorthWest&lt;br /&gt;
  : static_cast&amp;lt;Direction&amp;gt;( static_cast&amp;lt;uint8_t&amp;gt;(c) - 1 );&lt;br /&gt;
  return c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
inline Direction operator--( Direction &amp;amp; c, int ) {&lt;br /&gt;
  Direction result = c;&lt;br /&gt;
  --c;&lt;br /&gt;
  return result;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As you can see, the scoped enumerations provide an extra layer of functionality beyond what simple enumerations offer. The ability&lt;br /&gt;
to override the operators encapsulates the logic of the enumeration itself and saves the developer having to repeat similar logic across an application. In the examples shown in&lt;br /&gt;
this article, the developer does not need to remember the intrinsic values underlying the directions North or East nor does he need to consider how to increase the value when it reaches the end of the enumeration.&lt;br /&gt;
&lt;br /&gt;
In fact, all of the code would work if you change the initial enumeration declaration to (say) North = 57 ..&lt;br /&gt;
&lt;br /&gt;
I have included a simple application on GitHub that shows the enumeration in action : https://github.com/filmote/ScopedEnums&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Scoped_Enumerations&amp;diff=608</id>
		<title>Scoped Enumerations</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Scoped_Enumerations&amp;diff=608"/>
		<updated>2024-08-17T01:24:17Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* Getting more complex .. */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== &#039;&#039;&#039;What are Scoped Enumerations?&#039;&#039;&#039; ==&lt;br /&gt;
Almost all programming languages provide a concept of an enumeration – a set of related constant values. In most languages, enumerations simply resolve to integer values and can be cast back and forth as needed.&lt;br /&gt;
&lt;br /&gt;
In C++ this casting is done implicitly and it is this capability of swapping from enumeration elements to the underlying value that can lead to problems. A simple enumeration that describes the suits in a deck of cards is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum {&lt;br /&gt;
  Spades,&lt;br /&gt;
  Clubs,&lt;br /&gt;
  Diamonds,&lt;br /&gt;
  Hearts&lt;br /&gt;
};&lt;br /&gt;
 &lt;br /&gt;
int suitInPlay = Hearts;&lt;br /&gt;
suitInPlay++;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A couple of things to note about this code is that it is not ‘type safe’ due to the variable &amp;lt;code&amp;gt;suitInPlay&amp;lt;/code&amp;gt; being declared as a simple integer and therefore being able to accept any valid int value rather than only those nominated&lt;br /&gt;
&lt;br /&gt;
in the enumeration. The increment is also problematic as it takes a valid declaration of the &amp;lt;code&amp;gt;suitInPlay&amp;lt;/code&amp;gt; (Hearts which in turn has an underlying value of three) and then increments it to a value outside of the enumeration.&lt;br /&gt;
&lt;br /&gt;
C++ 11 extends the basic enumeration type with scoped enumerations that overcomes this problem. The following code shows a scoped enumeration definition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum class Suits {&lt;br /&gt;
  Spades,&lt;br /&gt;
  Clubs,&lt;br /&gt;
  Diamonds,&lt;br /&gt;
  Hearts&lt;br /&gt;
};&lt;br /&gt;
 &lt;br /&gt;
Suits suitInPlay = Suits::Clubs;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Attempting to increment the variable or convert it to a numeric will fail. The declaration does not imply an integral data type - byte, integer or long - and the developer should not make any assumptions as to the type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum class Suits : uint8_t {&lt;br /&gt;
  Spades,&lt;br /&gt;
  Clubs,&lt;br /&gt;
  Diamonds,&lt;br /&gt;
  Hearts&lt;br /&gt;
};&lt;br /&gt;
 &lt;br /&gt;
Suits suitInPlay = Clubs;&lt;br /&gt;
Serial.print(“suitInPlay = “);&lt;br /&gt;
Serial.println((uint8_t)suitInPlay);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
suitInPlay = 3;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, attempting to assign a numeric value to a variable of type &amp;lt;code&amp;gt;Suits&amp;lt;/code&amp;gt; will fail. Likewise attempting to assign an element of the &amp;lt;code&amp;gt;Suits&amp;lt;/code&amp;gt; enumeration to an integer variable will also fail.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Overloading Operators&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One major benefit of using a class enumerator is the ability to override the standard operators like ++, --, &amp;lt;, &amp;gt; and == to suit your needs.&lt;br /&gt;
&lt;br /&gt;
In my 1943 game, the enemy boats have gun turrets that rotate to point towards the player as they pass each other. To record the direction of the turret, I have created a scoped class called &amp;lt;code&amp;gt;Direction&amp;lt;/code&amp;gt; as shown below. As you would expect, the enumeration lists the various directions from &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;NorthEast&amp;lt;/code&amp;gt; in order. In my code, the actual values assigned to these elements are irrelevant and the default value of &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; has been assigned to &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;NorthEast&amp;lt;/code&amp;gt; and so forth up with &amp;lt;code&amp;gt;NorthWest&amp;lt;/code&amp;gt; having a value of &amp;lt;code&amp;gt;7&amp;lt;/code&amp;gt;. I have an additional direction, called &amp;lt;code&amp;gt;Unknown&amp;lt;/code&amp;gt;, which is assigned the value of &amp;lt;code&amp;gt;8&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum class Direction : uint8_t {&lt;br /&gt;
  North,&lt;br /&gt;
  NorthEast,&lt;br /&gt;
  East,&lt;br /&gt;
  SouthEast,&lt;br /&gt;
  South,&lt;br /&gt;
  SouthWest,&lt;br /&gt;
  West,&lt;br /&gt;
  NorthWest,&lt;br /&gt;
  Unknown&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Values in an enumeration can be compared together using the standard equality and inequality comparisons. However, the introduction of the &amp;lt;code&amp;gt;Unknown&amp;lt;/code&amp;gt; element in the &amp;lt;code&amp;gt;Direction&amp;lt;/code&amp;gt; enumeration allows me to illustrate how we can override the standard operation.&lt;br /&gt;
&lt;br /&gt;
When comparing two directions together, the comparison should return false if either or both arguments are equal to &amp;lt;code&amp;gt;Direction::Unknown&amp;lt;/code&amp;gt;. The following operator override demonstrates this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator==(const Direction lhs, const Direction rhs) {&lt;br /&gt;
  return ((uint8_t)lhs == (uint8_t)Direction::Unknown || (uint8_t)rhs == (uint8_t)Direction::Unknown&lt;br /&gt;
  ? false&lt;br /&gt;
  : (uint8_t)lhs == (uint8_t)rhs);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Putting this operator to work reveals the following results:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Direction a = Direction::East;&lt;br /&gt;
Direction b = Direction::East;&lt;br /&gt;
 &lt;br /&gt;
if (a == b) { Serial.println(“1 a is equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::North;&lt;br /&gt;
 &lt;br /&gt;
if (!(a == b)) { Serial.println(“2 a is not equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
a = Direction::Unknown;&lt;br /&gt;
b = Direction::Unknown;&lt;br /&gt;
 &lt;br /&gt;
if (!(a == b)) { Serial.println(“3 a is not equal to b”); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1 a is equal to b&lt;br /&gt;
2 a is not equal to b&lt;br /&gt;
3 a is not equal to b&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that in the last test, the comparison between the two Unknown directions returned false due to the rule created in the equality comparison.&lt;br /&gt;
&lt;br /&gt;
Hang on, what’s with the &amp;lt;code&amp;gt;(!(a == b))&amp;lt;/code&amp;gt; syntax? What about using the standard ‘not equals’ syntax?  As we have overridden the equality operator we need to override the inequality operator as well. The inequality operator shown below simply calls the modified equality operator and returns the NOT of it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator!=(const Direction lhs, const Direction rhs)  {&lt;br /&gt;
  return ! (lhs == rhs);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this new operator override in place, we can use the standard notation shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::North;&lt;br /&gt;
 &lt;br /&gt;
if (a != b) { Serial.println(“2 a is not equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
a = Direction::Unknown;&lt;br /&gt;
b = Direction::Unknown;&lt;br /&gt;
 &lt;br /&gt;
if (a != b) { Serial.println(“3 a is not equal to b”); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1 a is equal to b&lt;br /&gt;
2 a is not equal to b&lt;br /&gt;
3 a is not equal to b&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Getting more complex .. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
So now we can compare directions, what about comparing values?&lt;br /&gt;
&lt;br /&gt;
Assume the boat is at the bottom of the screen entering from the right and leaving towards the left. If the player is stationary and located at the top-centre of the screen then to shoot towards the player the gun will need to be pointed towards the &amp;lt;code&amp;gt;North-West&amp;lt;/code&amp;gt; initially and will swing through &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt; and then towards the &amp;lt;code&amp;gt;North-East&amp;lt;/code&amp;gt; as the boat leaves the screen. In my code, I was hoping to compare the current direction of the turret to the player’s position and simply add or subtract one to swing it around as required. Although the enumeration details an order to the directions where &amp;lt;code&amp;gt;South&amp;lt;/code&amp;gt; is greater than &amp;lt;code&amp;gt;SouthEast&amp;lt;/code&amp;gt; (in a clockwise view of the world), it does not consider that &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt; is greater than &amp;lt;code&amp;gt;NorthWest&amp;lt;/code&amp;gt;, ie it has no concept of the circular nature of a compass.&lt;br /&gt;
&lt;br /&gt;
The following operators override the standard ‘less than’ and ‘greater than’ operators. Before calculating a return value, the operator determines whether it needs to consider the resetting of values at &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt; by subtracting the integer values from each other. Where the comparison must cater for &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt;, adding &amp;lt;code&amp;gt;8&amp;lt;/code&amp;gt; (a full circle) to the either the right or left hand side argument (for ‘less than’ or ‘greater than’ respectively) ensures that the comparison caters for this appropriately.&lt;br /&gt;
 						&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator&amp;lt;(const Direction lhs, const Direction rhs)  {&lt;br /&gt;
    return (abs((uint8_t)lhs - (uint8_t)rhs) &amp;lt; 4 &lt;br /&gt;
            ? (uint8_t)lhs - (uint8_t)rhs &lt;br /&gt;
            : (uint8_t)lhs - (8 + (uint8_t)rhs)) &amp;lt; 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
inline bool operator&amp;gt;(const Direction  lhs, const Direction  rhs)  {&lt;br /&gt;
    return (abs((uint8_t)lhs - (uint8_t)rhs) &amp;lt; 4 &lt;br /&gt;
            ? (uint8_t)lhs - (uint8_t)rhs &lt;br /&gt;
            : (8 + (uint8_t)lhs) - (uint8_t)rhs) &amp;gt; 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Putting these two operators through their paces reveals the correct results shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Direction a = Direction::East;&lt;br /&gt;
Direction b = Direction::South;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“1 East is less than South”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“2 South is greater than East”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::North;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“3 NorthWest is less than North”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“4 North is greater than NorthWest”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::NorthEast;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“5 NorthWest is less than NorthEast”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“6 NorthEast is greater than NorthWest”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::North;&lt;br /&gt;
b = Direction::NorthEast;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“7 North is less than NorthEast”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“8 NorthEast is greater than North”); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1 East is less than South&lt;br /&gt;
2 South is greater than East&lt;br /&gt;
3 NorthWest is less than North&lt;br /&gt;
4 North is greater than NorthWest&lt;br /&gt;
5 NorthWest is less than NorthEast&lt;br /&gt;
6 NorthEast is greater than NorthWest&lt;br /&gt;
7 North is less than NorthEast&lt;br /&gt;
8 NorthEast is greater than North&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now that we have the ‘equality’, ‘greater than’ and ‘less than’ operators in place, we can add the following two operators to complete the comparisons.&lt;br /&gt;
&lt;br /&gt;
Note they perform the logical &amp;lt;code&amp;gt;NOT&amp;lt;/code&amp;gt; of those operators previously defined.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator&amp;lt;=(const Direction lhs, const Direction rhs) { return !(lhs &amp;gt; rhs); }&lt;br /&gt;
inline bool operator&amp;gt;=(const Direction lhs, const Direction rhs) { return !(lhs &amp;lt; rhs); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== And just plain confusing .. ==&lt;br /&gt;
&lt;br /&gt;
Not really confusing but overriding the standard increment and decrement operators reveal an interesting compiler trick that you just have to take for granted actually works. The operator below describes a pre-increment operator.&lt;br /&gt;
&lt;br /&gt;
If the supplied direction is NorthWest&lt;br /&gt;
it returns North otherwise it returns the direction adjacent to the supplied on in the enumeration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction &amp;amp;operator++( Direction &amp;amp;c ) {&lt;br /&gt;
  c = ( c == Direction::NorthWest )&lt;br /&gt;
  ? Direction::North&lt;br /&gt;
  : static_cast&amp;lt;Direction&amp;gt;( static_cast&amp;lt;uint8_t&amp;gt;(c) + 1 );&lt;br /&gt;
  return c;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A post-increment operator includes an anonymous second parameter of type integer. This parameter is not used in the code and is simply there to differentiate between the pre- and post- increment functions.&lt;br /&gt;
&lt;br /&gt;
The compiler must recognise this method signature and assume it is a post-increment function. The implementation of this operator simply calls the pre-incrementer and returns its value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction operator++( Direction &amp;amp;c, int ) {&lt;br /&gt;
  Direction result = c;&lt;br /&gt;
  ++c;&lt;br /&gt;
  return result;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The decrement functions are nearly identical to the increment operators and are shown below for completeness.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction &amp;amp;operator--( Direction &amp;amp; c ) {&lt;br /&gt;
  c = ( c == Direction::North )&lt;br /&gt;
  ? Direction::NorthWest&lt;br /&gt;
  : static_cast&amp;lt;Direction&amp;gt;( static_cast&amp;lt;uint8_t&amp;gt;(c) - 1 );&lt;br /&gt;
  return c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
inline Direction operator--( Direction &amp;amp; c, int ) {&lt;br /&gt;
  Direction result = c;&lt;br /&gt;
  --c;&lt;br /&gt;
  return result;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As you can see, the scoped enumerations provide an extra layer of functionality beyond what simple enumerations offer. The ability&lt;br /&gt;
to override the operators encapsulates the logic of the enumeration itself and saves the developer having to repeat similar logic across an application. In the examples shown in&lt;br /&gt;
this article, the developer does not need to remember the intrinsic values underlying the directions North or East nor does he need to consider how to increase the value when it reaches the end of the enumeration.&lt;br /&gt;
&lt;br /&gt;
n fact, all of the code would work if you change the initial enumeration declaration to (say) North = 57 ..&lt;br /&gt;
&lt;br /&gt;
I have included a simple application on GitHub that shows the enumeration in action : https://github.com/filmote/ScopedEnums&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Scoped_Enumerations&amp;diff=607</id>
		<title>Scoped Enumerations</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Scoped_Enumerations&amp;diff=607"/>
		<updated>2024-08-17T01:23:35Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* Getting more complex .. */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== &#039;&#039;&#039;What are Scoped Enumerations?&#039;&#039;&#039; ==&lt;br /&gt;
Almost all programming languages provide a concept of an enumeration – a set of related constant values. In most languages, enumerations simply resolve to integer values and can be cast back and forth as needed.&lt;br /&gt;
&lt;br /&gt;
In C++ this casting is done implicitly and it is this capability of swapping from enumeration elements to the underlying value that can lead to problems. A simple enumeration that describes the suits in a deck of cards is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum {&lt;br /&gt;
  Spades,&lt;br /&gt;
  Clubs,&lt;br /&gt;
  Diamonds,&lt;br /&gt;
  Hearts&lt;br /&gt;
};&lt;br /&gt;
 &lt;br /&gt;
int suitInPlay = Hearts;&lt;br /&gt;
suitInPlay++;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A couple of things to note about this code is that it is not ‘type safe’ due to the variable &amp;lt;code&amp;gt;suitInPlay&amp;lt;/code&amp;gt; being declared as a simple integer and therefore being able to accept any valid int value rather than only those nominated&lt;br /&gt;
&lt;br /&gt;
in the enumeration. The increment is also problematic as it takes a valid declaration of the &amp;lt;code&amp;gt;suitInPlay&amp;lt;/code&amp;gt; (Hearts which in turn has an underlying value of three) and then increments it to a value outside of the enumeration.&lt;br /&gt;
&lt;br /&gt;
C++ 11 extends the basic enumeration type with scoped enumerations that overcomes this problem. The following code shows a scoped enumeration definition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum class Suits {&lt;br /&gt;
  Spades,&lt;br /&gt;
  Clubs,&lt;br /&gt;
  Diamonds,&lt;br /&gt;
  Hearts&lt;br /&gt;
};&lt;br /&gt;
 &lt;br /&gt;
Suits suitInPlay = Suits::Clubs;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Attempting to increment the variable or convert it to a numeric will fail. The declaration does not imply an integral data type - byte, integer or long - and the developer should not make any assumptions as to the type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum class Suits : uint8_t {&lt;br /&gt;
  Spades,&lt;br /&gt;
  Clubs,&lt;br /&gt;
  Diamonds,&lt;br /&gt;
  Hearts&lt;br /&gt;
};&lt;br /&gt;
 &lt;br /&gt;
Suits suitInPlay = Clubs;&lt;br /&gt;
Serial.print(“suitInPlay = “);&lt;br /&gt;
Serial.println((uint8_t)suitInPlay);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
suitInPlay = 3;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, attempting to assign a numeric value to a variable of type &amp;lt;code&amp;gt;Suits&amp;lt;/code&amp;gt; will fail. Likewise attempting to assign an element of the &amp;lt;code&amp;gt;Suits&amp;lt;/code&amp;gt; enumeration to an integer variable will also fail.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Overloading Operators&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One major benefit of using a class enumerator is the ability to override the standard operators like ++, --, &amp;lt;, &amp;gt; and == to suit your needs.&lt;br /&gt;
&lt;br /&gt;
In my 1943 game, the enemy boats have gun turrets that rotate to point towards the player as they pass each other. To record the direction of the turret, I have created a scoped class called &amp;lt;code&amp;gt;Direction&amp;lt;/code&amp;gt; as shown below. As you would expect, the enumeration lists the various directions from &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;NorthEast&amp;lt;/code&amp;gt; in order. In my code, the actual values assigned to these elements are irrelevant and the default value of &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; has been assigned to &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;NorthEast&amp;lt;/code&amp;gt; and so forth up with &amp;lt;code&amp;gt;NorthWest&amp;lt;/code&amp;gt; having a value of &amp;lt;code&amp;gt;7&amp;lt;/code&amp;gt;. I have an additional direction, called &amp;lt;code&amp;gt;Unknown&amp;lt;/code&amp;gt;, which is assigned the value of &amp;lt;code&amp;gt;8&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum class Direction : uint8_t {&lt;br /&gt;
  North,&lt;br /&gt;
  NorthEast,&lt;br /&gt;
  East,&lt;br /&gt;
  SouthEast,&lt;br /&gt;
  South,&lt;br /&gt;
  SouthWest,&lt;br /&gt;
  West,&lt;br /&gt;
  NorthWest,&lt;br /&gt;
  Unknown&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Values in an enumeration can be compared together using the standard equality and inequality comparisons. However, the introduction of the &amp;lt;code&amp;gt;Unknown&amp;lt;/code&amp;gt; element in the &amp;lt;code&amp;gt;Direction&amp;lt;/code&amp;gt; enumeration allows me to illustrate how we can override the standard operation.&lt;br /&gt;
&lt;br /&gt;
When comparing two directions together, the comparison should return false if either or both arguments are equal to &amp;lt;code&amp;gt;Direction::Unknown&amp;lt;/code&amp;gt;. The following operator override demonstrates this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator==(const Direction lhs, const Direction rhs) {&lt;br /&gt;
  return ((uint8_t)lhs == (uint8_t)Direction::Unknown || (uint8_t)rhs == (uint8_t)Direction::Unknown&lt;br /&gt;
  ? false&lt;br /&gt;
  : (uint8_t)lhs == (uint8_t)rhs);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Putting this operator to work reveals the following results:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Direction a = Direction::East;&lt;br /&gt;
Direction b = Direction::East;&lt;br /&gt;
 &lt;br /&gt;
if (a == b) { Serial.println(“1 a is equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::North;&lt;br /&gt;
 &lt;br /&gt;
if (!(a == b)) { Serial.println(“2 a is not equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
a = Direction::Unknown;&lt;br /&gt;
b = Direction::Unknown;&lt;br /&gt;
 &lt;br /&gt;
if (!(a == b)) { Serial.println(“3 a is not equal to b”); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1 a is equal to b&lt;br /&gt;
2 a is not equal to b&lt;br /&gt;
3 a is not equal to b&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that in the last test, the comparison between the two Unknown directions returned false due to the rule created in the equality comparison.&lt;br /&gt;
&lt;br /&gt;
Hang on, what’s with the &amp;lt;code&amp;gt;(!(a == b))&amp;lt;/code&amp;gt; syntax? What about using the standard ‘not equals’ syntax?  As we have overridden the equality operator we need to override the inequality operator as well. The inequality operator shown below simply calls the modified equality operator and returns the NOT of it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator!=(const Direction lhs, const Direction rhs)  {&lt;br /&gt;
  return ! (lhs == rhs);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this new operator override in place, we can use the standard notation shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::North;&lt;br /&gt;
 &lt;br /&gt;
if (a != b) { Serial.println(“2 a is not equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
a = Direction::Unknown;&lt;br /&gt;
b = Direction::Unknown;&lt;br /&gt;
 &lt;br /&gt;
if (a != b) { Serial.println(“3 a is not equal to b”); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1 a is equal to b&lt;br /&gt;
2 a is not equal to b&lt;br /&gt;
3 a is not equal to b&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Getting more complex .. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
So now we can compare directions, what about comparing values?&lt;br /&gt;
&lt;br /&gt;
Assume the boat is at the bottom of the screen entering from the right and leaving towards the left. If the player is stationary and located at the top-centre of the screen then to shoot towards the player the gun will need to be pointed towards the &amp;lt;code&amp;gt;North-West&amp;lt;/code&amp;gt; initially and will swing through &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt; and then towards the &amp;lt;code&amp;gt;North-East&amp;lt;/code&amp;gt; as the boat leaves the screen. In my code, I was hoping to compare the current direction of the turret to the player’s position and simply add or subtract one to swing it around as required. Although the enumeration details an order to the directions where &amp;lt;code&amp;gt;South&amp;lt;/code&amp;gt; is greater than &amp;lt;code&amp;gt;SouthEast&amp;lt;/code&amp;gt; (in a clockwise view of the world), it does not consider that &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt; is greater than &amp;lt;code&amp;gt;NorthWest&amp;lt;/code&amp;gt;, ie it has no concept of the circular nature of a compass.&lt;br /&gt;
&lt;br /&gt;
The following operators override the standard ‘less than’ and ‘greater than’ operators. Before calculating a return value, the operator determines whether it needs to consider the resetting of values at &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt; by subtracting the integer values from each other. Where the comparison must cater for &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt;, adding &amp;lt;code&amp;gt;8&amp;lt;c/ode&amp;gt; (a full circle) to the either the right or left hand side argument (for ‘less than’ or ‘greater than’ respectively) ensures that the comparison caters for this appropriately.&lt;br /&gt;
 						&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator&amp;lt;(const Direction lhs, const Direction rhs)  {&lt;br /&gt;
    return (abs((uint8_t)lhs - (uint8_t)rhs) &amp;lt; 4 &lt;br /&gt;
            ? (uint8_t)lhs - (uint8_t)rhs &lt;br /&gt;
            : (uint8_t)lhs - (8 + (uint8_t)rhs)) &amp;lt; 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
inline bool operator&amp;gt;(const Direction  lhs, const Direction  rhs)  {&lt;br /&gt;
    return (abs((uint8_t)lhs - (uint8_t)rhs) &amp;lt; 4 &lt;br /&gt;
            ? (uint8_t)lhs - (uint8_t)rhs &lt;br /&gt;
            : (8 + (uint8_t)lhs) - (uint8_t)rhs) &amp;gt; 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Putting these two operators through their paces reveals the correct results shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Direction a = Direction::East;&lt;br /&gt;
Direction b = Direction::South;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“1 East is less than South”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“2 South is greater than East”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::North;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“3 NorthWest is less than North”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“4 North is greater than NorthWest”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::NorthEast;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“5 NorthWest is less than NorthEast”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“6 NorthEast is greater than NorthWest”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::North;&lt;br /&gt;
b = Direction::NorthEast;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“7 North is less than NorthEast”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“8 NorthEast is greater than North”); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1 East is less than South&lt;br /&gt;
2 South is greater than East&lt;br /&gt;
3 NorthWest is less than North&lt;br /&gt;
4 North is greater than NorthWest&lt;br /&gt;
5 NorthWest is less than NorthEast&lt;br /&gt;
6 NorthEast is greater than NorthWest&lt;br /&gt;
7 North is less than NorthEast&lt;br /&gt;
8 NorthEast is greater than North&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now that we have the ‘equality’, ‘greater than’ and ‘less than’ operators in place, we can add the following two operators to complete the comparisons.&lt;br /&gt;
&lt;br /&gt;
Note they perform the logical &amp;lt;code&amp;gt;NOT&amp;lt;/code&amp;gt; of those operators previously defined.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator&amp;lt;=(const Direction lhs, const Direction rhs) { return !(lhs &amp;gt; rhs); }&lt;br /&gt;
inline bool operator&amp;gt;=(const Direction lhs, const Direction rhs) { return !(lhs &amp;lt; rhs); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== And just plain confusing .. ==&lt;br /&gt;
&lt;br /&gt;
Not really confusing but overriding the standard increment and decrement operators reveal an interesting compiler trick that you just have to take for granted actually works. The operator below describes a pre-increment operator.&lt;br /&gt;
&lt;br /&gt;
If the supplied direction is NorthWest&lt;br /&gt;
it returns North otherwise it returns the direction adjacent to the supplied on in the enumeration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction &amp;amp;operator++( Direction &amp;amp;c ) {&lt;br /&gt;
  c = ( c == Direction::NorthWest )&lt;br /&gt;
  ? Direction::North&lt;br /&gt;
  : static_cast&amp;lt;Direction&amp;gt;( static_cast&amp;lt;uint8_t&amp;gt;(c) + 1 );&lt;br /&gt;
  return c;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A post-increment operator includes an anonymous second parameter of type integer. This parameter is not used in the code and is simply there to differentiate between the pre- and post- increment functions.&lt;br /&gt;
&lt;br /&gt;
The compiler must recognise this method signature and assume it is a post-increment function. The implementation of this operator simply calls the pre-incrementer and returns its value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction operator++( Direction &amp;amp;c, int ) {&lt;br /&gt;
  Direction result = c;&lt;br /&gt;
  ++c;&lt;br /&gt;
  return result;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The decrement functions are nearly identical to the increment operators and are shown below for completeness.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction &amp;amp;operator--( Direction &amp;amp; c ) {&lt;br /&gt;
  c = ( c == Direction::North )&lt;br /&gt;
  ? Direction::NorthWest&lt;br /&gt;
  : static_cast&amp;lt;Direction&amp;gt;( static_cast&amp;lt;uint8_t&amp;gt;(c) - 1 );&lt;br /&gt;
  return c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
inline Direction operator--( Direction &amp;amp; c, int ) {&lt;br /&gt;
  Direction result = c;&lt;br /&gt;
  --c;&lt;br /&gt;
  return result;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As you can see, the scoped enumerations provide an extra layer of functionality beyond what simple enumerations offer. The ability&lt;br /&gt;
to override the operators encapsulates the logic of the enumeration itself and saves the developer having to repeat similar logic across an application. In the examples shown in&lt;br /&gt;
this article, the developer does not need to remember the intrinsic values underlying the directions North or East nor does he need to consider how to increase the value when it reaches the end of the enumeration.&lt;br /&gt;
&lt;br /&gt;
n fact, all of the code would work if you change the initial enumeration declaration to (say) North = 57 ..&lt;br /&gt;
&lt;br /&gt;
I have included a simple application on GitHub that shows the enumeration in action : https://github.com/filmote/ScopedEnums&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Scoped_Enumerations&amp;diff=606</id>
		<title>Scoped Enumerations</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Scoped_Enumerations&amp;diff=606"/>
		<updated>2024-08-17T01:20:59Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* Overloading Operators */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== &#039;&#039;&#039;What are Scoped Enumerations?&#039;&#039;&#039; ==&lt;br /&gt;
Almost all programming languages provide a concept of an enumeration – a set of related constant values. In most languages, enumerations simply resolve to integer values and can be cast back and forth as needed.&lt;br /&gt;
&lt;br /&gt;
In C++ this casting is done implicitly and it is this capability of swapping from enumeration elements to the underlying value that can lead to problems. A simple enumeration that describes the suits in a deck of cards is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum {&lt;br /&gt;
  Spades,&lt;br /&gt;
  Clubs,&lt;br /&gt;
  Diamonds,&lt;br /&gt;
  Hearts&lt;br /&gt;
};&lt;br /&gt;
 &lt;br /&gt;
int suitInPlay = Hearts;&lt;br /&gt;
suitInPlay++;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A couple of things to note about this code is that it is not ‘type safe’ due to the variable &amp;lt;code&amp;gt;suitInPlay&amp;lt;/code&amp;gt; being declared as a simple integer and therefore being able to accept any valid int value rather than only those nominated&lt;br /&gt;
&lt;br /&gt;
in the enumeration. The increment is also problematic as it takes a valid declaration of the &amp;lt;code&amp;gt;suitInPlay&amp;lt;/code&amp;gt; (Hearts which in turn has an underlying value of three) and then increments it to a value outside of the enumeration.&lt;br /&gt;
&lt;br /&gt;
C++ 11 extends the basic enumeration type with scoped enumerations that overcomes this problem. The following code shows a scoped enumeration definition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum class Suits {&lt;br /&gt;
  Spades,&lt;br /&gt;
  Clubs,&lt;br /&gt;
  Diamonds,&lt;br /&gt;
  Hearts&lt;br /&gt;
};&lt;br /&gt;
 &lt;br /&gt;
Suits suitInPlay = Suits::Clubs;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Attempting to increment the variable or convert it to a numeric will fail. The declaration does not imply an integral data type - byte, integer or long - and the developer should not make any assumptions as to the type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum class Suits : uint8_t {&lt;br /&gt;
  Spades,&lt;br /&gt;
  Clubs,&lt;br /&gt;
  Diamonds,&lt;br /&gt;
  Hearts&lt;br /&gt;
};&lt;br /&gt;
 &lt;br /&gt;
Suits suitInPlay = Clubs;&lt;br /&gt;
Serial.print(“suitInPlay = “);&lt;br /&gt;
Serial.println((uint8_t)suitInPlay);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
suitInPlay = 3;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, attempting to assign a numeric value to a variable of type &amp;lt;code&amp;gt;Suits&amp;lt;/code&amp;gt; will fail. Likewise attempting to assign an element of the &amp;lt;code&amp;gt;Suits&amp;lt;/code&amp;gt; enumeration to an integer variable will also fail.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Overloading Operators&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One major benefit of using a class enumerator is the ability to override the standard operators like ++, --, &amp;lt;, &amp;gt; and == to suit your needs.&lt;br /&gt;
&lt;br /&gt;
In my 1943 game, the enemy boats have gun turrets that rotate to point towards the player as they pass each other. To record the direction of the turret, I have created a scoped class called &amp;lt;code&amp;gt;Direction&amp;lt;/code&amp;gt; as shown below. As you would expect, the enumeration lists the various directions from &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;NorthEast&amp;lt;/code&amp;gt; in order. In my code, the actual values assigned to these elements are irrelevant and the default value of &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; has been assigned to &amp;lt;code&amp;gt;North&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;NorthEast&amp;lt;/code&amp;gt; and so forth up with &amp;lt;code&amp;gt;NorthWest&amp;lt;/code&amp;gt; having a value of &amp;lt;code&amp;gt;7&amp;lt;/code&amp;gt;. I have an additional direction, called &amp;lt;code&amp;gt;Unknown&amp;lt;/code&amp;gt;, which is assigned the value of &amp;lt;code&amp;gt;8&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum class Direction : uint8_t {&lt;br /&gt;
  North,&lt;br /&gt;
  NorthEast,&lt;br /&gt;
  East,&lt;br /&gt;
  SouthEast,&lt;br /&gt;
  South,&lt;br /&gt;
  SouthWest,&lt;br /&gt;
  West,&lt;br /&gt;
  NorthWest,&lt;br /&gt;
  Unknown&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Values in an enumeration can be compared together using the standard equality and inequality comparisons. However, the introduction of the &amp;lt;code&amp;gt;Unknown&amp;lt;/code&amp;gt; element in the &amp;lt;code&amp;gt;Direction&amp;lt;/code&amp;gt; enumeration allows me to illustrate how we can override the standard operation.&lt;br /&gt;
&lt;br /&gt;
When comparing two directions together, the comparison should return false if either or both arguments are equal to &amp;lt;code&amp;gt;Direction::Unknown&amp;lt;/code&amp;gt;. The following operator override demonstrates this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator==(const Direction lhs, const Direction rhs) {&lt;br /&gt;
  return ((uint8_t)lhs == (uint8_t)Direction::Unknown || (uint8_t)rhs == (uint8_t)Direction::Unknown&lt;br /&gt;
  ? false&lt;br /&gt;
  : (uint8_t)lhs == (uint8_t)rhs);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Putting this operator to work reveals the following results:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Direction a = Direction::East;&lt;br /&gt;
Direction b = Direction::East;&lt;br /&gt;
 &lt;br /&gt;
if (a == b) { Serial.println(“1 a is equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::North;&lt;br /&gt;
 &lt;br /&gt;
if (!(a == b)) { Serial.println(“2 a is not equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
a = Direction::Unknown;&lt;br /&gt;
b = Direction::Unknown;&lt;br /&gt;
 &lt;br /&gt;
if (!(a == b)) { Serial.println(“3 a is not equal to b”); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1 a is equal to b&lt;br /&gt;
2 a is not equal to b&lt;br /&gt;
3 a is not equal to b&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that in the last test, the comparison between the two Unknown directions returned false due to the rule created in the equality comparison.&lt;br /&gt;
&lt;br /&gt;
Hang on, what’s with the &amp;lt;code&amp;gt;(!(a == b))&amp;lt;/code&amp;gt; syntax? What about using the standard ‘not equals’ syntax?  As we have overridden the equality operator we need to override the inequality operator as well. The inequality operator shown below simply calls the modified equality operator and returns the NOT of it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator!=(const Direction lhs, const Direction rhs)  {&lt;br /&gt;
  return ! (lhs == rhs);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this new operator override in place, we can use the standard notation shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::North;&lt;br /&gt;
 &lt;br /&gt;
if (a != b) { Serial.println(“2 a is not equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
a = Direction::Unknown;&lt;br /&gt;
b = Direction::Unknown;&lt;br /&gt;
 &lt;br /&gt;
if (a != b) { Serial.println(“3 a is not equal to b”); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1 a is equal to b&lt;br /&gt;
2 a is not equal to b&lt;br /&gt;
3 a is not equal to b&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Getting more complex .. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
So now we can compare directions, what about comparing values?&lt;br /&gt;
&lt;br /&gt;
Assume the boat is at the bottom of the screen entering from the right and leaving towards the left. If the player is stationary and located at the top-centre of the screen then to shoot towards the player the gun will need to be pointed towards the North-West initially and will swing through North and then towards the North-East as the boat leaves the screen. In my code, I was hoping to compare the current direction of the turret to the player’s position and simply add or subtract one to swing it around as required. Although the enumeration details an order to the directions where South is greater than SouthEast (in a clockwise view of the world), it does not consider that North is greater than NorthWest, ie it has no concept of the circular nature of a compass.&lt;br /&gt;
&lt;br /&gt;
The following operators override the standard ‘less than’ and ‘greater than’ operators. Before calculating a return value, the operator determines whether it needs to consider the resetting of values at North by subtracting the integer values from each other. Where the comparison must cater for North, adding 8 (a full circle) to the either the right or left hand side argument (for ‘less than’ or ‘greater than’ respectively) ensures that the comparison caters for this appropriately.&lt;br /&gt;
 						&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator&amp;lt;(const Direction lhs, const Direction rhs)  {&lt;br /&gt;
    return (abs((uint8_t)lhs - (uint8_t)rhs) &amp;lt; 4 ? (uint8_t)lhs - (uint8_t)rhs : (uint8_t)lhs -&lt;br /&gt;
(8 + (uint8_t)rhs)) &amp;lt; 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
inline bool operator&amp;gt;(const Direction  lhs, const Direction  rhs)  {&lt;br /&gt;
    return (abs((uint8_t)lhs - (uint8_t)rhs) &amp;lt; 4 ? (uint8_t)lhs - (uint8_t)rhs : (8 + (uint8_t)&lt;br /&gt;
lhs) - (uint8_t)rhs) &amp;gt; 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Putting these two operators through their paces reveals the correct results shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Direction a = Direction::East;&lt;br /&gt;
Direction b = Direction::South;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“1 East is less than South”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“2 South is greater than East”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::North;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“3 NorthWest is less than North”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“4 North is greater than NorthWest”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::NorthEast;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“5 NorthWest is less than NorthEast”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“6 NorthEast is greater than NorthWest”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::North;&lt;br /&gt;
b = Direction::NorthEast;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“7 North is less than NorthEast”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“8 NorthEast is greater than North”); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1 East is less than South&lt;br /&gt;
2 South is greater than East&lt;br /&gt;
3 NorthWest is less than North&lt;br /&gt;
4 North is greater than NorthWest&lt;br /&gt;
5 NorthWest is less than NorthEast&lt;br /&gt;
6 NorthEast is greater than NorthWest&lt;br /&gt;
7 North is less than NorthEast&lt;br /&gt;
8 NorthEast is greater than North&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now that we have the ‘equality’, ‘greater than’ and ‘less than’ operators in place, we can add the following two operators to complete the comparisons.&lt;br /&gt;
&lt;br /&gt;
Note they perform the logical &amp;lt;code&amp;gt;NOT&amp;lt;/code&amp;gt; of those operators previously defined.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator&amp;lt;=(const Direction lhs, const Direction rhs) { return !(lhs &amp;gt; rhs); }&lt;br /&gt;
inline bool operator&amp;gt;=(const Direction lhs, const Direction rhs) { return !(lhs &amp;lt; rhs); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== And just plain confusing .. ==&lt;br /&gt;
&lt;br /&gt;
Not really confusing but overriding the standard increment and decrement operators reveal an interesting compiler trick that you just have to take for granted actually works. The operator below describes a pre-increment operator.&lt;br /&gt;
&lt;br /&gt;
If the supplied direction is NorthWest&lt;br /&gt;
it returns North otherwise it returns the direction adjacent to the supplied on in the enumeration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction &amp;amp;operator++( Direction &amp;amp;c ) {&lt;br /&gt;
  c = ( c == Direction::NorthWest )&lt;br /&gt;
  ? Direction::North&lt;br /&gt;
  : static_cast&amp;lt;Direction&amp;gt;( static_cast&amp;lt;uint8_t&amp;gt;(c) + 1 );&lt;br /&gt;
  return c;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A post-increment operator includes an anonymous second parameter of type integer. This parameter is not used in the code and is simply there to differentiate between the pre- and post- increment functions.&lt;br /&gt;
&lt;br /&gt;
The compiler must recognise this method signature and assume it is a post-increment function. The implementation of this operator simply calls the pre-incrementer and returns its value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction operator++( Direction &amp;amp;c, int ) {&lt;br /&gt;
  Direction result = c;&lt;br /&gt;
  ++c;&lt;br /&gt;
  return result;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The decrement functions are nearly identical to the increment operators and are shown below for completeness.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction &amp;amp;operator--( Direction &amp;amp; c ) {&lt;br /&gt;
  c = ( c == Direction::North )&lt;br /&gt;
  ? Direction::NorthWest&lt;br /&gt;
  : static_cast&amp;lt;Direction&amp;gt;( static_cast&amp;lt;uint8_t&amp;gt;(c) - 1 );&lt;br /&gt;
  return c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
inline Direction operator--( Direction &amp;amp; c, int ) {&lt;br /&gt;
  Direction result = c;&lt;br /&gt;
  --c;&lt;br /&gt;
  return result;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As you can see, the scoped enumerations provide an extra layer of functionality beyond what simple enumerations offer. The ability&lt;br /&gt;
to override the operators encapsulates the logic of the enumeration itself and saves the developer having to repeat similar logic across an application. In the examples shown in&lt;br /&gt;
this article, the developer does not need to remember the intrinsic values underlying the directions North or East nor does he need to consider how to increase the value when it reaches the end of the enumeration.&lt;br /&gt;
&lt;br /&gt;
n fact, all of the code would work if you change the initial enumeration declaration to (say) North = 57 ..&lt;br /&gt;
&lt;br /&gt;
I have included a simple application on GitHub that shows the enumeration in action : https://github.com/filmote/ScopedEnums&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Scoped_Enumerations&amp;diff=605</id>
		<title>Scoped Enumerations</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Scoped_Enumerations&amp;diff=605"/>
		<updated>2024-08-17T01:16:06Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* What are Scoped Enumerations? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== &#039;&#039;&#039;What are Scoped Enumerations?&#039;&#039;&#039; ==&lt;br /&gt;
Almost all programming languages provide a concept of an enumeration – a set of related constant values. In most languages, enumerations simply resolve to integer values and can be cast back and forth as needed.&lt;br /&gt;
&lt;br /&gt;
In C++ this casting is done implicitly and it is this capability of swapping from enumeration elements to the underlying value that can lead to problems. A simple enumeration that describes the suits in a deck of cards is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum {&lt;br /&gt;
  Spades,&lt;br /&gt;
  Clubs,&lt;br /&gt;
  Diamonds,&lt;br /&gt;
  Hearts&lt;br /&gt;
};&lt;br /&gt;
 &lt;br /&gt;
int suitInPlay = Hearts;&lt;br /&gt;
suitInPlay++;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A couple of things to note about this code is that it is not ‘type safe’ due to the variable &amp;lt;code&amp;gt;suitInPlay&amp;lt;/code&amp;gt; being declared as a simple integer and therefore being able to accept any valid int value rather than only those nominated&lt;br /&gt;
&lt;br /&gt;
in the enumeration. The increment is also problematic as it takes a valid declaration of the &amp;lt;code&amp;gt;suitInPlay&amp;lt;/code&amp;gt; (Hearts which in turn has an underlying value of three) and then increments it to a value outside of the enumeration.&lt;br /&gt;
&lt;br /&gt;
C++ 11 extends the basic enumeration type with scoped enumerations that overcomes this problem. The following code shows a scoped enumeration definition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum class Suits {&lt;br /&gt;
  Spades,&lt;br /&gt;
  Clubs,&lt;br /&gt;
  Diamonds,&lt;br /&gt;
  Hearts&lt;br /&gt;
};&lt;br /&gt;
 &lt;br /&gt;
Suits suitInPlay = Suits::Clubs;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Attempting to increment the variable or convert it to a numeric will fail. The declaration does not imply an integral data type - byte, integer or long - and the developer should not make any assumptions as to the type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
enum class Suits : uint8_t {&lt;br /&gt;
  Spades,&lt;br /&gt;
  Clubs,&lt;br /&gt;
  Diamonds,&lt;br /&gt;
  Hearts&lt;br /&gt;
};&lt;br /&gt;
 &lt;br /&gt;
Suits suitInPlay = Clubs;&lt;br /&gt;
Serial.print(“suitInPlay = “);&lt;br /&gt;
Serial.println((uint8_t)suitInPlay);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
suitInPlay = 3;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, attempting to assign a numeric value to a variable of type &amp;lt;code&amp;gt;Suits&amp;lt;/code&amp;gt; will fail. Likewise attempting to assign an element of the &amp;lt;code&amp;gt;Suits&amp;lt;/code&amp;gt; enumeration to an integer variable will also fail.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Overloading Operators&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One major benefit of using a class enumerator is the ability to override the standard operators like ++, --, &amp;lt;, &amp;gt; and == to suit your needs.&lt;br /&gt;
&lt;br /&gt;
In my 1943 game, the enemy boats have gun turrets that rotate to point towards the player as they pass each other. To record the direction of the turret, I have created a scoped class called Direction as shown below. As you would expect, the enumeration lists the various directions from North to NorthEast in order. In my code, the actual values assigned to these elements are irrelevant and the default value of zero has been assigned to North, one to NorthEast and so forth up with NorthWest having a value of 7. I have an additional direction, called Unknown, which is assigned the value of 8.&lt;br /&gt;
 enum class Direction : uint8_t {&lt;br /&gt;
   North,&lt;br /&gt;
   NorthEast,&lt;br /&gt;
   East,&lt;br /&gt;
   SouthEast,&lt;br /&gt;
   South,&lt;br /&gt;
   SouthWest,&lt;br /&gt;
   West,&lt;br /&gt;
   NorthWest,&lt;br /&gt;
   Unknown&lt;br /&gt;
 };&lt;br /&gt;
Values in an enumeration can be compared together using the standard equality and inequality comparisons. However, the introduction of the Unknown element in the Direction enumeration allows me to illustrate how we can override the standard operation.&lt;br /&gt;
&lt;br /&gt;
When comparing two directions together, the comparison should return false if either or both arguments are equal to Direction::Unknown. The following operator override demonstrates this:&lt;br /&gt;
&lt;br /&gt;
 inline bool operator==(const Direction lhs, const Direction rhs) {&lt;br /&gt;
     return ((uint8_t)lhs == (uint8_t)Direction::Unknown&lt;br /&gt;
     ? false&lt;br /&gt;
     : (uint8_t)lhs == (uint8_t)rhs);&lt;br /&gt;
 }&lt;br /&gt;
Putting this operator to work reveals the following results:&lt;br /&gt;
&lt;br /&gt;
 Direction a = Direction::East;&lt;br /&gt;
 Direction b = Direction::East;&lt;br /&gt;
 &lt;br /&gt;
 if (a == b) { Serial.println(“1 a is equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
 a = Direction::NorthWest;&lt;br /&gt;
 b = Direction::North;&lt;br /&gt;
 &lt;br /&gt;
 if (!(a == b)) { Serial.println(“2 a is not equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
 a = Direction::Unknown;&lt;br /&gt;
 b = Direction::Unknown;&lt;br /&gt;
 &lt;br /&gt;
 if (!(a == b)) { Serial.println(“3 a is not equal to b”); }&lt;br /&gt;
Output:&lt;br /&gt;
 1 a is equal to b&lt;br /&gt;
 2 a is not equal to b&lt;br /&gt;
 3 a is not equal to b&lt;br /&gt;
Note that in the last test, the comparison between the two Unknown directions returned false due to the rule created in the equality comparison.&lt;br /&gt;
&lt;br /&gt;
Hang on, what’s with the (!(a == b)) syntax? What about using the standard ‘not equals’ syntax?  As we have overridden the equality operator we need to override the inequality operator as well. The inequality operator shown below simply calls the modified equality operator and returns the NOT of it.&lt;br /&gt;
&lt;br /&gt;
 inline bool operator!=(const Direction lhs, const Direction rhs)  {&lt;br /&gt;
   return ! (lhs == rhs);&lt;br /&gt;
 }&lt;br /&gt;
With this new operator override in place, we can use the standard notation shown below:&lt;br /&gt;
 ...&lt;br /&gt;
 a = Direction::NorthWest;&lt;br /&gt;
 b = Direction::North;&lt;br /&gt;
 &lt;br /&gt;
 if (a != b) { Serial.println(“2 a is not equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
 a = Direction::Unknown;&lt;br /&gt;
 b = Direction::Unknown;&lt;br /&gt;
 &lt;br /&gt;
 if (a != b) { Serial.println(“3 a is not equal to b”); }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 1 a is equal to b&lt;br /&gt;
 2 a is not equal to b&lt;br /&gt;
 3 a is not equal to b&lt;br /&gt;
&lt;br /&gt;
== Getting more complex .. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
So now we can compare directions, what about comparing values?&lt;br /&gt;
&lt;br /&gt;
Assume the boat is at the bottom of the screen entering from the right and leaving towards the left. If the player is stationary and located at the top-centre of the screen then to shoot towards the player the gun will need to be pointed towards the North-West initially and will swing through North and then towards the North-East as the boat leaves the screen. In my code, I was hoping to compare the current direction of the turret to the player’s position and simply add or subtract one to swing it around as required. Although the enumeration details an order to the directions where South is greater than SouthEast (in a clockwise view of the world), it does not consider that North is greater than NorthWest, ie it has no concept of the circular nature of a compass.&lt;br /&gt;
&lt;br /&gt;
The following operators override the standard ‘less than’ and ‘greater than’ operators. Before calculating a return value, the operator determines whether it needs to consider the resetting of values at North by subtracting the integer values from each other. Where the comparison must cater for North, adding 8 (a full circle) to the either the right or left hand side argument (for ‘less than’ or ‘greater than’ respectively) ensures that the comparison caters for this appropriately.&lt;br /&gt;
 						&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator&amp;lt;(const Direction lhs, const Direction rhs)  {&lt;br /&gt;
    return (abs((uint8_t)lhs - (uint8_t)rhs) &amp;lt; 4 ? (uint8_t)lhs - (uint8_t)rhs : (uint8_t)lhs -&lt;br /&gt;
(8 + (uint8_t)rhs)) &amp;lt; 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
inline bool operator&amp;gt;(const Direction  lhs, const Direction  rhs)  {&lt;br /&gt;
    return (abs((uint8_t)lhs - (uint8_t)rhs) &amp;lt; 4 ? (uint8_t)lhs - (uint8_t)rhs : (8 + (uint8_t)&lt;br /&gt;
lhs) - (uint8_t)rhs) &amp;gt; 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Putting these two operators through their paces reveals the correct results shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Direction a = Direction::East;&lt;br /&gt;
Direction b = Direction::South;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“1 East is less than South”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“2 South is greater than East”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::North;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“3 NorthWest is less than North”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“4 North is greater than NorthWest”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::NorthEast;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“5 NorthWest is less than NorthEast”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“6 NorthEast is greater than NorthWest”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::North;&lt;br /&gt;
b = Direction::NorthEast;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“7 North is less than NorthEast”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“8 NorthEast is greater than North”); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1 East is less than South&lt;br /&gt;
2 South is greater than East&lt;br /&gt;
3 NorthWest is less than North&lt;br /&gt;
4 North is greater than NorthWest&lt;br /&gt;
5 NorthWest is less than NorthEast&lt;br /&gt;
6 NorthEast is greater than NorthWest&lt;br /&gt;
7 North is less than NorthEast&lt;br /&gt;
8 NorthEast is greater than North&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now that we have the ‘equality’, ‘greater than’ and ‘less than’ operators in place, we can add the following two operators to complete the comparisons.&lt;br /&gt;
&lt;br /&gt;
Note they perform the logical &amp;lt;code&amp;gt;NOT&amp;lt;/code&amp;gt; of those operators previously defined.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator&amp;lt;=(const Direction lhs, const Direction rhs) { return !(lhs &amp;gt; rhs); }&lt;br /&gt;
inline bool operator&amp;gt;=(const Direction lhs, const Direction rhs) { return !(lhs &amp;lt; rhs); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== And just plain confusing .. ==&lt;br /&gt;
&lt;br /&gt;
Not really confusing but overriding the standard increment and decrement operators reveal an interesting compiler trick that you just have to take for granted actually works. The operator below describes a pre-increment operator.&lt;br /&gt;
&lt;br /&gt;
If the supplied direction is NorthWest&lt;br /&gt;
it returns North otherwise it returns the direction adjacent to the supplied on in the enumeration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction &amp;amp;operator++( Direction &amp;amp;c ) {&lt;br /&gt;
  c = ( c == Direction::NorthWest )&lt;br /&gt;
  ? Direction::North&lt;br /&gt;
  : static_cast&amp;lt;Direction&amp;gt;( static_cast&amp;lt;uint8_t&amp;gt;(c) + 1 );&lt;br /&gt;
  return c;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A post-increment operator includes an anonymous second parameter of type integer. This parameter is not used in the code and is simply there to differentiate between the pre- and post- increment functions.&lt;br /&gt;
&lt;br /&gt;
The compiler must recognise this method signature and assume it is a post-increment function. The implementation of this operator simply calls the pre-incrementer and returns its value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction operator++( Direction &amp;amp;c, int ) {&lt;br /&gt;
  Direction result = c;&lt;br /&gt;
  ++c;&lt;br /&gt;
  return result;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The decrement functions are nearly identical to the increment operators and are shown below for completeness.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction &amp;amp;operator--( Direction &amp;amp; c ) {&lt;br /&gt;
  c = ( c == Direction::North )&lt;br /&gt;
  ? Direction::NorthWest&lt;br /&gt;
  : static_cast&amp;lt;Direction&amp;gt;( static_cast&amp;lt;uint8_t&amp;gt;(c) - 1 );&lt;br /&gt;
  return c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
inline Direction operator--( Direction &amp;amp; c, int ) {&lt;br /&gt;
  Direction result = c;&lt;br /&gt;
  --c;&lt;br /&gt;
  return result;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As you can see, the scoped enumerations provide an extra layer of functionality beyond what simple enumerations offer. The ability&lt;br /&gt;
to override the operators encapsulates the logic of the enumeration itself and saves the developer having to repeat similar logic across an application. In the examples shown in&lt;br /&gt;
this article, the developer does not need to remember the intrinsic values underlying the directions North or East nor does he need to consider how to increase the value when it reaches the end of the enumeration.&lt;br /&gt;
&lt;br /&gt;
n fact, all of the code would work if you change the initial enumeration declaration to (say) North = 57 ..&lt;br /&gt;
&lt;br /&gt;
I have included a simple application on GitHub that shows the enumeration in action : https://github.com/filmote/ScopedEnums&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Scoped_Enumerations&amp;diff=604</id>
		<title>Scoped Enumerations</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Scoped_Enumerations&amp;diff=604"/>
		<updated>2024-08-17T01:14:14Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* What are Scoped Enumerations? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== &#039;&#039;&#039;What are Scoped Enumerations?&#039;&#039;&#039; ==&lt;br /&gt;
Almost all programming languages provide a concept of an enumeration – a set of related constant values. In most languages, enumerations simply resolve to integer values and can be cast back and forth as needed.&lt;br /&gt;
&lt;br /&gt;
In C++ this casting is done implicitly and it is this capability of swapping from enumeration elements to the underlying value that can lead to problems. A simple enumeration that describes the suits in a deck of cards is shown below:&lt;br /&gt;
&lt;br /&gt;
 enum {&lt;br /&gt;
   Spades,&lt;br /&gt;
   Clubs,&lt;br /&gt;
   Diamonds,&lt;br /&gt;
   Hearts&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 int suitInPlay = Hearts;&lt;br /&gt;
 suitInPlay++;&lt;br /&gt;
A couple of things to note about this code is that it is not ‘type safe’ due to the variable suitInPlay being declared as a simple integer and therefore being able to accept any valid int value rather than only those nominated&lt;br /&gt;
&lt;br /&gt;
in the enumeration. The increment is also problematic as it takes a valid declaration of the suitInPlay (Hearts which in turn has an underlying value of three) and then increments it to a value outside of the enumeration.&lt;br /&gt;
&lt;br /&gt;
C++ 11 extends the basic enumeration type with scoped enumerations that overcomes this problem. The following code shows a scoped enumeration definition:&lt;br /&gt;
&lt;br /&gt;
 enum class Suits {&lt;br /&gt;
   Spades,&lt;br /&gt;
   Clubs,&lt;br /&gt;
   Diamonds,&lt;br /&gt;
   Hearts&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 Suits suitInPlay = Suits::Clubs;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Attempting to increment the variable or convert it to a numeric will fail. The declaration does not imply an integral data type - byte, integer or long - and the developer should not make any assumptions as to the type.&lt;br /&gt;
&lt;br /&gt;
 enum class Suits : uint8_t {&lt;br /&gt;
   Spades,&lt;br /&gt;
   Clubs,&lt;br /&gt;
   Diamonds,&lt;br /&gt;
   Hearts&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 Suits suitInPlay = Clubs;&lt;br /&gt;
 Serial.print(“suitInPlay = “);&lt;br /&gt;
 Serial.println((uint8_t)suitInPlay);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
suitInPlay = 3;&lt;br /&gt;
&lt;br /&gt;
However, attempting to assign a numeric value to a variable of type Suits will fail. Likewise attempting to assign an element of the Suits enumeration to an integer variable will also fail.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Overloading Operators&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One major benefit of using a class enumerator is the ability to override the standard operators like ++, --, &amp;lt;, &amp;gt; and == to suit your needs.&lt;br /&gt;
&lt;br /&gt;
In my 1943 game, the enemy boats have gun turrets that rotate to point towards the player as they pass each other. To record the direction of the turret, I have created a scoped class called Direction as shown below. As you would expect, the enumeration lists the various directions from North to NorthEast in order. In my code, the actual values assigned to these elements are irrelevant and the default value of zero has been assigned to North, one to NorthEast and so forth up with NorthWest having a value of 7. I have an additional direction, called Unknown, which is assigned the value of 8.&lt;br /&gt;
 enum class Direction : uint8_t {&lt;br /&gt;
   North,&lt;br /&gt;
   NorthEast,&lt;br /&gt;
   East,&lt;br /&gt;
   SouthEast,&lt;br /&gt;
   South,&lt;br /&gt;
   SouthWest,&lt;br /&gt;
   West,&lt;br /&gt;
   NorthWest,&lt;br /&gt;
   Unknown&lt;br /&gt;
 };&lt;br /&gt;
Values in an enumeration can be compared together using the standard equality and inequality comparisons. However, the introduction of the Unknown element in the Direction enumeration allows me to illustrate how we can override the standard operation.&lt;br /&gt;
&lt;br /&gt;
When comparing two directions together, the comparison should return false if either or both arguments are equal to Direction::Unknown. The following operator override demonstrates this:&lt;br /&gt;
&lt;br /&gt;
 inline bool operator==(const Direction lhs, const Direction rhs) {&lt;br /&gt;
     return ((uint8_t)lhs == (uint8_t)Direction::Unknown&lt;br /&gt;
     ? false&lt;br /&gt;
     : (uint8_t)lhs == (uint8_t)rhs);&lt;br /&gt;
 }&lt;br /&gt;
Putting this operator to work reveals the following results:&lt;br /&gt;
&lt;br /&gt;
 Direction a = Direction::East;&lt;br /&gt;
 Direction b = Direction::East;&lt;br /&gt;
 &lt;br /&gt;
 if (a == b) { Serial.println(“1 a is equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
 a = Direction::NorthWest;&lt;br /&gt;
 b = Direction::North;&lt;br /&gt;
 &lt;br /&gt;
 if (!(a == b)) { Serial.println(“2 a is not equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
 a = Direction::Unknown;&lt;br /&gt;
 b = Direction::Unknown;&lt;br /&gt;
 &lt;br /&gt;
 if (!(a == b)) { Serial.println(“3 a is not equal to b”); }&lt;br /&gt;
Output:&lt;br /&gt;
 1 a is equal to b&lt;br /&gt;
 2 a is not equal to b&lt;br /&gt;
 3 a is not equal to b&lt;br /&gt;
Note that in the last test, the comparison between the two Unknown directions returned false due to the rule created in the equality comparison.&lt;br /&gt;
&lt;br /&gt;
Hang on, what’s with the (!(a == b)) syntax? What about using the standard ‘not equals’ syntax?  As we have overridden the equality operator we need to override the inequality operator as well. The inequality operator shown below simply calls the modified equality operator and returns the NOT of it.&lt;br /&gt;
&lt;br /&gt;
 inline bool operator!=(const Direction lhs, const Direction rhs)  {&lt;br /&gt;
   return ! (lhs == rhs);&lt;br /&gt;
 }&lt;br /&gt;
With this new operator override in place, we can use the standard notation shown below:&lt;br /&gt;
 ...&lt;br /&gt;
 a = Direction::NorthWest;&lt;br /&gt;
 b = Direction::North;&lt;br /&gt;
 &lt;br /&gt;
 if (a != b) { Serial.println(“2 a is not equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
 a = Direction::Unknown;&lt;br /&gt;
 b = Direction::Unknown;&lt;br /&gt;
 &lt;br /&gt;
 if (a != b) { Serial.println(“3 a is not equal to b”); }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 1 a is equal to b&lt;br /&gt;
 2 a is not equal to b&lt;br /&gt;
 3 a is not equal to b&lt;br /&gt;
&lt;br /&gt;
== Getting more complex .. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
So now we can compare directions, what about comparing values?&lt;br /&gt;
&lt;br /&gt;
Assume the boat is at the bottom of the screen entering from the right and leaving towards the left. If the player is stationary and located at the top-centre of the screen then to shoot towards the player the gun will need to be pointed towards the North-West initially and will swing through North and then towards the North-East as the boat leaves the screen. In my code, I was hoping to compare the current direction of the turret to the player’s position and simply add or subtract one to swing it around as required. Although the enumeration details an order to the directions where South is greater than SouthEast (in a clockwise view of the world), it does not consider that North is greater than NorthWest, ie it has no concept of the circular nature of a compass.&lt;br /&gt;
&lt;br /&gt;
The following operators override the standard ‘less than’ and ‘greater than’ operators. Before calculating a return value, the operator determines whether it needs to consider the resetting of values at North by subtracting the integer values from each other. Where the comparison must cater for North, adding 8 (a full circle) to the either the right or left hand side argument (for ‘less than’ or ‘greater than’ respectively) ensures that the comparison caters for this appropriately.&lt;br /&gt;
 						&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator&amp;lt;(const Direction lhs, const Direction rhs)  {&lt;br /&gt;
    return (abs((uint8_t)lhs - (uint8_t)rhs) &amp;lt; 4 ? (uint8_t)lhs - (uint8_t)rhs : (uint8_t)lhs -&lt;br /&gt;
(8 + (uint8_t)rhs)) &amp;lt; 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
inline bool operator&amp;gt;(const Direction  lhs, const Direction  rhs)  {&lt;br /&gt;
    return (abs((uint8_t)lhs - (uint8_t)rhs) &amp;lt; 4 ? (uint8_t)lhs - (uint8_t)rhs : (8 + (uint8_t)&lt;br /&gt;
lhs) - (uint8_t)rhs) &amp;gt; 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Putting these two operators through their paces reveals the correct results shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Direction a = Direction::East;&lt;br /&gt;
Direction b = Direction::South;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“1 East is less than South”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“2 South is greater than East”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::North;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“3 NorthWest is less than North”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“4 North is greater than NorthWest”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::NorthEast;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“5 NorthWest is less than NorthEast”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“6 NorthEast is greater than NorthWest”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::North;&lt;br /&gt;
b = Direction::NorthEast;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“7 North is less than NorthEast”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“8 NorthEast is greater than North”); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1 East is less than South&lt;br /&gt;
2 South is greater than East&lt;br /&gt;
3 NorthWest is less than North&lt;br /&gt;
4 North is greater than NorthWest&lt;br /&gt;
5 NorthWest is less than NorthEast&lt;br /&gt;
6 NorthEast is greater than NorthWest&lt;br /&gt;
7 North is less than NorthEast&lt;br /&gt;
8 NorthEast is greater than North&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now that we have the ‘equality’, ‘greater than’ and ‘less than’ operators in place, we can add the following two operators to complete the comparisons.&lt;br /&gt;
&lt;br /&gt;
Note they perform the logical &amp;lt;code&amp;gt;NOT&amp;lt;/code&amp;gt; of those operators previously defined.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator&amp;lt;=(const Direction lhs, const Direction rhs) { return !(lhs &amp;gt; rhs); }&lt;br /&gt;
inline bool operator&amp;gt;=(const Direction lhs, const Direction rhs) { return !(lhs &amp;lt; rhs); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== And just plain confusing .. ==&lt;br /&gt;
&lt;br /&gt;
Not really confusing but overriding the standard increment and decrement operators reveal an interesting compiler trick that you just have to take for granted actually works. The operator below describes a pre-increment operator.&lt;br /&gt;
&lt;br /&gt;
If the supplied direction is NorthWest&lt;br /&gt;
it returns North otherwise it returns the direction adjacent to the supplied on in the enumeration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction &amp;amp;operator++( Direction &amp;amp;c ) {&lt;br /&gt;
  c = ( c == Direction::NorthWest )&lt;br /&gt;
  ? Direction::North&lt;br /&gt;
  : static_cast&amp;lt;Direction&amp;gt;( static_cast&amp;lt;uint8_t&amp;gt;(c) + 1 );&lt;br /&gt;
  return c;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A post-increment operator includes an anonymous second parameter of type integer. This parameter is not used in the code and is simply there to differentiate between the pre- and post- increment functions.&lt;br /&gt;
&lt;br /&gt;
The compiler must recognise this method signature and assume it is a post-increment function. The implementation of this operator simply calls the pre-incrementer and returns its value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction operator++( Direction &amp;amp;c, int ) {&lt;br /&gt;
  Direction result = c;&lt;br /&gt;
  ++c;&lt;br /&gt;
  return result;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The decrement functions are nearly identical to the increment operators and are shown below for completeness.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction &amp;amp;operator--( Direction &amp;amp; c ) {&lt;br /&gt;
  c = ( c == Direction::North )&lt;br /&gt;
  ? Direction::NorthWest&lt;br /&gt;
  : static_cast&amp;lt;Direction&amp;gt;( static_cast&amp;lt;uint8_t&amp;gt;(c) - 1 );&lt;br /&gt;
  return c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
inline Direction operator--( Direction &amp;amp; c, int ) {&lt;br /&gt;
  Direction result = c;&lt;br /&gt;
  --c;&lt;br /&gt;
  return result;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As you can see, the scoped enumerations provide an extra layer of functionality beyond what simple enumerations offer. The ability&lt;br /&gt;
to override the operators encapsulates the logic of the enumeration itself and saves the developer having to repeat similar logic across an application. In the examples shown in&lt;br /&gt;
this article, the developer does not need to remember the intrinsic values underlying the directions North or East nor does he need to consider how to increase the value when it reaches the end of the enumeration.&lt;br /&gt;
&lt;br /&gt;
n fact, all of the code would work if you change the initial enumeration declaration to (say) North = 57 ..&lt;br /&gt;
&lt;br /&gt;
I have included a simple application on GitHub that shows the enumeration in action : https://github.com/filmote/ScopedEnums&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Scoped_Enumerations&amp;diff=603</id>
		<title>Scoped Enumerations</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Scoped_Enumerations&amp;diff=603"/>
		<updated>2024-08-17T01:13:59Z</updated>

		<summary type="html">&lt;p&gt;Filmote: Created page with &amp;quot;=== &amp;#039;&amp;#039;&amp;#039;What are Scoped Enumerations?&amp;#039;&amp;#039;&amp;#039; === Almost all programming languages provide a concept of an enumeration – a set of related constant values. In most languages, enumerations simply resolve to integer values and can be cast back and forth as needed.  In C++ this casting is done implicitly and it is this capability of swapping from enumeration elements to the underlying value that can lead to problems. A simple enumeration that describes the suits in a deck of car...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=== &#039;&#039;&#039;What are Scoped Enumerations?&#039;&#039;&#039; ===&lt;br /&gt;
Almost all programming languages provide a concept of an enumeration – a set of related constant values. In most languages, enumerations simply resolve to integer values and can be cast back and forth as needed.&lt;br /&gt;
&lt;br /&gt;
In C++ this casting is done implicitly and it is this capability of swapping from enumeration elements to the underlying value that can lead to problems. A simple enumeration that describes the suits in a deck of cards is shown below:&lt;br /&gt;
&lt;br /&gt;
 enum {&lt;br /&gt;
   Spades,&lt;br /&gt;
   Clubs,&lt;br /&gt;
   Diamonds,&lt;br /&gt;
   Hearts&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 int suitInPlay = Hearts;&lt;br /&gt;
 suitInPlay++;&lt;br /&gt;
A couple of things to note about this code is that it is not ‘type safe’ due to the variable suitInPlay being declared as a simple integer and therefore being able to accept any valid int value rather than only those nominated&lt;br /&gt;
&lt;br /&gt;
in the enumeration. The increment is also problematic as it takes a valid declaration of the suitInPlay (Hearts which in turn has an underlying value of three) and then increments it to a value outside of the enumeration.&lt;br /&gt;
&lt;br /&gt;
C++ 11 extends the basic enumeration type with scoped enumerations that overcomes this problem. The following code shows a scoped enumeration definition:&lt;br /&gt;
&lt;br /&gt;
 enum class Suits {&lt;br /&gt;
   Spades,&lt;br /&gt;
   Clubs,&lt;br /&gt;
   Diamonds,&lt;br /&gt;
   Hearts&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 Suits suitInPlay = Suits::Clubs;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Attempting to increment the variable or convert it to a numeric will fail. The declaration does not imply an integral data type - byte, integer or long - and the developer should not make any assumptions as to the type.&lt;br /&gt;
&lt;br /&gt;
 enum class Suits : uint8_t {&lt;br /&gt;
   Spades,&lt;br /&gt;
   Clubs,&lt;br /&gt;
   Diamonds,&lt;br /&gt;
   Hearts&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 Suits suitInPlay = Clubs;&lt;br /&gt;
 Serial.print(“suitInPlay = “);&lt;br /&gt;
 Serial.println((uint8_t)suitInPlay);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
suitInPlay = 3;&lt;br /&gt;
&lt;br /&gt;
However, attempting to assign a numeric value to a variable of type Suits will fail. Likewise attempting to assign an element of the Suits enumeration to an integer variable will also fail.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Overloading Operators&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One major benefit of using a class enumerator is the ability to override the standard operators like ++, --, &amp;lt;, &amp;gt; and == to suit your needs.&lt;br /&gt;
&lt;br /&gt;
In my 1943 game, the enemy boats have gun turrets that rotate to point towards the player as they pass each other. To record the direction of the turret, I have created a scoped class called Direction as shown below. As you would expect, the enumeration lists the various directions from North to NorthEast in order. In my code, the actual values assigned to these elements are irrelevant and the default value of zero has been assigned to North, one to NorthEast and so forth up with NorthWest having a value of 7. I have an additional direction, called Unknown, which is assigned the value of 8.&lt;br /&gt;
 enum class Direction : uint8_t {&lt;br /&gt;
   North,&lt;br /&gt;
   NorthEast,&lt;br /&gt;
   East,&lt;br /&gt;
   SouthEast,&lt;br /&gt;
   South,&lt;br /&gt;
   SouthWest,&lt;br /&gt;
   West,&lt;br /&gt;
   NorthWest,&lt;br /&gt;
   Unknown&lt;br /&gt;
 };&lt;br /&gt;
Values in an enumeration can be compared together using the standard equality and inequality comparisons. However, the introduction of the Unknown element in the Direction enumeration allows me to illustrate how we can override the standard operation.&lt;br /&gt;
&lt;br /&gt;
When comparing two directions together, the comparison should return false if either or both arguments are equal to Direction::Unknown. The following operator override demonstrates this:&lt;br /&gt;
&lt;br /&gt;
 inline bool operator==(const Direction lhs, const Direction rhs) {&lt;br /&gt;
     return ((uint8_t)lhs == (uint8_t)Direction::Unknown&lt;br /&gt;
     ? false&lt;br /&gt;
     : (uint8_t)lhs == (uint8_t)rhs);&lt;br /&gt;
 }&lt;br /&gt;
Putting this operator to work reveals the following results:&lt;br /&gt;
&lt;br /&gt;
 Direction a = Direction::East;&lt;br /&gt;
 Direction b = Direction::East;&lt;br /&gt;
 &lt;br /&gt;
 if (a == b) { Serial.println(“1 a is equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
 a = Direction::NorthWest;&lt;br /&gt;
 b = Direction::North;&lt;br /&gt;
 &lt;br /&gt;
 if (!(a == b)) { Serial.println(“2 a is not equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
 a = Direction::Unknown;&lt;br /&gt;
 b = Direction::Unknown;&lt;br /&gt;
 &lt;br /&gt;
 if (!(a == b)) { Serial.println(“3 a is not equal to b”); }&lt;br /&gt;
Output:&lt;br /&gt;
 1 a is equal to b&lt;br /&gt;
 2 a is not equal to b&lt;br /&gt;
 3 a is not equal to b&lt;br /&gt;
Note that in the last test, the comparison between the two Unknown directions returned false due to the rule created in the equality comparison.&lt;br /&gt;
&lt;br /&gt;
Hang on, what’s with the (!(a == b)) syntax? What about using the standard ‘not equals’ syntax?  As we have overridden the equality operator we need to override the inequality operator as well. The inequality operator shown below simply calls the modified equality operator and returns the NOT of it.&lt;br /&gt;
&lt;br /&gt;
 inline bool operator!=(const Direction lhs, const Direction rhs)  {&lt;br /&gt;
   return ! (lhs == rhs);&lt;br /&gt;
 }&lt;br /&gt;
With this new operator override in place, we can use the standard notation shown below:&lt;br /&gt;
 ...&lt;br /&gt;
 a = Direction::NorthWest;&lt;br /&gt;
 b = Direction::North;&lt;br /&gt;
 &lt;br /&gt;
 if (a != b) { Serial.println(“2 a is not equal to b”); }&lt;br /&gt;
 &lt;br /&gt;
 a = Direction::Unknown;&lt;br /&gt;
 b = Direction::Unknown;&lt;br /&gt;
 &lt;br /&gt;
 if (a != b) { Serial.println(“3 a is not equal to b”); }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 1 a is equal to b&lt;br /&gt;
 2 a is not equal to b&lt;br /&gt;
 3 a is not equal to b&lt;br /&gt;
&lt;br /&gt;
== Getting more complex .. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
So now we can compare directions, what about comparing values?&lt;br /&gt;
&lt;br /&gt;
Assume the boat is at the bottom of the screen entering from the right and leaving towards the left. If the player is stationary and located at the top-centre of the screen then to shoot towards the player the gun will need to be pointed towards the North-West initially and will swing through North and then towards the North-East as the boat leaves the screen. In my code, I was hoping to compare the current direction of the turret to the player’s position and simply add or subtract one to swing it around as required. Although the enumeration details an order to the directions where South is greater than SouthEast (in a clockwise view of the world), it does not consider that North is greater than NorthWest, ie it has no concept of the circular nature of a compass.&lt;br /&gt;
&lt;br /&gt;
The following operators override the standard ‘less than’ and ‘greater than’ operators. Before calculating a return value, the operator determines whether it needs to consider the resetting of values at North by subtracting the integer values from each other. Where the comparison must cater for North, adding 8 (a full circle) to the either the right or left hand side argument (for ‘less than’ or ‘greater than’ respectively) ensures that the comparison caters for this appropriately.&lt;br /&gt;
 						&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator&amp;lt;(const Direction lhs, const Direction rhs)  {&lt;br /&gt;
    return (abs((uint8_t)lhs - (uint8_t)rhs) &amp;lt; 4 ? (uint8_t)lhs - (uint8_t)rhs : (uint8_t)lhs -&lt;br /&gt;
(8 + (uint8_t)rhs)) &amp;lt; 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
inline bool operator&amp;gt;(const Direction  lhs, const Direction  rhs)  {&lt;br /&gt;
    return (abs((uint8_t)lhs - (uint8_t)rhs) &amp;lt; 4 ? (uint8_t)lhs - (uint8_t)rhs : (8 + (uint8_t)&lt;br /&gt;
lhs) - (uint8_t)rhs) &amp;gt; 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Putting these two operators through their paces reveals the correct results shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Direction a = Direction::East;&lt;br /&gt;
Direction b = Direction::South;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“1 East is less than South”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“2 South is greater than East”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::North;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“3 NorthWest is less than North”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“4 North is greater than NorthWest”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::NorthWest;&lt;br /&gt;
b = Direction::NorthEast;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“5 NorthWest is less than NorthEast”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“6 NorthEast is greater than NorthWest”); }&lt;br /&gt;
&lt;br /&gt;
a = Direction::North;&lt;br /&gt;
b = Direction::NorthEast;&lt;br /&gt;
&lt;br /&gt;
if (a &amp;lt; b) { Serial.println(“7 North is less than NorthEast”); }&lt;br /&gt;
if (b &amp;gt; a) { Serial.println(“8 NorthEast is greater than North”); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1 East is less than South&lt;br /&gt;
2 South is greater than East&lt;br /&gt;
3 NorthWest is less than North&lt;br /&gt;
4 North is greater than NorthWest&lt;br /&gt;
5 NorthWest is less than NorthEast&lt;br /&gt;
6 NorthEast is greater than NorthWest&lt;br /&gt;
7 North is less than NorthEast&lt;br /&gt;
8 NorthEast is greater than North&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now that we have the ‘equality’, ‘greater than’ and ‘less than’ operators in place, we can add the following two operators to complete the comparisons.&lt;br /&gt;
&lt;br /&gt;
Note they perform the logical &amp;lt;code&amp;gt;NOT&amp;lt;/code&amp;gt; of those operators previously defined.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline bool operator&amp;lt;=(const Direction lhs, const Direction rhs) { return !(lhs &amp;gt; rhs); }&lt;br /&gt;
inline bool operator&amp;gt;=(const Direction lhs, const Direction rhs) { return !(lhs &amp;lt; rhs); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== And just plain confusing .. ==&lt;br /&gt;
&lt;br /&gt;
Not really confusing but overriding the standard increment and decrement operators reveal an interesting compiler trick that you just have to take for granted actually works. The operator below describes a pre-increment operator.&lt;br /&gt;
&lt;br /&gt;
If the supplied direction is NorthWest&lt;br /&gt;
it returns North otherwise it returns the direction adjacent to the supplied on in the enumeration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction &amp;amp;operator++( Direction &amp;amp;c ) {&lt;br /&gt;
  c = ( c == Direction::NorthWest )&lt;br /&gt;
  ? Direction::North&lt;br /&gt;
  : static_cast&amp;lt;Direction&amp;gt;( static_cast&amp;lt;uint8_t&amp;gt;(c) + 1 );&lt;br /&gt;
  return c;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A post-increment operator includes an anonymous second parameter of type integer. This parameter is not used in the code and is simply there to differentiate between the pre- and post- increment functions.&lt;br /&gt;
&lt;br /&gt;
The compiler must recognise this method signature and assume it is a post-increment function. The implementation of this operator simply calls the pre-incrementer and returns its value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction operator++( Direction &amp;amp;c, int ) {&lt;br /&gt;
  Direction result = c;&lt;br /&gt;
  ++c;&lt;br /&gt;
  return result;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The decrement functions are nearly identical to the increment operators and are shown below for completeness.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inline Direction &amp;amp;operator--( Direction &amp;amp; c ) {&lt;br /&gt;
  c = ( c == Direction::North )&lt;br /&gt;
  ? Direction::NorthWest&lt;br /&gt;
  : static_cast&amp;lt;Direction&amp;gt;( static_cast&amp;lt;uint8_t&amp;gt;(c) - 1 );&lt;br /&gt;
  return c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
inline Direction operator--( Direction &amp;amp; c, int ) {&lt;br /&gt;
  Direction result = c;&lt;br /&gt;
  --c;&lt;br /&gt;
  return result;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
As you can see, the scoped enumerations provide an extra layer of functionality beyond what simple enumerations offer. The ability&lt;br /&gt;
to override the operators encapsulates the logic of the enumeration itself and saves the developer having to repeat similar logic across an application. In the examples shown in&lt;br /&gt;
this article, the developer does not need to remember the intrinsic values underlying the directions North or East nor does he need to consider how to increase the value when it reaches the end of the enumeration.&lt;br /&gt;
&lt;br /&gt;
n fact, all of the code would work if you change the initial enumeration declaration to (say) North = 57 ..&lt;br /&gt;
&lt;br /&gt;
I have included a simple application on GitHub that shows the enumeration in action : https://github.com/filmote/ScopedEnums&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Main_Page&amp;diff=602</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Main_Page&amp;diff=602"/>
		<updated>2024-08-17T00:56:05Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* Tutorials */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Getting started ==&lt;br /&gt;
&lt;br /&gt;
* [[What is the Arduboy|What is the Arduboy?]]&lt;br /&gt;
** [[What is the Arduboy#Arduboy Variants|Arduboy Variants]]&lt;br /&gt;
* [[Installing Games]]&lt;br /&gt;
* [[Developing Games]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Don&#039;t know where to start at all? See the official [https://www.arduboy.com/quick-start Quickstart guide] for a simplified overview. &lt;br /&gt;
&lt;br /&gt;
There is also a [https://community.arduboy.com/t/quick-start-guide/2790 Legacy Quickstart guide] that might be of some use (made for older Arduboy variants but still mostly relevant)&lt;br /&gt;
&lt;br /&gt;
== Community ==&lt;br /&gt;
&lt;br /&gt;
Come chat with the community and show off your games at https://community.arduboy.com/! The bulk of the Arduboy community exists on the forums; there isn&#039;t an official Discord/etc.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Main Arduboy website:&#039;&#039;&#039; https://www.arduboy.com/&lt;br /&gt;
* &#039;&#039;&#039;Community website:&#039;&#039;&#039; https://community.arduboy.com/&lt;br /&gt;
&lt;br /&gt;
== Tools ==&lt;br /&gt;
* [[Ardens]] - Debugger, profiler, and emulator&lt;br /&gt;
* [[Arduboy Toolset]] - Various tools for uploading, converting, etc&lt;br /&gt;
* [[Cart Builder]] - Home for the official flashcart + all games made available for it&lt;br /&gt;
* [[Arduboy-Python-Utilities]] - Command line python tools for uploading, converting, etc&lt;br /&gt;
&lt;br /&gt;
== Tutorials ==&lt;br /&gt;
* Miscellaneous&lt;br /&gt;
** [[Reducing PROGMEM and RAM Usage]]&lt;br /&gt;
** [[Scoped Enumerations]]&lt;br /&gt;
* State Machines&lt;br /&gt;
** [[State%20Machines|Introduction]]&lt;br /&gt;
** [[State Machines - Organising Variables|Organising Variables]]&lt;br /&gt;
* High Scores&lt;br /&gt;
** [[High Score - Getting Started|Getting Started]]&lt;br /&gt;
** [[High Score - High Scores and Slots|High Scores and Slots]]&lt;br /&gt;
** [[High Score - Capturing The Players Initials|Capturing The Players Initials]]&lt;br /&gt;
** [[High Score - Creating a New Entry in the Correct Location|Creating a New Entry in the Correct Location]]&lt;br /&gt;
** [[High Score - Writing it to the EEPROM|Writing it to the EEPROM]]&lt;br /&gt;
* Frogger&lt;br /&gt;
** [[Frogger Getting Started|Getting Started]]&lt;br /&gt;
** [[Frogger - Implementing the Cars|Implementing the Cars]]&lt;br /&gt;
** [[Frogger - Mixing up the Car Movements|Mixing up the Car Movements]]&lt;br /&gt;
** [[Frogger - Adding the Frog|Adding the Player&#039;s Frog]]&lt;br /&gt;
** [[Frogger - Collision Detection and Explosion Graphics|Collision Detection and Explosion Graphics]]&lt;br /&gt;
** [[Frogger - Adding Logs and Turtles|Adding Logs and Turtles]]&lt;br /&gt;
** [[Frogger - Turtle Submerge Logic|Turtle Submerge Logic]]&lt;br /&gt;
** [[Frogger - Adding Home Logic|Adding Home Logic]]&lt;br /&gt;
** [[Frogger - Adding Score Logic|Adding Score Logic]]&lt;br /&gt;
** [[Frogger - Adding a Title and Game Over Screen|Adding a Title and Game Over Screen]]&lt;br /&gt;
* [[ArduboyG Tutorial]]&lt;br /&gt;
** [[ArduboyG Tutorial#History of Greyscale on the Arduboy|History of the Greyscale on the Arduboy]]&lt;br /&gt;
** [[ArduboyG Tutorial#Getting Started|Getting Started]]&lt;br /&gt;
** [[ArduboyG Tutorial#Creating Graphics|Creating Graphics]]&lt;br /&gt;
** [[ArduboyG Tutorial#Displaying the Graphics|Displaying the Graphics]]&lt;br /&gt;
** [[ArduboyG Tutorial#Hints and Tricks|Hints and Tricks]]&lt;br /&gt;
&lt;br /&gt;
== Documentation ==&lt;br /&gt;
* [[Flashcart Format]]&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Developing_Games&amp;diff=601</id>
		<title>Developing Games</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Developing_Games&amp;diff=601"/>
		<updated>2024-08-16T11:50:51Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* Utility Libraries */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Developing games for the Arduboy is an exciting endeavor that blends creativity with technical skills. The Arduboy is a compact, credit card-sized gaming platform based on the Arduino microcontroller, making it accessible for both beginners and experienced developers. Its simplicity and portability offer a unique challenge due to its limited resources, such as a monochrome 128x64 pixel display and minimal memory. However, these constraints inspire innovation and efficient coding practices.&lt;br /&gt;
&lt;br /&gt;
Standard libraries for sound and graphics play a significant role in the development process. The Arduboy2 library provides essential functions for managing the display, handling input, and maintaining game states. It facilitates tasks such as drawing shapes, rendering text, and controlling frame rates, allowing developers to concentrate on game mechanics. For audio, the ArduboyTones library enables the addition of sound effects and music, enhancing the game&#039;s atmosphere. Using these libraries, developers can create functional and engaging games that work well within the Arduboy&#039;s hardware limitations.&lt;br /&gt;
&lt;br /&gt;
== Prerequisites for Developing Games ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Arduino Development IDEs ==&lt;br /&gt;
Arduino development environments provide a range of tools for creating, testing, and deploying projects to the Arduboy. The primary environment is the Arduino Integrated Development Environment (IDE), known for its user-friendly interface that simplifies writing, compiling, and uploading code. It supports various Arduino models and includes a library manager for easy access to numerous libraries. Additionally, Visual Studio Code (VSCode) can be used for Arduino development, offering advanced features like integrated debugging and a customizable interface. These environments cater to both beginners and advanced users, offering tools that range from straightforward to professional-grade.&lt;br /&gt;
&lt;br /&gt;
=== Visual Studio Code ===&lt;br /&gt;
&lt;br /&gt;
The [https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.vscode-arduino Arduino plugin] for Visual Studio Code is an easy way to develop and build Arduboy games with a full-featured IDE.&lt;br /&gt;
&lt;br /&gt;
1. Download and install [https://code.visualstudio.com/ Visual Studio Code].&lt;br /&gt;
&lt;br /&gt;
2. Install the &amp;quot;Arduino&amp;quot; extension developed by Microsoft.&lt;br /&gt;
&lt;br /&gt;
3. Configure the extension to use the bundled version of Arduino CLI.&lt;br /&gt;
&lt;br /&gt;
4. Add the following URL to the &amp;quot;Additional Urls&amp;quot; setting for the Arduino extension: &amp;lt;pre&amp;gt;https://raw.githubusercontent.com/MrBlinky/Arduboy-homemade-package/master/package_arduboy_homemade_index.json&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
5. Open the Arduino Board Manager and click &amp;quot;Refresh Package Indexes&amp;quot;. Install the Arduboy homemade package by Mr.Blinky.&lt;br /&gt;
&lt;br /&gt;
6. Add a file &amp;quot;.vscode/arduino.json&amp;quot; to your project to define how to build your project. For example, to build a game for the Arduboy FX, you might have:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;configuration&amp;quot;: &amp;quot;core=arduboy-core,boot=cathy3k&amp;quot;,&lt;br /&gt;
    &amp;quot;board&amp;quot;: &amp;quot;arduboy-homemade:avr:arduboy-fx&amp;quot;,&lt;br /&gt;
    &amp;quot;sketch&amp;quot;: &amp;quot;my_game.ino&amp;quot;,&lt;br /&gt;
    &amp;quot;output&amp;quot;: &amp;quot;build/&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. You can now build your project with the &amp;quot;Arduino: Verify&amp;quot; command. The command will create a &amp;quot;build&amp;quot; directory inside your project directory, and generate .hex and .elf files there.&lt;br /&gt;
&lt;br /&gt;
== Common Libraries ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An Arduboy library is a collection of pre-written code designed to simplify and standardize the development of games and applications for the Arduboy platform. These libraries provide a range of functionalities, such as handling graphics, sound, input, and other core features of the device. For example, the Arduboy2 library includes functions for drawing shapes, rendering text, managing game states, and handling user input, while the ArduboyTones library offers tools for generating sound effects and music. By utilizing these libraries, developers can streamline their coding process, focus on the creative aspects of game design, and ensure their applications run efficiently on the limited hardware of the Arduboy.&lt;br /&gt;
&lt;br /&gt;
=== Base Libraries ===&lt;br /&gt;
&lt;br /&gt;
==== [[Arduboy2]] ====&lt;br /&gt;
The basic Arduboy library, often referred to as Arduboy2, is an essential toolkit for developing games on the Arduboy platform. This library provides a comprehensive set of functions that manage the device&#039;s core features, making it easier for developers to create games and applications. It includes capabilities for drawing graphics on the 128x64 pixel monochrome display, such as drawing lines, circles, and rectangles, as well as rendering text. The library also handles input from the device&#039;s buttons, allowing developers to implement game controls efficiently. Additionally, Arduboy2 offers functions for managing frame rates and game states, ensuring smooth game play experiences. By using the Arduboy2 library, developers can focus more on the creative aspects of game development while leveraging standardized, efficient code for fundamental tasks.&lt;br /&gt;
&lt;br /&gt;
Additionally, the library provides basic [[Collision Detection]] functions.&lt;br /&gt;
&lt;br /&gt;
=== Graphic Libraries ===&lt;br /&gt;
&lt;br /&gt;
==== [[Sprites / SpritesB Library|Sprites]] / SpritesB ====&lt;br /&gt;
Shipped as part of the [[Arduboy2]] library, the Sprites library provides functions for rendering both masked and unmasked bitmaps and tilesheets. When working with masked bitmaps, the library supports various formats including standalone masks (allowing them to be used with multiple bitmaps) or interlaced within the bitmap image themselves (for best performance). The library can only render images from PROGMEM and provides no FX chip support.&lt;br /&gt;
&lt;br /&gt;
The SpritesB library is functionally identical to the standard Sprites library but the code has been optimised for size at the expense of speed.&lt;br /&gt;
&lt;br /&gt;
==== [[ArduboyG Tutorial|ArduboyG]] ====&lt;br /&gt;
The ArduboyG library extends the basic Arduboy2 library with advanced functions for handling sprites, animations, and complex graphical operations, including support for 3 and 4 color greyscale. The library allows images to be rendered from PROGMEM or directly from the FX chip. Images can include transparent sections and must be a multiple of 8 bits high.  These are converted using a python script provided with the library.&lt;br /&gt;
&lt;br /&gt;
==== [[ArdBitmap]] ====&lt;br /&gt;
The ArdBitmap library works with compressed and uncompressed bitmaps and provides horizontal and vertical mirroring and real-time image resizing (downscaling).  It can only render images from PROGMEM and provides no FX chip support.&lt;br /&gt;
&lt;br /&gt;
The original forum post can be found [https://community.arduboy.com/t/ardbitmap-bitmap-library-tools/2631/6 here] and the library and documentation can be found [https://github.com/igvina/ArdBitmap here]&lt;br /&gt;
&lt;br /&gt;
=== Sound Libraries ===&lt;br /&gt;
&lt;br /&gt;
=== Utility Libraries ===&lt;br /&gt;
&lt;br /&gt;
==== [[Fixed Point]] ====&lt;br /&gt;
The Arduboy is based on a simple processor which does not handle floating point calculations natively.  As a result, any attempt to use floating point numbers and arithmetic results in a excess code and RAM use as well as poor performance.  Fixed Point numbers use a different approach and removes many of the issues found in floating point numbers.  This library, developed by Pharap, implements fixed point numbers that have varying scale and precision.&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=600</id>
		<title>Fixed Point</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=600"/>
		<updated>2024-08-16T10:17:06Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* Fixed Points in Action */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
Most of us are familiar with the integer data types used by the Arduboy environment – of various lengths, both signed and unsigned. We take care to choose appropriate data types that consider both the range of our data as well as the memory required to store them. Operations involving integer values are incredibly efficient and for many sketches are all that are required.&lt;br /&gt;
&lt;br /&gt;
Sometimes however, integer values are simply not flexible enough. Imagine trying to calculate the movement of an accelerating rocket or calculate the trajectory of a rocket or a missile in an Arduboy game. To make these calculations, you need numbers with both an integer and fractional component. The typical way to represent such numbers on a computer is through floating points.&lt;br /&gt;
&lt;br /&gt;
C++ includes two floating point data types of different precision – the float and the double. On most platforms the float is 4 bytes and the double is 8 bytes, however on the Arduino (and thus the Arduboy) they are actually same precision (4 bytes) and are therefore interchangeable. These data types can store numbers in the range of -3.4028235 x 1038 to +3. 4028235 x 1038 (for comparison there is estimated to be 7.5 x 1018 grains of sand in the world) but at a precision of only 6 or 7 decimal digits.&lt;br /&gt;
&lt;br /&gt;
For the Arduboy, in contrast to integer operations, floating point calculations are internally quite complicated and thus incredibly processor intensive. Including numerous calculations to reposition the player and enemies within the main loop of a program might prevent the game from performing well or worse yet even being playable.&lt;br /&gt;
&lt;br /&gt;
However, there is an alternative to &#039;&#039;&#039;floating point&#039;&#039;&#039; arithmetic. This alternative is &#039;&#039;&#039;fixed point&#039;&#039;&#039; arithmetic. Fixed point numbers behave more similarly to the integer values you are familiar with. As such, the processing power required to manipulate fixed point numbers is more similar to integer calculations than to floating point calculations. &lt;br /&gt;
&lt;br /&gt;
There is however a tradeoff for the benefit that fixed point data types bring. The tradeoff is that you must explicitly nominate the size  of both the integer and fractional portion of the variable – much in the same way you nominate the available range when choosing integer data types. In addition to the obvious data range restrictions of the integer component, the resolution (or ‘granularity’)  of the fractional component must also be chosen.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 8 bit fixed point type with a 4 bit integer part and a 4 bit fractional part would permit an integer range of 0 to 15 and a fractional range of 0/16 to 15/16. This equates to a range of 0 to 15.9375 (15/16 is equivalent to 0.9375).&lt;br /&gt;
&lt;br /&gt;
Note that the fractional part can only go up in increments of 1/16 (i.e. 0.0625), hence that particular format cannot accurately represent numbers at a lower resolution, for example the value 0.05 could not be accurately represented, it would have to be rounded to 0.0625. However, by using larger types or by increasing the number of bits afforded to the fractional part at the expense of the integer part you can balance the format to suit your needs.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 16 bit type with 8 bits of integer part and 8 bits of fractional part could represent all the positions on the Arduboy’s screen, whilst providing a reasonable range of fractional steps in between the precise pixel locations. This would mean having much smoother movement than using regular integers, but in a way that’s cheaper than floating points in terms of both code use and processing power.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Fixed Points in Action&#039;&#039;&#039; ==&lt;br /&gt;
In the examples below, I am using @Pharap’s [https://community.arduboy.com/t/a-fixed-point-arithmetic-library/4129 FixedPoints] library which defines a number of signed and unsigned data types which are listed at the end of this article. It also includes overrides for the common arithmetic and comparison operators (+, -, / , *, &amp;lt;, &amp;gt;, etc) as well as some handy functions like &amp;lt;code&amp;gt;ceilFixed()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;floorFixed()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;roundFixed()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Firstly, some simple assignments and calculations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar = 7.0;&lt;br /&gt;
myVar = myVar + 3;&lt;br /&gt;
&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 4);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
static_cast&amp;lt;float&amp;gt;(myVar) : 10.0000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Please note that an extra ‘level of precision’ argument is being used in the print code here. Without supplying this extra argument, the printed results suffer from rounding errors made by the &amp;lt;code&amp;gt;Serial.print()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Serial.println()&amp;lt;/code&amp;gt; functions to simplify the code for printing floats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Simple. Now let’s introduce a decimal place.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar = 3.7;&lt;br /&gt;
Serial.print(“myVar.getInteger() : “);&lt;br /&gt;
Serial.println(myVar.getInteger());&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 8);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myVar.getInteger() : 3&lt;br /&gt;
static_cast&amp;lt;float&amp;gt;(myVar) :  3.69921875&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But hang on, we set the variable to 3.7 and it printed out 3.69921875. Why so? As mentioned earlier, both &#039;&#039;&#039;floating point&#039;&#039;&#039; and &#039;&#039;&#039;fixed point&#039;&#039;&#039; numbers introduce a degree of inaccuracy when storing numbers. With Fixed Point numbers, the precision (and by extension the accuracy) is specified by the programmer.&lt;br /&gt;
&lt;br /&gt;
In the sample code above, the variable chosen was a two-byte incarnation and it  can store a number roughly between -128 and +128. The integer component is stored in 7 bits which allows a range of 0 to 127 and the fractional part is stored in 8 bits and has a range between 0 / 256 to 255 / 256. The largest fractional portion (255 / 256) converted to decimal is 0.9961 hence the data range cannot quite store +128, but it can store up to 127.9961.&lt;br /&gt;
&lt;br /&gt;
If you could view the bits in the fractional part of the variable (which you can through &amp;lt;code&amp;gt;myVar.getFraction()&amp;lt;/code&amp;gt;), you would see that it is stored as 179. As it turns out 179 / 256 is equal to 0.69921875.&lt;br /&gt;
&lt;br /&gt;
The inaccuracy introduced by a relatively small (two-byte) &#039;&#039;&#039;fixed point&#039;&#039;&#039; is irrelevant if all you are trying to do is simulate some realistic movements of characters in a game. If you have a need to calculate more accurate values, you can simply increase the size of the &#039;&#039;&#039;fixed point&#039;&#039;&#039; variable to either an &amp;lt;code&amp;gt;SQ15x16&amp;lt;/code&amp;gt; or an &amp;lt;code&amp;gt;SQ31x32&amp;lt;/code&amp;gt; (though be aware that some operations do not work on &amp;lt;code&amp;gt;SQ31x32&amp;lt;/code&amp;gt; due to presently unavoidable restrictions). The code below illustrates the accuracy of each data type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar7x8 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar7x8), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar7x8 = myVar7x8 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
Serial.println();&lt;br /&gt;
 &lt;br /&gt;
SQ15x16 myVar15x16 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar15x16), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar15x16 = myVar15x16 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
Serial.println();&lt;br /&gt;
 &lt;br /&gt;
SQ31x32 myVar31x32 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar31x32), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar31x32 = myVar31x32 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
Serial.println();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 4.00000000 4.09765625 4.19531250 4.29296875 4.39062500 4.48828125 4.58593750 4.68359375 4.78125000 4.87890625 4.97656250&lt;br /&gt;
 4.00000000 4.09999084 4.19998168 4.29997253 4.39996337 4.49995422 4.59994506 4.69993591 4.79992675 4.89991760 4.99990844 &lt;br /&gt;
 4.00000000 4.09999990 4.19999980 4.30000019 4.40000009 4.50000000 4.59999990 4.69999980 4.80000019 4.90000009 5.00000000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One thing to note with the output is the compounding effect that adding two inaccurate numbers results in. This can be seen easily if we repeat the same exercise on the smallest, one-byte data type as shown below. The fractional part of an &amp;lt;code&amp;gt;SQ3x4&amp;lt;/code&amp;gt; number is stored in 1/16th increments or 0.0625 in decimal.&lt;br /&gt;
&lt;br /&gt;
When we add the first increment of 0.1  we immediately introduce an inaccuracy as the number gets rounded down to the nearest 1/16th of a number. As we repeat the addition, the error increases. The following code shows that.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ3x4 myVar3x4 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar3x4), 4);&lt;br /&gt;
  Serial.print(“ “);&lt;br /&gt;
  myVar3x4 = myVar3x4 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 4.0000 4.0625 4.1250 4.1875 4.2500 4.3125 4.3750 4.4375 4.5000 4.5625 4.6250&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choosing larger &#039;&#039;&#039;fixed point&#039;&#039;&#039; data types increases the range of the number and accuracy of the fractional component at the expense of memory.&lt;br /&gt;
&lt;br /&gt;
== Why would I bother with Fixed Points at all? ==&lt;br /&gt;
&lt;br /&gt;
Imagine you are building a sideways-scrolling game where aliens (or cars, planes or other moving objects) are moving towards you at various speeds. With each pass through the program’s loop you could decrement the x coordinate of each alien but this would result in all aliens moving at the same speed. You could try to move aliens every second or third time through the loop by keeping some sort of counter but this can be error prone and&lt;br /&gt;
&lt;br /&gt;
may result in the aliens moving slowly. To really allow flexibility in the aliens’ movement, you could store the x and y in fixed point variables and increment these by a decimal value allowing the aliens to move at similar but slightly different speeds.&lt;br /&gt;
&lt;br /&gt;
Consider the Alien class below. Each alien is described with an x and y coordinate and takes care of its own movement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Alien {&lt;br /&gt;
 &lt;br /&gt;
   public:&lt;br /&gt;
 &lt;br /&gt;
     Alien(SQ7x8 x, SQ7x8 y, SQ7x8 incX, SQ7x8 incY, const uint8_t *bitmap) {&lt;br /&gt;
&lt;br /&gt;
       _x = x;&lt;br /&gt;
       _y = y;&lt;br /&gt;
       _incX = incX;&lt;br /&gt;
       _incY = incY;&lt;br /&gt;
       _bitmap = bitmap; &lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 getX() const { return _x; }&lt;br /&gt;
     SQ7x8 getY() const { return _y; }&lt;br /&gt;
 &lt;br /&gt;
     void setX(const SQ7x8 value) { _x = value; }&lt;br /&gt;
     void setY(const SQ7x8 value) { _y = value; }&lt;br /&gt;
 &lt;br /&gt;
     inline void Alien::move(uint8_t frame) {&lt;br /&gt;
 &lt;br /&gt;
       _x = _x - _incX;&lt;br /&gt;
       _y = _y - _incY;&lt;br /&gt;
       Sprites::drawOverwrite(_x.getInteger(), _y.getInteger(), _bitmap, frame);&lt;br /&gt;
   } &lt;br /&gt;
 &lt;br /&gt;
   private:&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 _x;&lt;br /&gt;
     SQ7x8 _y;&lt;br /&gt;
     SQ7x8 _incX;&lt;br /&gt;
     SQ7x8 _incY;&lt;br /&gt;
     const uint8_t *_bitmap;&lt;br /&gt;
   &lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And our main .ino file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Alien alien1 = {127, 16, (SQ7x8)0.9, (SQ7x8)1.34, alienBitmap };&lt;br /&gt;
Alien alien2 = {127, 32, (SQ7x8)1.7, (SQ7x8)1.76, alienBitmap };&lt;br /&gt;
Alien alien3 = {127, 48, (SQ7x8)1.5, (SQ7x8)2.13, alienBitmap }; &lt;br /&gt;
 &lt;br /&gt;
void loop() {&lt;br /&gt;
  alien1.move(frame);&lt;br /&gt;
  alien2.move(frame);&lt;br /&gt;
  alien3.move(frame);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With fixed point data types, we can have our aliens move at slightly different speeds without the overhead of using floating points.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Common Type Aliases&#039;&#039;&#039; ==&lt;br /&gt;
@Pharap’s library includes the following data types. However the library is implemented with templates and it is a relatively simple matter to declare your own type aliases.&lt;br /&gt;
&lt;br /&gt;
The predeclared aliases (which can be  used as an example of how to declare such an alias) can be found in &#039;&#039;&#039;src\ FixedPointsCommon\SFixedCommon.h and src\ FixedPointsCommon\UfixedCommon.h.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Unsigned Data Type Aliases:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|-&lt;br /&gt;
|UQ4x4&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 15&lt;br /&gt;
|1 / 16th&lt;br /&gt;
|0 to 16&lt;br /&gt;
|-&lt;br /&gt;
|UQ8x8&lt;br /&gt;
|16 bit (2 bytes)&lt;br /&gt;
|0 to 255&lt;br /&gt;
|1 / 256th&lt;br /&gt;
|0 to 256&lt;br /&gt;
|-&lt;br /&gt;
|UQ16x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|0 to 65,355&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|0 to 65,356&lt;br /&gt;
|-&lt;br /&gt;
|UQ32x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|0 to 4,294,967,295&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|0 to 4,294,967,296&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x7&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 128th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x15&lt;br /&gt;
|16 bit (2 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 32,768th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x31&lt;br /&gt;
|32 bit (4 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 2,147,483,648th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x63&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1/ 9,223,372,036,854,775,808th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Signed Data Type Aliases:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|-&lt;br /&gt;
|SQ3x4&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;15 to +15&lt;br /&gt;
|1 / 16th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;16 to +16&lt;br /&gt;
|-&lt;br /&gt;
|SQ7x8&lt;br /&gt;
|16 bit (2 bytes)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;127 to +127&lt;br /&gt;
|1 / 256th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;128 to +128&lt;br /&gt;
|-&lt;br /&gt;
|SQ15x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;32,767 to +32,767&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;32,768 to +32,768&lt;br /&gt;
|-&lt;br /&gt;
|SQ31x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2,147,483,647 to +2,147,483,647&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2,147,483,648 to +2,147,483,648&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x6&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;1 to +1&lt;br /&gt;
|1 / 64th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x14&lt;br /&gt;
|16 bit (2 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;1 to +1&lt;br /&gt;
|1 / 16,384th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x30&lt;br /&gt;
|32 bit (4 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;1 to +1&lt;br /&gt;
|1 / 1,073,741,824th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x62&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;1 to +1&lt;br /&gt;
|1/ 4,611,686,018,427,387,904th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2 to +2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt; Lower bound is inclusive, upper bound is exclusive&lt;br /&gt;
&lt;br /&gt;
FixedPoints can be found through the Arduino IDE library manager. If you have not used the library manager before see here: &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;https://arduboy.com/download-and-learn-arduino/&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=599</id>
		<title>Fixed Point</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=599"/>
		<updated>2024-08-16T10:16:14Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
Most of us are familiar with the integer data types used by the Arduboy environment – of various lengths, both signed and unsigned. We take care to choose appropriate data types that consider both the range of our data as well as the memory required to store them. Operations involving integer values are incredibly efficient and for many sketches are all that are required.&lt;br /&gt;
&lt;br /&gt;
Sometimes however, integer values are simply not flexible enough. Imagine trying to calculate the movement of an accelerating rocket or calculate the trajectory of a rocket or a missile in an Arduboy game. To make these calculations, you need numbers with both an integer and fractional component. The typical way to represent such numbers on a computer is through floating points.&lt;br /&gt;
&lt;br /&gt;
C++ includes two floating point data types of different precision – the float and the double. On most platforms the float is 4 bytes and the double is 8 bytes, however on the Arduino (and thus the Arduboy) they are actually same precision (4 bytes) and are therefore interchangeable. These data types can store numbers in the range of -3.4028235 x 1038 to +3. 4028235 x 1038 (for comparison there is estimated to be 7.5 x 1018 grains of sand in the world) but at a precision of only 6 or 7 decimal digits.&lt;br /&gt;
&lt;br /&gt;
For the Arduboy, in contrast to integer operations, floating point calculations are internally quite complicated and thus incredibly processor intensive. Including numerous calculations to reposition the player and enemies within the main loop of a program might prevent the game from performing well or worse yet even being playable.&lt;br /&gt;
&lt;br /&gt;
However, there is an alternative to &#039;&#039;&#039;floating point&#039;&#039;&#039; arithmetic. This alternative is &#039;&#039;&#039;fixed point&#039;&#039;&#039; arithmetic. Fixed point numbers behave more similarly to the integer values you are familiar with. As such, the processing power required to manipulate fixed point numbers is more similar to integer calculations than to floating point calculations. &lt;br /&gt;
&lt;br /&gt;
There is however a tradeoff for the benefit that fixed point data types bring. The tradeoff is that you must explicitly nominate the size  of both the integer and fractional portion of the variable – much in the same way you nominate the available range when choosing integer data types. In addition to the obvious data range restrictions of the integer component, the resolution (or ‘granularity’)  of the fractional component must also be chosen.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 8 bit fixed point type with a 4 bit integer part and a 4 bit fractional part would permit an integer range of 0 to 15 and a fractional range of 0/16 to 15/16. This equates to a range of 0 to 15.9375 (15/16 is equivalent to 0.9375).&lt;br /&gt;
&lt;br /&gt;
Note that the fractional part can only go up in increments of 1/16 (i.e. 0.0625), hence that particular format cannot accurately represent numbers at a lower resolution, for example the value 0.05 could not be accurately represented, it would have to be rounded to 0.0625. However, by using larger types or by increasing the number of bits afforded to the fractional part at the expense of the integer part you can balance the format to suit your needs.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 16 bit type with 8 bits of integer part and 8 bits of fractional part could represent all the positions on the Arduboy’s screen, whilst providing a reasonable range of fractional steps in between the precise pixel locations. This would mean having much smoother movement than using regular integers, but in a way that’s cheaper than floating points in terms of both code use and processing power.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Fixed Points in Action&#039;&#039;&#039; ==&lt;br /&gt;
In the examples below, I am using @Pharap’s [https://community.arduboy.com/t/a-fixed-point-arithmetic-library/4129 FixedPoints] library which defines a number of signed and unsigned data types which are listed at the end of this article. It also includes overrides for the common arithmetic and comparison operators (+, -, / , *, &amp;lt;, &amp;gt;, etc) as well as some handy functions like &amp;lt;code&amp;gt;ceilFixed()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;floorFixed()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;roundFixed()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Firstly, some simple assignments and calculations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar = 7.0;&lt;br /&gt;
myVar = myVar + 3;&lt;br /&gt;
&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 4);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
static_cast&amp;lt;float&amp;gt;(myVar) : 10.0000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Please note that an extra ‘level of precision’ argument is being used in the print code here. Without supplying this extra argument, the printed results suffer from rounding errors made by the &amp;lt;code&amp;gt;Serial.print()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Serial.println()&amp;lt;/code&amp;gt; functions to simplify the code for printing floats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Simple. Now let’s introduce a decimal place.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar = 3.7;&lt;br /&gt;
Serial.print(“myVar.getInteger() : “);&lt;br /&gt;
Serial.println(myVar.getInteger());&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 8);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myVar.getInteger() : 3&lt;br /&gt;
static_cast&amp;lt;float&amp;gt;(myVar) :  3.69921875&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But hang on, we set the variable to 3.7 and it printed out 3.69921875. Why so? As mentioned earlier, both &#039;&#039;&#039;floating point&#039;&#039;&#039; and &#039;&#039;&#039;fixed point&#039;&#039;&#039; numbers introduce a degree of inaccuracy when storing numbers. With Fixed Point numbers, the precision (and by extension the accuracy) is specified by the programmer.&lt;br /&gt;
&lt;br /&gt;
In the sample code above, the variable chosen was a two-byte incarnation and it  can store a number roughly between -128 and +128. The integer component is stored in 7 bits which allows a range of 0 to 127 and the fractional part is stored in 8 bits and has a range between 0 / 256 to 255 / 256. The largest fractional portion (255 / 256) converted to decimal is 0.9961 hence the data range cannot quite store +128, but it can store up to 127.9961.&lt;br /&gt;
&lt;br /&gt;
If you could view the bits in the fractional part of the variable (which you can through &amp;lt;code&amp;gt;myVar.getFraction()&amp;lt;/code&amp;gt;), you would see that it is stored as 179. As it turns out 179 / 256 is equal to 0.69921875.&lt;br /&gt;
&lt;br /&gt;
The inaccuracy introduced by a relatively small (two-byte) &#039;&#039;&#039;fixed point&#039;&#039;&#039; is irrelevant if all you are trying to do is simulate some realistic movements of characters in a game. If you have a need to calculate more accurate values, you can simply increase the size of the &#039;&#039;&#039;fixed point&#039;&#039;&#039; variable to either an SQ15x16 or an SQ31x32 (though be aware that some operations do not work on SQ31x32 due to presently unavoidable restrictions). The code below illustrates the accuracy of each data type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar7x8 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar7x8), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar7x8 = myVar7x8 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
Serial.println();&lt;br /&gt;
 &lt;br /&gt;
SQ15x16 myVar15x16 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar15x16), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar15x16 = myVar15x16 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
Serial.println();&lt;br /&gt;
 &lt;br /&gt;
SQ31x32 myVar31x32 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar31x32), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar31x32 = myVar31x32 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
Serial.println();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 4.00000000 4.09765625 4.19531250 4.29296875 4.39062500 4.48828125 4.58593750 4.68359375 4.78125000 4.87890625 4.97656250&lt;br /&gt;
 4.00000000 4.09999084 4.19998168 4.29997253 4.39996337 4.49995422 4.59994506 4.69993591 4.79992675 4.89991760 4.99990844 &lt;br /&gt;
 4.00000000 4.09999990 4.19999980 4.30000019 4.40000009 4.50000000 4.59999990 4.69999980 4.80000019 4.90000009 5.00000000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One thing to note with the output is the compounding effect that adding two inaccurate numbers results in. This can be seen easily if we repeat the same exercise on the smallest, one-byte data type as shown below. The fractional part of an &amp;lt;code&amp;gt;SQ3x4&amp;lt;/code&amp;gt; number is stored in 1/16th increments or 0.0625 in decimal.&lt;br /&gt;
&lt;br /&gt;
When we add the first increment of 0.1  we immediately introduce an inaccuracy as the number gets rounded down to the nearest 1/16th of a number. As we repeat the addition, the error increases. The following code shows that.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ3x4 myVar3x4 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar3x4), 4);&lt;br /&gt;
  Serial.print(“ “);&lt;br /&gt;
  myVar3x4 = myVar3x4 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 4.0000 4.0625 4.1250 4.1875 4.2500 4.3125 4.3750 4.4375 4.5000 4.5625 4.6250&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choosing larger &#039;&#039;&#039;fixed point&#039;&#039;&#039; data types increases the range of the number and accuracy of the fractional component at the expense of memory.&lt;br /&gt;
&lt;br /&gt;
== Why would I bother with Fixed Points at all? ==&lt;br /&gt;
&lt;br /&gt;
Imagine you are building a sideways-scrolling game where aliens (or cars, planes or other moving objects) are moving towards you at various speeds. With each pass through the program’s loop you could decrement the x coordinate of each alien but this would result in all aliens moving at the same speed. You could try to move aliens every second or third time through the loop by keeping some sort of counter but this can be error prone and&lt;br /&gt;
&lt;br /&gt;
may result in the aliens moving slowly. To really allow flexibility in the aliens’ movement, you could store the x and y in fixed point variables and increment these by a decimal value allowing the aliens to move at similar but slightly different speeds.&lt;br /&gt;
&lt;br /&gt;
Consider the Alien class below. Each alien is described with an x and y coordinate and takes care of its own movement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Alien {&lt;br /&gt;
 &lt;br /&gt;
   public:&lt;br /&gt;
 &lt;br /&gt;
     Alien(SQ7x8 x, SQ7x8 y, SQ7x8 incX, SQ7x8 incY, const uint8_t *bitmap) {&lt;br /&gt;
&lt;br /&gt;
       _x = x;&lt;br /&gt;
       _y = y;&lt;br /&gt;
       _incX = incX;&lt;br /&gt;
       _incY = incY;&lt;br /&gt;
       _bitmap = bitmap; &lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 getX() const { return _x; }&lt;br /&gt;
     SQ7x8 getY() const { return _y; }&lt;br /&gt;
 &lt;br /&gt;
     void setX(const SQ7x8 value) { _x = value; }&lt;br /&gt;
     void setY(const SQ7x8 value) { _y = value; }&lt;br /&gt;
 &lt;br /&gt;
     inline void Alien::move(uint8_t frame) {&lt;br /&gt;
 &lt;br /&gt;
       _x = _x - _incX;&lt;br /&gt;
       _y = _y - _incY;&lt;br /&gt;
       Sprites::drawOverwrite(_x.getInteger(), _y.getInteger(), _bitmap, frame);&lt;br /&gt;
   } &lt;br /&gt;
 &lt;br /&gt;
   private:&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 _x;&lt;br /&gt;
     SQ7x8 _y;&lt;br /&gt;
     SQ7x8 _incX;&lt;br /&gt;
     SQ7x8 _incY;&lt;br /&gt;
     const uint8_t *_bitmap;&lt;br /&gt;
   &lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And our main .ino file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Alien alien1 = {127, 16, (SQ7x8)0.9, (SQ7x8)1.34, alienBitmap };&lt;br /&gt;
Alien alien2 = {127, 32, (SQ7x8)1.7, (SQ7x8)1.76, alienBitmap };&lt;br /&gt;
Alien alien3 = {127, 48, (SQ7x8)1.5, (SQ7x8)2.13, alienBitmap }; &lt;br /&gt;
 &lt;br /&gt;
void loop() {&lt;br /&gt;
  alien1.move(frame);&lt;br /&gt;
  alien2.move(frame);&lt;br /&gt;
  alien3.move(frame);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With fixed point data types, we can have our aliens move at slightly different speeds without the overhead of using floating points.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Common Type Aliases&#039;&#039;&#039; ==&lt;br /&gt;
@Pharap’s library includes the following data types. However the library is implemented with templates and it is a relatively simple matter to declare your own type aliases.&lt;br /&gt;
&lt;br /&gt;
The predeclared aliases (which can be  used as an example of how to declare such an alias) can be found in &#039;&#039;&#039;src\ FixedPointsCommon\SFixedCommon.h and src\ FixedPointsCommon\UfixedCommon.h.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Unsigned Data Type Aliases:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|-&lt;br /&gt;
|UQ4x4&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 15&lt;br /&gt;
|1 / 16th&lt;br /&gt;
|0 to 16&lt;br /&gt;
|-&lt;br /&gt;
|UQ8x8&lt;br /&gt;
|16 bit (2 bytes)&lt;br /&gt;
|0 to 255&lt;br /&gt;
|1 / 256th&lt;br /&gt;
|0 to 256&lt;br /&gt;
|-&lt;br /&gt;
|UQ16x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|0 to 65,355&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|0 to 65,356&lt;br /&gt;
|-&lt;br /&gt;
|UQ32x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|0 to 4,294,967,295&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|0 to 4,294,967,296&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x7&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 128th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x15&lt;br /&gt;
|16 bit (2 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 32,768th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x31&lt;br /&gt;
|32 bit (4 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 2,147,483,648th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x63&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1/ 9,223,372,036,854,775,808th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Signed Data Type Aliases:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|-&lt;br /&gt;
|SQ3x4&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;15 to +15&lt;br /&gt;
|1 / 16th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;16 to +16&lt;br /&gt;
|-&lt;br /&gt;
|SQ7x8&lt;br /&gt;
|16 bit (2 bytes)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;127 to +127&lt;br /&gt;
|1 / 256th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;128 to +128&lt;br /&gt;
|-&lt;br /&gt;
|SQ15x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;32,767 to +32,767&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;32,768 to +32,768&lt;br /&gt;
|-&lt;br /&gt;
|SQ31x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2,147,483,647 to +2,147,483,647&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2,147,483,648 to +2,147,483,648&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x6&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;1 to +1&lt;br /&gt;
|1 / 64th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x14&lt;br /&gt;
|16 bit (2 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;1 to +1&lt;br /&gt;
|1 / 16,384th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x30&lt;br /&gt;
|32 bit (4 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;1 to +1&lt;br /&gt;
|1 / 1,073,741,824th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x62&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;1 to +1&lt;br /&gt;
|1/ 4,611,686,018,427,387,904th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2 to +2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt; Lower bound is inclusive, upper bound is exclusive&lt;br /&gt;
&lt;br /&gt;
FixedPoints can be found through the Arduino IDE library manager. If you have not used the library manager before see here: &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;https://arduboy.com/download-and-learn-arduino/&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=598</id>
		<title>Fixed Point</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=598"/>
		<updated>2024-08-16T10:14:34Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* Common Type Aliases */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
Most of us are familiar with the integer data types used by the Arduboy environment – of various lengths, both signed and unsigned. We take care to choose appropriate data types that consider both the range of our data as well as the memory required to store them. Operations involving integer values are incredibly efficient and for many sketches are all that are required.&lt;br /&gt;
&lt;br /&gt;
Sometimes however, integer values are simply not flexible enough. Imagine trying to calculate the movement of an accelerating rocket or calculate the trajectory of a rocket or a missile in an Arduboy game. To make these calculations, you need numbers with both an integer and fractional component. The typical way to represent such numbers on a computer is through floating points.&lt;br /&gt;
&lt;br /&gt;
C++ includes two floating point data types of different precision – the float and the double. On most platforms the float is 4 bytes and the double is 8 bytes, however on the Arduino (and thus the Arduboy) they are actually same precision (4 bytes) and are therefore interchangeable. These data types can store numbers in the range of -3.4028235 x 1038 to +3. 4028235 x 1038 (for comparison there is estimated to be 7.5 x 1018 grains of sand in the world) but at a precision of only 6 or 7 decimal digits.&lt;br /&gt;
&lt;br /&gt;
For the Arduboy, in contrast to integer operations, floating point calculations are internally quite complicated and thus incredibly processor intensive. Including numerous calculations to reposition the player and enemies within the main loop of a program might prevent the game from performing well or worse yet even being playable.&lt;br /&gt;
&lt;br /&gt;
However, there is an alternative to &#039;&#039;&#039;floating point&#039;&#039;&#039; arithmetic. This alternative is &#039;&#039;&#039;fixed point&#039;&#039;&#039; arithmetic. Fixed point numbers behave more similarly to the integer values you are familiar with. As such, the processing power required to manipulate fixed point numbers is more similar to integer calculations than to floating point calculations. &lt;br /&gt;
&lt;br /&gt;
There is however a tradeoff for the benefit that fixed point data types bring. The tradeoff is that you must explicitly nominate the size  of both the integer and fractional portion of the variable – much in the same way you nominate the available range when choosing integer data types. In addition to the obvious data range restrictions of the integer component, the resolution (or ‘granularity’)  of the fractional component must also be chosen.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 8 bit fixed point type with a 4 bit integer part and a 4 bit fractional part would permit an integer range of 0 to 15 and a fractional range of 0/16 to 15/16. This equates to a range of 0 to 15.9375 (15/16 is equivalent to 0.9375).&lt;br /&gt;
&lt;br /&gt;
Note that the fractional part can only go up in increments of 1/16 (i.e. 0.0625), hence that particular format cannot accurately represent numbers at a lower resolution, for example the value 0.05 could not be accurately represented, it would have to be rounded to 0.0625. However, by using larger types or by increasing the number of bits afforded to the fractional part at the expense of the integer part you can balance the format to suit your needs.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 16 bit type with 8 bits of integer part and 8 bits of fractional part could represent all the positions on the Arduboy’s screen, whilst providing a reasonable range of fractional steps in between the precise pixel locations. This would mean having much smoother movement than using regular integers, but in a way that’s cheaper than floating points in terms of both code use and processing power.&lt;br /&gt;
&lt;br /&gt;
=== &#039;&#039;&#039;Fixed Points in Action&#039;&#039;&#039; ===&lt;br /&gt;
In the examples below, I am using @Pharap’s [https://community.arduboy.com/t/a-fixed-point-arithmetic-library/4129 FixedPoints] library which defines a number of signed and unsigned data types which are listed at the end of this article. It also includes overrides for the common arithmetic and comparison operators (+, -, / , *, &amp;lt;, &amp;gt;, etc) as well as some handy functions like &amp;lt;code&amp;gt;ceilFixed()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;floorFixed()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;roundFixed()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Firstly, some simple assignments and calculations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar = 7.0;&lt;br /&gt;
myVar = myVar + 3;&lt;br /&gt;
&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 4);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
static_cast&amp;lt;float&amp;gt;(myVar) : 10.0000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Please note that an extra ‘level of precision’ argument is being used in the print code here. Without supplying this extra argument, the printed results suffer from rounding errors made by the &amp;lt;code&amp;gt;Serial.print()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Serial.println()&amp;lt;/code&amp;gt; functions to simplify the code for printing floats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Simple. Now let’s introduce a decimal place.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar = 3.7;&lt;br /&gt;
Serial.print(“myVar.getInteger() : “);&lt;br /&gt;
Serial.println(myVar.getInteger());&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 8);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myVar.getInteger() : 3&lt;br /&gt;
static_cast&amp;lt;float&amp;gt;(myVar) :  3.69921875&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But hang on, we set the variable to 3.7 and it printed out 3.69921875. Why so? As mentioned earlier, both &#039;&#039;&#039;floating point&#039;&#039;&#039; and &#039;&#039;&#039;fixed point&#039;&#039;&#039; numbers introduce a degree of inaccuracy when storing numbers. With Fixed Point numbers, the precision (and by extension the accuracy) is specified by the programmer.&lt;br /&gt;
&lt;br /&gt;
In the sample code above, the variable chosen was a two-byte incarnation and it  can store a number roughly between -128 and +128. The integer component is stored in 7 bits which allows a range of 0 to 127 and the fractional part is stored in 8 bits and has a range between 0 / 256 to 255 / 256. The largest fractional portion (255 / 256) converted to decimal is 0.9961 hence the data range cannot quite store +128, but it can store up to 127.9961.&lt;br /&gt;
&lt;br /&gt;
If you could view the bits in the fractional part of the variable (which you can through &#039;&#039;&#039;myVar.getFraction()&#039;&#039;&#039;), you would see that it is stored as 179. As it turns out 179 / 256 is equal to 0.69921875.&lt;br /&gt;
&lt;br /&gt;
The inaccuracy introduced by a relatively small (two-byte) &#039;&#039;&#039;fixed point&#039;&#039;&#039; is irrelevant if all you are trying to do is simulate some realistic movements of characters in a game. If you have a need to calculate more accurate values, you can simply increase the size of the &#039;&#039;&#039;fixed point&#039;&#039;&#039; variable to either an SQ15x16 or an SQ31x32 (though be aware that some operations do not work on SQ31x32 due to presently unavoidable restrictions). The code below illustrates the accuracy of each data type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar7x8 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar7x8), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar7x8 = myVar7x8 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
Serial.println();&lt;br /&gt;
 &lt;br /&gt;
SQ15x16 myVar15x16 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar15x16), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar15x16 = myVar15x16 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
Serial.println();&lt;br /&gt;
 &lt;br /&gt;
SQ31x32 myVar31x32 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar31x32), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar31x32 = myVar31x32 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
Serial.println();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 4.00000000 4.09765625 4.19531250 4.29296875 4.39062500 4.48828125 4.58593750 4.68359375 4.78125000 4.87890625 4.97656250&lt;br /&gt;
 4.00000000 4.09999084 4.19998168 4.29997253 4.39996337 4.49995422 4.59994506 4.69993591 4.79992675 4.89991760 4.99990844 &lt;br /&gt;
 4.00000000 4.09999990 4.19999980 4.30000019 4.40000009 4.50000000 4.59999990 4.69999980 4.80000019 4.90000009 5.00000000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One thing to note with the output is the compounding effect that adding two inaccurate numbers results in. This can be seen easily if we repeat the same exercise on the smallest, one-byte data type as shown below. The fractional part of an &amp;lt;code&amp;gt;SQ3x4&amp;lt;/code&amp;gt; number is stored in 1/16th increments or 0.0625 in decimal.&lt;br /&gt;
&lt;br /&gt;
When we add the first increment of 0.1  we immediately introduce an inaccuracy as the number gets rounded down to the nearest 1/16th of a number. As we repeat the addition, the error increases. The following code shows that.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ3x4 myVar3x4 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar3x4), 4);&lt;br /&gt;
  Serial.print(“ “);&lt;br /&gt;
  myVar3x4 = myVar3x4 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 4.0000 4.0625 4.1250 4.1875 4.2500 4.3125 4.3750 4.4375 4.5000 4.5625 4.6250&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choosing larger &#039;&#039;&#039;fixed point&#039;&#039;&#039; data types increases the range of the number and accuracy of the fractional component at the expense of memory.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Why would I bother with Fixed Points at all?&#039;&#039;&#039; ==&lt;br /&gt;
Imagine you are building a sideways-scrolling game where aliens (or cars, planes or other moving objects) are moving towards you at various speeds. With each pass through the program’s loop you could decrement the x coordinate of each alien but this would result in all aliens moving at the same speed. You could try to move aliens every second or third time through the loop by keeping some sort of counter but this can be error prone and&lt;br /&gt;
&lt;br /&gt;
may result in the aliens moving slowly. To really allow flexibility in the aliens’ movement, you could store the x and y in fixed point variables and increment these by a decimal value allowing the aliens to move at similar but slightly different speeds.&lt;br /&gt;
&lt;br /&gt;
Consider the Alien class below. Each alien is described with an x and y coordinate and takes care of its own movement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Alien {&lt;br /&gt;
 &lt;br /&gt;
   public:&lt;br /&gt;
 &lt;br /&gt;
     Alien(SQ7x8 x, SQ7x8 y, SQ7x8 incX, SQ7x8 incY, const uint8_t *bitmap) {&lt;br /&gt;
&lt;br /&gt;
       _x = x;&lt;br /&gt;
       _y = y;&lt;br /&gt;
       _incX = incX;&lt;br /&gt;
       _incY = incY;&lt;br /&gt;
       _bitmap = bitmap; &lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 getX() const { return _x; }&lt;br /&gt;
     SQ7x8 getY() const { return _y; }&lt;br /&gt;
 &lt;br /&gt;
     void setX(const SQ7x8 value) { _x = value; }&lt;br /&gt;
     void setY(const SQ7x8 value) { _y = value; }&lt;br /&gt;
 &lt;br /&gt;
     inline void Alien::move(uint8_t frame) {&lt;br /&gt;
 &lt;br /&gt;
       _x = _x - _incX;&lt;br /&gt;
       _y = _y - _incY;&lt;br /&gt;
       Sprites::drawOverwrite(_x.getInteger(), _y.getInteger(), _bitmap, frame);&lt;br /&gt;
   } &lt;br /&gt;
 &lt;br /&gt;
   private:&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 _x;&lt;br /&gt;
     SQ7x8 _y;&lt;br /&gt;
     SQ7x8 _incX;&lt;br /&gt;
     SQ7x8 _incY;&lt;br /&gt;
     const uint8_t *_bitmap;&lt;br /&gt;
   &lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And our main .ino file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Alien alien1 = {127, 16, (SQ7x8)0.9, (SQ7x8)1.34, alienBitmap };&lt;br /&gt;
Alien alien2 = {127, 32, (SQ7x8)1.7, (SQ7x8)1.76, alienBitmap };&lt;br /&gt;
Alien alien3 = {127, 48, (SQ7x8)1.5, (SQ7x8)2.13, alienBitmap }; &lt;br /&gt;
 &lt;br /&gt;
void loop() {&lt;br /&gt;
  alien1.move(frame);&lt;br /&gt;
  alien2.move(frame);&lt;br /&gt;
  alien3.move(frame);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With fixed point data types, we can have our aliens move at slightly different speeds without the overhead of using floating points.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Common Type Aliases&#039;&#039;&#039; ==&lt;br /&gt;
@Pharap’s library includes the following data types. However the library is implemented with templates and it is a relatively simple matter to declare your own type aliases.&lt;br /&gt;
&lt;br /&gt;
The predeclared aliases (which can be  used as an example of how to declare such an alias) can be found in &#039;&#039;&#039;src\ FixedPointsCommon\SFixedCommon.h and src\ FixedPointsCommon\UfixedCommon.h.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Unsigned Data Type Aliases:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|-&lt;br /&gt;
|UQ4x4&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 15&lt;br /&gt;
|1 / 16th&lt;br /&gt;
|0 to 16&lt;br /&gt;
|-&lt;br /&gt;
|UQ8x8&lt;br /&gt;
|16 bit (2 bytes)&lt;br /&gt;
|0 to 255&lt;br /&gt;
|1 / 256th&lt;br /&gt;
|0 to 256&lt;br /&gt;
|-&lt;br /&gt;
|UQ16x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|0 to 65,355&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|0 to 65,356&lt;br /&gt;
|-&lt;br /&gt;
|UQ32x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|0 to 4,294,967,295&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|0 to 4,294,967,296&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x7&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 128th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x15&lt;br /&gt;
|16 bit (2 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 32,768th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x31&lt;br /&gt;
|32 bit (4 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 2,147,483,648th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x63&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1/ 9,223,372,036,854,775,808th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Signed Data Type Aliases:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|-&lt;br /&gt;
|SQ3x4&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;15 to +15&lt;br /&gt;
|1 / 16th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;16 to +16&lt;br /&gt;
|-&lt;br /&gt;
|SQ7x8&lt;br /&gt;
|16 bit (2 bytes)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;127 to +127&lt;br /&gt;
|1 / 256th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;128 to +128&lt;br /&gt;
|-&lt;br /&gt;
|SQ15x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;32,767 to +32,767&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;32,768 to +32,768&lt;br /&gt;
|-&lt;br /&gt;
|SQ31x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2,147,483,647 to +2,147,483,647&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2,147,483,648 to +2,147,483,648&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x6&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;1 to +1&lt;br /&gt;
|1 / 64th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x14&lt;br /&gt;
|16 bit (2 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;1 to +1&lt;br /&gt;
|1 / 16,384th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x30&lt;br /&gt;
|32 bit (4 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;1 to +1&lt;br /&gt;
|1 / 1,073,741,824th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x62&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;1 to +1&lt;br /&gt;
|1/ 4,611,686,018,427,387,904th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;2 to +2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt; Lower bound is inclusive, upper bound is exclusive&lt;br /&gt;
&lt;br /&gt;
FixedPoints can be found through the Arduino IDE library manager. If you have not used the library manager before see here: &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;https://arduboy.com/download-and-learn-arduino/&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=597</id>
		<title>Fixed Point</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=597"/>
		<updated>2024-08-16T10:13:54Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* Common Type Aliases */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
Most of us are familiar with the integer data types used by the Arduboy environment – of various lengths, both signed and unsigned. We take care to choose appropriate data types that consider both the range of our data as well as the memory required to store them. Operations involving integer values are incredibly efficient and for many sketches are all that are required.&lt;br /&gt;
&lt;br /&gt;
Sometimes however, integer values are simply not flexible enough. Imagine trying to calculate the movement of an accelerating rocket or calculate the trajectory of a rocket or a missile in an Arduboy game. To make these calculations, you need numbers with both an integer and fractional component. The typical way to represent such numbers on a computer is through floating points.&lt;br /&gt;
&lt;br /&gt;
C++ includes two floating point data types of different precision – the float and the double. On most platforms the float is 4 bytes and the double is 8 bytes, however on the Arduino (and thus the Arduboy) they are actually same precision (4 bytes) and are therefore interchangeable. These data types can store numbers in the range of -3.4028235 x 1038 to +3. 4028235 x 1038 (for comparison there is estimated to be 7.5 x 1018 grains of sand in the world) but at a precision of only 6 or 7 decimal digits.&lt;br /&gt;
&lt;br /&gt;
For the Arduboy, in contrast to integer operations, floating point calculations are internally quite complicated and thus incredibly processor intensive. Including numerous calculations to reposition the player and enemies within the main loop of a program might prevent the game from performing well or worse yet even being playable.&lt;br /&gt;
&lt;br /&gt;
However, there is an alternative to &#039;&#039;&#039;floating point&#039;&#039;&#039; arithmetic. This alternative is &#039;&#039;&#039;fixed point&#039;&#039;&#039; arithmetic. Fixed point numbers behave more similarly to the integer values you are familiar with. As such, the processing power required to manipulate fixed point numbers is more similar to integer calculations than to floating point calculations. &lt;br /&gt;
&lt;br /&gt;
There is however a tradeoff for the benefit that fixed point data types bring. The tradeoff is that you must explicitly nominate the size  of both the integer and fractional portion of the variable – much in the same way you nominate the available range when choosing integer data types. In addition to the obvious data range restrictions of the integer component, the resolution (or ‘granularity’)  of the fractional component must also be chosen.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 8 bit fixed point type with a 4 bit integer part and a 4 bit fractional part would permit an integer range of 0 to 15 and a fractional range of 0/16 to 15/16. This equates to a range of 0 to 15.9375 (15/16 is equivalent to 0.9375).&lt;br /&gt;
&lt;br /&gt;
Note that the fractional part can only go up in increments of 1/16 (i.e. 0.0625), hence that particular format cannot accurately represent numbers at a lower resolution, for example the value 0.05 could not be accurately represented, it would have to be rounded to 0.0625. However, by using larger types or by increasing the number of bits afforded to the fractional part at the expense of the integer part you can balance the format to suit your needs.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 16 bit type with 8 bits of integer part and 8 bits of fractional part could represent all the positions on the Arduboy’s screen, whilst providing a reasonable range of fractional steps in between the precise pixel locations. This would mean having much smoother movement than using regular integers, but in a way that’s cheaper than floating points in terms of both code use and processing power.&lt;br /&gt;
&lt;br /&gt;
=== &#039;&#039;&#039;Fixed Points in Action&#039;&#039;&#039; ===&lt;br /&gt;
In the examples below, I am using @Pharap’s [https://community.arduboy.com/t/a-fixed-point-arithmetic-library/4129 FixedPoints] library which defines a number of signed and unsigned data types which are listed at the end of this article. It also includes overrides for the common arithmetic and comparison operators (+, -, / , *, &amp;lt;, &amp;gt;, etc) as well as some handy functions like &amp;lt;code&amp;gt;ceilFixed()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;floorFixed()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;roundFixed()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Firstly, some simple assignments and calculations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar = 7.0;&lt;br /&gt;
myVar = myVar + 3;&lt;br /&gt;
&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 4);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
static_cast&amp;lt;float&amp;gt;(myVar) : 10.0000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Please note that an extra ‘level of precision’ argument is being used in the print code here. Without supplying this extra argument, the printed results suffer from rounding errors made by the &amp;lt;code&amp;gt;Serial.print()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Serial.println()&amp;lt;/code&amp;gt; functions to simplify the code for printing floats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Simple. Now let’s introduce a decimal place.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar = 3.7;&lt;br /&gt;
Serial.print(“myVar.getInteger() : “);&lt;br /&gt;
Serial.println(myVar.getInteger());&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 8);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myVar.getInteger() : 3&lt;br /&gt;
static_cast&amp;lt;float&amp;gt;(myVar) :  3.69921875&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But hang on, we set the variable to 3.7 and it printed out 3.69921875. Why so? As mentioned earlier, both &#039;&#039;&#039;floating point&#039;&#039;&#039; and &#039;&#039;&#039;fixed point&#039;&#039;&#039; numbers introduce a degree of inaccuracy when storing numbers. With Fixed Point numbers, the precision (and by extension the accuracy) is specified by the programmer.&lt;br /&gt;
&lt;br /&gt;
In the sample code above, the variable chosen was a two-byte incarnation and it  can store a number roughly between -128 and +128. The integer component is stored in 7 bits which allows a range of 0 to 127 and the fractional part is stored in 8 bits and has a range between 0 / 256 to 255 / 256. The largest fractional portion (255 / 256) converted to decimal is 0.9961 hence the data range cannot quite store +128, but it can store up to 127.9961.&lt;br /&gt;
&lt;br /&gt;
If you could view the bits in the fractional part of the variable (which you can through &#039;&#039;&#039;myVar.getFraction()&#039;&#039;&#039;), you would see that it is stored as 179. As it turns out 179 / 256 is equal to 0.69921875.&lt;br /&gt;
&lt;br /&gt;
The inaccuracy introduced by a relatively small (two-byte) &#039;&#039;&#039;fixed point&#039;&#039;&#039; is irrelevant if all you are trying to do is simulate some realistic movements of characters in a game. If you have a need to calculate more accurate values, you can simply increase the size of the &#039;&#039;&#039;fixed point&#039;&#039;&#039; variable to either an SQ15x16 or an SQ31x32 (though be aware that some operations do not work on SQ31x32 due to presently unavoidable restrictions). The code below illustrates the accuracy of each data type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar7x8 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar7x8), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar7x8 = myVar7x8 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
Serial.println();&lt;br /&gt;
 &lt;br /&gt;
SQ15x16 myVar15x16 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar15x16), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar15x16 = myVar15x16 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
Serial.println();&lt;br /&gt;
 &lt;br /&gt;
SQ31x32 myVar31x32 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar31x32), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar31x32 = myVar31x32 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
Serial.println();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 4.00000000 4.09765625 4.19531250 4.29296875 4.39062500 4.48828125 4.58593750 4.68359375 4.78125000 4.87890625 4.97656250&lt;br /&gt;
 4.00000000 4.09999084 4.19998168 4.29997253 4.39996337 4.49995422 4.59994506 4.69993591 4.79992675 4.89991760 4.99990844 &lt;br /&gt;
 4.00000000 4.09999990 4.19999980 4.30000019 4.40000009 4.50000000 4.59999990 4.69999980 4.80000019 4.90000009 5.00000000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One thing to note with the output is the compounding effect that adding two inaccurate numbers results in. This can be seen easily if we repeat the same exercise on the smallest, one-byte data type as shown below. The fractional part of an &amp;lt;code&amp;gt;SQ3x4&amp;lt;/code&amp;gt; number is stored in 1/16th increments or 0.0625 in decimal.&lt;br /&gt;
&lt;br /&gt;
When we add the first increment of 0.1  we immediately introduce an inaccuracy as the number gets rounded down to the nearest 1/16th of a number. As we repeat the addition, the error increases. The following code shows that.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ3x4 myVar3x4 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar3x4), 4);&lt;br /&gt;
  Serial.print(“ “);&lt;br /&gt;
  myVar3x4 = myVar3x4 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 4.0000 4.0625 4.1250 4.1875 4.2500 4.3125 4.3750 4.4375 4.5000 4.5625 4.6250&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choosing larger &#039;&#039;&#039;fixed point&#039;&#039;&#039; data types increases the range of the number and accuracy of the fractional component at the expense of memory.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Why would I bother with Fixed Points at all?&#039;&#039;&#039; ==&lt;br /&gt;
Imagine you are building a sideways-scrolling game where aliens (or cars, planes or other moving objects) are moving towards you at various speeds. With each pass through the program’s loop you could decrement the x coordinate of each alien but this would result in all aliens moving at the same speed. You could try to move aliens every second or third time through the loop by keeping some sort of counter but this can be error prone and&lt;br /&gt;
&lt;br /&gt;
may result in the aliens moving slowly. To really allow flexibility in the aliens’ movement, you could store the x and y in fixed point variables and increment these by a decimal value allowing the aliens to move at similar but slightly different speeds.&lt;br /&gt;
&lt;br /&gt;
Consider the Alien class below. Each alien is described with an x and y coordinate and takes care of its own movement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Alien {&lt;br /&gt;
 &lt;br /&gt;
   public:&lt;br /&gt;
 &lt;br /&gt;
     Alien(SQ7x8 x, SQ7x8 y, SQ7x8 incX, SQ7x8 incY, const uint8_t *bitmap) {&lt;br /&gt;
&lt;br /&gt;
       _x = x;&lt;br /&gt;
       _y = y;&lt;br /&gt;
       _incX = incX;&lt;br /&gt;
       _incY = incY;&lt;br /&gt;
       _bitmap = bitmap; &lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 getX() const { return _x; }&lt;br /&gt;
     SQ7x8 getY() const { return _y; }&lt;br /&gt;
 &lt;br /&gt;
     void setX(const SQ7x8 value) { _x = value; }&lt;br /&gt;
     void setY(const SQ7x8 value) { _y = value; }&lt;br /&gt;
 &lt;br /&gt;
     inline void Alien::move(uint8_t frame) {&lt;br /&gt;
 &lt;br /&gt;
       _x = _x - _incX;&lt;br /&gt;
       _y = _y - _incY;&lt;br /&gt;
       Sprites::drawOverwrite(_x.getInteger(), _y.getInteger(), _bitmap, frame);&lt;br /&gt;
   } &lt;br /&gt;
 &lt;br /&gt;
   private:&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 _x;&lt;br /&gt;
     SQ7x8 _y;&lt;br /&gt;
     SQ7x8 _incX;&lt;br /&gt;
     SQ7x8 _incY;&lt;br /&gt;
     const uint8_t *_bitmap;&lt;br /&gt;
   &lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And our main .ino file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Alien alien1 = {127, 16, (SQ7x8)0.9, (SQ7x8)1.34, alienBitmap };&lt;br /&gt;
Alien alien2 = {127, 32, (SQ7x8)1.7, (SQ7x8)1.76, alienBitmap };&lt;br /&gt;
Alien alien3 = {127, 48, (SQ7x8)1.5, (SQ7x8)2.13, alienBitmap }; &lt;br /&gt;
 &lt;br /&gt;
void loop() {&lt;br /&gt;
  alien1.move(frame);&lt;br /&gt;
  alien2.move(frame);&lt;br /&gt;
  alien3.move(frame);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With fixed point data types, we can have our aliens move at slightly different speeds without the overhead of using floating points.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Common Type Aliases&#039;&#039;&#039; ==&lt;br /&gt;
@Pharap’s library includes the following data types. However the library is implemented with templates and it is a relatively simple matter to declare your own type aliases.&lt;br /&gt;
&lt;br /&gt;
The predeclared aliases (which can be  used as an example of how to declare such an alias) can be found in &#039;&#039;&#039;src\ FixedPointsCommon\SFixedCommon.h and src\ FixedPointsCommon\UfixedCommon.h.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Unsigned Data Type Aliases:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|-&lt;br /&gt;
|UQ4x4&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 15&lt;br /&gt;
|1 / 16th&lt;br /&gt;
|0 to 16&lt;br /&gt;
|-&lt;br /&gt;
|UQ8x8&lt;br /&gt;
|16 bit (2 bytes)&lt;br /&gt;
|0 to 255&lt;br /&gt;
|1 / 256th&lt;br /&gt;
|0 to 256&lt;br /&gt;
|-&lt;br /&gt;
|UQ16x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|0 to 65,355&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|0 to 65,356&lt;br /&gt;
|-&lt;br /&gt;
|UQ32x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|0 to 4,294,967,295&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|0 to 4,294,967,296&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x7&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 128th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x15&lt;br /&gt;
|16 bit (2 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 32,768th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x31&lt;br /&gt;
|32 bit (4 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 2,147,483,648th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x63&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1/ 9,223,372,036,854,775,808th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Signed Data Type Aliases:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|-&lt;br /&gt;
|SQ3x4&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;15 to +15&lt;br /&gt;
|1 / 16th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-&amp;lt;/nowiki&amp;gt;16 to +16&lt;br /&gt;
|-&lt;br /&gt;
|SQ7x8&lt;br /&gt;
|16 bit (2 bytes)&lt;br /&gt;
|-127 to +127&lt;br /&gt;
|1 / 256th&lt;br /&gt;
|-128 to +128&lt;br /&gt;
|-&lt;br /&gt;
|SQ15x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|-32,767 to +32,767&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|-32,768 to +32,768&lt;br /&gt;
|-&lt;br /&gt;
|SQ31x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|-2,147,483,647 to +2,147,483,647&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|2,147,483,648 to +2,147,483,648&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x6&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|-1 to +1&lt;br /&gt;
|1 / 64th&lt;br /&gt;
|-2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x14&lt;br /&gt;
|16 bit (2 byte)&lt;br /&gt;
|-1 to +1&lt;br /&gt;
|1 / 16,384th&lt;br /&gt;
|-2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x30&lt;br /&gt;
|32 bit (4 byte)&lt;br /&gt;
|-1 to +1&lt;br /&gt;
|1 / 1,073,741,824th&lt;br /&gt;
|-2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x62&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|-1 to +1&lt;br /&gt;
|1/ 4,611,686,018,427,387,904th&lt;br /&gt;
|-2 to +2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt; Lower bound is inclusive, upper bound is exclusive&lt;br /&gt;
&lt;br /&gt;
FixedPoints can be found through the Arduino IDE library manager. If you have not used the library manager before see here: &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;https://arduboy.com/download-and-learn-arduino/&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=596</id>
		<title>Fixed Point</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=596"/>
		<updated>2024-08-16T10:12:06Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
Most of us are familiar with the integer data types used by the Arduboy environment – of various lengths, both signed and unsigned. We take care to choose appropriate data types that consider both the range of our data as well as the memory required to store them. Operations involving integer values are incredibly efficient and for many sketches are all that are required.&lt;br /&gt;
&lt;br /&gt;
Sometimes however, integer values are simply not flexible enough. Imagine trying to calculate the movement of an accelerating rocket or calculate the trajectory of a rocket or a missile in an Arduboy game. To make these calculations, you need numbers with both an integer and fractional component. The typical way to represent such numbers on a computer is through floating points.&lt;br /&gt;
&lt;br /&gt;
C++ includes two floating point data types of different precision – the float and the double. On most platforms the float is 4 bytes and the double is 8 bytes, however on the Arduino (and thus the Arduboy) they are actually same precision (4 bytes) and are therefore interchangeable. These data types can store numbers in the range of -3.4028235 x 1038 to +3. 4028235 x 1038 (for comparison there is estimated to be 7.5 x 1018 grains of sand in the world) but at a precision of only 6 or 7 decimal digits.&lt;br /&gt;
&lt;br /&gt;
For the Arduboy, in contrast to integer operations, floating point calculations are internally quite complicated and thus incredibly processor intensive. Including numerous calculations to reposition the player and enemies within the main loop of a program might prevent the game from performing well or worse yet even being playable.&lt;br /&gt;
&lt;br /&gt;
However, there is an alternative to &#039;&#039;&#039;floating point&#039;&#039;&#039; arithmetic. This alternative is &#039;&#039;&#039;fixed point&#039;&#039;&#039; arithmetic. Fixed point numbers behave more similarly to the integer values you are familiar with. As such, the processing power required to manipulate fixed point numbers is more similar to integer calculations than to floating point calculations. &lt;br /&gt;
&lt;br /&gt;
There is however a tradeoff for the benefit that fixed point data types bring. The tradeoff is that you must explicitly nominate the size  of both the integer and fractional portion of the variable – much in the same way you nominate the available range when choosing integer data types. In addition to the obvious data range restrictions of the integer component, the resolution (or ‘granularity’)  of the fractional component must also be chosen.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 8 bit fixed point type with a 4 bit integer part and a 4 bit fractional part would permit an integer range of 0 to 15 and a fractional range of 0/16 to 15/16. This equates to a range of 0 to 15.9375 (15/16 is equivalent to 0.9375).&lt;br /&gt;
&lt;br /&gt;
Note that the fractional part can only go up in increments of 1/16 (i.e. 0.0625), hence that particular format cannot accurately represent numbers at a lower resolution, for example the value 0.05 could not be accurately represented, it would have to be rounded to 0.0625. However, by using larger types or by increasing the number of bits afforded to the fractional part at the expense of the integer part you can balance the format to suit your needs.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 16 bit type with 8 bits of integer part and 8 bits of fractional part could represent all the positions on the Arduboy’s screen, whilst providing a reasonable range of fractional steps in between the precise pixel locations. This would mean having much smoother movement than using regular integers, but in a way that’s cheaper than floating points in terms of both code use and processing power.&lt;br /&gt;
&lt;br /&gt;
=== &#039;&#039;&#039;Fixed Points in Action&#039;&#039;&#039; ===&lt;br /&gt;
In the examples below, I am using @Pharap’s [https://community.arduboy.com/t/a-fixed-point-arithmetic-library/4129 FixedPoints] library which defines a number of signed and unsigned data types which are listed at the end of this article. It also includes overrides for the common arithmetic and comparison operators (+, -, / , *, &amp;lt;, &amp;gt;, etc) as well as some handy functions like &amp;lt;code&amp;gt;ceilFixed()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;floorFixed()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;roundFixed()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Firstly, some simple assignments and calculations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar = 7.0;&lt;br /&gt;
myVar = myVar + 3;&lt;br /&gt;
&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 4);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
static_cast&amp;lt;float&amp;gt;(myVar) : 10.0000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Please note that an extra ‘level of precision’ argument is being used in the print code here. Without supplying this extra argument, the printed results suffer from rounding errors made by the &amp;lt;code&amp;gt;Serial.print()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Serial.println()&amp;lt;/code&amp;gt; functions to simplify the code for printing floats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Simple. Now let’s introduce a decimal place.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar = 3.7;&lt;br /&gt;
Serial.print(“myVar.getInteger() : “);&lt;br /&gt;
Serial.println(myVar.getInteger());&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 8);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myVar.getInteger() : 3&lt;br /&gt;
static_cast&amp;lt;float&amp;gt;(myVar) :  3.69921875&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But hang on, we set the variable to 3.7 and it printed out 3.69921875. Why so? As mentioned earlier, both &#039;&#039;&#039;floating point&#039;&#039;&#039; and &#039;&#039;&#039;fixed point&#039;&#039;&#039; numbers introduce a degree of inaccuracy when storing numbers. With Fixed Point numbers, the precision (and by extension the accuracy) is specified by the programmer.&lt;br /&gt;
&lt;br /&gt;
In the sample code above, the variable chosen was a two-byte incarnation and it  can store a number roughly between -128 and +128. The integer component is stored in 7 bits which allows a range of 0 to 127 and the fractional part is stored in 8 bits and has a range between 0 / 256 to 255 / 256. The largest fractional portion (255 / 256) converted to decimal is 0.9961 hence the data range cannot quite store +128, but it can store up to 127.9961.&lt;br /&gt;
&lt;br /&gt;
If you could view the bits in the fractional part of the variable (which you can through &#039;&#039;&#039;myVar.getFraction()&#039;&#039;&#039;), you would see that it is stored as 179. As it turns out 179 / 256 is equal to 0.69921875.&lt;br /&gt;
&lt;br /&gt;
The inaccuracy introduced by a relatively small (two-byte) &#039;&#039;&#039;fixed point&#039;&#039;&#039; is irrelevant if all you are trying to do is simulate some realistic movements of characters in a game. If you have a need to calculate more accurate values, you can simply increase the size of the &#039;&#039;&#039;fixed point&#039;&#039;&#039; variable to either an SQ15x16 or an SQ31x32 (though be aware that some operations do not work on SQ31x32 due to presently unavoidable restrictions). The code below illustrates the accuracy of each data type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar7x8 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar7x8), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar7x8 = myVar7x8 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
Serial.println();&lt;br /&gt;
 &lt;br /&gt;
SQ15x16 myVar15x16 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar15x16), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar15x16 = myVar15x16 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
Serial.println();&lt;br /&gt;
 &lt;br /&gt;
SQ31x32 myVar31x32 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar31x32), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar31x32 = myVar31x32 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
Serial.println();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 4.00000000 4.09765625 4.19531250 4.29296875 4.39062500 4.48828125 4.58593750 4.68359375 4.78125000 4.87890625 4.97656250&lt;br /&gt;
 4.00000000 4.09999084 4.19998168 4.29997253 4.39996337 4.49995422 4.59994506 4.69993591 4.79992675 4.89991760 4.99990844 &lt;br /&gt;
 4.00000000 4.09999990 4.19999980 4.30000019 4.40000009 4.50000000 4.59999990 4.69999980 4.80000019 4.90000009 5.00000000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One thing to note with the output is the compounding effect that adding two inaccurate numbers results in. This can be seen easily if we repeat the same exercise on the smallest, one-byte data type as shown below. The fractional part of an &amp;lt;code&amp;gt;SQ3x4&amp;lt;/code&amp;gt; number is stored in 1/16th increments or 0.0625 in decimal.&lt;br /&gt;
&lt;br /&gt;
When we add the first increment of 0.1  we immediately introduce an inaccuracy as the number gets rounded down to the nearest 1/16th of a number. As we repeat the addition, the error increases. The following code shows that.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ3x4 myVar3x4 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar3x4), 4);&lt;br /&gt;
  Serial.print(“ “);&lt;br /&gt;
  myVar3x4 = myVar3x4 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 4.0000 4.0625 4.1250 4.1875 4.2500 4.3125 4.3750 4.4375 4.5000 4.5625 4.6250&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choosing larger &#039;&#039;&#039;fixed point&#039;&#039;&#039; data types increases the range of the number and accuracy of the fractional component at the expense of memory.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Why would I bother with Fixed Points at all?&#039;&#039;&#039; ==&lt;br /&gt;
Imagine you are building a sideways-scrolling game where aliens (or cars, planes or other moving objects) are moving towards you at various speeds. With each pass through the program’s loop you could decrement the x coordinate of each alien but this would result in all aliens moving at the same speed. You could try to move aliens every second or third time through the loop by keeping some sort of counter but this can be error prone and&lt;br /&gt;
&lt;br /&gt;
may result in the aliens moving slowly. To really allow flexibility in the aliens’ movement, you could store the x and y in fixed point variables and increment these by a decimal value allowing the aliens to move at similar but slightly different speeds.&lt;br /&gt;
&lt;br /&gt;
Consider the Alien class below. Each alien is described with an x and y coordinate and takes care of its own movement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Alien {&lt;br /&gt;
 &lt;br /&gt;
   public:&lt;br /&gt;
 &lt;br /&gt;
     Alien(SQ7x8 x, SQ7x8 y, SQ7x8 incX, SQ7x8 incY, const uint8_t *bitmap) {&lt;br /&gt;
&lt;br /&gt;
       _x = x;&lt;br /&gt;
       _y = y;&lt;br /&gt;
       _incX = incX;&lt;br /&gt;
       _incY = incY;&lt;br /&gt;
       _bitmap = bitmap; &lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 getX() const { return _x; }&lt;br /&gt;
     SQ7x8 getY() const { return _y; }&lt;br /&gt;
 &lt;br /&gt;
     void setX(const SQ7x8 value) { _x = value; }&lt;br /&gt;
     void setY(const SQ7x8 value) { _y = value; }&lt;br /&gt;
 &lt;br /&gt;
     inline void Alien::move(uint8_t frame) {&lt;br /&gt;
 &lt;br /&gt;
       _x = _x - _incX;&lt;br /&gt;
       _y = _y - _incY;&lt;br /&gt;
       Sprites::drawOverwrite(_x.getInteger(), _y.getInteger(), _bitmap, frame);&lt;br /&gt;
   } &lt;br /&gt;
 &lt;br /&gt;
   private:&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 _x;&lt;br /&gt;
     SQ7x8 _y;&lt;br /&gt;
     SQ7x8 _incX;&lt;br /&gt;
     SQ7x8 _incY;&lt;br /&gt;
     const uint8_t *_bitmap;&lt;br /&gt;
   &lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And our main .ino file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Alien alien1 = {127, 16, (SQ7x8)0.9, (SQ7x8)1.34, alienBitmap };&lt;br /&gt;
Alien alien2 = {127, 32, (SQ7x8)1.7, (SQ7x8)1.76, alienBitmap };&lt;br /&gt;
Alien alien3 = {127, 48, (SQ7x8)1.5, (SQ7x8)2.13, alienBitmap }; &lt;br /&gt;
 &lt;br /&gt;
void loop() {&lt;br /&gt;
  alien1.move(frame);&lt;br /&gt;
  alien2.move(frame);&lt;br /&gt;
  alien3.move(frame);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With fixed point data types, we can have our aliens move at slightly different speeds without the overhead of using floating points.&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Common Type Aliases&#039;&#039;&#039; ==&lt;br /&gt;
@Pharap’s library includes the following data types. However the library is implemented with templates and it is a relatively simple matter to declare your own type aliases.&lt;br /&gt;
&lt;br /&gt;
The predeclared aliases (which can be  used as an example of how to declare such an alias) can be found in &#039;&#039;&#039;src\ FixedPointsCommon\SFixedCommon.h and src\ FixedPointsCommon\UfixedCommon.h.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Unsigned Data Type Aliases:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|-&lt;br /&gt;
|UQ4x4&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 15&lt;br /&gt;
|1 / 16th&lt;br /&gt;
|0 to 16&lt;br /&gt;
|-&lt;br /&gt;
|UQ8x8&lt;br /&gt;
|16 bit (2 bytes)&lt;br /&gt;
|0 to 255&lt;br /&gt;
|1 / 256th&lt;br /&gt;
|0 to 256&lt;br /&gt;
|-&lt;br /&gt;
|UQ16x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|0 to 65,355&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|0 to 65,356&lt;br /&gt;
|-&lt;br /&gt;
|UQ32x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|0 to 4,294,967,295&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|0 to 4,294,967,296&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x7&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 128th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x15&lt;br /&gt;
|16 bit (2 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 32,768th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x31&lt;br /&gt;
|32 bit (4 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 2,147,483,648th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x63&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1/ 9,223,372,036,854,775,808th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Signed Data Type Aliases:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|-&lt;br /&gt;
|SQ3x4&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|-15 to +15&lt;br /&gt;
|1 / 16th&lt;br /&gt;
|-16 to +16&lt;br /&gt;
|-&lt;br /&gt;
|SQ7x8&lt;br /&gt;
|16 bit (2 bytes)&lt;br /&gt;
|-127 to +127&lt;br /&gt;
|1 / 256th&lt;br /&gt;
|-128 to +128&lt;br /&gt;
|-&lt;br /&gt;
|SQ15x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|-32,767 to +32,767&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|-32,768 to +32,768&lt;br /&gt;
|-&lt;br /&gt;
|SQ31x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|-2,147,483,647 to +2,147,483,647&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|2,147,483,648 to +2,147,483,648&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x6&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|-1 to +1&lt;br /&gt;
|1 / 64th&lt;br /&gt;
|-2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x14&lt;br /&gt;
|16 bit (2 byte)&lt;br /&gt;
|-1 to +1&lt;br /&gt;
|1 / 16,384th&lt;br /&gt;
|-2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x30&lt;br /&gt;
|32 bit (4 byte)&lt;br /&gt;
|-1 to +1&lt;br /&gt;
|1 / 1,073,741,824th&lt;br /&gt;
|-2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x62&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|-1 to +1&lt;br /&gt;
|1/ 4,611,686,018,427,387,904th&lt;br /&gt;
|-2 to +2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt; Lower bound is inclusive, upper bound is exclusive&lt;br /&gt;
&lt;br /&gt;
FixedPoints can be found through the Arduino IDE library manager. If you have not used the library manager before see here: &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;https://arduboy.com/download-and-learn-arduino/&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=595</id>
		<title>Fixed Point</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=595"/>
		<updated>2024-08-16T10:11:19Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
Most of us are familiar with the integer data types used by the Arduboy environment – of various lengths, both signed and unsigned. We take care to choose appropriate data types that consider both the range of our data as well as the memory required to store them. Operations involving integer values are incredibly efficient and for many sketches are all that are required.&lt;br /&gt;
&lt;br /&gt;
Sometimes however, integer values are simply not flexible enough. Imagine trying to calculate the movement of an accelerating rocket or calculate the trajectory of a rocket or a missile in an Arduboy game. To make these calculations, you need numbers with both an integer and fractional component. The typical way to represent such numbers on a computer is through floating points.&lt;br /&gt;
&lt;br /&gt;
C++ includes two floating point data types of different precision – the float and the double. On most platforms the float is 4 bytes and the double is 8 bytes, however on the Arduino (and thus the Arduboy) they are actually same precision (4 bytes) and are therefore interchangeable. These data types can store numbers in the range of -3.4028235 x 1038 to +3. 4028235 x 1038 (for comparison there is estimated to be 7.5 x 1018 grains of sand in the world) but at a precision of only 6 or 7 decimal digits.&lt;br /&gt;
&lt;br /&gt;
For the Arduboy, in contrast to integer operations, floating point calculations are internally quite complicated and thus incredibly processor intensive. Including numerous calculations to reposition the player and enemies within the main loop of a program might prevent the game from performing well or worse yet even being playable.&lt;br /&gt;
&lt;br /&gt;
However, there is an alternative to &#039;&#039;&#039;floating point&#039;&#039;&#039; arithmetic. This alternative is &#039;&#039;&#039;fixed point&#039;&#039;&#039; arithmetic. Fixed point numbers behave more similarly to the integer values you are familiar with. As such, the processing power required to manipulate fixed point numbers is more similar to integer calculations than to floating point calculations. &lt;br /&gt;
&lt;br /&gt;
There is however a tradeoff for the benefit that fixed point data types bring. The tradeoff is that you must explicitly nominate the size  of both the integer and fractional portion of the variable – much in the same way you nominate the available range when choosing integer data types. In addition to the obvious data range restrictions of the integer component, the resolution (or ‘granularity’)  of the fractional component must also be chosen.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 8 bit fixed point type with a 4 bit integer part and a 4 bit fractional part would permit an integer range of 0 to 15 and a fractional range of 0/16 to 15/16. This equates to a range of 0 to 15.9375 (15/16 is equivalent to 0.9375).&lt;br /&gt;
&lt;br /&gt;
Note that the fractional part can only go up in increments of 1/16 (i.e. 0.0625), hence that particular format cannot accurately represent numbers at a lower resolution, for example the value 0.05 could not be accurately represented, it would have to be rounded to 0.0625. However, by using larger types or by increasing the number of bits afforded to the fractional part at the expense of the integer part you can balance the format to suit your needs.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 16 bit type with 8 bits of integer part and 8 bits of fractional part could represent all the positions on the Arduboy’s screen, whilst providing a reasonable range of fractional steps in between the precise pixel locations. This would mean having much smoother movement than using regular integers, but in a way that’s cheaper than floating points in terms of both code use and processing power.&lt;br /&gt;
&lt;br /&gt;
=== &#039;&#039;&#039;Fixed Points in Action&#039;&#039;&#039; ===&lt;br /&gt;
In the examples below, I am using @Pharap’s [https://community.arduboy.com/t/a-fixed-point-arithmetic-library/4129 FixedPoints] library which defines a number of signed and unsigned data types which are listed at the end of this article. It also includes overrides for the common arithmetic and comparison operators (+, -, / , *, &amp;lt;, &amp;gt;, etc) as well as some handy functions like &amp;lt;code&amp;gt;ceilFixed()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;floorFixed()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;roundFixed()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Firstly, some simple assignments and calculations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar = 7.0;&lt;br /&gt;
myVar = myVar + 3;&lt;br /&gt;
&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 4);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
static_cast&amp;lt;float&amp;gt;(myVar) : 10.0000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Please note that an extra ‘level of precision’ argument is being used in the print code here. Without supplying this extra argument, the printed results suffer from rounding errors made by the &amp;lt;code&amp;gt;Serial.print()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Serial.println()&amp;lt;/code&amp;gt; functions to simplify the code for printing floats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Simple. Now let’s introduce a decimal place.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar = 3.7;&lt;br /&gt;
Serial.print(“myVar.getInteger() : “);&lt;br /&gt;
Serial.println(myVar.getInteger());&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 8);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myVar.getInteger() : 3&lt;br /&gt;
static_cast&amp;lt;float&amp;gt;(myVar) :  3.69921875&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But hang on, we set the variable to 3.7 and it printed out 3.69921875. Why so? As mentioned earlier, both &#039;&#039;&#039;floating point&#039;&#039;&#039; and &#039;&#039;&#039;fixed point&#039;&#039;&#039; numbers introduce a degree of inaccuracy when storing numbers. With Fixed Point numbers, the precision (and by extension the accuracy) is specified by the programmer.&lt;br /&gt;
&lt;br /&gt;
In the sample code above, the variable chosen was a two-byte incarnation and it  can store a number roughly between -128 and +128. The integer component is stored in 7 bits which allows a range of 0 to 127 and the fractional part is stored in 8 bits and has a range between 0 / 256 to 255 / 256. The largest fractional portion (255 / 256) converted to decimal is 0.9961 hence the data range cannot quite store +128, but it can store up to 127.9961.&lt;br /&gt;
&lt;br /&gt;
If you could view the bits in the fractional part of the variable (which you can through &#039;&#039;&#039;myVar.getFraction()&#039;&#039;&#039;), you would see that it is stored as 179. As it turns out 179 / 256 is equal to 0.69921875.&lt;br /&gt;
&lt;br /&gt;
The inaccuracy introduced by a relatively small (two-byte) &#039;&#039;&#039;fixed point&#039;&#039;&#039; is irrelevant if all you are trying to do is simulate some realistic movements of characters in a game. If you have a need to calculate more accurate values, you can simply increase the size of the &#039;&#039;&#039;fixed point&#039;&#039;&#039; variable to either an SQ15x16 or an SQ31x32 (though be aware that some operations do not work on SQ31x32 due to presently unavoidable restrictions). The code below illustrates the accuracy of each data type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar7x8 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar7x8), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar7x8 = myVar7x8 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
Serial.println();&lt;br /&gt;
 &lt;br /&gt;
SQ15x16 myVar15x16 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar15x16), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar15x16 = myVar15x16 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
Serial.println();&lt;br /&gt;
 &lt;br /&gt;
SQ31x32 myVar31x32 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar31x32), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar31x32 = myVar31x32 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
Serial.println();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 4.00000000 4.09765625 4.19531250 4.29296875 4.39062500 4.48828125 4.58593750 4.68359375 4.78125000 4.87890625 4.97656250&lt;br /&gt;
 4.00000000 4.09999084 4.19998168 4.29997253 4.39996337 4.49995422 4.59994506 4.69993591 4.79992675 4.89991760 4.99990844 &lt;br /&gt;
 4.00000000 4.09999990 4.19999980 4.30000019 4.40000009 4.50000000 4.59999990 4.69999980 4.80000019 4.90000009 5.00000000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One thing to note with the output is the compounding effect that adding two inaccurate numbers results in. This can be seen easily if we repeat the same exercise on the smallest, one-byte data type as shown below. The fractional part of an &amp;lt;code&amp;gt;SQ3x4&amp;lt;/code&amp;gt; number is stored in 1/16th increments or 0.0625 in decimal.&lt;br /&gt;
&lt;br /&gt;
When we add the first increment of 0.1  we immediately introduce an inaccuracy as the number gets rounded down to the nearest 1/16th of a number. As we repeat the addition, the error increases. The following code shows that.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ3x4 myVar3x4 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar3x4), 4);&lt;br /&gt;
  Serial.print(“ “);&lt;br /&gt;
  myVar3x4 = myVar3x4 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 4.0000 4.0625 4.1250 4.1875 4.2500 4.3125 4.3750 4.4375 4.5000 4.5625 4.6250&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choosing larger &#039;&#039;&#039;fixed point&#039;&#039;&#039; data types increases the range of the number and accuracy of the fractional component at the expense of memory.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Why would I bother with Fixed Points at all?&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
Imagine you are building a sideways-scrolling game where aliens (or cars, planes or other moving objects) are moving towards you at various speeds. With each pass through the program’s loop you could decrement the x coordinate of each alien but this would result in all aliens moving at the same speed. You could try to move aliens every second or third time through the loop by keeping some sort of counter but this can be error prone and&lt;br /&gt;
&lt;br /&gt;
may result in the aliens moving slowly. To really allow flexibility in the aliens’ movement, you could store the x and y in fixed point variables and increment these by a decimal value allowing the aliens to move at similar but slightly different speeds.&lt;br /&gt;
&lt;br /&gt;
Consider the Alien class below. Each alien is described with an x and y coordinate and takes care of its own movement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Alien {&lt;br /&gt;
 &lt;br /&gt;
   public:&lt;br /&gt;
 &lt;br /&gt;
     Alien(SQ7x8 x, SQ7x8 y, SQ7x8 incX, SQ7x8 incY, const uint8_t *bitmap) {&lt;br /&gt;
&lt;br /&gt;
       _x = x;&lt;br /&gt;
       _y = y;&lt;br /&gt;
       _incX = incX;&lt;br /&gt;
       _incY = incY;&lt;br /&gt;
       _bitmap = bitmap; &lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 getX() const { return _x; }&lt;br /&gt;
     SQ7x8 getY() const { return _y; }&lt;br /&gt;
 &lt;br /&gt;
     void setX(const SQ7x8 value) { _x = value; }&lt;br /&gt;
     void setY(const SQ7x8 value) { _y = value; }&lt;br /&gt;
 &lt;br /&gt;
     inline void Alien::move(uint8_t frame) {&lt;br /&gt;
 &lt;br /&gt;
       _x = _x - _incX;&lt;br /&gt;
       _y = _y - _incY;&lt;br /&gt;
       Sprites::drawOverwrite(_x.getInteger(), _y.getInteger(), _bitmap, frame);&lt;br /&gt;
   } &lt;br /&gt;
 &lt;br /&gt;
   private:&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 _x;&lt;br /&gt;
     SQ7x8 _y;&lt;br /&gt;
     SQ7x8 _incX;&lt;br /&gt;
     SQ7x8 _incY;&lt;br /&gt;
     const uint8_t *_bitmap;&lt;br /&gt;
   &lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And our main .ino file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Alien alien1 = {127, 16, (SQ7x8)0.9, (SQ7x8)1.34, alienBitmap };&lt;br /&gt;
Alien alien2 = {127, 32, (SQ7x8)1.7, (SQ7x8)1.76, alienBitmap };&lt;br /&gt;
Alien alien3 = {127, 48, (SQ7x8)1.5, (SQ7x8)2.13, alienBitmap }; &lt;br /&gt;
 &lt;br /&gt;
void loop() {&lt;br /&gt;
  alien1.move(frame);&lt;br /&gt;
  alien2.move(frame);&lt;br /&gt;
  alien3.move(frame);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
With fixed point data types, we can have our aliens move at slightly different speeds without the overhead of using floating points.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Common Type Aliases&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
@Pharap’s library includes the following data types. However the library is implemented with templates and it is a relatively simple matter to declare your own type aliases.&lt;br /&gt;
&lt;br /&gt;
The predeclared aliases (which can be  used as an example of how to declare such an alias) can be found in &#039;&#039;&#039;src\ FixedPointsCommon\SFixedCommon.h and src\ FixedPointsCommon\UfixedCommon.h.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Unsigned Data Type Aliases:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|-&lt;br /&gt;
|UQ4x4&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 15&lt;br /&gt;
|1 / 16th&lt;br /&gt;
|0 to 16&lt;br /&gt;
|-&lt;br /&gt;
|UQ8x8&lt;br /&gt;
|16 bit (2 bytes)&lt;br /&gt;
|0 to 255&lt;br /&gt;
|1 / 256th&lt;br /&gt;
|0 to 256&lt;br /&gt;
|-&lt;br /&gt;
|UQ16x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|0 to 65,355&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|0 to 65,356&lt;br /&gt;
|-&lt;br /&gt;
|UQ32x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|0 to 4,294,967,295&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|0 to 4,294,967,296&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x7&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 128th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x15&lt;br /&gt;
|16 bit (2 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 32,768th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x31&lt;br /&gt;
|32 bit (4 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 2,147,483,648th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x63&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1/ 9,223,372,036,854,775,808th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Signed Data Type Aliases:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|-&lt;br /&gt;
|SQ3x4&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|-15 to +15&lt;br /&gt;
|1 / 16th&lt;br /&gt;
|-16 to +16&lt;br /&gt;
|-&lt;br /&gt;
|SQ7x8&lt;br /&gt;
|16 bit (2 bytes)&lt;br /&gt;
|-127 to +127&lt;br /&gt;
|1 / 256th&lt;br /&gt;
|-128 to +128&lt;br /&gt;
|-&lt;br /&gt;
|SQ15x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|-32,767 to +32,767&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|-32,768 to +32,768&lt;br /&gt;
|-&lt;br /&gt;
|SQ31x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|-2,147,483,647 to +2,147,483,647&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|2,147,483,648 to +2,147,483,648&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x6&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|-1 to +1&lt;br /&gt;
|1 / 64th&lt;br /&gt;
|-2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x14&lt;br /&gt;
|16 bit (2 byte)&lt;br /&gt;
|-1 to +1&lt;br /&gt;
|1 / 16,384th&lt;br /&gt;
|-2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x30&lt;br /&gt;
|32 bit (4 byte)&lt;br /&gt;
|-1 to +1&lt;br /&gt;
|1 / 1,073,741,824th&lt;br /&gt;
|-2 to +2&lt;br /&gt;
|-&lt;br /&gt;
|SQ1x62&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|-1 to +1&lt;br /&gt;
|1/ 4,611,686,018,427,387,904th&lt;br /&gt;
|-2 to +2&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt; Lower bound is inclusive, upper bound is exclusive&lt;br /&gt;
&lt;br /&gt;
FixedPoints can be found through the Arduino IDE library manager. If you have not used the library manager before see here: &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;https://arduboy.com/download-and-learn-arduino/&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=594</id>
		<title>Fixed Point</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=594"/>
		<updated>2024-08-16T10:04:55Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
Most of us are familiar with the integer data types used by the Arduboy environment – of various lengths, both signed and unsigned. We take care to choose appropriate data types that consider both the range of our data as well as the memory required to store them. Operations involving integer values are incredibly efficient and for many sketches are all that are required.&lt;br /&gt;
&lt;br /&gt;
Sometimes however, integer values are simply not flexible enough. Imagine trying to calculate the movement of an accelerating rocket or calculate the trajectory of a rocket or a missile in an Arduboy game. To make these calculations, you need numbers with both an integer and fractional component. The typical way to represent such numbers on a computer is through floating points.&lt;br /&gt;
&lt;br /&gt;
C++ includes two floating point data types of different precision – the float and the double. On most platforms the float is 4 bytes and the double is 8 bytes, however on the Arduino (and thus the Arduboy) they are actually same precision (4 bytes) and are therefore interchangeable. These data types can store numbers in the range of -3.4028235 x 1038 to +3. 4028235 x 1038 (for comparison there is estimated to be 7.5 x 1018 grains of sand in the world) but at a precision of only 6 or 7 decimal digits.&lt;br /&gt;
&lt;br /&gt;
For the Arduboy, in contrast to integer operations, floating point calculations are internally quite complicated and thus incredibly processor intensive. Including numerous calculations to reposition the player and enemies within the main loop of a program might prevent the game from performing well or worse yet even being playable.&lt;br /&gt;
&lt;br /&gt;
However, there is an alternative to &#039;&#039;&#039;floating point&#039;&#039;&#039; arithmetic. This alternative is &#039;&#039;&#039;fixed point&#039;&#039;&#039; arithmetic. Fixed point numbers behave more similarly to the integer values you are familiar with. As such, the processing power required to manipulate fixed point numbers is more similar to integer calculations than to floating point calculations. &lt;br /&gt;
&lt;br /&gt;
There is however a tradeoff for the benefit that fixed point data types bring. The tradeoff is that you must explicitly nominate the size  of both the integer and fractional portion of the variable – much in the same way you nominate the available range when choosing integer data types. In addition to the obvious data range restrictions of the integer component, the resolution (or ‘granularity’)  of the fractional component must also be chosen.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 8 bit fixed point type with a 4 bit integer part and a 4 bit fractional part would permit an integer range of 0 to 15 and a fractional range of 0/16 to 15/16. This equates to a range of 0 to 15.9375 (15/16 is equivalent to 0.9375).&lt;br /&gt;
&lt;br /&gt;
Note that the fractional part can only go up in increments of 1/16 (i.e. 0.0625), hence that particular format cannot accurately represent numbers at a lower resolution, for example the value 0.05 could not be accurately represented, it would have to be rounded to 0.0625. However, by using larger types or by increasing the number of bits afforded to the fractional part at the expense of the integer part you can balance the format to suit your needs.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 16 bit type with 8 bits of integer part and 8 bits of fractional part could represent all the positions on the Arduboy’s screen, whilst providing a reasonable range of fractional steps in between the precise pixel locations. This would mean having much smoother movement than using regular integers, but in a way that’s cheaper than floating points in terms of both code use and processing power.&lt;br /&gt;
&lt;br /&gt;
=== &#039;&#039;&#039;Fixed Points in Action&#039;&#039;&#039; ===&lt;br /&gt;
In the examples below, I am using @Pharap’s [https://community.arduboy.com/t/a-fixed-point-arithmetic-library/4129 FixedPoints] library which defines a number of signed and unsigned data types which are listed at the end of this article. It also includes overrides for the common arithmetic and comparison operators (+, -, / , *, &amp;lt;, &amp;gt;, etc) as well as some handy functions like &amp;lt;code&amp;gt;ceilFixed()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;floorFixed()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;roundFixed()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Firstly, some simple assignments and calculations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar = 7.0;&lt;br /&gt;
myVar = myVar + 3;&lt;br /&gt;
&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 4);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
static_cast&amp;lt;float&amp;gt;(myVar) : 10.0000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Please note that an extra ‘level of precision’ argument is being used in the print code here. Without supplying this extra argument, the printed results suffer from rounding errors made by the &amp;lt;code&amp;gt;Serial.print()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Serial.println()&amp;lt;/code&amp;gt; functions to simplify the code for printing floats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Simple. Now let’s introduce a decimal place.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar = 3.7;&lt;br /&gt;
Serial.print(“myVar.getInteger() : “);&lt;br /&gt;
Serial.println(myVar.getInteger());&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 8);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myVar.getInteger() : 3&lt;br /&gt;
static_cast&amp;lt;float&amp;gt;(myVar) :  3.69921875&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But hang on, we set the variable to 3.7 and it printed out 3.69921875. Why so? As mentioned earlier, both &#039;&#039;&#039;floating point&#039;&#039;&#039; and &#039;&#039;&#039;fixed point&#039;&#039;&#039; numbers introduce a degree of inaccuracy when storing numbers. With Fixed Point numbers, the precision (and by extension the accuracy) is specified by the programmer.&lt;br /&gt;
&lt;br /&gt;
In the sample code above, the variable chosen was a two-byte incarnation and it  can store a number roughly between -128 and +128. The integer component is stored in 7 bits which allows a range of 0 to 127 and the fractional part is stored in 8 bits and has a range between 0 / 256 to 255 / 256. The largest fractional portion (255 / 256) converted to decimal is 0.9961 hence the data range cannot quite store +128, but it can store up to 127.9961.&lt;br /&gt;
&lt;br /&gt;
If you could view the bits in the fractional part of the variable (which you can through &#039;&#039;&#039;myVar.getFraction()&#039;&#039;&#039;), you would see that it is stored as 179. As it turns out 179 / 256 is equal to 0.69921875.&lt;br /&gt;
&lt;br /&gt;
The inaccuracy introduced by a relatively small (two-byte) &#039;&#039;&#039;fixed point&#039;&#039;&#039; is irrelevant if all you are trying to do is simulate some realistic movements of characters in a game. If you have a need to calculate more accurate values, you can simply increase the size of the &#039;&#039;&#039;fixed point&#039;&#039;&#039; variable to either an SQ15x16 or an SQ31x32 (though be aware that some operations do not work on SQ31x32 due to presently unavoidable restrictions). The code below illustrates the accuracy of each data type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar7x8 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar7x8), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar7x8 = myVar7x8 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
Serial.println();&lt;br /&gt;
 &lt;br /&gt;
SQ15x16 myVar15x16 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar15x16), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar15x16 = myVar15x16 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
Serial.println();&lt;br /&gt;
 &lt;br /&gt;
SQ31x32 myVar31x32 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar31x32), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar31x32 = myVar31x32 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
Serial.println();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 4.00000000 4.09765625 4.19531250 4.29296875 4.39062500 4.48828125 4.58593750 4.68359375 4.78125000 4.87890625 4.97656250&lt;br /&gt;
 4.00000000 4.09999084 4.19998168 4.29997253 4.39996337 4.49995422 4.59994506 4.69993591 4.79992675 4.89991760 4.99990844 &lt;br /&gt;
 4.00000000 4.09999990 4.19999980 4.30000019 4.40000009 4.50000000 4.59999990 4.69999980 4.80000019 4.90000009 5.00000000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One thing to note with the output is the compounding effect that adding two inaccurate numbers results in. This can be seen easily if we repeat the same exercise on the smallest, one-byte data type as shown below. The fractional part of an &amp;lt;code&amp;gt;SQ3x4&amp;lt;/code&amp;gt; number is stored in 1/16th increments or 0.0625 in decimal.&lt;br /&gt;
&lt;br /&gt;
When we add the first increment of 0.1  we immediately introduce an inaccuracy as the number gets rounded down to the nearest 1/16th of a number. As we repeat the addition, the error increases. The following code shows that.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ3x4 myVar3x4 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar3x4), 4);&lt;br /&gt;
  Serial.print(“ “);&lt;br /&gt;
  myVar3x4 = myVar3x4 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 4.0000 4.0625 4.1250 4.1875 4.2500 4.3125 4.3750 4.4375 4.5000 4.5625 4.6250&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choosing larger &#039;&#039;&#039;fixed point&#039;&#039;&#039; data types increases the range of the number and accuracy of the fractional component at the expense of memory.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Why would I bother with Fixed Points at all?&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
Imagine you are building a sideways-scrolling game where aliens (or cars, planes or other moving objects) are moving towards you at various speeds. With each pass through the program’s loop you could decrement the x coordinate of each alien but this would result in all aliens moving at the same speed. You could try to move aliens every second or third time through the loop by keeping some sort of counter but this can be error prone and&lt;br /&gt;
&lt;br /&gt;
may result in the aliens moving slowly. To really allow flexibility in the aliens’ movement, you could store the x and y in fixed point variables and increment these by a decimal value allowing the aliens to move at similar but slightly different speeds.&lt;br /&gt;
&lt;br /&gt;
Consider the Alien class below. Each alien is described with an x and y coordinate and takes care of its own movement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Alien {&lt;br /&gt;
 &lt;br /&gt;
   public:&lt;br /&gt;
 &lt;br /&gt;
     Alien(SQ7x8 x, SQ7x8 y, SQ7x8 incX, SQ7x8 incY, const uint8_t *bitmap) {&lt;br /&gt;
&lt;br /&gt;
       _x = x;&lt;br /&gt;
       _y = y;&lt;br /&gt;
       _incX = incX;&lt;br /&gt;
       _incY = incY;&lt;br /&gt;
       _bitmap = bitmap; &lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 getX() const { return _x; }&lt;br /&gt;
     SQ7x8 getY() const { return _y; }&lt;br /&gt;
 &lt;br /&gt;
     void setX(const SQ7x8 value) { _x = value; }&lt;br /&gt;
     void setY(const SQ7x8 value) { _y = value; }&lt;br /&gt;
 &lt;br /&gt;
     inline void Alien::move(uint8_t frame) {&lt;br /&gt;
 &lt;br /&gt;
       _x = _x - _incX;&lt;br /&gt;
       _y = _y - _incY;&lt;br /&gt;
       Sprites::drawOverwrite(_x.getInteger(), _y.getInteger(), _bitmap, frame);&lt;br /&gt;
   } &lt;br /&gt;
 &lt;br /&gt;
   private:&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 _x;&lt;br /&gt;
     SQ7x8 _y;&lt;br /&gt;
     SQ7x8 _incX;&lt;br /&gt;
     SQ7x8 _incY;&lt;br /&gt;
     const uint8_t *_bitmap;&lt;br /&gt;
   &lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And our main .ino file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Alien alien1 = {127, 16, (SQ7x8)0.9, (SQ7x8)1.34, alienBitmap };&lt;br /&gt;
Alien alien2 = {127, 32, (SQ7x8)1.7, (SQ7x8)1.76, alienBitmap };&lt;br /&gt;
Alien alien3 = {127, 48, (SQ7x8)1.5, (SQ7x8)2.13, alienBitmap }; &lt;br /&gt;
 &lt;br /&gt;
void loop() {&lt;br /&gt;
  alien1.move(frame);&lt;br /&gt;
  alien2.move(frame);&lt;br /&gt;
  alien3.move(frame);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
With fixed point data types, we can have our aliens move at slightly different speeds without the overhead of using floating points.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Common Type Aliases&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
@Pharap’s library includes the following data types. However the library is implemented with templates and it is a relatively simple matter to declare your own type aliases.&lt;br /&gt;
&lt;br /&gt;
The predeclared aliases (which can be  used as an example of how to declare such an alias) can be found in &#039;&#039;&#039;src\ FixedPointsCommon\SFixedCommon.h and src\ FixedPointsCommon\UfixedCommon.h.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Unsigned Data Type Aliases:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|-&lt;br /&gt;
|UQ4x4&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 15&lt;br /&gt;
|1 / 16th&lt;br /&gt;
|0 to 16&lt;br /&gt;
|-&lt;br /&gt;
|UQ8x8&lt;br /&gt;
|16 bit (2 bytes)&lt;br /&gt;
|0 to 255&lt;br /&gt;
|1 / 256th&lt;br /&gt;
|0 to 256&lt;br /&gt;
|-&lt;br /&gt;
|UQ16x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|0 to 65,355&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|0 to 65,356&lt;br /&gt;
|-&lt;br /&gt;
|UQ32x32&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|0 to 65,355&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|0 to 65,356&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|UQ1x7&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 128th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x15&lt;br /&gt;
|16 bit (2 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 32,768th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x31&lt;br /&gt;
|32 bit (4 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 2,147,483,648th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x63&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1/ 9,223,372,036,854,775,808th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-15 to +15&lt;br /&gt;
&lt;br /&gt;
-127 to +127&lt;br /&gt;
&lt;br /&gt;
-1 to +1&lt;br /&gt;
&lt;br /&gt;
-1 to +1&lt;br /&gt;
&lt;br /&gt;
-1 to +1&lt;br /&gt;
&lt;br /&gt;
1 / 16th&lt;br /&gt;
&lt;br /&gt;
1 / 256th&lt;br /&gt;
&lt;br /&gt;
1 / 65,536th 1 / 128th&lt;br /&gt;
&lt;br /&gt;
1 / 32,768th&lt;br /&gt;
&lt;br /&gt;
1 / 2,147,483,648th&lt;br /&gt;
&lt;br /&gt;
1 / 16th&lt;br /&gt;
&lt;br /&gt;
1 / 256th&lt;br /&gt;
&lt;br /&gt;
1 / 64th&lt;br /&gt;
&lt;br /&gt;
1 / 16,384th&lt;br /&gt;
&lt;br /&gt;
1 / 1,073,741,824th&lt;br /&gt;
&lt;br /&gt;
0 to 16&lt;br /&gt;
&lt;br /&gt;
0 to 256&lt;br /&gt;
&lt;br /&gt;
0 to 65,356&lt;br /&gt;
&lt;br /&gt;
0 to 1&lt;br /&gt;
&lt;br /&gt;
0 to 1&lt;br /&gt;
&lt;br /&gt;
0 to 1&lt;br /&gt;
&lt;br /&gt;
-16 to +16&lt;br /&gt;
&lt;br /&gt;
-128 to +128&lt;br /&gt;
&lt;br /&gt;
-2 to +2&lt;br /&gt;
&lt;br /&gt;
-2 to +2&lt;br /&gt;
&lt;br /&gt;
-2 to +2&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|UQ32x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|0 to 4,294,967,295&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|0 to 4,294,967,296&lt;br /&gt;
|}&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|UQ1x63&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1/ 9,223,372,036,854,775,808th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|}&lt;br /&gt;
Signed Data Type Aliases:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|}&lt;br /&gt;
SQ3x4&lt;br /&gt;
&lt;br /&gt;
SQ7x8&lt;br /&gt;
&lt;br /&gt;
SQ1x6&lt;br /&gt;
&lt;br /&gt;
SQ1x14&lt;br /&gt;
&lt;br /&gt;
SQ1x30&lt;br /&gt;
&lt;br /&gt;
8 bit (1 byte)&lt;br /&gt;
&lt;br /&gt;
16 bit (2 bytes)&lt;br /&gt;
&lt;br /&gt;
8 bit (1 byte)&lt;br /&gt;
&lt;br /&gt;
16 bit (2 byte)&lt;br /&gt;
&lt;br /&gt;
32 bit (4 byte)&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|SQ15x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-32,767 to +32,767&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-32,768 to +32,768&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|SQ31x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-2,147,483,647 to +2,147,483,647&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|2,147,483,648 to +2,147,483,648&lt;br /&gt;
|}&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|SQ1x62&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-1 to +1&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
|1/ 4,611,686,018,427,387,904th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-2 to +2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt; Lower bound is inclusive, upper bound is exclusive&lt;br /&gt;
&lt;br /&gt;
FixedPoints can be found through the Arduino IDE library manager. If you have not used thelibrary manager before see here: &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;https://arduboy.com/download-and-learn-arduino/&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=593</id>
		<title>Fixed Point</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=593"/>
		<updated>2024-08-16T10:03:03Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* Fixed Points in Action */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
Most of us are familiar with the integer data types used by the Arduboy environment – of various lengths, both signed and unsigned. We take care to choose appropriate data types that consider both the range of our data as well as the memory required to store them. Operations involving integer values are incredibly efficient and for many sketches are all that are required.&lt;br /&gt;
&lt;br /&gt;
Sometimes however, integer values are simply not flexible enough. Imagine trying to calculate the movement of an accelerating rocket or calculate the trajectory of a rocket or a missile in an Arduboy game. To make these calculations, you need numbers with both an integer and fractional component. The typical way to represent such numbers on a computer is through floating points.&lt;br /&gt;
&lt;br /&gt;
C++ includes two floating point data types of different precision – the float and the double. On most platforms the float is 4 bytes and the double is 8 bytes, however on the Arduino (and thus the Arduboy) they are actually same precision (4 bytes) and are therefore interchangeable. These data types can store numbers in the range of -3.4028235 x 1038 to +3. 4028235 x 1038 (for comparison there is estimated to be 7.5 x 1018 grains of sand in the world) but at a precision of only 6 or 7 decimal digits.&lt;br /&gt;
&lt;br /&gt;
For the Arduboy, in contrast to integer operations, floating point calculations are internally quite complicated and thus incredibly processor intensive. Including numerous calculations to reposition the player and enemies within the main loop of a program might prevent the game from performing well or worse yet even being playable.&lt;br /&gt;
&lt;br /&gt;
However, there is an alternative to &#039;&#039;&#039;floating point&#039;&#039;&#039; arithmetic. This alternative is &#039;&#039;&#039;fixed point&#039;&#039;&#039; arithmetic. Fixed point numbers behave more similarly to the integer values you are familiar with. As such, the processing power required to manipulate fixed point numbers is more similar to integer calculations than to floating point calculations. &lt;br /&gt;
&lt;br /&gt;
There is however a tradeoff for the benefit that fixed point data types bring. The tradeoff is that you must explicitly nominate the size  of both the integer and fractional portion of the variable – much in the same way you nominate the available range when choosing integer data types. In addition to the obvious data range restrictions of the integer component, the resolution (or ‘granularity’)  of the fractional component must also be chosen.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 8 bit fixed point type with a 4 bit integer part and a 4 bit fractional part would permit an integer range of 0 to 15 and a fractional range of 0/16 to 15/16. This equates to a range of 0 to 15.9375 (15/16 is equivalent to 0.9375).&lt;br /&gt;
&lt;br /&gt;
Note that the fractional part can only go up in increments of 1/16 (i.e. 0.0625), hence that particular format cannot accurately represent numbers at a lower resolution, for example the value 0.05 could not be accurately represented, it would have to be rounded to 0.0625. However, by using larger types or by increasing the number of bits afforded to the fractional part at the expense of the integer part you can balance the format to suit your needs.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 16 bit type with 8 bits of integer part and 8 bits of fractional part could represent all the positions on the Arduboy’s screen, whilst providing a reasonable range of fractional steps in between the precise pixel locations. This would mean having much smoother movement than using regular integers, but in a way that’s cheaper than floating points in terms of both code use and processing power.&lt;br /&gt;
&lt;br /&gt;
=== &#039;&#039;&#039;Fixed Points in Action&#039;&#039;&#039; ===&lt;br /&gt;
In the examples below, I am using @Pharap’s [https://community.arduboy.com/t/a-fixed-point-arithmetic-library/4129 FixedPoints] library which defines a number of signed and unsigned data types which are listed at the end of this article. It also includes overrides for the common arithmetic and comparison operators (+, -, / , *, &amp;lt;, &amp;gt;, etc) as well as some handy functions like &amp;lt;code&amp;gt;ceilFixed()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;floorFixed()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;roundFixed()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Firstly, some simple assignments and calculations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar = 7.0;&lt;br /&gt;
myVar = myVar + 3;&lt;br /&gt;
&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 4);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
static_cast&amp;lt;float&amp;gt;(myVar) : 10.0000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Please note that an extra ‘level of precision’ argument is being used in the print code here. Without supplying this extra argument, the printed results suffer from rounding errors made by the &amp;lt;code&amp;gt;Serial.print()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Serial.println()&amp;lt;/code&amp;gt; functions to simplify the code for printing floats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Simple. Now let’s introduce a decimal place.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar = 3.7;&lt;br /&gt;
Serial.print(“myVar.getInteger() : “);&lt;br /&gt;
Serial.println(myVar.getInteger());&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 8);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myVar.getInteger() : 3&lt;br /&gt;
static_cast&amp;lt;float&amp;gt;(myVar) :  3.69921875&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But hang on, we set the variable to 3.7 and it printed out 3.69921875. Why so? As mentioned earlier, both &#039;&#039;&#039;floating point&#039;&#039;&#039; and &#039;&#039;&#039;fixed point&#039;&#039;&#039; numbers introduce a degree of inaccuracy when storing numbers. With Fixed Point numbers, the precision (and by extension the accuracy) is specified by the programmer.&lt;br /&gt;
&lt;br /&gt;
In the sample code above, the variable chosen was a two-byte incarnation and it  can store a number roughly between -128 and +128. The integer component is stored in 7 bits which allows a range of 0 to 127 and the fractional part is stored in 8 bits and has a range between 0 / 256 to 255 / 256. The largest fractional portion (255 / 256) converted to decimal is 0.9961 hence the data range cannot quite store +128, but it can store up to 127.9961.&lt;br /&gt;
&lt;br /&gt;
If you could view the bits in the fractional part of the variable (which you can through &#039;&#039;&#039;myVar.getFraction()&#039;&#039;&#039;), you would see that it is stored as 179. As it turns out 179 / 256 is equal to 0.69921875.&lt;br /&gt;
&lt;br /&gt;
The inaccuracy introduced by a relatively small (two-byte) &#039;&#039;&#039;fixed point&#039;&#039;&#039; is irrelevant if all you are trying to do is simulate some realistic movements of characters in a game. If you have a need to calculate more accurate values, you can simply increase the size of the &#039;&#039;&#039;fixed point&#039;&#039;&#039; variable to either an SQ15x16 or an SQ31x32 (though be aware that some operations do not work on SQ31x32 due to presently unavoidable restrictions). The code below illustrates the accuracy of each data type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ7x8 myVar7x8 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar7x8), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar7x8 = myVar7x8 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
Serial.println();&lt;br /&gt;
 &lt;br /&gt;
SQ15x16 myVar15x16 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar15x16), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar15x16 = myVar15x16 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
Serial.println();&lt;br /&gt;
 &lt;br /&gt;
SQ31x32 myVar31x32 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar31x32), 8);&lt;br /&gt;
  Serial.print(&#039; &#039;);&lt;br /&gt;
  myVar31x32 = myVar31x32 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
Serial.println();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 4.00000000 4.09765625 4.19531250 4.29296875 4.39062500 4.48828125 4.58593750 4.68359375&lt;br /&gt;
 4.78125000 4.87890625 4.97656250&lt;br /&gt;
 4.00000000 4.09999084 4.19998168 4.29997253 4.39996337 4.49995422 4.59994506 4.69993591&lt;br /&gt;
 4.79992675 4.89991760 4.99990844 &lt;br /&gt;
 4.00000000 4.09999990 4.19999980 4.30000019 4.40000009 4.50000000 4.59999990 4.69999980&lt;br /&gt;
 4.80000019 4.90000009 5.00000000&lt;br /&gt;
 4.00000000 4.09999990 4.19999980 4.30000019 4.40000009 4.50000000 4.59999990 4.69999980&lt;br /&gt;
 4.80000019 4.90000009 5.00000000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One thing to note with the output is the compounding effect that adding two inaccurate numbers results in. This can be seen easily if we repeat the same exercise on the smallest, one-byte data type as shown below. The fractional part of an &amp;lt;code&amp;gt;SQ3x4&amp;lt;/code&amp;gt; number is stored in 1/16th increments or 0.0625 in decimal.&lt;br /&gt;
&lt;br /&gt;
When we add the first increment of 0.1  we immediately introduce an inaccuracy as the number gets rounded down to the nearest 1/16th of a number. As we repeat the addition, the error increases. The following code shows that.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQ3x4 myVar3x4 = 4;&lt;br /&gt;
 &lt;br /&gt;
for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
  Serial.print(static_cast&amp;lt;float&amp;gt;(myVar3x4), 4);&lt;br /&gt;
  Serial.print(“ “);&lt;br /&gt;
  myVar3x4 = myVar3x4 + 0.1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 4.0000 4.0625 4.1250 4.1875 4.2500 4.3125 4.3750 4.4375 4.5000 4.5625 4.6250&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Choosing larger &#039;&#039;&#039;fixed point&#039;&#039;&#039; data types increases the range of the number and accuracy of the fractional component at the expense of memory.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Why would I bother with Fixed Points at all?&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
Imagine you are building a sideways-scrolling game where aliens (or cars, planes or other moving objects) are moving towards you at various speeds. With each pass through the program’s loop you could decrement the x coordinate of each alien but this would result in all aliens moving at the same speed. You could try to move aliens every second or third time through the loop by keeping some sort of counter but this can be error prone and&lt;br /&gt;
&lt;br /&gt;
may result in the aliens moving slowly. To really allow flexibility in the aliens’ movement, you could store the x and y in fixed point variables and increment these by a decimal value allowing the aliens to move at similar but slightly different speeds.&lt;br /&gt;
&lt;br /&gt;
Consider the Alien class below. Each alien is described with an x and y coordinate and takes care of its own movement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Alien {&lt;br /&gt;
 &lt;br /&gt;
   public:&lt;br /&gt;
 &lt;br /&gt;
     Alien(SQ7x8 x, SQ7x8 y, SQ7x8 incX, SQ7x8 incY, const uint8_t *bitmap) {&lt;br /&gt;
&lt;br /&gt;
       _x = x;&lt;br /&gt;
       _y = y;&lt;br /&gt;
       _incX = incX;&lt;br /&gt;
       _incY = incY;&lt;br /&gt;
       _bitmap = bitmap; &lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 getX() const { return _x; }&lt;br /&gt;
     SQ7x8 getY() const { return _y; }&lt;br /&gt;
 &lt;br /&gt;
     void setX(const SQ7x8 value) { _x = value; }&lt;br /&gt;
     void setY(const SQ7x8 value) { _y = value; }&lt;br /&gt;
 &lt;br /&gt;
     inline void Alien::move(uint8_t frame) {&lt;br /&gt;
 &lt;br /&gt;
       _x = _x - _incX;&lt;br /&gt;
       _y = _y - _incY;&lt;br /&gt;
       Sprites::drawOverwrite(_x.getInteger(), _y.getInteger(), _bitmap, frame);&lt;br /&gt;
   } &lt;br /&gt;
 &lt;br /&gt;
   private:&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 _x;&lt;br /&gt;
     SQ7x8 _y;&lt;br /&gt;
     SQ7x8 _incX;&lt;br /&gt;
     SQ7x8 _incY;&lt;br /&gt;
     const uint8_t *_bitmap;&lt;br /&gt;
   &lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And our main .ino file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Alien alien1 = {127, 16, (SQ7x8)0.9, (SQ7x8)1.34, alienBitmap };&lt;br /&gt;
Alien alien2 = {127, 32, (SQ7x8)1.7, (SQ7x8)1.76, alienBitmap };&lt;br /&gt;
Alien alien3 = {127, 48, (SQ7x8)1.5, (SQ7x8)2.13, alienBitmap }; &lt;br /&gt;
 &lt;br /&gt;
void loop() {&lt;br /&gt;
  alien1.move(frame);&lt;br /&gt;
  alien2.move(frame);&lt;br /&gt;
  alien3.move(frame);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
With fixed point data types, we can have our aliens move at slightly different speeds without the overhead of using floating points.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Common Type Aliases&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
@Pharap’s library includes the following data types. However the library is implemented with templates and it is a relatively simple matter to declare your own type aliases.&lt;br /&gt;
&lt;br /&gt;
The predeclared aliases (which can be  used as an example of how to declare such an alias) can be found in &#039;&#039;&#039;src\ FixedPointsCommon\SFixedCommon.h and src\ FixedPointsCommon\UfixedCommon.h.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Unsigned Data Type Aliases:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|-&lt;br /&gt;
|UQ4x4&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 15&lt;br /&gt;
|1 / 16th&lt;br /&gt;
|0 to 16&lt;br /&gt;
|-&lt;br /&gt;
|UQ8x8&lt;br /&gt;
|16 bit (2 bytes)&lt;br /&gt;
|0 to 255&lt;br /&gt;
|1 / 256th&lt;br /&gt;
|0 to 256&lt;br /&gt;
|-&lt;br /&gt;
|UQ16x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|0 to 65,355&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|0 to 65,356&lt;br /&gt;
|-&lt;br /&gt;
|UQ32x32&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|0 to 65,355&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|0 to 65,356&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|UQ1x7&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 128th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x15&lt;br /&gt;
|16 bit (2 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 32,768th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x31&lt;br /&gt;
|32 bit (4 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 2,147,483,648th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x63&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1/ 9,223,372,036,854,775,808th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-15 to +15&lt;br /&gt;
&lt;br /&gt;
-127 to +127&lt;br /&gt;
&lt;br /&gt;
-1 to +1&lt;br /&gt;
&lt;br /&gt;
-1 to +1&lt;br /&gt;
&lt;br /&gt;
-1 to +1&lt;br /&gt;
&lt;br /&gt;
1 / 16th&lt;br /&gt;
&lt;br /&gt;
1 / 256th&lt;br /&gt;
&lt;br /&gt;
1 / 65,536th 1 / 128th&lt;br /&gt;
&lt;br /&gt;
1 / 32,768th&lt;br /&gt;
&lt;br /&gt;
1 / 2,147,483,648th&lt;br /&gt;
&lt;br /&gt;
1 / 16th&lt;br /&gt;
&lt;br /&gt;
1 / 256th&lt;br /&gt;
&lt;br /&gt;
1 / 64th&lt;br /&gt;
&lt;br /&gt;
1 / 16,384th&lt;br /&gt;
&lt;br /&gt;
1 / 1,073,741,824th&lt;br /&gt;
&lt;br /&gt;
0 to 16&lt;br /&gt;
&lt;br /&gt;
0 to 256&lt;br /&gt;
&lt;br /&gt;
0 to 65,356&lt;br /&gt;
&lt;br /&gt;
0 to 1&lt;br /&gt;
&lt;br /&gt;
0 to 1&lt;br /&gt;
&lt;br /&gt;
0 to 1&lt;br /&gt;
&lt;br /&gt;
-16 to +16&lt;br /&gt;
&lt;br /&gt;
-128 to +128&lt;br /&gt;
&lt;br /&gt;
-2 to +2&lt;br /&gt;
&lt;br /&gt;
-2 to +2&lt;br /&gt;
&lt;br /&gt;
-2 to +2&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|UQ32x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|0 to 4,294,967,295&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|0 to 4,294,967,296&lt;br /&gt;
|}&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|UQ1x63&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1/ 9,223,372,036,854,775,808th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|}&lt;br /&gt;
Signed Data Type Aliases:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|}&lt;br /&gt;
SQ3x4&lt;br /&gt;
&lt;br /&gt;
SQ7x8&lt;br /&gt;
&lt;br /&gt;
SQ1x6&lt;br /&gt;
&lt;br /&gt;
SQ1x14&lt;br /&gt;
&lt;br /&gt;
SQ1x30&lt;br /&gt;
&lt;br /&gt;
8 bit (1 byte)&lt;br /&gt;
&lt;br /&gt;
16 bit (2 bytes)&lt;br /&gt;
&lt;br /&gt;
8 bit (1 byte)&lt;br /&gt;
&lt;br /&gt;
16 bit (2 byte)&lt;br /&gt;
&lt;br /&gt;
32 bit (4 byte)&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|SQ15x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-32,767 to +32,767&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-32,768 to +32,768&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|SQ31x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-2,147,483,647 to +2,147,483,647&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|2,147,483,648 to +2,147,483,648&lt;br /&gt;
|}&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|SQ1x62&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-1 to +1&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
|1/ 4,611,686,018,427,387,904th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-2 to +2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt; Lower bound is inclusive, upper bound is exclusive&lt;br /&gt;
&lt;br /&gt;
FixedPoints can be found through the Arduino IDE library manager. If you have not used thelibrary manager before see here: &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;https://arduboy.com/download-and-learn-arduino/&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=592</id>
		<title>Fixed Point</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Fixed_Point&amp;diff=592"/>
		<updated>2024-08-16T09:57:42Z</updated>

		<summary type="html">&lt;p&gt;Filmote: Created page with &amp;quot;  Most of us are familiar with the integer data types used by the Arduboy environment – of various lengths, both signed and unsigned. We take care to choose appropriate data types that consider both the range of our data as well as the memory required to store them. Operations involving integer values are incredibly efficient and for many sketches are all that are required.  Sometimes however, integer values are simply not flexible enough. Imagine trying to calculate t...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
Most of us are familiar with the integer data types used by the Arduboy environment – of various lengths, both signed and unsigned. We take care to choose appropriate data types that consider both the range of our data as well as the memory required to store them. Operations involving integer values are incredibly efficient and for many sketches are all that are required.&lt;br /&gt;
&lt;br /&gt;
Sometimes however, integer values are simply not flexible enough. Imagine trying to calculate the movement of an accelerating rocket or calculate the trajectory of a rocket or a missile in an Arduboy game. To make these calculations, you need numbers with both an integer and fractional component. The typical way to represent such numbers on a computer is through floating points.&lt;br /&gt;
&lt;br /&gt;
C++ includes two floating point data types of different precision – the float and the double. On most platforms the float is 4 bytes and the double is 8 bytes, however on the Arduino (and thus the Arduboy) they are actually same precision (4 bytes) and are therefore interchangeable. These data types can store numbers in the range of -3.4028235 x 1038 to +3. 4028235 x 1038 (for comparison there is estimated to be 7.5 x 1018 grains of sand in the world) but at a precision of only 6 or 7 decimal digits.&lt;br /&gt;
&lt;br /&gt;
For the Arduboy, in contrast to integer operations, floating point calculations are internally quite complicated and thus incredibly processor intensive. Including numerous calculations to reposition the player and enemies within the main loop of a program might prevent the game from performing well or worse yet even being playable.&lt;br /&gt;
&lt;br /&gt;
However, there is an alternative to &#039;&#039;&#039;floating point&#039;&#039;&#039; arithmetic. This alternative is &#039;&#039;&#039;fixed point&#039;&#039;&#039; arithmetic. Fixed point numbers behave more similarly to the integer values you are familiar with. As such, the processing power required to manipulate fixed point numbers is more similar to integer calculations than to floating point calculations. &lt;br /&gt;
&lt;br /&gt;
There is however a tradeoff for the benefit that fixed point data types bring. The tradeoff is that you must explicitly nominate the size  of both the integer and fractional portion of the variable – much in the same way you nominate the available range when choosing integer data types. In addition to the obvious data range restrictions of the integer component, the resolution (or ‘granularity’)  of the fractional component must also be chosen.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 8 bit fixed point type with a 4 bit integer part and a 4 bit fractional part would permit an integer range of 0 to 15 and a fractional range of 0/16 to 15/16. This equates to a range of 0 to 15.9375 (15/16 is equivalent to 0.9375).&lt;br /&gt;
&lt;br /&gt;
Note that the fractional part can only go up in increments of 1/16 (i.e. 0.0625), hence that particular format cannot accurately represent numbers at a lower resolution, for example the value 0.05 could not be accurately represented, it would have to be rounded to 0.0625. However, by using larger types or by increasing the number of bits afforded to the fractional part at the expense of the integer part you can balance the format to suit your needs.&lt;br /&gt;
&lt;br /&gt;
For example, an unsigned 16 bit type with 8 bits of integer part and 8 bits of fractional part could represent all the positions on the Arduboy’s screen, whilst providing a reasonable range of fractional steps in between the precise pixel locations. This would mean having much smoother movement than using regular integers, but in a way that’s cheaper than floating points in terms of both code use and processing power.&lt;br /&gt;
&lt;br /&gt;
=== &#039;&#039;&#039;Fixed Points in Action&#039;&#039;&#039; ===&lt;br /&gt;
In the examples below, I am using @Pharap’s [https://community.arduboy.com/t/a-fixed-point-arithmetic-library/4129 FixedPoints] library which defines a number of signed and unsigned data types which are listed at the end of this article. It also includes overrides for the common arithmetic and comparison operators (+, -, / , *, &amp;lt;, &amp;gt;, etc) as well as some handy functions like ceilFixed(), floorFixed() and roundFixed().&lt;br /&gt;
&lt;br /&gt;
Firstly, some simple assignments and calculations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SQ7x8 myVar = 7.0;&lt;br /&gt;
&lt;br /&gt;
myVar = myVar + 3;&lt;br /&gt;
&lt;br /&gt;
Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
&lt;br /&gt;
Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 4);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
 static_cast&amp;lt;float&amp;gt;(myVar) : 10.0000&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Please note that an extra ‘level of precision’ argument is being used in the print code here. Without supplying this extra argument, the printed results suffer from rounding errors made by the Serial.print and Serial.println functions to simplify the code for printing floats.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Simple. Now let’s introduce a decimal place.&lt;br /&gt;
 SQ7x8 myVar = 3.7;&lt;br /&gt;
 Serial.print(“myVar.getInteger() : “);&lt;br /&gt;
 Serial.println(myVar.getInteger());&lt;br /&gt;
 Serial.print(“static_cast&amp;lt;float&amp;gt;(myVar) : “);&lt;br /&gt;
 Serial.println(static_cast&amp;lt;float&amp;gt;(myVar), 8);&lt;br /&gt;
Output:&lt;br /&gt;
 myVar.getInteger() : 3&lt;br /&gt;
 static_cast&amp;lt;float&amp;gt;(myVar) :  3.69921875&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
But hang on, we set the variable to 3.7 and it printed out 3.69921875. Why so? As mentioned earlier, both &#039;&#039;&#039;floating point&#039;&#039;&#039; and &#039;&#039;&#039;fixed point&#039;&#039;&#039; numbers introduce a degree of inaccuracy when storing numbers. With Fixed Point numbers, the precision (and by extension the accuracy) is specified by the programmer.&lt;br /&gt;
&lt;br /&gt;
In the sample code above, the variable chosen was a two-byte incarnation and it  can store a number roughly between -128 and +128. The integer component is stored in 7 bits which allows a range of 0 to 127 and the fractional part is stored in 8 bits and has a range between 0 / 256 to 255 / 256. The largest fractional portion (255 / 256) converted to decimal is 0.9961 hence the data range cannot quite store +128, but it can store up to 127.9961.&lt;br /&gt;
&lt;br /&gt;
If you could view the bits in the fractional part of the variable (which you can through &#039;&#039;&#039;myVar.getFraction()&#039;&#039;&#039;), you would see that it is stored as 179. As it turns out 179 / 256 is equal to 0.69921875.&lt;br /&gt;
&lt;br /&gt;
The inaccuracy introduced by a relatively small (two-byte) &#039;&#039;&#039;fixed point&#039;&#039;&#039; is irrelevant if all you are trying to do is simulate some realistic movements of characters in a game. If you have a need to calculate more accurate values, you can simply increase the size of the &#039;&#039;&#039;fixed point&#039;&#039;&#039; variable to either an SQ15x16 or an SQ31x32 (though be aware that some operations do not work on SQ31x32 due to presently unavoidable restrictions). The code below illustrates the accuracy of each data type.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 SQ7x8 myVar7x8 = 4;&lt;br /&gt;
 &lt;br /&gt;
 for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
   Serial.print(static_cast&amp;lt;float&amp;gt;(myVar7x8), 8);&lt;br /&gt;
   Serial.print(‘ ‘);&lt;br /&gt;
   myVar7x8 = myVar7x8 + 0.1;&lt;br /&gt;
 }&lt;br /&gt;
 Serial.println();&lt;br /&gt;
 &lt;br /&gt;
 SQ15x16 myVar15x16 = 4;&lt;br /&gt;
 &lt;br /&gt;
 for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
   Serial.print(static_cast&amp;lt;float&amp;gt;(myVar15x16), 8);&lt;br /&gt;
   Serial.print(‘ ‘);&lt;br /&gt;
   myVar15x16 = myVar15x16 + 0.1;&lt;br /&gt;
 }&lt;br /&gt;
 Serial.println();&lt;br /&gt;
 &lt;br /&gt;
 SQ31x32 myVar31x32 = 4;&lt;br /&gt;
 &lt;br /&gt;
 for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
   Serial.print(static_cast&amp;lt;float&amp;gt;(myVar31x32), 8);&lt;br /&gt;
   Serial.print(‘ ‘);&lt;br /&gt;
   myVar31x32 = myVar31x32 + 0.1;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 Serial.println();&lt;br /&gt;
&lt;br /&gt;
 }&lt;br /&gt;
 Serial.println();&lt;br /&gt;
&lt;br /&gt;
 SQ15x16 myVar15x16 = 4;&lt;br /&gt;
&lt;br /&gt;
 for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
   Serial.print(static_cast&amp;lt;float&amp;gt;(myVar15x16), 8);&lt;br /&gt;
   Serial.print(‘ ‘);&lt;br /&gt;
   myVar15x16 = myVar15x16 + 0.1;&lt;br /&gt;
&lt;br /&gt;
 }&lt;br /&gt;
 Serial.println();&lt;br /&gt;
&lt;br /&gt;
 SQ31x32 myVar31x32 = 4;&lt;br /&gt;
&lt;br /&gt;
 for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
   Serial.print(static_cast&amp;lt;float&amp;gt;(myVar31x32), 8);&lt;br /&gt;
   Serial.print(‘ ‘);&lt;br /&gt;
   myVar31x32 = myVar31x32 + 0.1;&lt;br /&gt;
&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 Serial.println();&lt;br /&gt;
Output:&lt;br /&gt;
 4.00000000 4.09765625 4.19531250 4.29296875 4.39062500 4.48828125 4.58593750 4.68359375&lt;br /&gt;
 4.78125000 4.87890625 4.97656250&lt;br /&gt;
 4.00000000 4.09999084 4.19998168 4.29997253 4.39996337 4.49995422 4.59994506 4.69993591&lt;br /&gt;
 4.79992675 4.89991760 4.99990844 &lt;br /&gt;
 4.00000000 4.09999990 4.19999980 4.30000019 4.40000009 4.50000000 4.59999990 4.69999980&lt;br /&gt;
 4.80000019 4.90000009 5.00000000&lt;br /&gt;
&lt;br /&gt;
 4.00000000 4.09999990 4.19999980 4.30000019 4.40000009 4.50000000 4.59999990 4.69999980&lt;br /&gt;
 4.80000019 4.90000009 5.00000000&lt;br /&gt;
One thing to note with the output is the compounding effect that adding two inaccurate numbers results in. This can be seen easily if we repeat the same exercise on the smallest, one-byte data type as shown below. The fractional part of an SQ3x4 number is stored in 1/16th increments or 0.0625 in decimal.&lt;br /&gt;
&lt;br /&gt;
When we add the first increment of 0.1  we immediately introduce an inaccuracy as the number gets rounded down to the nearest 1/16th of a number. As we repeat the addition, the error increases. The following code shows that.&lt;br /&gt;
&lt;br /&gt;
 SQ3x4 myVar3x4 = 4;&lt;br /&gt;
 &lt;br /&gt;
 for (uint8_t i = 0; i &amp;lt;= 10; ++i) {&lt;br /&gt;
   Serial.print(static_cast&amp;lt;float&amp;gt;(myVar3x4), 4);&lt;br /&gt;
   Serial.print(“ “);&lt;br /&gt;
   myVar3x4 = myVar3x4 + 0.1;&lt;br /&gt;
 }&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
 4.0000 4.0625 4.1250 4.1875 4.2500 4.3125 4.3750 4.4375 4.5000 4.5625 4.6250&lt;br /&gt;
Choosing larger &#039;&#039;&#039;fixed point&#039;&#039;&#039; data types increases the range of the number and accuracy of the fractional component at the expense of memory.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Why would I bother with Fixed Points at all?&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
Imagine you are building a sideways-scrolling game where aliens (or cars, planes or other moving objects) are moving towards you at various speeds. With each pass through the program’s loop you could decrement the x coordinate of each alien but this would result in all aliens moving at the same speed. You could try to move aliens every second or third time through the loop by keeping some sort of counter but this can be error prone and&lt;br /&gt;
&lt;br /&gt;
may result in the aliens moving slowly. To really allow flexibility in the aliens’ movement, you could store the x and y in fixed point variables and increment these by a decimal value allowing the aliens to move at similar but slightly different speeds.&lt;br /&gt;
&lt;br /&gt;
Consider the Alien class below. Each alien is described with an x and y coordinate and takes care of its own movement.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class Alien {&lt;br /&gt;
 &lt;br /&gt;
   public:&lt;br /&gt;
 &lt;br /&gt;
     Alien(SQ7x8 x, SQ7x8 y, SQ7x8 incX, SQ7x8 incY, const uint8_t *bitmap) {&lt;br /&gt;
&lt;br /&gt;
       _x = x;&lt;br /&gt;
       _y = y;&lt;br /&gt;
       _incX = incX;&lt;br /&gt;
       _incY = incY;&lt;br /&gt;
       _bitmap = bitmap; &lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 getX() const { return _x; }&lt;br /&gt;
     SQ7x8 getY() const { return _y; }&lt;br /&gt;
 &lt;br /&gt;
     void setX(const SQ7x8 value) { _x = value; }&lt;br /&gt;
     void setY(const SQ7x8 value) { _y = value; }&lt;br /&gt;
 &lt;br /&gt;
     inline void Alien::move(uint8_t frame) {&lt;br /&gt;
 &lt;br /&gt;
       _x = _x - _incX;&lt;br /&gt;
       _y = _y - _incY;&lt;br /&gt;
       Sprites::drawOverwrite(_x.getInteger(), _y.getInteger(), _bitmap, frame);&lt;br /&gt;
   } &lt;br /&gt;
 &lt;br /&gt;
   private:&lt;br /&gt;
 &lt;br /&gt;
     SQ7x8 _x;&lt;br /&gt;
     SQ7x8 _y;&lt;br /&gt;
     SQ7x8 _incX;&lt;br /&gt;
     SQ7x8 _incY;&lt;br /&gt;
     const uint8_t *_bitmap;&lt;br /&gt;
   &lt;br /&gt;
 };&lt;br /&gt;
And our main .ino file:&lt;br /&gt;
 Alien alien1 = {127, 16, (SQ7x8)0.9, (SQ7x8)1.34, alienBitmap };&lt;br /&gt;
 Alien alien2 = {127, 32, (SQ7x8)1.7, (SQ7x8)1.76, alienBitmap };&lt;br /&gt;
 Alien alien3 = {127, 48, (SQ7x8)1.5, (SQ7x8)2.13, alienBitmap }; &lt;br /&gt;
 &lt;br /&gt;
 void loop() {&lt;br /&gt;
   alien1.move(frame);&lt;br /&gt;
   alien2.move(frame);&lt;br /&gt;
   alien3.move(frame);&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
 void loop() {&lt;br /&gt;
    alien1.move(frame);&lt;br /&gt;
&lt;br /&gt;
   alien2.move(frame);&lt;br /&gt;
&lt;br /&gt;
   alien3.move(frame);&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
With fixed point data types, we can have our aliens move at slightly different speeds without the overhead of using floating points.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Common Type Aliases&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
@Pharap’s library includes the following data types. However the library is implemented with templates and it is a relatively simple matter to declare your own type aliases.&lt;br /&gt;
&lt;br /&gt;
The predeclared aliases (which can be  used as an example of how to declare such an alias) can be found in &#039;&#039;&#039;src\ FixedPointsCommon\SFixedCommon.h and src\ FixedPointsCommon\UfixedCommon.h.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Unsigned Data Type Aliases:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|-&lt;br /&gt;
|UQ4x4&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 15&lt;br /&gt;
|1 / 16th&lt;br /&gt;
|0 to 16&lt;br /&gt;
|-&lt;br /&gt;
|UQ8x8&lt;br /&gt;
|16 bit (2 bytes)&lt;br /&gt;
|0 to 255&lt;br /&gt;
|1 / 256th&lt;br /&gt;
|0 to 256&lt;br /&gt;
|-&lt;br /&gt;
|UQ16x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|0 to 65,355&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|0 to 65,356&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x7&lt;br /&gt;
|8 bit (1 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 128th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x15&lt;br /&gt;
|16 bit (2 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 32,768th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x31&lt;br /&gt;
|32 bit (4 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1 / 2,147,483,648th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|-&lt;br /&gt;
|UQ1x63&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1/ 9,223,372,036,854,775,808th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-15 to +15&lt;br /&gt;
&lt;br /&gt;
-127 to +127&lt;br /&gt;
&lt;br /&gt;
-1 to +1&lt;br /&gt;
&lt;br /&gt;
-1 to +1&lt;br /&gt;
&lt;br /&gt;
-1 to +1&lt;br /&gt;
&lt;br /&gt;
1 / 16th&lt;br /&gt;
&lt;br /&gt;
1 / 256th&lt;br /&gt;
&lt;br /&gt;
1 / 65,536th 1 / 128th&lt;br /&gt;
&lt;br /&gt;
1 / 32,768th&lt;br /&gt;
&lt;br /&gt;
1 / 2,147,483,648th&lt;br /&gt;
&lt;br /&gt;
1 / 16th&lt;br /&gt;
&lt;br /&gt;
1 / 256th&lt;br /&gt;
&lt;br /&gt;
1 / 64th&lt;br /&gt;
&lt;br /&gt;
1 / 16,384th&lt;br /&gt;
&lt;br /&gt;
1 / 1,073,741,824th&lt;br /&gt;
&lt;br /&gt;
0 to 16&lt;br /&gt;
&lt;br /&gt;
0 to 256&lt;br /&gt;
&lt;br /&gt;
0 to 65,356&lt;br /&gt;
&lt;br /&gt;
0 to 1&lt;br /&gt;
&lt;br /&gt;
0 to 1&lt;br /&gt;
&lt;br /&gt;
0 to 1&lt;br /&gt;
&lt;br /&gt;
-16 to +16&lt;br /&gt;
&lt;br /&gt;
-128 to +128&lt;br /&gt;
&lt;br /&gt;
-2 to +2&lt;br /&gt;
&lt;br /&gt;
-2 to +2&lt;br /&gt;
&lt;br /&gt;
-2 to +2&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|UQ32x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|0 to 4,294,967,295&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|0 to 4,294,967,296&lt;br /&gt;
|}&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|UQ1x63&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|0 to 1&lt;br /&gt;
|1/ 9,223,372,036,854,775,808th&lt;br /&gt;
|0 to 1&lt;br /&gt;
|}&lt;br /&gt;
Signed Data Type Aliases:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Data Type&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Size&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Integer Range&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Fractional Resolution&#039;&#039;&#039; &lt;br /&gt;
|&#039;&#039;&#039;Effective Range*&#039;&#039;&#039; &lt;br /&gt;
|}&lt;br /&gt;
SQ3x4&lt;br /&gt;
&lt;br /&gt;
SQ7x8&lt;br /&gt;
&lt;br /&gt;
SQ1x6&lt;br /&gt;
&lt;br /&gt;
SQ1x14&lt;br /&gt;
&lt;br /&gt;
SQ1x30&lt;br /&gt;
&lt;br /&gt;
8 bit (1 byte)&lt;br /&gt;
&lt;br /&gt;
16 bit (2 bytes)&lt;br /&gt;
&lt;br /&gt;
8 bit (1 byte)&lt;br /&gt;
&lt;br /&gt;
16 bit (2 byte)&lt;br /&gt;
&lt;br /&gt;
32 bit (4 byte)&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|SQ15x16&lt;br /&gt;
|32 bit (4 bytes)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-32,767 to +32,767&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
|1 / 65,536th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-32,768 to +32,768&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|SQ31x32&lt;br /&gt;
|64 bit (8 bytes)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-2,147,483,647 to +2,147,483,647&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
|1 / 4,294,967,296th&lt;br /&gt;
|2,147,483,648 to +2,147,483,648&lt;br /&gt;
|}&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|SQ1x62&lt;br /&gt;
|64-bit (8 byte)&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-1 to +1&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
|1/ 4,611,686,018,427,387,904th&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;-2 to +2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt; Lower bound is inclusive, upper bound is exclusive&lt;br /&gt;
&lt;br /&gt;
FixedPoints can be found through the Arduino IDE library manager. If you have not used thelibrary manager before see here: &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;https://arduboy.com/download-and-learn-arduino/&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Developing_Games&amp;diff=591</id>
		<title>Developing Games</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Developing_Games&amp;diff=591"/>
		<updated>2024-08-16T09:38:35Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* Common Libraries */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Developing games for the Arduboy is an exciting endeavor that blends creativity with technical skills. The Arduboy is a compact, credit card-sized gaming platform based on the Arduino microcontroller, making it accessible for both beginners and experienced developers. Its simplicity and portability offer a unique challenge due to its limited resources, such as a monochrome 128x64 pixel display and minimal memory. However, these constraints inspire innovation and efficient coding practices.&lt;br /&gt;
&lt;br /&gt;
Standard libraries for sound and graphics play a significant role in the development process. The Arduboy2 library provides essential functions for managing the display, handling input, and maintaining game states. It facilitates tasks such as drawing shapes, rendering text, and controlling frame rates, allowing developers to concentrate on game mechanics. For audio, the ArduboyTones library enables the addition of sound effects and music, enhancing the game&#039;s atmosphere. Using these libraries, developers can create functional and engaging games that work well within the Arduboy&#039;s hardware limitations.&lt;br /&gt;
&lt;br /&gt;
== Prerequisites for Developing Games ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Arduino Development IDEs ==&lt;br /&gt;
Arduino development environments provide a range of tools for creating, testing, and deploying projects to the Arduboy. The primary environment is the Arduino Integrated Development Environment (IDE), known for its user-friendly interface that simplifies writing, compiling, and uploading code. It supports various Arduino models and includes a library manager for easy access to numerous libraries. Additionally, Visual Studio Code (VSCode) can be used for Arduino development, offering advanced features like integrated debugging and a customizable interface. These environments cater to both beginners and advanced users, offering tools that range from straightforward to professional-grade.&lt;br /&gt;
&lt;br /&gt;
=== Visual Studio Code ===&lt;br /&gt;
&lt;br /&gt;
The [https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.vscode-arduino Arduino plugin] for Visual Studio Code is an easy way to develop and build Arduboy games with a full-featured IDE.&lt;br /&gt;
&lt;br /&gt;
1. Download and install [https://code.visualstudio.com/ Visual Studio Code].&lt;br /&gt;
&lt;br /&gt;
2. Install the &amp;quot;Arduino&amp;quot; extension developed by Microsoft.&lt;br /&gt;
&lt;br /&gt;
3. Configure the extension to use the bundled version of Arduino CLI.&lt;br /&gt;
&lt;br /&gt;
4. Add the following URL to the &amp;quot;Additional Urls&amp;quot; setting for the Arduino extension: &amp;lt;pre&amp;gt;https://raw.githubusercontent.com/MrBlinky/Arduboy-homemade-package/master/package_arduboy_homemade_index.json&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
5. Open the Arduino Board Manager and click &amp;quot;Refresh Package Indexes&amp;quot;. Install the Arduboy homemade package by Mr.Blinky.&lt;br /&gt;
&lt;br /&gt;
6. Add a file &amp;quot;.vscode/arduino.json&amp;quot; to your project to define how to build your project. For example, to build a game for the Arduboy FX, you might have:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;configuration&amp;quot;: &amp;quot;core=arduboy-core,boot=cathy3k&amp;quot;,&lt;br /&gt;
    &amp;quot;board&amp;quot;: &amp;quot;arduboy-homemade:avr:arduboy-fx&amp;quot;,&lt;br /&gt;
    &amp;quot;sketch&amp;quot;: &amp;quot;my_game.ino&amp;quot;,&lt;br /&gt;
    &amp;quot;output&amp;quot;: &amp;quot;build/&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. You can now build your project with the &amp;quot;Arduino: Verify&amp;quot; command. The command will create a &amp;quot;build&amp;quot; directory inside your project directory, and generate .hex and .elf files there.&lt;br /&gt;
&lt;br /&gt;
== Common Libraries ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An Arduboy library is a collection of pre-written code designed to simplify and standardize the development of games and applications for the Arduboy platform. These libraries provide a range of functionalities, such as handling graphics, sound, input, and other core features of the device. For example, the Arduboy2 library includes functions for drawing shapes, rendering text, managing game states, and handling user input, while the ArduboyTones library offers tools for generating sound effects and music. By utilizing these libraries, developers can streamline their coding process, focus on the creative aspects of game design, and ensure their applications run efficiently on the limited hardware of the Arduboy.&lt;br /&gt;
&lt;br /&gt;
=== Base Libraries ===&lt;br /&gt;
&lt;br /&gt;
==== [[Arduboy2]] ====&lt;br /&gt;
The basic Arduboy library, often referred to as Arduboy2, is an essential toolkit for developing games on the Arduboy platform. This library provides a comprehensive set of functions that manage the device&#039;s core features, making it easier for developers to create games and applications. It includes capabilities for drawing graphics on the 128x64 pixel monochrome display, such as drawing lines, circles, and rectangles, as well as rendering text. The library also handles input from the device&#039;s buttons, allowing developers to implement game controls efficiently. Additionally, Arduboy2 offers functions for managing frame rates and game states, ensuring smooth game play experiences. By using the Arduboy2 library, developers can focus more on the creative aspects of game development while leveraging standardized, efficient code for fundamental tasks.&lt;br /&gt;
&lt;br /&gt;
Additionally, the library provides basic [[Collision Detection]] functions.&lt;br /&gt;
&lt;br /&gt;
=== Graphic Libraries ===&lt;br /&gt;
&lt;br /&gt;
==== [[Sprites / SpritesB Library|Sprites]] / SpritesB ====&lt;br /&gt;
Shipped as part of the [[Arduboy2]] library, the Sprites library provides functions for rendering both masked and unmasked bitmaps and tilesheets. When working with masked bitmaps, the library supports various formats including standalone masks (allowing them to be used with multiple bitmaps) or interlaced within the bitmap image themselves (for best performance). The library can only render images from PROGMEM and provides no FX chip support.&lt;br /&gt;
&lt;br /&gt;
The SpritesB library is functionally identical to the standard Sprites library but the code has been optimised for size at the expense of speed.&lt;br /&gt;
&lt;br /&gt;
==== [[ArduboyG Tutorial|ArduboyG]] ====&lt;br /&gt;
The ArduboyG library extends the basic Arduboy2 library with advanced functions for handling sprites, animations, and complex graphical operations, including support for 3 and 4 color greyscale. The library allows images to be rendered from PROGMEM or directly from the FX chip. Images can include transparent sections and must be a multiple of 8 bits high.  These are converted using a python script provided with the library.&lt;br /&gt;
&lt;br /&gt;
==== [[ArdBitmap]] ====&lt;br /&gt;
The ArdBitmap library works with compressed and uncompressed bitmaps and provides horizontal and vertical mirroring and real-time image resizing (downscaling).  It can only render images from PROGMEM and provides no FX chip support.&lt;br /&gt;
&lt;br /&gt;
The original forum post can be found [https://community.arduboy.com/t/ardbitmap-bitmap-library-tools/2631/6 here] and the library and documentation can be found [https://github.com/igvina/ArdBitmap here]&lt;br /&gt;
&lt;br /&gt;
=== Sound Libraries ===&lt;br /&gt;
&lt;br /&gt;
=== Utility Libraries ===&lt;br /&gt;
[[Fixed Point]]&lt;br /&gt;
&lt;br /&gt;
The Arduboy is based on a simple processor which does not handle floating point calculations natively.  As a result, any attempt to use floating point numbers and arithmetic results in a excess code and RAM use as well as poor performance.  Fixed Point numbers use a different approach and removes many of the issues found in floating point numbers.  This library, developed by Pharap, implements fixed point numbers that have varying scale and precision.&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Developing_Games&amp;diff=590</id>
		<title>Developing Games</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Developing_Games&amp;diff=590"/>
		<updated>2024-08-16T05:07:03Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* Base Libraries */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Developing games for the Arduboy is an exciting endeavor that blends creativity with technical skills. The Arduboy is a compact, credit card-sized gaming platform based on the Arduino microcontroller, making it accessible for both beginners and experienced developers. Its simplicity and portability offer a unique challenge due to its limited resources, such as a monochrome 128x64 pixel display and minimal memory. However, these constraints inspire innovation and efficient coding practices.&lt;br /&gt;
&lt;br /&gt;
Standard libraries for sound and graphics play a significant role in the development process. The Arduboy2 library provides essential functions for managing the display, handling input, and maintaining game states. It facilitates tasks such as drawing shapes, rendering text, and controlling frame rates, allowing developers to concentrate on game mechanics. For audio, the ArduboyTones library enables the addition of sound effects and music, enhancing the game&#039;s atmosphere. Using these libraries, developers can create functional and engaging games that work well within the Arduboy&#039;s hardware limitations.&lt;br /&gt;
&lt;br /&gt;
== Prerequisites for Developing Games ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Arduino Development IDEs ==&lt;br /&gt;
Arduino development environments provide a range of tools for creating, testing, and deploying projects to the Arduboy. The primary environment is the Arduino Integrated Development Environment (IDE), known for its user-friendly interface that simplifies writing, compiling, and uploading code. It supports various Arduino models and includes a library manager for easy access to numerous libraries. Additionally, Visual Studio Code (VSCode) can be used for Arduino development, offering advanced features like integrated debugging and a customizable interface. These environments cater to both beginners and advanced users, offering tools that range from straightforward to professional-grade.&lt;br /&gt;
&lt;br /&gt;
=== Visual Studio Code ===&lt;br /&gt;
&lt;br /&gt;
The [https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.vscode-arduino Arduino plugin] for Visual Studio Code is an easy way to develop and build Arduboy games with a full-featured IDE.&lt;br /&gt;
&lt;br /&gt;
1. Download and install [https://code.visualstudio.com/ Visual Studio Code].&lt;br /&gt;
&lt;br /&gt;
2. Install the &amp;quot;Arduino&amp;quot; extension developed by Microsoft.&lt;br /&gt;
&lt;br /&gt;
3. Configure the extension to use the bundled version of Arduino CLI.&lt;br /&gt;
&lt;br /&gt;
4. Add the following URL to the &amp;quot;Additional Urls&amp;quot; setting for the Arduino extension: &amp;lt;pre&amp;gt;https://raw.githubusercontent.com/MrBlinky/Arduboy-homemade-package/master/package_arduboy_homemade_index.json&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
5. Open the Arduino Board Manager and click &amp;quot;Refresh Package Indexes&amp;quot;. Install the Arduboy homemade package by Mr.Blinky.&lt;br /&gt;
&lt;br /&gt;
6. Add a file &amp;quot;.vscode/arduino.json&amp;quot; to your project to define how to build your project. For example, to build a game for the Arduboy FX, you might have:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;configuration&amp;quot;: &amp;quot;core=arduboy-core,boot=cathy3k&amp;quot;,&lt;br /&gt;
    &amp;quot;board&amp;quot;: &amp;quot;arduboy-homemade:avr:arduboy-fx&amp;quot;,&lt;br /&gt;
    &amp;quot;sketch&amp;quot;: &amp;quot;my_game.ino&amp;quot;,&lt;br /&gt;
    &amp;quot;output&amp;quot;: &amp;quot;build/&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. You can now build your project with the &amp;quot;Arduino: Verify&amp;quot; command. The command will create a &amp;quot;build&amp;quot; directory inside your project directory, and generate .hex and .elf files there.&lt;br /&gt;
&lt;br /&gt;
== Common Libraries ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An Arduboy library is a collection of pre-written code designed to simplify and standardize the development of games and applications for the Arduboy platform. These libraries provide a range of functionalities, such as handling graphics, sound, input, and other core features of the device. For example, the Arduboy2 library includes functions for drawing shapes, rendering text, managing game states, and handling user input, while the ArduboyTones library offers tools for generating sound effects and music. By utilizing these libraries, developers can streamline their coding process, focus on the creative aspects of game design, and ensure their applications run efficiently on the limited hardware of the Arduboy.&lt;br /&gt;
&lt;br /&gt;
=== Base Libraries ===&lt;br /&gt;
&lt;br /&gt;
==== [[Arduboy2]] ====&lt;br /&gt;
The basic Arduboy library, often referred to as Arduboy2, is an essential toolkit for developing games on the Arduboy platform. This library provides a comprehensive set of functions that manage the device&#039;s core features, making it easier for developers to create games and applications. It includes capabilities for drawing graphics on the 128x64 pixel monochrome display, such as drawing lines, circles, and rectangles, as well as rendering text. The library also handles input from the device&#039;s buttons, allowing developers to implement game controls efficiently. Additionally, Arduboy2 offers functions for managing frame rates and game states, ensuring smooth game play experiences. By using the Arduboy2 library, developers can focus more on the creative aspects of game development while leveraging standardized, efficient code for fundamental tasks.&lt;br /&gt;
&lt;br /&gt;
Additionally, the library provides basic [[Collision Detection]] functions.&lt;br /&gt;
&lt;br /&gt;
=== Graphic Libraries ===&lt;br /&gt;
&lt;br /&gt;
==== [[Sprites / SpritesB Library|Sprites]] / SpritesB ====&lt;br /&gt;
Shipped as part of the [[Arduboy2]] library, the Sprites library provides functions for rendering both masked and unmasked bitmaps and tilesheets. When working with masked bitmaps, the library supports various formats including standalone masks (allowing them to be used with multiple bitmaps) or interlaced within the bitmap image themselves (for best performance). The library can only render images from PROGMEM and provides no FX chip support.&lt;br /&gt;
&lt;br /&gt;
The SpritesB library is functionally identical to the standard Sprites library but the code has been optimised for size at the expense of speed.&lt;br /&gt;
&lt;br /&gt;
==== [[ArduboyG Tutorial|ArduboyG]] ====&lt;br /&gt;
The ArduboyG library extends the basic Arduboy2 library with advanced functions for handling sprites, animations, and complex graphical operations, including support for 3 and 4 color greyscale. The library allows images to be rendered from PROGMEM or directly from the FX chip. Images can include transparent sections and must be a multiple of 8 bits high.  These are converted using a python script provided with the library.&lt;br /&gt;
&lt;br /&gt;
==== [[ArdBitmap]] ====&lt;br /&gt;
The ArdBitmap library works with compressed and uncompressed bitmaps and provides horizontal and vertical mirroring and real-time image resizing (downscaling).  It can only render images from PROGMEM and provides no FX chip support.&lt;br /&gt;
&lt;br /&gt;
The original forum post can be found [https://community.arduboy.com/t/ardbitmap-bitmap-library-tools/2631/6 here] and the library and documentation can be found [https://github.com/igvina/ArdBitmap here]&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Collision_Detection&amp;diff=589</id>
		<title>Collision Detection</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Collision_Detection&amp;diff=589"/>
		<updated>2024-08-16T04:59:38Z</updated>

		<summary type="html">&lt;p&gt;Filmote: /* But how does the collision detection work? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
The Arduboy2 library provides functions to detect when two rectangles or a rectangle and a point overlap.  It is implemented in such a way that is independent of other constructs such as the screen and sprites.  In practice though, the detection of collisions is often focused on the collision of sprites (players, bullets, etc) with other entities (enemy sprites, background elements or other play elements).  These collisions may occur onscreen or off and functions within the library are flexible enough to cater for this.&lt;br /&gt;
&lt;br /&gt;
The following examples and code assume you are familiar with the concept and functions available to render sprites.  More information on this can be found in the article covering the [[Sprites / SpritesB Library]].&lt;br /&gt;
&lt;br /&gt;
== But how does the collision detection work? ==&lt;br /&gt;
&lt;br /&gt;
The Arduboy2 library provides two collision detection methods which are shown below.  &amp;lt;pre&amp;gt;&lt;br /&gt;
bool collide(Point point, Rect rect);&lt;br /&gt;
bool collide(Rect rect1, Rect rect2);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It also provides two structures to assist.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
struct Rect {&lt;br /&gt;
  int16_t x;&lt;br /&gt;
  int16_t y;&lt;br /&gt;
  uint8_t width;&lt;br /&gt;
  uint8_t height; &lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
struct Point {&lt;br /&gt;
  int16_t x; &lt;br /&gt;
  int16_t y; &lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The structures describe a rectangle and a point in space - the x / y coordinates are arbitrary and their origin can be anywhere but it typical to align these with screen&#039;s coordinates.  Thus 0, 0 is located at the top-left corner of the screen with negative x and y values representing locations to the left and above the screen respectively.&lt;br /&gt;
&lt;br /&gt;
The first function tests to see if a point is contained within the specified rectangle.  The sample code below demonstrates this:&lt;br /&gt;
&lt;br /&gt;
[[File:Collision 00.png|thumb]]&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Point p;&lt;br /&gt;
Rect r;&lt;br /&gt;
&lt;br /&gt;
p.x = 5;&lt;br /&gt;
p.y = 5;&lt;br /&gt;
&lt;br /&gt;
r.x = 2;&lt;br /&gt;
r.y = 2;&lt;br /&gt;
r.width = 10;&lt;br /&gt;
r.height = 10;&lt;br /&gt;
&lt;br /&gt;
Serial.println(arduboy.collision(p, r));    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Running the above code will print 1 as the point at position 5, 5 is in the centre of the square whose top left corner is at position 2, 2 and lower right corner is at 11, 11.  &lt;br /&gt;
&lt;br /&gt;
Moving the point outside of the square with the following code:&lt;br /&gt;
[[File:Collision 01.png|thumb]]&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
p.x = 14;&lt;br /&gt;
p.y = 2;&lt;br /&gt;
&lt;br /&gt;
Serial.println(arduboy.collision(p, r));    &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Will print 0 as the point at position 14, 2 is outside of the square whose top left corner is at position 5, 5 and lower right corner is at 15, 15.&lt;br /&gt;
&lt;br /&gt;
The second function compares the position of two rectangles and tests to see if there is any overlap.  This is demonstrated in the code below:&lt;br /&gt;
[[File:Collision 03.png|thumb]]&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rect r1 = {2, 2, 5, 5};&lt;br /&gt;
Rect r2 = {5, 5, 7, 7};&lt;br /&gt;
&lt;br /&gt;
Serial.println(arduboy.collision(r1, r2));&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Running the above code will print 1 as the two rectangles overlap.  The first rectangle’s top left corner is at 2, 2 and its lower right corner is at 6, 6. The second rectangle’s top left corner is at 5, 5 and its lower left corner is at 11, 11. &lt;br /&gt;
&lt;br /&gt;
Moving the smaller rectangle away from the larger ones so they do not overlap: &lt;br /&gt;
&lt;br /&gt;
 [[File:Collision 04.png|thumb]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r1.x = 0;&lt;br /&gt;
r1.y = 0;&lt;br /&gt;
&lt;br /&gt;
Serial.println(arduboy.collision(r1, r2))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Running the above code will print 0 as the two rectangles do not overlap anymore. The first rectangle’s top left corner is now at 0, 0 and its lower right corner is at 4, 4. The second rectangle’s top left corner is at 5, 5 and its lower left corner is at 11, 11.&lt;br /&gt;
&lt;br /&gt;
== Using Collision Detection with Sprites ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It is common to want to detect collisions between sprites - such as a player and an enemy.  This often results in code like the following being scattered through a game&#039;s code to create Rects with the same position, width and height as the player and enemy sprites.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rect player = { player.x, player.y, 16, 16 };&lt;br /&gt;
Rect enemy =  { enemy.x, enemy.y, 16, 16 };&lt;br /&gt;
&lt;br /&gt;
Serial.println(arduboy.collide(player, enemy);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is perfectly acceptable when there are only a handful of sprites to track - but what if there were dozens, and their sizes all different?  The chance of coding errors increases significantly!&lt;br /&gt;
&lt;br /&gt;
Assuming you are using classes to represent your players, enemies and other game play elements, a typical design pattern for action games is to define a class to hold information about players including their current position.  A sample header and implementation class is shown below&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Player.h:&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;Arduboy2.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class Player {&lt;br /&gt;
&lt;br /&gt;
   public:&lt;br /&gt;
     Rect getRect();&lt;br /&gt;
     &lt;br /&gt;
   int16_t x;&lt;br /&gt;
   int16_t y;&lt;br /&gt;
   const uint8_t *bitmap;&lt;br /&gt;
   &lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Player.cpp:&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;Player.h&amp;quot;&lt;br /&gt;
#include &amp;quot;Arduboy2.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Rect Player::getRect() {&lt;br /&gt;
   &lt;br /&gt;
  return (Rect){x, y, pgm_read_byte(bitmap), pgm_read_byte(bitmap + 1)};&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The header declares three public variables for the player’s coordinates and the image used when rendering the screen.  It also prototypes a function that will return a rectangle structure that will describe the area on screen that the player currently occupies.  The implementation of that function is included in the .cpp file.  It simply creates and returns a populated Rect structure using the ‘x’ and ‘y’ coordinates from the class itself and the width and height of the image by reading these from the image array itself which happen to be the first two bytes when using the standard Sprites library. If you are unfamiliar with how sprite data is stored refer [[Sprites / SpritesB Library|here]].&lt;br /&gt;
&lt;br /&gt;
The code below shows the declaration of two player objects and shows how they can be used when testing collisions. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Player playerA;&lt;br /&gt;
Player playerB;&lt;br /&gt;
&lt;br /&gt;
playerA.x = 20;&lt;br /&gt;
playerA.y = 12;&lt;br /&gt;
playerA.bitmap = squareA;&lt;br /&gt;
&lt;br /&gt;
playerB.x = 100;&lt;br /&gt;
playerB.y = 24;&lt;br /&gt;
playerB.bitmap = squareB;&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
Serial.print(&amp;quot;A collides with B : &amp;quot;);&lt;br /&gt;
Serial.println(arduboy.collide(playerA.getRect(), playerB.getRect()));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In my opinion, this approach results in much more readable code. &lt;br /&gt;
&lt;br /&gt;
The simple collision application and the ‘advanced’ version using a ‘Player’ class can be found in my repository.&lt;br /&gt;
&lt;br /&gt;
https://github.com/filmote/Collide_Article1  (simple example)&lt;br /&gt;
&lt;br /&gt;
https://github.com/filmote/Collide_Article2  (using the ‘Player’ class).&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Collision_Detection&amp;diff=588</id>
		<title>Collision Detection</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Collision_Detection&amp;diff=588"/>
		<updated>2024-08-16T04:45:54Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
The Arduboy2 library provides functions to detect when two rectangles or a rectangle and a point overlap.  It is implemented in such a way that is independent of other constructs such as the screen and sprites.  In practice though, the detection of collisions is often focused on the collision of sprites (players, bullets, etc) with other entities (enemy sprites, background elements or other play elements).  These collisions may occur onscreen or off and functions within the library are flexible enough to cater for this.&lt;br /&gt;
&lt;br /&gt;
The following examples and code assume you are familiar with the concept and functions available to render sprites.  More information on this can be found in the article covering the [[Sprites / SpritesB Library]].&lt;br /&gt;
&lt;br /&gt;
== But how does the collision detection work? ==&lt;br /&gt;
&lt;br /&gt;
The Arduboy2 library provides two collision detection methods which are shown below.  &amp;lt;pre&amp;gt;&lt;br /&gt;
bool collide(Point point, Rect rect);&lt;br /&gt;
bool collide(Rect rect1, Rect rect2);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It also provides two structures to assist.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
struct Rect {&lt;br /&gt;
  int16_t x;&lt;br /&gt;
  int16_t y;&lt;br /&gt;
  uint8_t width;&lt;br /&gt;
  uint8_t height; &lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
struct Point {&lt;br /&gt;
  int16_t x; &lt;br /&gt;
  int16_t y; &lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The structures describe a rectangle and a point in space - the x / y coordinates are arbitrary and their origin can be anywhere but it typical to align these with screen&#039;s coordinates.  Thus 0, 0 is located at the top-left corner of the screen with negative x and y values representing locations to the left and above the screen respectively.&lt;br /&gt;
&lt;br /&gt;
The first function tests to see if a point is contained within the specified rectangle.  The sample code below demonstrates this:&lt;br /&gt;
&lt;br /&gt;
[[File:Collision 00.png|thumb]]&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Point p;&lt;br /&gt;
Rect r;&lt;br /&gt;
&lt;br /&gt;
p.x = 5;&lt;br /&gt;
p.y = 5;&lt;br /&gt;
&lt;br /&gt;
r.x = 2;&lt;br /&gt;
r.y = 2;&lt;br /&gt;
r.width = 10;&lt;br /&gt;
r.height = 10;&lt;br /&gt;
&lt;br /&gt;
Serial.println(arduboy.collision(p, r));    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Running the above code will print 1 as the point at position 5, 5 is in the centre of the square whose top left corner is at position 2, 2 and lower right corner is at 11, 11.  &lt;br /&gt;
&lt;br /&gt;
Moving the point outside of the square with the following code:&lt;br /&gt;
[[File:Collision 01.png|thumb]]&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
p.x = 14;&lt;br /&gt;
p.y = 2;&lt;br /&gt;
&lt;br /&gt;
Serial.println(arduboy.collision(p, r));    &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Will print 0 as the point at position 14, 2 is outside of the square whose top left corner is at position 5, 5 and lower right corner is at 15, 15.&lt;br /&gt;
&lt;br /&gt;
The second function compares the position of two rectangles and tests to see if there is any overlap.  This is demonstrated in the code below:&lt;br /&gt;
[[File:Collision 03.png|thumb]]&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rect r1 = {2, 2, 5, 5};&lt;br /&gt;
Rect r2 = {5, 5, 7, 7};&lt;br /&gt;
&lt;br /&gt;
Serial.println(arduboy.collision(r1, r2));&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Running the above code will print 1 as the two rectangles overlap.  The first rectangle’s top left corner is at 2, 2 and its lower right corner is at 6, 6. The second rectangle’s top left corner is at 5, 5 and its lower left corner is at 11, 11. &lt;br /&gt;
&lt;br /&gt;
Moving the smaller rectangle away from the larger ones so they do not overlap: &lt;br /&gt;
&lt;br /&gt;
 [[File:Collision 04.png|thumb]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r1.x = 0;&lt;br /&gt;
r1.y = 0;&lt;br /&gt;
&lt;br /&gt;
Serial.println(arduboy.collision(r1, r2))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Running the above code will print 0 as the two rectangles do not overlap anymore. The first rectangle’s top left corner is now at 0, 0 and its lower right corner is at 4, 4. The second rectangle’s top left corner is at 5, 5 and its lower left corner is at 11, 11.&lt;br /&gt;
&lt;br /&gt;
== Using Collision Detection with Sprites ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It is common to want to detect collisions between sprites - such as a player and an enemy.  This often results in code like the following being scattered through a game&#039;s code to create Rects with the same position, width and height as the player and enemy sprites.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rect player = { player.x, player.y, 16, 16 };&lt;br /&gt;
Rect enemy =  { enemy.x, enemy.y, 16, 16 };&lt;br /&gt;
&lt;br /&gt;
Serial.println(arduboy.collide(player, enemy);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is perfectly acceptable when there are only a handful of sprites to track - but what if there were dozens, and their sizes all different?  The chance of coding errors increases significantly!&lt;br /&gt;
&lt;br /&gt;
Assuming you are using classes to represent your players, enemies and other game play elements, a typical design pattern for action games is to define a class to hold information about players including their current position.  A sample header and implementation class is shown below&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Player.h:&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;Arduboy2.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class Player {&lt;br /&gt;
&lt;br /&gt;
   public:&lt;br /&gt;
     Rect getRect();&lt;br /&gt;
     &lt;br /&gt;
   int16_t x;&lt;br /&gt;
   int16_t y;&lt;br /&gt;
   const uint8_t *bitmap;&lt;br /&gt;
   &lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Player.cpp:&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;Player.h&amp;quot;&lt;br /&gt;
#include &amp;quot;Arduboy2.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Rect Player::getRect() {&lt;br /&gt;
   &lt;br /&gt;
  return (Rect){x, y, pgm_read_byte(bitmap), pgm_read_byte(bitmap + 1)};&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The header declares three public variables for the player’s coordinates and the image used when rendering the screen.  It also prototypes a function that will return a rectangle structure that will describe the area on screen that the player currently occupies.  The implementation of that function is included in the .cpp file.  It simply creates and returns a populated Rect structure using the ‘x’ and ‘y’ coordinates from the class itself and the width and height of the image by reading these from the image array itself which happen to be the first two bytes when using the standard Sprites library. If you are unfamiliar with how sprite data is stored refer [[Sprites / SpritesB Library|here]].&lt;br /&gt;
&lt;br /&gt;
The code below shows the declaration of two player objects and shows how they can be used when testing collisions. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Player playerA;&lt;br /&gt;
Player playerB;&lt;br /&gt;
&lt;br /&gt;
playerA.x = 20;&lt;br /&gt;
playerA.y = 12;&lt;br /&gt;
playerA.bitmap = squareA;&lt;br /&gt;
&lt;br /&gt;
playerB.x = 100;&lt;br /&gt;
playerB.y = 24;&lt;br /&gt;
playerB.bitmap = squareB;&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
Serial.print(&amp;quot;A collides with B : &amp;quot;);&lt;br /&gt;
Serial.println(arduboy.collide(playerA.getRect(), playerB.getRect()));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In my opinion, this approach results in much more readable code. &lt;br /&gt;
&lt;br /&gt;
The simple collision application and the ‘advanced’ version using a ‘Player’ class can be found in my repository.&lt;br /&gt;
&lt;br /&gt;
https://github.com/filmote/Collide_Article1  (simple example)&lt;br /&gt;
&lt;br /&gt;
https://github.com/filmote/Collide_Article2  (using the ‘Player’ class).&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Collision_Detection&amp;diff=587</id>
		<title>Collision Detection</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=Collision_Detection&amp;diff=587"/>
		<updated>2024-08-16T04:30:16Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=== Background ===&lt;br /&gt;
The Arduboy2 library provides functions to detect when two rectangles or a rectangle and a point overlap.  It is implemented in such a way that is independent of other constructs such as the screen and sprites.  In practice though, the detection of collisions is often focused on the collision of sprites (players, bullets, etc) with other entities (enemy sprites, background elements or other play elements).  These collisions may occur onscreen or off and functions within the library are flexible enough to cater for this.&lt;br /&gt;
&lt;br /&gt;
The following examples and code assume you are familiar with the concept and functions available to render sprites.  More information on this can be found in the article covering the [[Sprites / SpritesB Library]].&lt;br /&gt;
&lt;br /&gt;
=== But how does the collision detection work? ===&lt;br /&gt;
The Arduboy2 library provides two collision detection methods which are shown below.  &amp;lt;pre&amp;gt;&lt;br /&gt;
bool collide(Point point, Rect rect);&lt;br /&gt;
bool collide(Rect rect1, Rect rect2);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It also provides two structures to assist.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
struct Rect {&lt;br /&gt;
  int16_t x;&lt;br /&gt;
  int16_t y;&lt;br /&gt;
  uint8_t width;&lt;br /&gt;
  uint8_t height; &lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
struct Point {&lt;br /&gt;
  int16_t x; &lt;br /&gt;
  int16_t y; &lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The structures describe a rectangle and a point in space - the x / y coordinates are arbitrary and their origin can be anywhere but it typical to align these with screen&#039;s coordinates.  Thus 0, 0 is located at the top-left corner of the screen with negative x and y values representing locations to the left and above the screen respectively.&lt;br /&gt;
&lt;br /&gt;
The first function tests to see if a point is contained within the specified rectangle.  The sample code below demonstrates this:&lt;br /&gt;
&lt;br /&gt;
[[File:Collision 00.png|thumb]]&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Point p;&lt;br /&gt;
Rect r;&lt;br /&gt;
&lt;br /&gt;
p.x = 5;&lt;br /&gt;
p.y = 5;&lt;br /&gt;
&lt;br /&gt;
r.x = 2;&lt;br /&gt;
r.y = 2;&lt;br /&gt;
r.width = 10;&lt;br /&gt;
r.height = 10;&lt;br /&gt;
&lt;br /&gt;
Serial.println(arduboy.collision(p, r));    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Running the above code will print 1 as the point at position 5, 5 is in the centre of the square whose top left corner is at position 2, 2 and lower right corner is at 11, 11.  &lt;br /&gt;
&lt;br /&gt;
Moving the point outside of the square with the following code:&lt;br /&gt;
[[File:Collision 01.png|thumb]]&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
p.x = 14;&lt;br /&gt;
p.y = 2;&lt;br /&gt;
&lt;br /&gt;
Serial.println(arduboy.collision(p, r));    &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Will print 0 as the point at position 14, 2 is outside of the square whose top left corner is at position 5, 5 and lower right corner is at 15, 15.&lt;br /&gt;
&lt;br /&gt;
The second function compares the position of two rectangles and tests to see if there is any overlap.  This is demonstrated in the code below:&lt;br /&gt;
[[File:Collision 03.png|thumb]]&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rect r1 = {2, 2, 5, 5};&lt;br /&gt;
Rect r2 = {5, 5, 7, 7};&lt;br /&gt;
&lt;br /&gt;
Serial.println(arduboy.collision(r1, r2));&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Running the above code will print 1 as the two rectangles overlap.  The first rectangle’s top left corner is at 2, 2 and its lower right corner is at 6, 6. The second rectangle’s top left corner is at 5, 5 and its lower left corner is at 11, 11. &lt;br /&gt;
&lt;br /&gt;
Moving the smaller rectangle away from the larger ones so they do not overlap: &lt;br /&gt;
&lt;br /&gt;
 [[File:Collision 04.png|thumb]] &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r1.x = 0;&lt;br /&gt;
r1.y = 0;&lt;br /&gt;
&lt;br /&gt;
Serial.println(arduboy.collision(r1, r2))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Running the above code will print 0 as the two rectangles do not overlap anymore. The first rectangle’s top left corner is now at 0, 0 and its lower right corner is at 4, 4. The second rectangle’s top left corner is at 5, 5 and its lower left corner is at 11, 11.&lt;br /&gt;
&lt;br /&gt;
One (minor) issue with the detection code is that when detecting collisions, you need to construct the ‘rectangle’ and ‘point’ structures based on the positions of the players and obstacles in the game.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Serial.print(&amp;quot;A collides with B : &amp;quot;);&lt;br /&gt;
Serial.println(arduboy.collide( (Rect){xA, yA, 16, 16}, (Rect){xB, yB, 16, 16} ));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the sample code above, two rectangle structures are populated with the current positions of the moving squares and the image size (16 x 16 pixels).  This is relatively simple when there are only a few objects to consider but can become a little more complex and error prone if there are numerous objects with different sizes.&lt;br /&gt;
&lt;br /&gt;
A typical design pattern for action games is to define a class to hold information about players including their current position.  A sample header and implementation class is shown below&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Player.h:&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;Arduboy2.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class Player {&lt;br /&gt;
&lt;br /&gt;
   public:&lt;br /&gt;
     Rect getRect();&lt;br /&gt;
     &lt;br /&gt;
   int16_t x;&lt;br /&gt;
   int16_t y;&lt;br /&gt;
   const uint8_t *bitmap;&lt;br /&gt;
   &lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Player.cpp:&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;Player.h&amp;quot;&lt;br /&gt;
#include &amp;quot;Arduboy2.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Rect Player::getRect() {&lt;br /&gt;
   &lt;br /&gt;
  return (Rect){x, y, pgm_read_byte(bitmap), pgm_read_byte(bitmap + 1)};&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The header declares three public variables for the player’s coordinates and the image used when rendering the screen.  It also prototypes a function that will return a rectangle structure that will describe the area on screen that the player currently occupies.  The implementation of that function is included in the .cpp file.  It simply creates and returns a populated Rect structure using the ‘x’ and ‘y’ coordinates from the class itself and the width and height of the image by reading these from the image array itself.&lt;br /&gt;
&lt;br /&gt;
The code below shows the declaration of two player objects and shows how they can be used when testing collisions. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Player playerA;&lt;br /&gt;
Player playerB;&lt;br /&gt;
&lt;br /&gt;
playerA.x = 20;&lt;br /&gt;
playerA.y = 12;&lt;br /&gt;
playerA.bitmap = squareA;&lt;br /&gt;
&lt;br /&gt;
playerB.x = 100;&lt;br /&gt;
playerB.y = 24;&lt;br /&gt;
playerB.bitmap = squareB;&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
Serial.print(&amp;quot;A collides with B : &amp;quot;);&lt;br /&gt;
Serial.println(arduboy.collide(playerA.getRect(), playerB.getRect()));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In my opinion, this approach results in much more readable code. &lt;br /&gt;
&lt;br /&gt;
The simple collision application and the ‘advanced’ version using a ‘Player’ class can be found in my repository.&lt;br /&gt;
&lt;br /&gt;
https://github.com/filmote/Collide_Article1  (simple example)&lt;br /&gt;
https://github.com/filmote/Collide_Article2  (using the ‘Player’ class).&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=File:Collision_04.png&amp;diff=586</id>
		<title>File:Collision 04.png</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=File:Collision_04.png&amp;diff=586"/>
		<updated>2024-08-16T04:28:47Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Collision 04&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
	<entry>
		<id>https://bloggingadeadhorse.com/arduboyWiki/index.php?title=File:Collision_03.png&amp;diff=585</id>
		<title>File:Collision 03.png</title>
		<link rel="alternate" type="text/html" href="https://bloggingadeadhorse.com/arduboyWiki/index.php?title=File:Collision_03.png&amp;diff=585"/>
		<updated>2024-08-16T04:25:21Z</updated>

		<summary type="html">&lt;p&gt;Filmote: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Collision 03&lt;/div&gt;</summary>
		<author><name>Filmote</name></author>
	</entry>
</feed>