Reputation: 9
I'm looking to modify this code so that it can display the output into 8x8 characters. For example if I type "gotcha", the output should be "GOTCHA" and if I type numbers the output will space character. I don't know how to use if,else statement for each letter of input. Right now I only use if
-else
statement for letter "a","A","b","B". And one more, can I use void
for char_led display[]
.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int a, b;
char c;
char led_display[] =
{
0x3c, 0x24, 0x24, 0x7e, 0x62, 0x62, 0x62, 0x00,//A
0x7c, 0x24, 0x24, 0x3e, 0x32, 0x32, 0x7e, 0x00,//B
0x3e, 0x22, 0x20, 0x60, 0x60, 0x62, 0x7e, 0x00,//C
0x7e, 0x22, 0x22, 0x32, 0x32, 0x32, 0x7e, 0x00,//D
0x3e, 0x20, 0x20, 0x78, 0x60, 0x60, 0x7e, 0x00,//E
0x3e, 0x20, 0x20, 0x78, 0x60, 0x60, 0x60, 0x00,//F
0x3e, 0x22, 0x20, 0x6e, 0x62, 0x62, 0x7e, 0x00,//G
0x24, 0x24, 0x24, 0x7e, 0x62, 0x62, 0x62, 0x00,//H
0x3e, 0x08, 0x08, 0x18, 0x18, 0x18, 0x3E, 0x00,//I
0x1c, 0x08, 0x08, 0x0C, 0x0C, 0x4C, 0x7c, 0x00,//J
0x24, 0x24, 0x28, 0x70, 0x68, 0x68, 0x66, 0x00,//K
0x20, 0x20, 0x20, 0x60, 0x60, 0x62, 0x7e, 0x00,//L
0x36, 0x3e, 0x2a, 0x62, 0x62, 0x62, 0x62, 0x00,//M
0x32, 0x2a, 0x2a, 0x6a, 0x6a, 0x66, 0x62, 0x00,//N
0x3e, 0x22, 0x22, 0x62, 0x62, 0x62, 0x7e, 0x00,//O
0x3e, 0x22, 0x22, 0x7e, 0x60, 0x60, 0x60, 0x00,//P
0x3e, 0x22, 0x22, 0x62, 0x6a, 0x64, 0x7a, 0x00,//Q
0x3e, 0x22, 0x22, 0x7e, 0x68, 0x64, 0x66, 0x00,//R
0x3c, 0x24, 0x20, 0x3c, 0x0c, 0x4c, 0x7c, 0x00,//S
0x3e, 0x08, 0x08, 0x18, 0x18, 0x18, 0x18, 0x00,//T
0x22, 0x22, 0x22, 0x62, 0x62, 0x62, 0x7e, 0x00,//U
0x22, 0x22, 0x22, 0x64, 0x68, 0x70, 0x60, 0x00,//V
0x22, 0x22, 0x22, 0x6a, 0x6a, 0x7e, 0x76, 0x00,//W
0x42, 0x24, 0x18, 0x3c, 0x64, 0x64, 0x66, 0x00,//X
0x66, 0x24, 0x14, 0x0c, 0x0c, 0x18, 0x30, 0x00,//Y
0x7e, 0x06, 0x0c, 0x18, 0x30, 0x60, 0x7e, 0x00,//Z
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//1
};
char letter[10];
cout << "Enter a string <max. 9 letter>: ";
cin.getline(letter, 10);
for (int i = 0; i < strlen(letter); i++)
{
char ch = letter[i];
ch = toupper(letter[i]);
cout << " " << ch;
if ((strcmp(letter, "a") == 0) || (strcmp(letter, "A") == 0))
{
for (a = 0; a < 8; a++)
{
cout << endl;
for (b = 0; b < 8; b++)
{
c = led_display[a] >> (7 - b) & 1;
if (c == 0)
{
cout << " ";
}
else
{
cout << char{ 0xDB };
}
}
}
}
else if ((strcmp(letter, "b") == 0) || (strcmp(letter, "B") == 0))
{
for (a = 8; a < 16; a++)
{
cout << endl;
for (b = 0; b < 8; b++)
{
c = led_display[a] >> (7 - b) & 1;
if (c == 0)
{
cout << " ";
}
else
{
cout << char{ 0xDB };
}
}
}
}
}
return 0;
}
Upvotes: 0
Views: 701
Reputation: 38939
I would change the interior of your for
loop to something like this:
char ch = toupper(letter[i]);
cout << " " << ch;
if (ch >= 'A' && ch <= 'Z')
{
const int intermediate = (ch - 'A') * 8;
for (char* i = led_display + intermediate; i < led_display + intermediate + 8; ++i){
cout << *i << ' ';
}
}
else
{
cout << "space";
}
Note that you're using "8" for the stride.
The if
statement will weed out non character entries.
Upvotes: 1
Reputation: 57743
First, I believe you need to change your data structure:
const unsigned int SEGMENTS_IN_DIGIT = 8;
struct Segment_Pattern
{
uint8_t segments[SEGMENTS_IN_DIGIT];
};
An easy implementation is a lookup table, or in this case an array:
Segment_Pattern letter_patterns[] =
{
{0x3c,0x24,0x24,0x7e,0x62,0x62,0x62,0x00}, // A
// ...
};
You can lookup the letter by performing the calculation:
char c = toupper(letters[i]);
unsigned int index = c - 'A';
Segment Pattern const * const p_pattern = &letter_patterns[index];
Upvotes: 1