Advertisement
AdityaSriram

led.py (v2.0.1)

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