Displaying the code
Displaying the code
Now that we've got the needed code, it's time to use it in a program.
We've already shown that 'sprites.c' file that holds various icons' codes. Since all of those icons are declared as global constants, we're going to do the same thing here.
To do this next step, you have to open either CircuitBlocks or Arduino IDE. If you have setup Ringo library in any other text editor, that will do the trick as well.
This tutorial is going to be done inside CircuitBlocks, but the process itself is the same for Arduino.
First, open CircuitBlocks and start a new project.
You need to select the 'Code' button since we're going to have to type a little bit here.
This process can't be done in a 'Block' project. What you can do is take your block project's code, copy it to the code project, and then add the finishing touches there.
Now, let's copy our bitmap snippet as the global constant.
For that all we need to do is write the following line:
1 |
const unsigned short snake_icon[SIZE] PROGMEM = { ... } |
With our colors' hex codes being between the brackets.
That means that it would all look like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
const unsigned short snake_icon[0x270] PROGMEM = { 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x0000, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x0000, 0x0000, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x0000, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x0000, 0x0000, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0xf800, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0xf800, 0xf800, 0xf800, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xf800, 0xf800, 0xf800, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xf800, 0xf800, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, }; |
The SIZE variable actually determines how many pixels are there. Since we have an icon that is 24x26, that would be a total of 624 pixels. If you convert that to hexadecimal (notice the '0x' before the number) it would translate to 270, which is the number we've got here.
The rest of our variable are just color codes for each specific pixel. If you look closely, you'll notice that the white color (0xffff) that colors our icon's outline, is on the "edges" of the code.
Now, all we need to do is print that little icon out.
We've also added a little text so that the icon is not lonely on the screen!
Function 'mp.display.drawIcon()' has variables in this order: (imageFile, locationX, locationY, width, height, scale).
Pretty self-explanatory as it is, just like in the previous example.
Here is the full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <MAKERphone.h> MAKERphone mp; const unsigned short snake_icon[0x270] PROGMEM = { 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x0000, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x0000, 0x0000, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x0000, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x0000, 0x0000, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0xf800, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0xf800, 0xf800, 0xf800, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xf800, 0xf800, 0xf800, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xf800, 0xf800, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x2dc0, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0x42bc, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, }; void setup() { mp.begin(1); mp.display.fillScreen(TFT_BLACK); mp.display.setCursor(34,38); mp.display.setTextColor(TFT_GREEN); mp.display.print("SNAKE icon"); mp.display.drawIcon(snake_icon, 52, 56, 24, 26, 2); } void loop() { mp.update(); } |
See, it's not that hard!
Transparent bitmaps
What if we want to have an icon that is not a rectangle, but rather a circle or some other shape?
Well, since all icons are rectangles, that means that some pixels are going to have to be transparent.
The problem with bitmaps - they don't have a transparent channel. Luckily, you can just pick a color that will serve as a transparent one, so that each pixel with that color will not be shown.
In the function drawIcon(), you can also add a color parameter.
1 |
void TFT_eSPI::drawIcon(const unsigned short* icon, int16_t x, int16_t y, uint16_t width, uint16_t height, uint8_t scale, int32_t backgroundColor); |
So for example, if we wanted to draw our snake icon without white color outlines, our function would look something like this:
1 |
mp.display.drawIcon(snake_icon, 0, 0, 24, 26, 1, 0xffff); |
On our screen now we have 24x26 snake icon located in the upper left corner of the screen, and of course, without white borders.
Now you can get to drawing and importing some more complex images to your new apps!