简体   繁体   中英

seven segment display with width

I'm in the process of making a seven segment display emulator in python and have run into a small problem. What I have done is make a list for each number that has a series of numbers representing a character, like so ["000", "001", "000", "001", "000"] This represents the number 1, with 0 been a space, 1 been a pipe character (|) and 2 been a dash.

This works fine for a width of 1 but I want it to be able to go to any width. I've tried doing this by multiplying the character count by the width eg the number one with a width of two would look like ["000000", "000011", "000000", "000011", "000000"]

The problem I am having is when it encounters a pipe character it will print it on the same line rather than underneath it. Like so '| |' when it should be like

|
|

I've tried using \\n to print it on a new line but this messes everything else up.

Any suggestions on how do this in a better way or how to fix my problem would be appreciated.

Here is my code.

  uno = ["000", "001", "000", "001", "000"]
  temp = ""
    width = 2
    for line in uno:
    temp = ""
    for char in line:
        temp += char * width
    temp = temp.replace('0', ' ').replace("1", "|").replace('2', '-')
    print(temp)

Example Output

||

||

Wanted Output

|
|

|
|

Thanks

It took me a bit to grasp your input format. It's a bit silly to use 15 characters than can take any of three values to represent what is, almost by definition a seven bit output. It does make rendering easy, I guess.

If I understand your question correctly, you want to be able to turn your current width=1 output, where an 8 looks like this:

 -
| |
 -
| |
 -

To a scaled up version, where for width=2 an 8 would look like:

 --
|  |
|  |
 --
|  |
|  |
 --

Currently, a single-width 8 requires the input ['020', '101', '020', '101', '020'] . If you want to use the same rendering method (where you simply replace the numbers with characters), you need to translate this to: ['0220', '1001', '1001', '0220', '1001', '1001', '0220'] to increase the size.

Notice that the lines with the 1 s get repeated as a whole and the middle character is repeated on every line.

Here's a function that translates from width 1 to any other width:

def scale(code, factor):
    if factor == 1:
        return code

    result = ["{}{}{}".format(line[0], line[1:-1]*factor, line[-1])  # widen
              for line in code]

    for i in range(len(result)-2, 0, -2):
        result[i:i+1] = result[i:i+1]*factor       # stretch vertically

    return result

def render(code):
    for line in code:
        print(line.replace('0', ' ').replace('1', '|').replace('2', '-'))

Example output:

>>> render(['020', '101', '020', '101', '020'])
 - 
| |
 - 
| |
 - 
>>> render(scale(['020', '101', '020', '101', '020'], 2))
 -- 
|  |
|  |
 -- 
|  |
|  |
 -- 
>>> render(scale(['020', '101', '020', '101', '020'], 3))
 --- 
|   |
|   |
|   |
 --- 
|   |
|   |
|   |
 --- 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM