CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

call function with const tables.

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
georpo



Joined: 18 Nov 2008
Posts: 281
Location: Athens, Greece.

View user's profile Send private message

call function with const tables.
PostPosted: Wed Jul 10, 2019 11:03 pm     Reply with quote

Hi all!

I am using an oled with fonts created with "TheDotFactory".
Then I set/clear the pixels with my custom code:

Code:

void OledText(int TextX,int TextY,int Size){   
   int Character=0,Offset=0,Width=0,Height=0,S=0,E=0,p=0;
   int BitComp=0;

   while(TextBuffer[p]){
   
    Character=TextBuffer[p++];
    Character-=32;

    if(Size==1){
     Width=arialUnicodeMS_8ptDescriptors[Character][0];
     Height=arialUnicodeMS_8ptDescriptors[Character][1];
     Offset=arialUnicodeMS_8ptDescriptors[Character][2];
    }
    if(Size==2){
     Width=arialUnicodeMS_10ptDescriptors[Character][0];
     Height=arialUnicodeMS_10ptDescriptors[Character][1];
     Offset=arialUnicodeMS_10ptDescriptors[Character][2];
    }
    if(Size==3){
     Width=arialUnicodeMS_12ptDescriptors[Character][0];
     Height=arialUnicodeMS_12ptDescriptors[Character][1];
     Offset=arialUnicodeMS_12ptDescriptors[Character][2];
    }
    if(Size==4){
     Width=arialUnicodeMS_14ptDescriptors[Character][0];
     Height=arialUnicodeMS_14ptDescriptors[Character][1];
     Offset=arialUnicodeMS_14ptDescriptors[Character][2];
    }
    if(Size==5){
     Width=arialUnicodeMS_16ptDescriptors[Character][0];
     Height=arialUnicodeMS_16ptDescriptors[Character][1];
     Offset=arialUnicodeMS_16ptDescriptors[Character][2];
    }

    BitComp=1;

    for(S=0;S<Height;++S){           //Do 'Height' vertical pixels

     for(E=0;E<Width;++E){           //Do 'width' horizontal pixels
      if(Size==1){
       if(arialUnicodeMS_8ptBitmaps[Offset+E]&BitComp) SetPixel(TextX+E,TextY+S);
       else ClearPixel(TextX+E,TextY+S);
      }
      if(Size==2){
       if(arialUnicodeMS_10ptBitmaps[Offset+E]&BitComp) SetPixel(TextX+E,TextY+S);
       else ClearPixel(TextX+E,TextY+S);
      }
      if(Size==3){
       if(arialUnicodeMS_12ptBitmaps[Offset+E]&BitComp) SetPixel(TextX+E,TextY+S);
       else ClearPixel(TextX+E,TextY+S);
      }
      if(Size==4){
       if(arialUnicodeMS_14ptBitmaps[Offset+E]&BitComp) SetPixel(TextX+E,TextY+S);
       else ClearPixel(TextX+E,TextY+S);
      }
      if(Size==5){
       if(arialUnicodeMS_16ptBitmaps[Offset+E]&BitComp) SetPixel(TextX+E,TextY+S);
       else ClearPixel(TextX+E,TextY+S);
      }
     }                          //Finished 1 line of pixels

     ClearPixel(TextX+E,TextY+S);   //Clear 1 pixel to delete previous content.Related to 1 pixel space below

     BitComp*=2;               // Move to Higher bit for next compare

     if(BitComp>128){
      BitComp=1;               //Reset comparison to lowest bit
      Offset+=Width;            //and move to next set of bytes
     }
    }                        //finished 1 character

    TextX+=Width+1;            //increment X by last character width +1 pixel space

   }

}


I will need to add more fonts, so is there any more efficient way instead of all those "IFs"?

Thanks for any feedback!
_________________
George.
allenhuffman



Joined: 17 Jun 2019
Posts: 552
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Fri Jul 12, 2019 10:38 am     Reply with quote

You might consider making it table driven. What data type are the "arialUnicodeMS_8ptDescriptors" things?

You'd have a structure that contains the Size you want to look up, and the addresses of each of your fonts. Something like this:

Code:

typedef struct
{
    int     Size;
    int8    *FontPtr; // Whatever your font type is
} FontInfoStruct;

FontInfoStruct FontInfo[] =
{
    // Size Font
    { 1,    arialUnicodeMS_8ptDescriptors },
    { 2,    arialUnicodeMS_10ptDescriptors },
    { 3,    arialUnicodeMS_12ptDescriptors },
    { 4,    arialUnicodeMS_14ptDescriptors },
    { 5,    arialUnicodeMS_16ptDescriptors },
    { 0,    0 } // End of Table
};


Then you could just loop through that table looking for the matching size, and when you find it, you also have the address for the font structure you want to use. Something like this:

Code:

   // Size is your target size
   // Character is your character

    int Width, Height, Offset;
   
    Width = 0;
    Height = 0;
    Offset = 0;
   
    int Entry = 0;
    do
    {
        if (FontInfo[Entry].Size == Size)
        {
            Width = FontInfo[Entry].FontPtr[Character][0];
            Height = FontInfo[Entry].FontPtr[Character][1];
            Offset = FontInfo[Entry].FontPtr[Character][2];
            break;
        }
        Entry++;
    } while (FontInfo[Entry].Size != 0);

    printf( "Size: %u  Character: %u  Width: %u  Height %u  Offset: %u\r\n",
            Size, Character, Width, Height, Offset);


This is just pseudo code to give you an idea of what I mean.
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group