Changeset 1094

Show
Ignore:
Timestamp:
11/06/08 11:13:18 (2 months ago)
Author:
bhimebau
Message:

checkpoint commit

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • analog/pyeac/branches/pyeac_pygame/eeepc/controlpanel.py

    r1086 r1094  
    2929        self.td(e) 
    3030         
    31         e = gui.Input(value="f1",name="%s,function"%(params['pin']),size=5) 
     31        e = gui.Input(value="1",name="%s,function"%(params['pin']),size=5) 
    3232        self.td(e) 
    3333         
     
    7070        self.td(CellControl(pin='5,5'),align=0) 
    7171 
    72 class TableBorder: 
    73      
    74     def __init__(self): 
    75         self.surface = pygame.Surface((200,200)) 
    76         self.cellrect = pygame.Rect((0,0),(50,20)) 
    77         self.get_border() 
    78      
    79     def get_border(self): 
    80         pygame.draw.rect(self.surface,(255,255,255),self.cellrect,1) 
    81         return (self.surface) 
    82      
     72class ButtonPanel(gui.Table): 
    8373 
     74    def __init__(self, resetf=None, ledonf=None, ledofff=None, **params): 
     75        gui.Table.__init__(self,**params) 
     76         
     77#         print reset_func 
    8478 
     79        self.ledonf = ledonf 
     80        self.ledofff = ledofff 
     81        self.tr() 
     82        b1 = gui.Button("RESET",width=70) 
     83        b1.connect(gui.CLICK,resetf) 
     84        self.td(b1,align=-1) 
     85 
     86        self.tr() 
     87        self.s1 = gui.Switch(value=True) 
     88        self.s1.connect(gui.CHANGE,self.handler) 
     89        self.td(gui.Label("LED ENABLE",color=(255,255,255)),align=-1,width=70) 
     90        self.td(self.s1,align=-1) 
     91    
     92    def handler(self): 
     93        if self.s1.value: 
     94            self.ledonf() 
     95        else: 
     96            self.ledofff() 
     97             
    8598if __name__ == '__main__': 
    8699    screen = pygame.display.set_mode((1024,600)) 
     
    88101    app = gui.App() 
    89102    ecc = EacPanel() 
     103    bcc = ButtonPanel() 
     104     
    90105    c = gui.Container(align=-1,valign=-1) 
    91106    c.add(ecc,0,325) 
     107    c.add(bcc,325,0) 
    92108    app.init(c) 
    93109 
  • analog/pyeac/branches/pyeac_pygame/eeepc/eac_physical.py

    r1086 r1094  
    3737from numpy import array,arange,zeros,meshgrid 
    3838 
     39 
     40class LLA: 
     41     
     42    def __init__(self, eac, input_pin, output_pin, num_desc, function, period): 
     43        self.eac = eac  
     44        self.enable = True 
     45        self.input_pin = input_pin 
     46        self.output_pin = output_pin 
     47        self.num_desc = num_desc 
     48        self.function = function 
     49        self.period = period 
     50        self.eac.lla_add(input_pin,output_pin,num_desc,function,period) 
     51        self.input_current = None 
     52        self.output_current = None 
     53 
     54    def disable(self): 
     55        self.enable = False 
     56        self.eac.lla_disable(self.num_desc) 
     57 
     58    def enable(self): 
     59        self.enable = True 
     60        self.eac.lla_enable(self.num_desc) 
     61 
     62    def update(self): 
     63        (self.input_current,self.output_current) = self.eac.lla_report(self.num_desc) 
     64 
     65class Pin: 
     66 
     67    def __init__(self,pin=None): 
     68        # V, L, S 
     69        self.pin = pin 
     70        self.mode = "V" 
     71        self.voltage = 0.0 
     72        self.current_input = None 
     73        self.current_output = None 
     74 
     75class PinGrid: 
     76     
     77    def __init__(self,ueac_device='/dev/ttyUSB0',baud=19200): 
     78        self.lla_dict = {} 
     79        self.lla_count = 1 
     80        self.ueac = ueac(ueac_device='/dev/ttyUSB0',baud=19200) 
     81        self.ueac.reset() 
     82        self.grid = [] 
     83        for y in range(5): 
     84            temp = [] 
     85            for x in range(5): 
     86                temp.append(Pin((x+1,y+1))) 
     87            self.grid.append(temp) 
     88 
     89    def read_grid_current(self): 
     90        self.ueac.read_i() 
     91        for y in range(5): 
     92            for x in range(5): 
     93                if self.grid[y][x].mode == 'S': 
     94                    self.grid[y][x].current_output = ueac.current_values[y][x] 
     95 
     96    def read_grid_voltage(self): 
     97        self.ueac.read_v() 
     98        for y in range(5): 
     99            for x in range(5): 
     100                self.grid[y][x].voltage = ueac.voltage_values[y][x] 
     101 
     102    def set_current_source(self, pin, value): 
     103        (x,y) = pin 
     104        self.ueac.write_i(x,y,value) 
     105        x -= 1 
     106        y -= 1 
     107        if self.grid[y][x].mode != 'S': 
     108            self.grid[y][x].mode = 'S' 
     109        self.grid[y][x].current_output = value  
     110 
     111    def lla_add(self, tag, input_pin, output_pin, function, period): 
     112        if self.lla_count >= 10: 
     113            return (None) 
     114 
     115        if self.lla_dict.has_key(tag): 
     116            return (None) 
     117        else: 
     118            self.set_voltage_input(input_pin) 
     119            (x,y) = input_pin 
     120            self.grid[y-1][x-1].mode = 'L' 
     121            self.lla_dict[tag] = LLA(self.ueac, input_pin, output_pin, self.lla_count, function, period) 
     122            self.lla_count += 1 
     123            return self.lla_count-1 
     124 
     125    def lla_disable(self, tag): 
     126        if self.lla_dict.has_key(tag): 
     127            self.lla_dict[tag].disable()  
     128            return True 
     129        else: 
     130            return (None) 
     131 
     132    def lla_enable(self, tag): 
     133        if self.lla_dict.has_key(tag): 
     134            self.lla_dict[tag].enable()  
     135            return True 
     136        else: 
     137            return (None) 
     138 
     139    def lla_update(self, tag): 
     140        if self.lla_dict.has_key(tag): 
     141            return self.lla_dict[tag].update()  
     142        else: 
     143            return (None) 
     144 
     145    def set_voltage_input(self, pin): 
     146        (x,y) = pin 
     147        self.ueac.write_i(x,y,0) 
     148        x -= 1 
     149        y -= 1 
     150        if self.grid[y][x].mode != 'V': 
     151            self.grid[y][x].mode = 'V' 
     152        self.grid[y][x].current_output = None 
     153 
     154    def get_data(self): 
     155        self.read_grid_voltage() 
     156        self.read_grid_current() 
     157        for tag in self.lla_dict.keys(): 
     158            self.lla_update(tag) 
     159     
     160    def reset(self): 
     161        self.lla_dict = {} 
     162        self.lla_count = 1 
     163        self.grid = [] 
     164        for y in range(5): 
     165            temp = [] 
     166            for x in range(5): 
     167                temp.append(Pin((x+1,y+1))) 
     168            self.grid.append(temp) 
     169        self.ueac.reset() 
     170        self.get_data() 
     171 
     172 
     173    def lof(self): 
     174        self.ueac.lof() 
     175 
     176    def lon(self): 
     177        self.ueac.lon() 
     178 
     179    def update(self, update_dict, full_form): 
     180        for key in update_dict.keys(): 
     181            (x,y,op) =  key.split(',') 
     182            x = int(x) 
     183            y = int(y) 
     184            value = update_dict[key] 
     185            if op == 'type': 
     186                if value == 'V': 
     187                    self.set_voltage_input((x,y)) 
     188                if value == 'I': 
     189                    current_value_key = "%s,%s,current"%(x,y) 
     190                    self.set_current_source((x,y),int(full_form[current_value_key])) 
     191                if value == 'L': 
     192                    current_value_key = "%d,%d,function"%(x,y) 
     193                    self.lla_add("lla1",(x,y),(0,0),int(full_form[current_value_key]),1) 
     194                    print int(full_form[current_value_key]) 
     195            #                     print "need to add lla" 
     196            elif op == 'function': 
     197                print "lla function changed to",value 
     198            elif op == "current": 
     199                self.set_current_source((x,y),int(value)) 
     200        
     201 
     202    def __str__(self): 
     203        outstr = "" 
     204        for y in range(5): 
     205            # Row 1  
     206            for x in range(5): 
     207                outstr += "    %s    "%(self.grid[y][x].mode) 
     208            outstr += "\n" 
     209 
     210            # Row 2 
     211            for x in range(5): 
     212                outstr += "  %4.3f  "%(self.grid[y][x].voltage) 
     213            outstr += "\n" 
     214             
     215            # Row 3 
     216            for x in range(5): 
     217                if self.grid[y][x].mode == 'V': 
     218                    outstr += "         " 
     219                elif self.grid[y][x].mode == 'S': 
     220                    outstr += "    %d   " % (self.grid[y][x].current_output) 
     221                elif self.grid[y][x].mode == 'L': 
     222                    for key in self.lla_dict.keys(): 
     223                        (lx,ly) = self.lla_dict[key].input_pin 
     224                        if lx == (x+1) and ly == (y+1): 
     225                            outstr += "   %d    " % (int(self.lla_dict[key].input_current)) 
     226            outstr += "\n" 
     227             
     228            # Row 4 
     229            for x in range(5): 
     230                if self.grid[y][x].mode == 'V': 
     231                    outstr += "         " 
     232                elif self.grid[y][x].mode == 'S': 
     233                    outstr += "         " 
     234                elif self.grid[y][x].mode == 'L': 
     235                    for key in self.lla_dict.keys(): 
     236                        (lx,ly) = self.lla_dict[key].input_pin 
     237                        if lx == (x+1) and ly == (y+1): 
     238                            outstr += "   %d    " % (int(self.lla_dict[key].output_current)) 
     239            outstr += "\n" 
     240            outstr += "\n" 
     241        outstr += "############################################" 
     242        return outstr 
     243 
    39244class ueac: 
    40     """ Class used to comminicate with the uEAC R002 board""" 
     245    """ Class used to communicate with the uEAC R002 board""" 
    41246 
    42247    voltage_values = zeros((5,5)) 
     
    102307        self.ser.write('p,v\n') 
    103308        self.ser.flush() 
    104         self.convert_ueac_string_v(self.ser.readline()) 
     309        line = self.ser.readline() 
     310        if line != None: 
     311            self.convert_ueac_string_v(line) 
    105312        sleep(0.01) 
    106313 
     
    109316        self.ser.write('p,i\n') 
    110317        self.ser.flush() 
    111         self.convert_ueac_string_i(self.ser.readline()) 
     318         
     319        line = self.ser.readline() 
     320        if line != None: 
     321            self.convert_ueac_string_i(line) 
    112322        sleep(0.01) 
    113323  
     
    120330        sleep(0.01) 
    121331 
    122     def lla_add(self,x,y): 
    123         """ Adds an lla at the node specified by x,y. The function is hard coded in this version, intended just to be used as input""" 
    124         write_string = "l,a,"+repr(x)+","+repr(y)+",0,0,1,18,1\n" 
    125         self.ser.write(write_string
     332    def lla_add(self,input,output,num_desc,function,period): 
     333        (xi,yi) = input 
     334        (xo,yo) = output 
     335        self.ser.write("l,a,%d,%d,%d,%d,%d,%d,%d\n" % (xi,yi,xo,yo,num_desc,function,period)
    126336        self.ser.flush() 
    127337        self.ser.readline() 
    128338        sleep(0.01) 
     339 
     340    def lla_enable(self,num_desc): 
     341        self.ser.write("l,e,1,1,0,0,%d,18,1\n"%(num_desc)) 
     342        self.ser.flush() 
     343        self.ser.readline() 
     344        sleep(0.01) 
     345 
     346    def lla_disable(self,num_desc): 
     347        self.ser.write("l,d,1,1,0,0,%d,18,1\n"%(num_desc)) 
     348        self.ser.flush() 
     349        self.ser.readline() 
     350        sleep(0.01) 
     351 
     352    def lla_report(self,num_desc): 
     353        self.ser.write("l,r,2,2,0,0,%d,18,1\n"%(num_desc)) 
     354        self.ser.flush() 
     355        line = self.ser.readline() 
     356        if line != None and line != "\rueac> NOK\n": 
     357            fields = line.split(',') 
     358            (temp,icurrent) = fields[0].split(' ') 
     359            ocurrent = fields[1] 
     360        sleep(0.01) 
     361        return ((icurrent,ocurrent)) 
    129362 
    130363    def raw_read(self): 
     
    139372    def close_port(self): 
    140373        self.ser.close() 
    141  
    142     def update(self, update_dict, full_form): 
    143         for key in update_dict.keys(): 
    144             (x,y,op) =  key.split(',') 
    145             value = update_dict[key] 
    146             if op == 'type': 
    147                 if value == 'V': 
    148                     self.write_i(int(x),int(y),0) 
    149                 if value == 'I': 
    150                     current_value_key = "%s,%s,current"%(x,y) 
    151                     self.write_i(int(x),int(y),int(full_form[current_value_key])) 
    152                 if value == 'L': 
    153                     pass 
    154             elif op == 'function': 
    155                 print "lla function changed to",value 
    156             elif op == "current": 
    157                 self.write_i(int(x),int(y),int(value)) 
    158              
    159  
    160374         
    161375if __name__ == "__main__": 
    162     ue = ueac(ueac_device='/dev/ttyUSB0')  # Connect to the uEAC device 
    163     ue.reset()                             # Reset the board to clear any previous config  
    164     ue.lof()                               # turn off the board Leds 
    165     ue.lla_add(3,3)                        # Instantiate an lla at position (3,3).  
    166     ue.write_i(1,1,100)                    # write pin (1,1) as current source 
    167     ue.write_i(5,5,100)                    # write pin (5,5) as current source 
    168  
    169     for i in range(20): 
    170         ue.read_v()                            # read the sheet voltages from the board - store in class variable (voltage_values) 
    171         ue.read_i()                            # read the pin currents from the board - store in class variable (current_values)  
    172         print "** voltage values **\n",ue.voltage_values,"\n" 
    173         print "** current values **\n",ue.current_values,"\n" 
    174  
    175     ue.close_port()                     
    176  
    177 # Code end  
     376    pg = PinGrid() 
     377    pg.lof() 
     378    pg.read_grid_voltage() 
     379    pg.lla_add("lla1",(3,3),(0,0),18,1) 
     380    pg.set_current_source((1,1),70) 
     381 
     382    pg.get_data() 
     383    print pg 
     384     
     385    pg.lla_add("lla2",(5,5),(0,0),18,1) 
     386    pg.get_data() 
     387    print pg 
     388 
     389    pg.reset() 
     390    pg.lof() 
     391    pg.get_data() 
     392    print pg 
     393     
  • analog/pyeac/branches/pyeac_pygame/eeepc/pyeac.py

    r1092 r1094  
    2424if __name__ == '__main__': 
    2525    from visualizer import Visualizer 
    26     from eac_physical import ueac 
     26    from eac_physical import PinGrid 
    2727    import pygame 
    2828    import random 
     
    3131    import sys; sys.path.insert(0, ".")  
    3232    from pgu import gui 
    33     from controlpanel import EacPanel, TableBorder 
     33    from controlpanel import EacPanel, ButtonPanel 
    3434    from raw_display import RawDisplay 
    3535 
    36     phyeac = ueac(ueac_device='/dev/ttyUSB0')  # Connect to the uEAC device 
    37     phyeac.reset()                             # Reset the board to clear any previous config  
    38     phyeac.lof()                               # turn off the board Leds 
    39     phyeac.lla_add(3,3)                        # Instantiate an lla at position (3,3).  
    40     phyeac.write_i(1,1,100)                    # write pin (1,1) as current source 
    41     phyeac.write_i(5,5,100)                    # write pin (5,5) as current source 
     36     
     37    pg = PinGrid(ueac_device='/dev/ttyUSB0') 
     38 
     39#     phyeac = ueac(ueac_device='/dev/ttyUSB0')  # Connect to the uEAC device 
     40#     phyeac.reset()                             # Reset the board to clear any previous config  
     41#     phyeac.lof()                               # turn off the board Leds 
     42#     phyeac.lla_add(3,3,1)                        # Instantiate an lla at position (3,3).  
     43#     phyeac.write_i(1,1,100)                    # write pin (1,1) as current source 
     44#     phyeac.write_i(5,5,100)                    # write pin (5,5) as current source 
    4245     
    4346    pygame.init() 
     
    4952 
    5053    phyeac_vis = Visualizer(absolute=True) 
    51     phyeac_vis.update(phyeac.voltage_values
     54    phyeac_vis.update(pg
    5255    phyeac_raw = RawDisplay() 
    53     phyeac_raw.update(phyeac.voltage_values) 
    54  
     56    phyeac_raw.update(pg) 
    5557 
    5658    form = gui.Form() 
    5759    app = gui.App() 
    5860    ecc = EacPanel() 
     61    bcc = ButtonPanel(pg.reset, pg.lon, pg.lof) 
     62 
    5963    c = gui.Container(align=-1,valign=-1) 
    6064    c.add(ecc,0,325) 
     65 #    c.add(bcc,720,100) 
     66    c.add(bcc,650,100) 
     67 
    6168    app.init(c) 
    6269 
     
    6471    _quit = 0 
    6572     
    66     tb = TableBorder() 
     73#     tb = TableBorder() 
    6774 
    6875    clock = pygame.time.Clock() 
     
    7279        changes = diff_dict(_form_last,_form) 
    7380        if changes != None: 
    74             phyeac.update(changes,_form) 
     81            pg.update(changes,_form) 
    7582 
    7683        for event in pygame.event.get(): 
     
    7986            app.event(event) 
    8087 
    81         phyeac.read_v() 
    82         phyeac_vis.update(phyeac.voltage_values) 
     88        pg.get_data() 
    8389 
    84         phyeac_raw.update(phyeac.voltage_values) 
     90#         phyeac.read_v() 
     91        phyeac_vis.update(pg) 
     92        phyeac_raw.update(pg) 
     93 
     94#         phyeac.read_i() 
     95    #    print phyeac.current_values 
     96#         phyeac.lla_report(2) 
    8597 
    8698        screen.fill((0,0,0)) 
  • analog/pyeac/branches/pyeac_pygame/eeepc/raw_display.py

    r1092 r1094  
    4343        pygame.draw.line(self.master_surface,(255,255,255),(0,240),(300,240),1) 
    4444      
    45     def update(self,vdata): 
     45    def update(self,pg): 
    4646        self.clear_surface() 
    4747        self.draw_grid() 
    48         xoffset = 15 
    49         yoffset = 30 
     48        xoffset = 10 
     49        yoffset = 5 
    5050        for y in range(5): 
    5151            for x in range(5): 
    52                 voltage = float(vdata[y][x]) 
    53                 text.write(self.master_surface,self.font,(xoffset+x*60,yoffset+y*60),(255,255,255),"%4.3f" % (voltage)) 
    54                  
     52                voltage = float(pg.grid[y][x].voltage) 
     53                text.write(self.master_surface,self.font,(xoffset+x*60,yoffset+y*60),(0,255,0),"V = %4.3f" % (voltage)) 
     54                if pg.grid[y][x].mode == 'S': 
     55                    loc = ((xoffset)+x*60,(yoffset+10)+y*60) 
     56                    value = int(pg.grid[y][x].current_output) 
     57                    if value >= 0: 
     58                        text.write(self.master_surface,self.font,loc,(255,0,0),"Io = %d" % (value)) 
     59                    else: 
     60                        text.write(self.master_surface,self.font,loc,(0,0,255),"Io = %d" % (value)) 
     61                if pg.grid[y][x].mode == 'L': 
     62                    loc = ((xoffset)+x*60,(yoffset+10)+y*60) 
     63                    text.write(self.master_surface,self.font,loc,(255,255,0),"LLA") 
     64                                                        
     65 
    5566if __name__ == '__main__': 
    5667    size = (300,300) 
  • analog/pyeac/branches/pyeac_pygame/eeepc/visualizer.py

    r1093 r1094  
    2929        self.surface.fill(color,fill_window) 
    3030 
    31     def update(self,vdata): 
     31    def update(self,pg): 
    3232        if self.absolute == True: 
    3333            max = 5.0 
     
    3535            for y in range(5): 
    3636                for x in range(5): 
    37                     voltage = float(vdata[y][x]
     37                    voltage = float(pg.grid[y][x].voltage
    3838                    if voltage > max: 
    3939                        voltage = max 
     
    4747            for y in range(5): 
    4848                for x in range(5): 
    49                     voltage = float(vdata[y][x]
     49                    voltage = float(pg.grid[y][x].voltage
    5050                    if voltage > max: 
    5151                        max = voltage 
     
    5757                for y in range(5): 
    5858                    for x in range(5): 
    59                         voltage = float(vdata[y][x]
     59                        voltage = float(pg.grid[y][x].voltage
    6060                        vnorm = (voltage - min)/span  
    6161                        cdata = self.palette[int(vnorm*255)]