Version 8 of 7-segment ASCII graphics

Updated 2004-04-23 16:05:48

Richard Suchenwirth 2004-04-23 - Yet another tiny fun project, here's how to convert a string of digits and some other characters to a multiline string that in a fixed-pitch font looks like a 7-segment LCD/LED display:

 proc num->7seg string {
    foreach i {a b c} {set $i " "}
    array set 7segment {
    0 {{ _ } {| |} {|_|}}
    1 {{   } {  |} {  |}}
    2 {{ _ } { _|} {|_ }}
    3 {{ _ } { _|} { _|}}
    4 {{   } {|_|} {  |}}
    5 {{ _ } {|_ } { _|}}
    6 {{ _ } {|_ } {|_|}}
    7 {{ _ } {  |} {  |}}
    8 {{ _ } {|_|} {|_|}}
    9 {{ _ } {|_|} { _|}}
    - {{   } { _ } {   }}
    E {{ _ } {|_ } {|_ }}
    r {{   } { _ } {|  }}
    o {{   } { _ } {|_|}}
    A {{ _ } {|_|} {| |}}
    B {{   } {|_ } {|_|}}    
    C {{ _ } {|  } {|_ }}
    D {{   } { _|} {|_|}}
    F {{ _ } {|_ } {|  }}
    }
    foreach char [split $string ""] {
       if {$char eq "."} {
          set c [string replace $c end end .]
       } else {
          foreach i {a b c} row $7segment($char) {
             append $i $row " "
          }
       }
    }
    return $a\n$b\n$c
 }
 % num->7seg 12345.67890-Error
      _   _       _   _   _   _   _   _       _                  
   |  _|  _| |_| |_  |_    | |_| |_| | |  _  |_   _   _   _   _  
   | |_   _|   |  _|.|_|   | |_|  _| |_|     |_  |   |   |_| |   

MPJ : "Drive-by hacking" ... I added A,B,C,D,F so hex displays look right ;-) - RS: Thanks Mike, but that B is indistinguishable from 8, and D from 0... We'd have to use lowercase for these two:

    B {{   } {|_ } {|_|}}
    D {{   } { _|} {|_|}}
 % num->7seg 012345.6789ABCDEF
  _       _   _       _   _   _   _   _   _       _       _   _  
 | |   |  _|  _| |_| |_  |_    | |_| |_| |_| |_  |    _| |_  |_  
 |_|   | |_   _|   |  _|.|_|   | |_|  _| | | |_| |_  |_| |_  |   

MPJ: You're right; I just saw that on LCD hexa panel.


Arts and crafts of Tcl-Tk programming