local inputs = { {"Play", SOURCE}, {"Dur", VALUE, 1, 30, 15}, {"Test", VALUE, 0, 1, 0} } local outputs = { "Rate" } local wait_end = 0 local Rate = 0 local History local NumElements = 0 local PlayDone = false List = {} function List.new () print ('list.new') return {first = 0, last = -1} end function List.pushleft (list, value) local first = list.first - 1 list.first = first list[first] = value end function List.pushright (list, value) local last = list.last + 1 list.last = last list[last] = value end function List.popleft (list) local first = list.first if first > list.last then error("list is empty") end local value = list[first] list[first] = nil -- to allow garbage collection list.first = first + 1 return value end function List.popright (list) local last = list.last if list.first > last then error("list is empty") end local value = list[last] list[last] = nil -- to allow garbage collection list.last = last - 1 return value end local function init() History = List.new() end local wait_end = 0 local function run(Play, Duration, Test) if getTime() >= wait_end then print ('working') wait_end = getTime() + 100 local alt if Test == 0 then alt = getValue('altitude') else alt = getValue('thr') / 100 end print ('pushing alt '.. alt) List.pushright(History, alt) NumElements = NumElements + 1 while NumElements > Duration do List.popleft(History) NumElements = NumElements - 1 end print ('num elements: '..NumElements) if NumElements == Duration then local StartAlt = List.popleft(History) NumElements = NumElements - 1 Rate = 60 * (alt - StartAlt) / Duration -- unit m/min print ('rate: '..Rate) end end if Play > 500 then if PlayDone == false then PlayDone = true local rr = math.floor(Rate * 10 + 0.5) print('rounded rate: '.. rr) playNumber(rr, 0, PREC1) end else PlayDone = false end return Rate * 10.24 end return { run=run, input=inputs, output=outputs, init=init }