2017-10-04 7 views
1

"REG_LOADS"という名前の単一のリストで以下のようなPython 2.7スクリプトを起動しましたが、今度はREG_LOADS_40に名前を変更して "REG_LOADS_41"などの追加リストを追加したいと思います。 "_40"または "_041"または "_xx"を関数に追加するために、すでに存在する関数を変更するにはどうすればよいですか?私の最終的な目標は、40,41,42,43などと呼ばれるいくつかのリストを持つことです。プログラムはリストをインクリメントします。代わりに、個別に名前のリストの数と各リストに対応する機能を有するので、私はリストの辞書などのように、そのリストの鍵を取る関数をお勧めします既存の関数にインクリメンタルな数字を追加する

おかげで、

def bytes(word): 
global data_high 
global data_low 
data_high, data_low = divmod(word, 0x100) 


def read_reg_22(): #must read register 22 (0x16) before chaging the page 
    print(hex(REG_LOADS[x][0]), hex(0x16), hex(2)) 

def set_page_4(): #set phy page to 4 for writing to non-volatile memory 
    print (hex(REG_LOADS[x][0]), hex(0x16), hex(0x0004), hex(2)) 

def write_to_nvm(): #write to 88E1112 non-volatile memory register 
    print (hex(REG_LOADS[x][0]), hex(0x12), hex(0xA000+(REG_LOADS[x][data_ctr])), hex(0x02)) 

def write_to_nvm_high(): #write to 88E1112 non-volatile memory register 
    print (hex(REG_LOADS[x][0]), hex(0x12), hex(0xA000+(data_high)), hex(0x02)) 

def write_to_nvm_low(): #write to 88E1112 non-volatile memory register 
    print (hex(REG_LOADS[x][0]), hex(0x12), hex(0xA000+(data_low)), hex(0x02)) 

def send_nvm_to_eeprom(): #write to 88E1112 to send data in nvm to EEPROM 
    print (hex(REG_LOADS[x][0]), hex(0x10), hex(0xA000+(eeprom_ctr)), hex(0x02)) 

# PHY_ADDR, PHY_PAGE, PHY_REG, REG_DATA 
REG_LOADS = [[0x40, 0x00, 0x01, 0x1234],[0x41, 0xFF, 0xFF, 0xFFFF],[0x42, 0xAA, 0xAA, 0xAAAA], [0x40, 0xAA, 0xAA, 0xAAAA]] 


print ("Checking that all PHYs are available") 
if (1): 
    PHY_Success = 1 
    print ("Success, PHYs [64, 65, 67, 71, 72, 75, 76, 79, 80] were found") 
else: 
    PHY_Success = 0 
    print ("Failure, all PHYs were not found") 

if PHY_Success == 1: 
    data_ctr = 1 
    eeprom_ctr = 0x00 
    x = 0 
    for a in range(len(REG_LOADS)): 
     read_reg_22() 
     set_page_4() 
     for i in range(len(REG_LOADS[0])+1): 
      if data_ctr <= 2: 
       write_to_nvm() 
       send_nvm_to_eeprom() 
      if data_ctr == 3: 
       bytes (REG_LOADS[x][data_ctr]) 
       write_to_nvm_high() 
       send_nvm_to_eeprom() 
      if data_ctr == 4: 
       write_to_nvm_low() 
       send_nvm_to_eeprom() 
      data_ctr +=1 
      eeprom_ctr +=1 
     data_ctr = 1 
     eeprom_ctr =0x00 
     x += 1 

else: 
    print ("Fail") 

答えて

0

# Assuming the numbers refer to PHYs: 
phys = { 
    40: [], 
    41: [], 
    ... 
} 

def increment_phy(phy_key): 
    phys[phy_key] = whatever you want to do with it 
関連する問題