Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace data structure of segment maps #2

Open
Hydrantz opened this issue May 14, 2023 · 1 comment
Open

Replace data structure of segment maps #2

Hydrantz opened this issue May 14, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@Hydrantz
Copy link
Owner

Hydrantz commented May 14, 2023

byte seven_segment_map[10][8] = {
{1,1,1,1,1,1,0,0},//0
{0,1,1,0,0,0,0,0},//1
{1,1,0,1,1,0,1,0},//2
{1,1,1,1,0,0,1,0},//3
{0,1,1,0,0,1,1,0},//4
{1,0,1,1,0,1,1,0},//5
{1,0,1,1,1,1,1,0},//6
{1,1,1,0,0,0,0,0},//7
{1,1,1,1,1,1,1,0},//8
{1,1,1,1,0,1,1,0}, //9
};
byte sixteen_segment_map[42][16] = {
{1,1,1,1,1,1,1,1,0,0,0,0,1,0,0,1},
{0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0},
{1,1,1,0,1,1,1,0,1,1,0,0,0,0,0,0},
{1,1,1,1,1,1,0,0,1,1,0,0,0,0,0,0},
{0,0,1,1,0,0,0,1,1,1,0,0,0,0,0,0},
{1,1,0,1,1,1,0,1,1,1,0,0,0,0,0,0},
{1,1,0,1,1,1,1,1,1,1,0,0,0,0,0,0},
{1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
{1,1,1,1,1,1,0,1,1,1,0,0,0,0,0,0},
{1,1,1,1,0,0,1,1,1,1,0,0,0,0,0,0},
{1,1,1,1,1,1,0,0,0,1,0,1,0,0,1,0},
{1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,0,0,0,0,0,1,0,0,1,0},
{1,1,0,0,1,1,1,1,1,1,0,0,0,0,0,0},
{1,1,0,0,0,0,1,1,1,0,0,0,0,0,0,0},
{1,1,0,1,1,1,1,1,0,1,0,0,0,0,0,0},
{0,0,1,1,0,0,1,1,1,1,0,0,0,0,0,0},
{1,1,0,0,1,1,0,0,0,0,0,1,0,0,1,0},
{0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0},
{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
{0,0,1,1,0,0,1,1,0,0,1,0,1,0,0,0},
{0,0,1,1,0,0,1,1,0,0,1,0,0,1,0,0},
{1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0},
{1,1,1,0,0,0,1,1,1,1,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,0,0,0,0,0,1,0,0},
{1,1,1,0,0,0,1,1,1,1,0,0,0,1,0,0},
{1,1,0,1,1,1,0,1,1,1,0,0,0,0,0,0},
{1,1,0,0,0,0,0,0,0,0,0,1,0,0,1,0},
{0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,1},
{0,0,1,1,0,0,1,1,0,0,0,0,0,1,0,1},
{0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,1},
{0,0,1,1,1,1,0,1,1,1,0,0,0,0,0,0},
{1,1,0,0,1,1,0,0,0,0,0,0,1,0,0,1},
{1,1,0,1,1,1,0,1,1,1,0,1,0,0,1,0},
{1,0,0,1,0,1,0,1,1,1,0,1,1,0,1,1},
{1,0,0,0,1,1,1,0,1,0,1,1,0,1,0,0},
{0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};

Consider replacing data structures of seven_segment_map and sixteen_segment_map with more efficient options.

@Hydrantz Hydrantz self-assigned this May 14, 2023
@Hydrantz Hydrantz added the enhancement New feature or request label May 14, 2023
@idanai
Copy link

idanai commented May 17, 2023

You can use binary literals and uint16_t: https://stackoverflow.com/questions/537303/binary-literals

Try something like this for storage:

uint8_t seven_segment_map[10] = {
	0b11111100,//0
	0b01100000,//1
	// etc...
};

uint16_t sixteen_segment_map[42] = {
	0b1111111100001001,
	0b0011000000001000,
	// etc...
};

Try something like this for access:

// mask: value with up to 32 bits. Types with less bits are complemented with 0s automatically when cast.
// bit_index: number of the bit [0..31]. 0 is LSB, 31 is MSB, greater will return 0.
bool read_mask_bit(uint32_t mask, uint8_t bit_index) {
	return (mask >> bit_index) & 1;
}

I also recommend making your arrays const or using constexpr to prevent accidental writes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants