我正在为我的学校项目编写一个Max72xx库,我找到了一种使用数据表附带的寄存器设置列的方法.现在,我也找到了一种设定高度的方法.所以我创建了一个带有两个参数的函数:row基本上将x轴发送到芯片,以及设置y轴的高度.
我在这段代码中所做的基本上如下:
const uint8_t& row)uint8_t height)我想实现以下目标:
height设置为3:应创建以下位模式:1110 000.或者,如果用户使用8,我希望创建该功能1111 1111.或者如果用户将5设置为y轴,我希望fucntin创建一个位模式1111 1000,等等.我提供的代码如下(这段代码在函数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;
Run Code Online (Sandbox Code Playgroud)
MSB是显示器上的最低像素,LSB是最高像素.现在这个代码就像我之前描述的那样工作,但我认为有一种更简单,更有效的方法来解决这个问题.我想知道一些建议.提前致谢.
只要用这个:
uint8_t pattern = (0xff00u >> height);
Run Code Online (Sandbox Code Playgroud)
或这个:
uint8_t pattern = (0xffu << (8 - height));
Run Code Online (Sandbox Code Playgroud)
只要参数是无符号的,就不要害怕位移过程中的溢出,行为是明确定义的.