Advertisement
AdityaSriram

led.py

Feb 3rd, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.07 KB | None | 0 0
  1. from Tkinter import *
  2.  
  3. class LED:
  4.     """Basic LED functionality to display Hexadecimal numbers
  5.    (may be extended to handle all alphanumeric characters) as
  6.    a 7-segment LED display."""
  7.     master = None
  8.     canvas = None
  9.     height = 200
  10.     width = 100
  11.     chars = {'0':'abcdef',
  12.              '1':'ed',
  13.              '2':'bcefg',
  14.              '3':'cdefg',
  15.              '4':'adeg',
  16.              '5':'acdfg',
  17.              '6':'abcdfg',
  18.              '7':'edf',
  19.              '8':'abcdefg',
  20.              '9':'acdefg',
  21.              'a':'abdefg',
  22.              'b':'abcdg',
  23.              'c':'abcf',
  24.              'd':'bcdeg',
  25.              'e':'abcfg',
  26.              'f':'abfg',
  27.              'g':'abcdf',
  28.              'h':'abdeg',
  29.              'i':'ab',
  30.              'j':'cde',
  31.              'l':'abc',
  32.              'm':'bdfg',
  33.              'n':'bdg',
  34.              'o':'bcdg',
  35.              'p':'abefg',
  36.              'q':'adefg',
  37.              'r':'bg',
  38.              's':'acdfg',
  39.              'u':'bcd',
  40.              'y':'adeg',
  41.              'z':'bcefg',
  42.              ' ':'',
  43.              '' :'',
  44.              '_':'c'}
  45.  
  46.     def __init__(self, master, value, locX, locY, width = 100, height = 200):
  47.         self.canvas = Canvas(master, height=height, width=width, bg='black')
  48.         self.canvas.place(x=locX, y=locY)
  49.         self.height = height
  50.         self.width = width
  51.         self.drawSeg()
  52.         self.colorSeg(value)
  53.  
  54.     def __call__(self, value):
  55.         self.colorSeg(value)
  56.  
  57.     def drawSeg(self):
  58.         widthRatio = int(0.20*self.width) # vertical segment thickness
  59.         heightRatio = int(0.1*self.height) # horizontal segment thickness
  60.         padx = int(0.1*self.width) # padding of inner rectangle from canvas
  61.         pady = int(0.05*self.height)
  62.         segHeight = self.height/2 - pady # height of vertical segment
  63.         coords = {
  64.         'a':[(padx,                             pady),
  65.              (padx,                             pady + segHeight),
  66.              (padx + widthRatio,                pady + segHeight - heightRatio),
  67.              (padx + widthRatio,                pady + heightRatio)],
  68.  
  69.         'b':[(padx,                             pady + segHeight),
  70.              (padx,                             pady + segHeight*2),
  71.              (padx + widthRatio,                pady + segHeight*2 - heightRatio),
  72.              (padx + widthRatio,                pady + segHeight + heightRatio)],
  73.  
  74.         'e':[(self.width - padx,                pady),
  75.              (self.width - padx,                pady + segHeight),
  76.              (self.width - (padx + widthRatio), pady + segHeight - heightRatio),
  77.              (self.width - (padx + widthRatio), pady + heightRatio)],
  78.  
  79.         'd':[(self.width - padx,                pady + segHeight),
  80.              (self.width - padx,                pady + segHeight*2),
  81.              (self.width - (padx + widthRatio), pady + segHeight*2 - heightRatio),
  82.              (self.width - (padx + widthRatio), pady + segHeight + heightRatio)],
  83.  
  84.         'f':[(padx,                             pady),
  85.              (self.width - padx,                pady),
  86.              (self.width - (padx + widthRatio), pady + heightRatio),
  87.              (padx + widthRatio,                pady + heightRatio)],
  88.  
  89.         'c':[(padx,                             pady + segHeight*2),
  90.              (self.width - padx,                pady + segHeight*2),
  91.              (self.width - (padx + widthRatio), pady + segHeight*2 - heightRatio),
  92.              (padx + widthRatio,                pady + segHeight*2 - heightRatio)],
  93.  
  94.         'g':[(padx,                             pady + segHeight),
  95.              (padx + widthRatio,                pady + segHeight - heightRatio),
  96.              (self.width - (padx + widthRatio), pady + segHeight - heightRatio),
  97.              (self.width - padx,                pady + segHeight),
  98.              (self.width - (padx + widthRatio), pady + segHeight + heightRatio),
  99.              (padx + widthRatio,                pady + segHeight + heightRatio)],
  100.                  }
  101.         for char in coords.keys():
  102.             self.canvas.create_polygon(coords[char], fill='black', tag=char, outline='black', width=3)
  103.  
  104.     def drawSeg2(self):
  105.     """Alternate design of led's"""
  106.         padx = int(0.05*self.height)
  107.         pady = int(0.1*self.width)
  108.         hSegThick = int((1.0/8)*self.width)
  109.         vSegThick = int((1.0/16)*self.height)
  110.         segHeight = self.height/2 - padx/2 - hSegThick
  111.         vCoords = [(padx + vSegThick/2, pady + hSegThick/2            ),
  112.                    (padx              , pady + hSegThick              ),
  113.                    (padx              , pady + segHeight              ),
  114.                    (padx + vSegThick/2, pady + hSegThick/2 + segHeight),
  115.                    (padx + vSegThick  , pady + segHeight              ),
  116.                    (padx + vSegThick  , pady + hSegThick              )]
  117.         hCoords = [(padx + vSegThick/2             , pady + hSegThick/2),
  118.                    (padx + vSegThick               , pady + hSegThick  ),
  119.                    (self.width - padx              , pady + hSegThick  ),
  120.                    (self.width - padx + vSegThick/2, pady + hSegThick/2),
  121.                    (self.width - padx              , pady              ),
  122.                    (padx + vSegThick               , pady)]
  123.         coords = {'a':transform(vCoords, 0                  , 0          ),
  124.                   'b':transform(vCoords, 0                  , segHeight  ),
  125.                   'd':transform(vCoords, self.width - padx*2, segHeight  ),
  126.                   'e':transform(vCoords, self.width - padx*2, 0          ),
  127.                   'c':transform(hCoords, 0                  , segHeight*2),
  128.                   'f':transform(hCoords, 0                  , 0          ),
  129.                   'g':transform(hCoords, 0                  , segHeight  )}
  130.         for char in coords.keys():
  131.             self.canvas.create_polygon(coords[char], fill='#200000', tag=char, outline='black', width=3)
  132.  
  133.  
  134.     def colorSeg(self, value):
  135.         if not str(value) in self.chars.keys(): return
  136.         segList = self.chars[str(value)]
  137.         for char in set('abcdefg').difference(set(segList)):
  138.             self.canvas.itemconfigure(char, fill='#200000')
  139.         for char in segList:
  140.             self.canvas.itemconfigure(char, fill='#FF0000')
  141.  
  142.         '''for char in set(segList).intersection(set(coords.keys())):
  143.                self.canvas.create_polygon(coords[char], fill='red')'''
  144.  
  145. # Note for optimization of segment generation:
  146. # Define just two or four points and obtain remaining by adding or subtracting
  147. # appropriate measures.
  148.  
  149. def transform(l, inrx, inry):
  150.     return [(i + inrx,j + inry) for i,j in l]
  151.  
  152. def f(e):
  153.     global count, l1
  154.     try:
  155.         l1(count.next())
  156.     except StopIteration:
  157.         print "All done"
  158.  
  159. def main():
  160.     global count, l1
  161.     count = (i for i in sorted(LED.chars.keys()))
  162.     root = Tk()
  163.     root.geometry('320x400+50+50')
  164.     l1 = LED(root, "1", 50, 50)
  165.     root.bind('<Button-1>', f)
  166.     root.mainloop()
  167.  
  168. if __name__ == '__main__':
  169.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement