Hello guys
I have a working N5110 library on my Stm32F401 which using plain C, but recently, I came across this Arduino library:
https://github.com/cbm80amiga/N5110_SPIWhat I like about this library is the extensive fonts that it has. So, I wanted to just port 2 function from this library which are (N5110_SPI::setFont) and (N5110_SPI::printChar) , and use my library to initialize the display and send data using SPI.
Bellow, is the C++ code:
//------------------------------------------------------------------------
void N5110_SPI::setFont(const uint8_t* f)
{
font = f;
xSize =-pgm_read_byte(font+0);
ySize = pgm_read_byte(font+1);
firstCh = pgm_read_byte(font+2);
lastCh = pgm_read_byte(font+3);
ySize8 = (ySize+7)/8;
minCharWd = 0;
minDigitWd = 0;
cr = 0;
invertCh = 0;
invertMask = 0xff;
}
//--------------------------------------------------------------------------
int N5110_SPI::printChar(int x, uint8_t row, uint8_t _ch)
{
int ch = _ch;
if(!font || ch<firstCh || ch>lastCh || x>=SCR_WD || row>=SCR_HT/8) return 0;
int j,i, idx = 4 + (ch - firstCh)*(xSize*ySize8+1);
int wd = pgm_read_byte(font + idx++);
int wdL = 0, wdR = 1; // default spacing before and behind char
if((*isNumberFun)(ch)) {
if(minDigitWd>wd) {
wdL = (minDigitWd-wd)/2;
wdR += (minDigitWd-wd-wdL);
}
} else
if(minCharWd>wd) {
wdL = (minCharWd-wd)/2;
wdR += (minCharWd-wd-wdL);
}
if(x+wd+wdL+wdR>SCR_WD) wdR = max(SCR_WD-x-wdL-wd, 0);
if(x+wd+wdL+wdR>SCR_WD) wd = max(SCR_WD-x-wdL, 0);
if(x+wd+wdL+wdR>SCR_WD) wdL = max(SCR_WD-x, 0);
for(j=0; j<ySize8; j++) {
gotoXY(x, row+j);
setDat();
startCS();
if(!invertCh) {
for(i=0; i<wdL; i++) sendData(0);
for(i=0; i<wd; i++) sendData(pgm_read_byte(font+idx+i*ySize8+j));
for(i=0; i<wdR; i++) sendData(0);
} else {
for(i=0; i<wdL; i++) sendData(invertMask);
for(i=0; i<wd; i++) sendData(pgm_read_byte(font+idx+i*ySize8+j)^invertMask);
for(i=0; i<wdR; i++) sendData(invertMask);
}
stopCS();
}
return wd+wdL+wdR;
}
My code is below, that compiles, but doesn't display anything on the display:
//------------------------------------------------------------------
void N5110_setFont(const uint8_t* f)
{
font = f;
xSize =-(font[0]+0);
ySize = (font[0]+1);
firstCh = (font[0]+2);
lastCh = (font[0]+3);
ySize8 = (ySize+7)/8;
minCharWd = 0;
minDigitWd = 0;
cr = 0;
invertCh = 0;
invertMask = 0xff;
}
int N5110_printChar(int x, uint8_t row, uint8_t _ch)
{
int ch = _ch;
if(!font || ch<firstCh || ch>lastCh || x>=SCR_WD || row>=SCR_HT/8) return 0;
int j,i, idx = 4 + (ch - firstCh)*(xSize*ySize8+1);
int wd = (font[0] + idx++);
int wdL = 0, wdR = 1; // default spacing before and behind char
if((*isNumberFun)(ch)) {
if(minDigitWd>wd) {
wdL = (minDigitWd-wd)/2;
wdR += (minDigitWd-wd-wdL);
}
} else
if(minCharWd>wd) {
wdL = (minCharWd-wd)/2;
wdR += (minCharWd-wd-wdL);
}
if(x+wd+wdL+wdR>SCR_WD) wdR = max(SCR_WD-x-wdL-wd, 0);
if(x+wd+wdL+wdR>SCR_WD) wd = max(SCR_WD-x-wdL, 0);
if(x+wd+wdL+wdR>SCR_WD) wdL = max(SCR_WD-x, 0);
for(j=0; j<ySize8; j++) {
gotoXY(x, row+j);
setDat();
startCS();
if(!invertCh) {
for(i=0; i<wdL; i++) glcd_data(0);
for(i=0; i<wd; i++) glcd_data((font[0]+idx+i*ySize8+j));
for(i=0; i<wdR; i++) glcd_data(0);
} else {
for(i=0; i<wdL; i++) glcd_data(invertMask);
for(i=0; i<wd; i++) glcd_data((font[0]+idx+i*ySize8+j)^invertMask);
for(i=0; i<wdR; i++) glcd_data(invertMask);
}
stopCS();
}
return wd+wdL+wdR;
}
for the "max" function, because it doesn't exist in C, I found online which is:
int max(int a, int b) {
return (a > b) ? a : b;
}
I sure something is wrong with my code, but I can't figure it out by my self. Please, guys don't be hard on me, as I'm just a hobbyist, not an expert in programming. Can someone guide me how to port these 2 functions to plain C please?
Thank you in advance