我制作了一个小小的JavaScript应用程序这里,允许人们生成生成字符的位图。目前,它生成字典的代码。但我也想要产生一个例子,让他们玩像阿杜诺样例,使用它作为起点或使用它的-原样。
这是我编写的代码,它允许某人使用7段显示。
我想要一些关于代码本身的反馈,比如变量和函数名的建议,事物的顺序,美学等等。
//common cathode turns on with HIGH, and common anode turns on with LOW
const bool LED_P HIGH
const uint8_t myDisplayPins[8] = {3, 4, 5, 6, 7, 8, 9, 10}; //dp A B C D E F G
byte font[] = {
B1111110, // [0] => "0"
B0110000, // [1] => "1"
B1101101, // [2] => "2"
B1111001, // [3] => "3"
B0110011, // [4] => "4"
B1011011, // [5] => "5"
B1011111, // [6] => "6"
B1110000, // [7] => "7"
B1111111, // [8] => "8"
B1111011, // [9] => "9"
B1110111, // [10] => "A"
B0000001, // [11] => "dash"
};
void setup(){
display7Setup(myDisplayPins); //configure myDisplayPins as outputs
}
void loop(){
display7(myDisplayPins, font[0]); //display "0"
delay(1000);
display7(myDisplayPins, font[9]); //display "A"
delay(1000);
display7(myDisplayPins, font[11]); //display "-"
delay(1000);
display7(myDisplayPins, B00000000); //turn all segments OFF
delay(1000);
//count 0 to 9
for(int i=0; i<=9; i++){
display7(myDisplayPins, font[i]);
delay(1000);
}
}
/* Configure pins as outputs */
void display7Setup(const uint8_t displayPins[]){
for(uint8_t i=0; i<8; ++i){
pinMode(displayPins[i], OUTPUT);
}
}
/* Function that writes a bitmap to a 7-segment display */
void display7(const uint8_t displayPins[], byte bitmap){
for(uint8_t i=0; i<8; ++i){
byte segment = bitmap & (0x1<<i); //apply a mask to select just the desired segment bit
bool state = segment>>i; //shift the bit to LSF (rightmost) to get a boolean value 0x1 or 0x0
if(!LEDP) state = !state; //invert level for common anode
digitalWrite(displayPins[i], state);
}
}发布于 2014-11-18 17:58:22
loop重命名为demo。value对digitalWrite的参数只能是HIGH或LOW,并且没有详细说明它们的位表示形式。即使您的代码可以工作(我想),最好还是坚持规范。LEDP永远不会改变。您不需要反转循环中的每个单独的位。一次倒置整个位图看起来更好: if (LEDP == LOW) {位图^= 0xff;}display7循环的主体可以简化为for (uint8_t i=0;i<8;++i) { digitalWrite(displayPins我,(位图& 0x01)?>>= 1位图;}注意到segment和state完全消失了。https://codereview.stackexchange.com/questions/70184
复制相似问题