|
Post by powersoft on Jul 23, 2018 4:41:30 GMT -8
Dear James,
I while ago you posted this routine to calculate the width of a text string. This was very handy to position my text left centered. In the past when I used the GD2 it was working fine. When I used it as in the code below the return value is 2295!!! I'm using now the GD3.
Please can you take a look. Kindley regards,
Jan
insert code here #include <EEPROM.h> #include <SPI.h> #include <GD2.h>
int text_width(byte font,const char *s) { unsigned char widths[128]; uint32_t font_addr = GD.rd32(0xffffc); font_addr += 148 * (font - 16); for (int i = 0; i < 128; i++) widths = GD.rd(font_addr + i); int w = 0; while (*s) w += widths[*s++]; return w; }
void setup() { GD.begin(0); Serial.begin(115200); const char *msg1 = "UNIVERSUM"; int width=text_width(29,msg1); Serial.println(width);
}
void loop() { // put your main code here, to run repeatedly:
}
|
|
|
Post by jamesbowman on Jul 23, 2018 19:26:06 GMT -8
Aha, yes that code depends on ROM addresses, which differ between GD2 and GD3. In the current library there is a new function GD.textsize(). It returns the width and height of text, and is portable across GD2 and GD3. To use it you do: const char *msg1 = "UNIVERSUM"; int width, height;
GD.textsize(width, height, 29, msg1); In this case it computes the width as 136. Complete sketch is here: textsize.ino (301 B) My apologies - this is currently not documented.
|
|