Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to do a lookup table in C?

I am working on an embedded C project. I have an LCD display and for each character there is a 5x7 dot matrix. To display a specific character you have to shift in 5 bytes that correlate with the dots to turn on. So I need to make some kind of look-up table with a key where I can pass in an ASCII character, and get an array of 5 bytes returned... For example, a call to this function like this,

GetDisplayBytes('A');

should return `an array like this...

C[0] = 0x7E : C[1] = 0x90 : C[2] = 0x90 : C[3] = 0x90 : C[4] = 0x7E

What would be the best way to do this in C?

like image 365
PICyourBrain Avatar asked Aug 04 '10 18:08

PICyourBrain


People also ask

What is a lookup table in C?

Well a lookup table is simply an initialized array that contains precalculated information. They are typically used to avoid performing complex (and hence time consuming) calculations. For example, it is well known that the speed of CRC calculations may be significantly increased by use of a lookup table.

How is lookup table faster in execution?

The execution speed is faster than that for unevenly spaced data, because the position search is faster and the interpolation requires a simple division.

What is table lookup method?

A table lookup is performed when you use the value of one (or more) variables (e.g. a patient identification code) to determine the value of another variable or variables (e.g. patient name). Often this second piece of information must be 'looked up' in some other secondary table or location.

What is lookup table with example?

In data analysis applications, such as image processing, a lookup table (LUT) is used to transform the input data into a more desirable output format. For example, a grayscale picture of the planet Saturn will be transformed into a color image to emphasize the differences in its rings.


2 Answers

I would make arrays for the contiguous ASCII blocks you want to use. data. Something like this:

uint8_t displayBytesLetters[] = 
{
  0x73, 0x90, 0x90, 0x90, 0x73, // 'A'
  .
  .
  .
};

uint8_t displayBytesDigits[] = 
{
  0x12, 0x15, 0x25, 0x58, 0x80, // '0'
  .
  .
  .
};

Then your GetDisplayBytes() is something like:

uint8_t *GetDisplayBytes(char c)
{
  if (isdigit(c))
    return &displayBytes[5*(c - '0')];
  else if (isupper(c))
    return &displayBytes[5*(c - 'A')];
  else
    return NULL;
}

Pass the returned pointer to whatever function outputs the data:

void DoDisplay(uint8_t *displayBytes)
{
  int i;
  for (i = 0; i < 5; i++) 
  {
     SendOutput(displayBytes[i]);
  }
}
like image 63
Carl Norum Avatar answered Sep 25 '22 06:09

Carl Norum


typedef char LCDDATA[5];   

LCDDATA lcdTable[256] = { {0,0,0,0,0},  // char 0
                          {.....},       // char 1
                        }

LCDDATA GetDisplayBytes(char chr)
{
     return lcdTable[chr];
}

This is basically making an array of arrays.

like image 28
James Curran Avatar answered Sep 24 '22 06:09

James Curran