1 #!/usr/bin/env python
    2 
    3 # Copyright 2003 Tom Rothamel <tom-potw@rothamel.us>
    4 # 
    5 # Permission is hereby granted, free of charge, to any person
    6 # obtaining a copy of this software and associated documentation files
    7 # (the "Software"), to deal in the Software without restriction,
    8 # including without limitation the rights to use, copy, modify, merge,
    9 # publish, distribute, sublicense, and/or sell copies of the Software,
   10 # and to permit persons to whom the Software is furnished to do so,
   11 # subject to the following conditions:
   12 # 
   13 # The above copyright notice and this permission notice shall be
   14 # included in all copies or substantial portions of the Software.
   15 # 
   16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
   20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
   21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
   22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   23 
   24 import curses
   25 import optparse
   26 import time
   27 
   28 digits = {
   29     ':' : ( ':', ':', ':', ':' ),
   30     ' ' : ( ' ', ' ', ' ', ' ' ),
   31     
   32     '0' : ( '.', '.', '.', '.' ),
   33     '1' : ( '.', '.', '.', 'O' ),
   34     '2' : ( '.', '.', 'O', '.' ),
   35     '3' : ( '.', '.', 'O', 'O' ),
   36     '4' : ( '.', 'O', '.', '.' ),
   37     '5' : ( '.', 'O', '.', 'O' ),
   38     '6' : ( '.', 'O', 'O', '.' ),
   39     '7' : ( '.', 'O', 'O', 'O' ),
   40     '8' : ( 'O', '.', '.', '.' ),
   41     '9' : ( 'O', '.', '.', 'O' ),
   42     }
   43 
   44 def bcdtime():
   45     """
   46     This returns four strings, which together comprise the BCD
   47     representation of the current time. The first string contains the
   48     most significant bits of the numbers, with the fourth containing
   49     the least significant bits.
   50     """
   51 
   52     st = time.strftime("%02H : %02M : %02S")
   53 
   54     rv = []
   55 
   56     # For each bit-place, from MSB to LSB
   57     for i in range(0, 4):
   58         rs = ""
   59 
   60         # For each place in the current time.
   61         for c in st:
   62             rs += digits[c][i]
   63 
   64         rv.append(rs)
   65 
   66     return rv
   67 
   68 
   69 def curses_clock():
   70     """
   71     This displays a binary-coded-decimal clock in a terminal, using
   72     curses. The display updates every second.
   73     """
   74 
   75     scr = curses.initscr()
   76 
   77     # We put everything to do with curses in a try block, so that if
   78     # an error occurs, we call endwin() before displaying the
   79     # traceback.
   80     
   81     try:    
   82         # We only wait for input for a maximum of 1 second, or 10/10
   83         # seconds as specified here.
   84         curses.halfdelay(10)
   85         curses.noecho()
   86 
   87         while True:
   88 
   89             # Show the BCDtime.
   90             lines = bcdtime()
   91 
   92             for i, l in enumerate(lines):
   93                 scr.addstr(i, 0, l)
   94 
   95             # Move the cursor out of the way.
   96             maxy, maxx = scr.getmaxyx()
   97             scr.move(maxy - 1, maxx - 1)            
   98             scr.refresh()
   99 
  100             # If we get 'Q', quit. This can take up to 1 second.
  101             c = scr.getch()
  102             if c == ord('q') or c == ord('Q'):
  103                 break
  104 
  105     finally:
  106         curses.endwin()
  107 
  108 
  109 def main():
  110     op = optparse.OptionParser(usage="bcd.py [options]",
  111                                version="bcd.py 1")
  112 
  113     op.add_option("--single", action="store_true", dest="single",
  114                   default=False,
  115                   help="Print out the current time, then quit.")
  116 
  117     options, args = op.parse_args()
  118 
  119     # If single, print out the current bcdtime, and exit.
  120     if options.single:
  121         for l in bcdtime():
  122             print l
  123 
  124         return
  125 
  126     curses_clock()
  127 
  128 if __name__ == "__main__":
  129     main()
  130 
  131         
  132     
  133