mirror of
https://github.com/elua/elua.git
synced 2025-01-25 01:02:54 +08:00
0437a25b30
more friendly to less advanced users.
66 lines
2.1 KiB
Lua
66 lines
2.1 KiB
Lua
-- Acquire ADC samples as quickly as possible, without the use of a timer
|
|
-- provides statistics on time and memory usage while running
|
|
|
|
if pd.board() == "ET-STM32" then
|
|
adcchannels = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
|
|
adcsmoothing = {4, 4, 4, 4, 16, 16, 16, 16, 32, 32, 32, 32, 64, 128, 64, 128}
|
|
numiter = 50
|
|
else
|
|
adcchannels = {0,1,2,3}
|
|
adcsmoothing = {4, 16, 64, 128}
|
|
numiter = 200
|
|
end
|
|
|
|
-- Setup ADC
|
|
for i, v in ipairs(adcchannels) do
|
|
adc.setblocking(v,1) -- block, waiting for samples when an adc.get* function is used
|
|
adc.setclock(v,0) -- set clock to zero: no timer, acquire samples as fast as possible
|
|
adc.setsmoothing(v,adcsmoothing[i]) -- apply moving average filter using lengths from adcsmoothing
|
|
end
|
|
|
|
-- Draw static text on terminal
|
|
term.clrscr()
|
|
term.gotoxy(1,1)
|
|
term.putstr("ADC Status:")
|
|
term.gotoxy(1,3)
|
|
term.putstr(" CH SLEN RES")
|
|
term.gotoxy(1,#adcchannels+7)
|
|
term.putstr("Press ESC to exit.")
|
|
|
|
-- Use some locals for speed
|
|
local adcvals = {}
|
|
local key, stime, etime, dtime, i, v
|
|
local sample = adc.sample
|
|
local insertsamples = adc.insertsamples
|
|
local tread = tmr.read
|
|
local tstart = tmr.start
|
|
|
|
while true do
|
|
stime = tstart(0) -- start timer
|
|
for j=1,numiter do -- acuire numiter samples
|
|
sample(adcchannels, 1)
|
|
for i, v in ipairs(adcchannels) do
|
|
insertsamples(v,adcvals,i,1) -- for each iteration j, get samples and put them in adcvals
|
|
end
|
|
end
|
|
etime = tread(0) -- get cycle end time
|
|
dtime = tmr.diff(0,etime,stime)/numiter -- compute average acquisition time per cycle
|
|
|
|
-- draw last acquired samples on the console
|
|
term.gotoxy(1,4)
|
|
for i, v in ipairs(adcchannels) do
|
|
term.putstr(string.format("ADC%02d (%03d): %04d\n", v, adcsmoothing[i],adcvals[i]))
|
|
term.gotoxy(1,i+4)
|
|
end
|
|
|
|
-- draw acquisition statistics
|
|
term.putstr(string.format("Tcyc: %06d (us)\n",dtime))
|
|
term.gotoxy(1,#adcchannels+5)
|
|
term.putstr(string.format("Mem: %03.2f (kB)\n",collectgarbage("count")))
|
|
|
|
key = term.getch( term.NOWAIT )
|
|
if key == term.KC_ESC then break end -- exit if user hits Escape
|
|
end
|
|
|
|
term.clrscr()
|
|
term.gotoxy( 1 , 1 ) |