[Open-source] 7-segment display

I made this 7-segment display.

This only supports letters and numbers, and characters that are compatible with 7-segment displays. It also doesn’t include stuff like R and W, which has a 7-segment representaion, but personally I think it’s really weird.
image image
Also note that, uppercase and lowercase letters are different, n doesn’t have an uppercase represenation, it only has a lowercase one, if you inputted N, it will be ignored.

If you want to add them, you can really! I will show you how you can add whatever you want.

The script is fully commeneted, but how does this work exactly?

Welp, basically, each number will have its own unique pattern that it uses to get displayed on one of these. Let’s say each of 7-segment’s segments is recognized by a number, which is also how I name each segment UI.

3f0429dfa4565cc36e66e2556b96f115d824da9a image

If I wanted to display 7, I need to check which segments are turned on, and basically, the pattern will be a table of 7 members, where each value is either true or false, where true means trun on and false turn off. Each boolean value will correspond with a certain index, and that index is which segment to turn on. If we looked at 7, we need to note down which segments are on. They’re 3 and 6 and 7, so all of these indices in the table need to be true, the rest is false.

{false, false, true, false, false, true, true}

If you ever wanna add something, just find what segments need to be on, and make the pattern table.

This is creation’s file:
7-segment.rbxl (26.5 KB)
This is the texture I made and that I used for the segments: link :link:
7-segment

Thank you!

38 Likes

Alternatively, you could try adding diagonals and some other segments in between for 14-segment display instead. Would that be nice?

1 Like

Hay there! Good idea, taking the time to add that, and adding more patterns and making older ones compatible is a great idea.

1 Like

Nice but isn’t binaries 0b1100100 (or 0x64 or 100) a more efficient way than arrays of booleans { false, false, true, false, false, true, true } to dispaly patterns? For something like this I’d use math.floor(math.log(pattern, 2)) to get the length and bit32.rshift(pattern, i) % 2 == 1 to check if it’s on or off.

5 Likes

That’s pretty impressive, haven’t thought about that! I think. I haven’t played with bit wise operations a lot.
I assume

math.floor(math.log(pattern, 2)) 

Converts to base 2, which is basically binary, correct me if I’m wrong.
But I don’t understand the behaviour of this part:

bit32.rshift(pattern, i) % 2 == 1

How does this figure out if it’s on or off, and what would i be?

2 Likes

i would the be position of the number, it basically shifts the number to the right i amount of times, and % 2 checks if the last number is on or off, basically (pattern DIV (i ^ 2)) MOD 2 and if it returns 1 then it’s on, and if it’s returns 0 then it’s off.
This can also be done (pattern >> i) % 2 == 1 in Lua 5.3

To demostrate that it works:

#include <stdio.h>
#include <math.h>

void get_all_pos(int n)
{
    for (int i = 0; i < (log(n) / log(2)); i++)
    {
        printf("Position: %d, Value: %d\n", i, (n >> i) % 2);
    }
}
int get_pos(int n, int index)
{
    return (n >> index) % 2;
}
int main()
{
    int a = 0b01001001;
    int b = 0b1101010010;
    int c = 22;
    printf("--\n");
    get_all_pos(a);
    printf("\n--\n");
    get_all_pos(b);
    printf("\n--\n");
    get_all_pos(c);

    return 0;
}


Position: 0, Value: 1
Position: 1, Value: 0
Position: 2, Value: 0
Position: 3, Value: 1
Position: 4, Value: 0
Position: 5, Value: 0
Position: 6, Value: 1

Position: 0, Value: 0
Position: 1, Value: 1
Position: 2, Value: 0
Position: 3, Value: 0
Position: 4, Value: 1
Position: 5, Value: 0
Position: 6, Value: 1
Position: 7, Value: 0
Position: 8, Value: 1
Position: 9, Value: 1

Position: 0, Value: 0
Position: 1, Value: 1
Position: 2, Value: 1
Position: 3, Value: 0
Position: 4, Value: 1

2 Likes