C语言实现按照比特位的数组在目标空间靠左靠右及其中间居中

在LED行业中,一般一个灯亮或者不亮用一个bit位来表示(这里就不谈七彩或者灰度控制卡),现在加入我们屏幕大小是128点,,相当于宽度16个字节,如果我们让两个汉字居中显示(两个汉字占宽度4个字节),轻轻松松就可以算出如果要实现居中,只要偏移(16 – 4) / 2 = 6个字节宽度,当然这里我们都是假象的理想情况,假如需要显示的字符宽度不是固定并且显示内容也经常变化,这种情况下,无法手动计算。本文的代码就实现这个功能,字符宽度可以随意设置,字符个数小于256,而且还可以设置靠左、靠右和居中显示,代码读起来可能不是那么顺畅,写起来也不顺畅,经反复测试没出任何问题。

#include <QCoreApplication>#include <QDebug>/******************************************************************************** * 从字节0xff某个高位开始往低位的方向截取长度(0~8),所得的内容 * 比如从bit6开始往低位截图长度为2,我们很快可以得知为0x60 * 这里面就是Get_Middle_Byte[7 – 6][2] ********************************************************************************/const uint8_t Get_Middle_Byte[8][9] = {{0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff},{0x00, 0x40, 0x60, 0x70, 0x78, 0x7c, 0x7e, 0x7f, 0x00},{0x00, 0x20, 0x30, 0x38, 0x3c, 0x3e, 0x3f, 0x00, 0x00},{0x00, 0x10, 0x18, 0x1c, 0x1e, 0x1f, 0x00, 0x00, 0x00},{0x00, 0x08, 0x0c, 0x0e, 0x0f, 0x00, 0x00, 0x00, 0x00},{0x00, 0x04, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00},{0x00, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},{0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},};/******************************************************************************** * *dest:目标空间头指针 * *src:需要填充的字符内容头指针 * *width,需要填充的字符宽度头指针 * charNum:需要填充的字符长度 * screenWidth:目标空间的总长度(按照bit位计算) * align:对其方式 0(左对齐) 1(居中对其) 2(靠右对其) ********************************************************************************/uint8_t Organization_One_Row_Char(uint8_t *dest, uint8_t *src, uint8_t *charWidth, uint8_t charNum, uint16_t screenWidth, uint8_t align){uint8_t count = 0;uint16_t allCharWidth = 0, offset = 0;uint8_t quo, rem, tmp;while (count < charNum)//计算所有填充位的长度{allCharWidth += charWidth[count];count++;}if (allCharWidth > screenWidth) //如果总长度大于屏幕长度,那就传递过来速度有错误。{return 1;}switch (align){case 1://居中显示offset = (screenWidth – allCharWidth) >> 1;break;case 2://靠右显示offset = screenWidth – allCharWidth;break;}//求出需要填充的当前字节和当前bit位quo = (uint8_t)(offset >> 3);rem = (uint8_t)(offset % 8);count = 0;while (count < charNum){if (rem + charWidth[count] < 8) //如果当前填充bit位与需要填充字符相加的值小于8,那就{//当前填充字节可以完全填充完,不需要切换下一个字节填充dest[quo] &= ~Get_Middle_Byte[rem][charWidth[count]];//填充空间的bit位长度charWidth[count]清零。dest[quo] |= ((src[count] & Get_Middle_Byte[0][charWidth[count]]) >> rem); //填充rem += charWidth[count]; //bit位填充空间往前移动当前填充字符的宽度}else{tmp = 8 – rem;//求当前字节未填充的bit长度,用作临时变量dest[quo] &= ~Get_Middle_Byte[rem][tmp]; //rem~bit7这几位清零dest[quo] |= ((src[count] & Get_Middle_Byte[0][tmp]) >> rem); //填充quo++;//切换到下一个字节dest[quo] &= ~Get_Middle_Byte[0][charWidth[count] – tmp];//当前填充字节(剩余未填充完的字符长度)长度清零dest[quo] |= ((src[count] & Get_Middle_Byte[tmp][charWidth[count] – tmp]) << (tmp)); //填充rem = charWidth[count] – tmp;//bit位重新切换到新位置}count++;}return 0;}int main(int argc, char *argv[]){QCoreApplication a(argc, argv);uint8_t t1[] = {0x00, 0x00, 0x00, 0x00, 0x00};uint8_t t2[] = {0xff, 0xff, 0xff, 0xff};uint8_t width[] = {7, 8, 8, 8};Organization_One_Row_Char(t1, t2, width, 4, 40, 1);for (int i = 0; i < 5; i++){qDebug() << i << hex << t1[i];}return a.exec();}

这一秒不放弃,下一秒就会有希望。

C语言实现按照比特位的数组在目标空间靠左靠右及其中间居中

相关文章:

你感兴趣的文章:

标签云: