简体   繁体   English

将uint_8更改为x乘以二进制1 MSB

[英]Change uint_8 to x times binary 1 MSB

I'm writing a Max72xx library for my school project and I have found a way to set columns using the registers that comes with the datasheet. 我正在为我的学校项目编写一个Max72xx库,我找到了一种使用数据表附带的寄存器设置列的方法。 Now, I also found a way to set their height. 现在,我也找到了一种设定高度的方法。 So I have created a function with two parameters: row , which basically sends the x-axis to the chip, and height which sets the y-axis. 所以我创建了一个带有两个参数的函数: row ,它基本上将x轴发送到芯片,以及设置y轴的高度。

What I did in this code is basically the following: 我在这段代码中所做的基本上如下:

  • The user sets an x-axis between 1 and 8 (parameter const uint8_t& row ) 用户将x轴设置在1到8之间(参数const uint8_t& row
  • The user sets an y-axis between 1 and 8 (parameter uint8_t height ) 用户将y轴设置在1到8之间(参数uint8_t height

I wanted to achieve the following: 我想实现以下目标:

  • If the height is set to three: the following bit pattern should be created: 1110 000 . 如果height设置为3:应创建以下位模式: 1110 000 Or, if the user uses 8, I want the function to create 1111 1111 . 或者,如果用户使用8,我希望该功能创建1111 1111 Or if the user sets 5 as y-axis I want the fucntin to create a bit pattern of 1111 1000 , etcetera. 或者,如果用户将5设置为y轴,我希望fucntin创建一个1111 1000的位模式,等等。

The code I have provided is the following (this piece of code is within the function setColumn): 我提供的代码如下(这段代码在函数setColumn中):

if (height >= 1 && height <= 8) { // if the height is between the matrix range
    uint8_t pattern = 0x00; // create a pattern to send to the chip
    uint8_t counter = 0; // counter for counting the zeros to shift left later on

    for (uint8_t i = 0; i < height - 1; i++) { // this loop is repeated height amount of times
        pattern |= 0x01; // create 1s in the pattern
        pattern <<= 0x01; // shift it 1 to the left
    }
    pattern |= 0x01; // OR the LSB bit of the pattern

    for (uint8_t i = 7; i > 0; i--) { // count the leading 0 bits to shift them to the MSB position
        if (!(pattern >> i) & 1)
            counter++;
        else break;
    }
    pattern <<= counter;

The MSB is the lowest pixel on the display, the LSB the highest pixel. MSB是显示器上的最低像素,LSB是最高像素。 Now this code works as I described earlier, but I think there is an easier and more efficient way to work this out. 现在这个代码就像我之前描述的那样工作,但我认为有一种更简单,更有效的方法来解决这个问题。 I'd like to know some suggestions. 我想知道一些建议。 Thanks in advance. 提前致谢。

Just use this: 只要用这个:

uint8_t pattern = (0xff00u >> height);

Or this: 或这个:

uint8_t pattern = (0xffu << (8 - height));

Don't be afraid of overflows during bit shifting as long as the arguments are unsigned , the behavior is well-defined . 只要参数是无符号的 ,就不要害怕位移过程中的溢出,行为是明确定义的

Only 8 patterns? 只有8种模式? I would just define a const array: 我只想定义一个const数组:

```
static const uint8_t patterns[] = {
  0x10000000,
  0x11000000,
  0x11100000,
  0x11110000,
  0x11111000,
  0x11111100,
  0x11111110,
  0x11111111,
};

if (height >= 1 && height <=8)
  return patterns[height - 1];

// raise an exception or so

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM