在C中执行查找表的最佳方法是什么?

时间:2021-03-28 07:37:00

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,

我正在开发一个嵌入式C项目。我有一个LCD显示屏,每个字符有一个5x7点阵。要显示特定字符​​,您必须以5个字节移动,这些字节与要打开的点相关联。所以我需要使用一个键来制作某种查找表,我可以传入一个ASCII字符,并获得一个返回5个字节的数组...例如,调用这个函数就像这样,

GetDisplayBytes('A');

should return `an array like this...

应该返回`这样的数组......

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

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?

在C中这样做的最佳方法是什么?

2 个解决方案

#1


14  

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

我会为你想要使用的连续ASCII块创建数组。数据。像这样的东西:

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

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

Then your GetDisplayBytes() is something like:

然后你的GetDisplayBytes()是这样的:

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]);
  }
}

#2


2  

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.

这基本上是一个数组的数组。

#1


14  

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

我会为你想要使用的连续ASCII块创建数组。数据。像这样的东西:

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

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

Then your GetDisplayBytes() is something like:

然后你的GetDisplayBytes()是这样的:

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]);
  }
}

#2


2  

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.

这基本上是一个数组的数组。