Build-a-Driver #11: Fix delay after changing resolution

Here's the eleventh post in the Build-a-Driver series for MCP9808.
(If this post seems out of context, please refer to "Build-a-Driver Introduction" post.)


A few posts ago I intended to correct the problem of the driver reporting values in the wrong resolution immediately following a resolution setting change.   See here for that close, but not quite right, solution. 

The intent was to delay for "conversion time" and it seemed lucky that I was indexed into the array of conversion times.  The error was in using the conversion time for the resolution being requested. 

The problem became most apparent when we went from .0625 to .5 resolutions.  The conversion time for .0625 is 250ms and for .5 it is 30ms.  So, using the 30ms delay when going from .0625 to .5 was not enough time. 

The point of the delay is to allow the sensor to finish its current measurement in it's current conversion time and start sampling in the new conversion time. The driver will now delay for the conversion time we are exiting plus the conversion time we are entering.  

To implement I decided to start saving an index into the resolution time array in the global data, rather than the resolution time itself.  This way I don't have to look through the array (short as it may be) for that conversion time.   

This change did ripple throughout the driver wherever that global data resolution field was referenced.  This version also includes naming cleanups intended to improve readability.

This version mcp9808-fix-delay.c
To view complete revision history go here.

Labels: , ,