2009-06-29 16:45:28 +00:00
|
|
|
-- Acquire ADC samples using a timer with polling for available samples
|
2009-03-21 19:59:49 +00:00
|
|
|
|
|
|
|
if pd.board() == "ET-STM32" then
|
|
|
|
timer = 2
|
2009-03-25 16:17:19 +00:00
|
|
|
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}
|
2009-03-21 19:59:49 +00:00
|
|
|
else
|
|
|
|
timer = 0
|
|
|
|
adcchannels = {0,1,2,3}
|
2009-03-25 16:17:19 +00:00
|
|
|
adcsmoothing = {4, 64, 32, 16}
|
2009-03-21 19:59:49 +00:00
|
|
|
end
|
|
|
|
|
2009-06-29 16:45:28 +00:00
|
|
|
-- Setup ADC and start sampling
|
2009-03-21 19:59:49 +00:00
|
|
|
for i, v in ipairs(adcchannels) do
|
2009-06-29 16:45:28 +00:00
|
|
|
adc.setblocking(v,0) -- no blocking on any channels
|
|
|
|
adc.setsmoothing(v,adcsmoothing[i]) -- set smoothing from adcsmoothing table
|
|
|
|
adc.setclock(v, 4 ,timer) -- get 4 samples per second, per channel
|
2009-03-21 19:59:49 +00:00
|
|
|
end
|
|
|
|
|
2009-06-29 16:45:28 +00:00
|
|
|
adc.sample(adcchannels,128) -- start sampling on all channels
|
2009-03-21 19:59:49 +00:00
|
|
|
|
2009-06-29 16:45:28 +00:00
|
|
|
-- Draw static text on terminal
|
2009-03-21 19:59:49 +00:00
|
|
|
term.clrscr()
|
|
|
|
term.gotoxy(1,1)
|
|
|
|
term.putstr("ADC Status:")
|
|
|
|
term.gotoxy(1,3)
|
|
|
|
term.putstr(" CH SLEN RES")
|
|
|
|
term.gotoxy(1,#adcchannels+5)
|
|
|
|
term.putstr("Press ESC to exit.")
|
|
|
|
|
2009-06-29 16:45:28 +00:00
|
|
|
-- use some locals for speed
|
2009-03-21 19:59:49 +00:00
|
|
|
local sample = adc.sample
|
|
|
|
local getsample = adc.getsample
|
|
|
|
local samplesready = adc.samplesready
|
2009-06-29 16:45:28 +00:00
|
|
|
local i, v, tsample
|
2009-03-21 19:59:49 +00:00
|
|
|
|
|
|
|
while true do
|
|
|
|
for i, v in ipairs(adcchannels) do
|
2009-06-29 16:45:28 +00:00
|
|
|
tsample = getsample(v) -- try to get a sample
|
|
|
|
if not (tsample == nil) then -- if we have a new sample, then update display
|
2009-03-21 19:59:49 +00:00
|
|
|
term.gotoxy(1,i+3)
|
2009-04-12 20:55:34 +00:00
|
|
|
term.putstr(string.format("ADC%02d (%03d): %04d\n", v, adcsmoothing[i], tsample))
|
2009-03-21 19:59:49 +00:00
|
|
|
end
|
2009-06-29 16:45:28 +00:00
|
|
|
if adc.isdone(v) == 1 then adc.sample(v,128) end -- if samples have run out, start collection again
|
2009-03-21 19:59:49 +00:00
|
|
|
end
|
|
|
|
key = term.getch( term.NOWAIT )
|
2009-06-29 16:45:28 +00:00
|
|
|
if key == term.KC_ESC then break end -- exit if user hits Escape
|
2009-03-21 19:59:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
term.clrscr()
|
|
|
|
term.gotoxy(1, 1)
|