Sunday 15 February 2015

Android: Custom Oscilliscope View Scan Rate -



Android: Custom Oscilliscope View Scan Rate -

i have written custom view shows simulated oscilloscope, holds series of points plot, , periodically places pulse (a separate set of points) main series. problem i'd update points @ right rate of 25mm/sec on 1 plot , 4mm/sec on another.

setrate() called before view drawn. updatedata() called thread runs loop @ 60fps systemclock.elapsedrealtime(). problem pxtodraw not right value maintain @ rate i'd like.

here code

static class fakeplot extends oscview.plot { public void setrate(int width, displaymetrics dm) { float xdpi = dm.xdpi; mwidthmm = width/ xdpi * 25.4f; pxpersec =typedvalue.applydimension(typedvalue.complex_unit_mm, mscanratemm, dm); pxpertick = ((float)width) / mdata.length; lastpulse = systemclock.elapsedrealtime(); log.e(tag,string.format("setrate() %s: pxpersec=%f pxpertick=%f",mtitle,pxpersec,pxpertick)); } /** * re-create next set of points along * @param realtime */ public void updatedata(long realtime) { long diff = realtime - lastupdate; lastupdate = realtime; float pxtodraw = ((diff/1000f)/pxpersec); float tickstodraw = pxtodraw/(1f/pxpertick); (int j = 0; j < tickstodraw; j++){ // update mdata next tickstodrawpoints } setchanged(); notifyobservers(); } }

a total working illustration (without right scan rate here: http://pastebin.com/nhbxumhv)

the right code is:

float extratodraw; public void updatedata(long realtime) { float mmpertick = mwidthmm/mdata.length; float mmtodraw = (mscanratemm*diffsec)+extratodraw; if(mmtodraw < 1) {extratodraw = mmtodraw;return;} extratodraw = 0; float drawn = 0; while(drawn < mmtodraw) { drawn+= mmpertick; // draw next tick } }

android android-view

No comments:

Post a Comment