--[[ ******************************** Licence: GPL (c) msl 2011/05/14 ******************************** @title Config R&W @param a Lng 0=auto 1=en 2=de @default a 0 ]] --PLAUSIBILITY if a < 0 or a > 2 then a = 0 end --GLOBAL VARIABLES-- -- file number number = 1 -- Config ID max. id_max = get_config_value(0) -- Language lng_mode = a lng = 1 -- en=1 de=2 -- Text txt_keys = " [\18] [SET] [\29] [MENU]" txt_space= " " txt_arrow= "\16" txt_line = "-------------------------" txt_log = {} txt_log[1] = "Backup CHDK configuration" txt_log[2] = "Sicherung CHDK-Konfiguration" txt_decl = {} txt_decl[1] = "Select OK File End" txt_decl[2] = "Auswahl OK Datei Ende" txt_save = {} txt_save[1] = "Saving Configuration" txt_save[2] = "Sichere Konfiguration" txt_write = {} txt_write[1]= "Writing Configuration" txt_write[2]= "Schreibe Konfiguration" txt_att = {} txt_att[1] = " Attention!" txt_att[2] = " Achtung!" txt_att1a = {} txt_att1a[1]= "Should old configuration" txt_att1a[2]= " Soll alte Konfiguration" txt_att1b = {} txt_att1b[1]= " Existing file should" txt_att1b[2]= " Soll vorhandene Datei" txt_att2 = {} txt_att2[1] = " be overwritten?" txt_att2[2] = " überschrieben werden?" txt_att3 = {} txt_att3[1] = " [SET]-Yes [MENU]-No" txt_att3[2] = " [SET]-Ja [MENU]-Nein" txt_w1 = {} txt_w1[1] = "Backup created!" txt_w1[2] = "Sicherung erstellt!" txt_rw1 = {} txt_rw1[1] = "Read & write data." txt_rw1[2] = "Lese & schreibe Daten." txt_rw2 = {} txt_rw2[1] = "Error in config file!" txt_rw2[2] = "Fehler in Konfig-Datei!" txt_rw3 = {} txt_rw3[1] = "Configuration created!" txt_rw3[2] = "Konfiguration erstellt!" txt_rw4 = {} txt_rw4[1] = "Datei nicht verfügbar!" txt_rw4[2] = "File not available!" --FUNCTIONS-- -- get language function get_language() local language, lang if get_propset() == 1 then lang = get_prop(196)/256 -- 1/2 en, 257/258 de, 513/514 fr, 2305/2306 es, 2817/2818 ru if lang > 7 then lang = lang + 1 end -- ukrainian not in propset1 if lang > 22 then lang = lang + 1 end -- romanian not in propset1 else lang = get_prop(61)/256 -- 0/1 en, 256/257 de, 512/513 fr, 2560/2561 es, 3072/3073 ru end return lang + 1 end -- Create file name function file_name(nn) local file_path = "A/CHDK/LOGS/" local file_prefix = "cfg_" local file_ending = ".cfg" local file_name = file_path..file_prefix..nn..file_ending return file_name end -- Write recent config in file function write_conf(config_file) local key, x, y if os.stat(config_file) then key= warning(1) if key ~= "SET" then return end end file=io.open(config_file,"wb") file:write("# "..txt_log[lng].."\n") file:write("# ", os.date("%d.%m.%Y %X"), "\n") file:write("# \n") file:write("# ", get_buildinfo().platform, " ", get_buildinfo().platsub, "\n") file:write("# ", get_buildinfo().version, " ", get_buildinfo().build_number, "\n") file:write("# \n") for i=1, id_max do x, y = get_config_value(i,-1) if type(x)=="table" then x="{"..table.concat(x,",").."}" end if y == nil then y = -2 end -- if y invalid file:write(i,";",x ,";",y,"\n") end file:close() print(txt_w1[lng]) console_redraw() sleep(2000) end -- Check config file available -- Reading lines in config file -- adjust typ of ID value function read_and_set_conf(config_file) local key, array, ID, x_array, x, y, xx, yy key= warning(2) if key == "SET" then if os.stat(config_file) then print(txt_rw1[lng]) console_redraw() sleep(2000) file = io.open(config_file) for line in file:lines(config_file) do if string.find(line, "#") == nil then array = string.split(line,";") if array[1] == nil then print(txt_rw2[lng]) console_redraw() sleep(2000) else ID = tonumber(array[1]) if string.find(array[2], "{") == 1 then x_array=string.gsub(array[2],"{","") x_array=string.gsub(x_array,"}","") x = string.split(x_array,",") else x = tonumber(array[2]) if x == nil then x = array[2] end end y = tonumber(array[3]) xx, yy = get_config_value(ID,-1) if yy == nil then yy = -2 end if type(x) == "table" then set_config_value(ID, x) else if x ~= -1 then if (x ~= xx) or (x==XX and y~=yy) then set_config_value(ID, x, y) end end end end end end file:close() print(txt_rw3[lng]) console_redraw() sleep(2000) return else print(txt_rw4[lng]) console_redraw() sleep(2000) end end end -- Split string and write in array -> string.split(string,delimiter) function string:split(delimiter) local result = { } local from = 1 local delim_from, delim_to = string.find( self, delimiter, from ) while delim_from do table.insert( result, string.sub( self, from , delim_from-1 ) ) from = delim_to + 1 delim_from, delim_to = string.find( self, delimiter, from ) end table.insert( result, string.sub( self, from ) ) return result end -- Warning overwrite function warning(n) local key_pressed print(txt_att[lng]) print(txt_line) if n == 1 then print(txt_att1b[lng]) else print(txt_att1a[lng]) end print(txt_att2[lng]) print(txt_line) print(txt_att3[lng]) console_redraw() wait_click(0) if is_pressed "set" then key_pressed = "SET" elseif is_pressed "menu" then key_pressed = "MENU" end cls() return key_pressed end -- Menu function menu() local curitem=1 while true do cls() print(txt_keys) print(txt_decl[lng]) print(txt_line) if curitem == 1 then print(txt_arrow, txt_save[lng]) print(txt_space, txt_write[lng]) else print(txt_space, txt_save[lng]) print(txt_arrow, txt_write[lng]) end print(txt_line) print(txt_space,file_name(number)) console_redraw() wait_click(0) if is_pressed("down") then curitem = curitem - 1 if curitem < 1 then curitem=2 end elseif is_pressed("up") then curitem = curitem + 1 if curitem > 2 then curitem=1 end elseif is_pressed("left") then number = number - 1 if number < 1 then number=10 end elseif is_pressed("right") then number = number + 1 if number > 10 then number=1 end elseif is_pressed("set") then if curitem == 1 then cls() write_conf(file_name(number)) end if curitem == 2 then cls() read_and_set_conf(file_name(number)) end elseif is_pressed("menu") then cls() break end end end -- Close file -- Reset script console function restore() file:close() set_console_layout(0, 0, 25, 5) set_console_autoredraw(1) end --MAIN-- if lng_mode == 0 then res = get_language() if res == 2 then lng = 2 else lng = 1 end else lng = lng_mode end set_console_layout(8, 3, 33, 10) set_console_autoredraw(0) menu() sleep(1000) cls() set_console_layout(0, 0, 25, 5) set_console_autoredraw(1)