rudi
an other theme: "table results with ptpcam.exe"
I provide a solution for return results from simple lua-tables with ptpcam. The most tables have one or two dimensions. My code convert tables to a formatted string for retrieve.
Good. This is important.
Some thoughts:
C code seems complex, could be better to do this in lua. We can have lua code in a C string if we want hardcoded in CHDK.
Would be good to have a standard library of lua functions to use with PTP somehow. Not sure the best way to do this, loading from SD card every time would be slow, sending over PTP every time not so nice either.
Format is OK to display values, but it's ambiguous in some cases and quite limited. Doesn't support non-numeric keys in second level table, e.g. {t={a='a',b='b'}}, showing nil in one and not the other is a bit odd.
OK for now, but I think we'll want to revisit, real work crunch is over for a while.
My plan was to format table returns as a string of lua code, using lua (but only simple types as you have, and no cyclic references etc.) When the PC side has lua this is very convenient. It's also quite readable on it's own, but maybe not easy to parse in autoit.
I'll have a little more time to work on CHDK now.
lua version of table format code. Could be simpler, but I was trying to match output of rudi's code as closely as possible. There are probably some minor differences still
function t_to_s(t)
local v2s=function(v)
local t=type(v)
if t=='string' then
return v
end
if t=='number' or t=='boolean' or t=='nil' then
return tostring(v)
end
return ""
end
local r=""
for k,v in pairs(t) do
local s,vs=""
if type(v)=='table' then
for i=1,table.maxn(v) do
s=s..'\t'..v2s(v[i])
end
else
vs=v2s(v)
if #vs then
s=s..'\t'..vs
end
end
vs=v2s(k)
if #vs>0 and #s>0 then
r=r..vs..s..'\n'
end
end
return r
end
test (! is pc side lua in my client)
___> !print(t_to_s({1,2,3,4}))
1 1
2 2
3 3
4 4
___> !print(t_to_s({X=44,G=2}))
G 2
X 44
___> !print(t_to_s({"Monday","Tuesday","Wednesday","Thursday","Friday"}))
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
___> !print(t_to_s({{3,8,55},G=2}))
1 3 8 55
G 2
___> !print(t_to_s({{3,8,55}, D={"S","M","D"},G=2,"value",678}))
1 3 8 55
2 value
3 678
G 2
D S M D
___> !print(t_to_s({U={3,8,55}, D={"S","M","D"},G=2}))
U 3 8 55
D S M D
G 2
___> !print(t_to_s({}))
___> !print(t_to_s({{},{}}))
___> !print(t_to_s({A={},{}}))
___> !print(t_to_s({A={},B={}}))
___> !print(t_to_s({A={1},B={33}}))
A 1
B 33
___> !print(t_to_s({A={1,2},B={33}}))
A 1 2
B 33
___> !print(t_to_s({A={1,2},B={33,{},44}}))
A 1 2
B 33 44
___> !print(t_to_s({A={1,2,"true",false},B={33,{},44}}))
A 1 2 true false
B 33 44
___> !print(t_to_s({A={1,2,true,"false"},B={33,{},44}}))
A 1 2 true false
B 33 44
___> !print(t_to_s({3,4,nil,5,6,true,7}))
1 3
2 4
4 5
5 6
6 true
7 7
___> !print(t_to_s({{3,4,nil,5,6,true,7}}))
1 3 4 nil 5 6 true 7