Replies: 1 comment
-
For everyone who comes across this thread with the same question - here is the answer. Including the option to display the memory used (for RP2040) #include <malloc.h>
#include <Arduino_GFX_Library.h>
// Define color codes in RGB565 format
#define COLOR_WHITE 0xFFFF // #FFFFFF
#define COLOR_GREY100 0xF7BE // #F7F7F7
#define COLOR_GREY200 0xEF7D // #ECEDED
#define COLOR_GREY300 0xDEFB // #DEDEDE
#define COLOR_GREY400 0xC638 // #C5C5C7
#define COLOR_GREY500 0xAD75 // #ADADAD
#define COLOR_GREY600 0x9492 // #929294
#define COLOR_GREY700 0x5ACB // #58585A
#define COLOR_GREY800 0x31C8 // #343A40
#define COLOR_BLACK 0x0000 // #000000
#define COLOR_REDDARK 0xA024 // #A70623
#define COLOR_RED 0xC0A4 // #C31727
#define COLOR_YELLOW 0xFDC2 // #FFBA10
#define COLOR_GREEN 0x13E7 // #107E3E
#define COLOR_Colors 14 // Total number of defined colors
// Define the data bus and display settings
Arduino_DataBus *bus = new Arduino_SWSPI(GFX_NOT_DEFINED /* DC */, 9 /* CS */, 10 /* SCK */, 11 /* MOSI */, 12 /* MISO */);
Arduino_GFX *gfx = new Arduino_ST7796(bus, 13 /* RST */, 1 /* rotation */, true /* IPS */, 272, 480, 0, 0, 24, 0);
// Create an Indexed Canvas with the display
Arduino_Canvas_Indexed *canvas = new Arduino_Canvas_Indexed(480, 272, gfx, 0, 0, 0, 0);
void getfreeRAM()
{
struct mallinfo mi = mallinfo();
int freeRAM = mi.fordblks; // Free memory in bytes
gfx->setCursor(300, 8);
gfx->setTextColor(RED);
gfx->printf("Free SRAM: %d", rp2040.getFreeHeap()); // free RAM in Bytes
gfx->setCursor(150, 8);
gfx->printf("Used SRAM: %d", rp2040.getUsedHeap()); // free RAM in Bytes
gfx->setCursor(10, 8);
gfx->printf("Total SRAM: %d", rp2040.getTotalHeap()); // free RAM in Bytes
}
void setup() {
// Initialize the display
gfx->begin();
gfx->fillScreen(0x319F); // Fill the screen
getfreeRAM();
delay(2000);
canvas->begin(GFX_SKIP_OUTPUT_BEGIN);
// Array containing all the defined colors
uint16_t colors[COLOR_Colors] = {
COLOR_WHITE, // White
COLOR_GREY100, // Grey 100
COLOR_GREY200, // Grey 200
COLOR_GREY300, // Grey 300
COLOR_GREY400, // Grey 400
COLOR_GREY500, // Grey 500
COLOR_GREY600, // Grey 600
COLOR_GREY700, // Grey 700
COLOR_GREY800, // Grey 800
COLOR_BLACK, // Black
COLOR_REDDARK, // Dark Red
COLOR_RED, // Red
COLOR_YELLOW, // Yellow
COLOR_GREEN, // Green
};
// Assign colors to the canvas' color index
for (int i = 0; i < COLOR_Colors; i++)
{
canvas->getColorIndex()[i] = colors[i];
}
canvas->setDirectUseColorIndex(true); // Enable direct use of color indices
canvas->fillScreen(5);
// Calculate stripe parameters
int margin = 20;
int stripeTilt = 20;
int availableWidth = 480 - 2 * margin - stripeTilt;
int stripeWidth = availableWidth / COLOR_Colors;
// Draw diagonal stripes across the canvas
for (int i = 0; i < COLOR_Colors; i++)
{
for (int j = 0; j < stripeWidth; j++)
{
canvas->drawLine(margin + stripeTilt + j + i * stripeWidth, margin, margin + j + i * stripeWidth, 272 - margin, i);
}
}
// Update the display to show the canvas content
canvas->flush();
// Alternative way to draw the canvas
// gfx->drawIndexedBitmap(0, 0, canvas->getFramebuffer(), canvas->getColorIndex(), 480, 272);
getfreeRAM();
}
void loop() {
}
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello Community,
I'm on the verge of despair.
I've been trying for 2 days to get a Canvas example running so that I can later incorporate it into my code.
I want to try it with Canvas_Indexed later on, but at the moment, it seems to be failing at the very basics.
Please help!
What's happening:
The content of setup() is still being drawn. However, everything in loop() no longer appears on the display.
I've tried both flush() and draw16bitRGBBitmap().
Does anyone have a working code for Arduino_Canvas or Arduino_Canvas_Indexed?
Beta Was this translation helpful? Give feedback.
All reactions