v: current ADC_raw
low: min test-range ADC_raw
high: max test-range ADC_raw
vpercent=100*(v-low)/(high-low)
You are right. But.....
Lego firmware provided a "calibration mechanism", so user doesn't care the min or max raw value things when they using light or sound sensor.Just put a sensor block.Or put a calibrate block at the beginning if they want to calibrate. And NOTHING changes in code except adding calibrate block.
And you code is the SysComputeCalibValue doing exactly.
I would love to plug in some NXC light sensor calibration and read calibrated value functions into the official API headers and online documentation if anyone (hint, hint) wanted to provide me with some nice ones.
Let me explain the implementation of SysComputeCalibValue and SysUpdateCalibCacheInfo in lego firmware. Actually, I want to provide some words for document, but you guys known my english level
.
1.Threre are two places to store calibration data. One is in the file system naming "Sound sensor.cal" or "Light sensor.cal", the other one in the memory loading from file when program start.
2.There are only two values for each calibration data -- min raw value and max.
3.SysUpdateCalibCacheInfo will update the calibration data in memory.
4.The SysComputeCalibValue, do something like doc-helmut 's code. Find the calibaration data, then calculate the scale value using the min or max raw value . The min and max will be 0 and 1023 if calibration data don't exist.
5.That's all almost.
6.In NXT-G, a calibration block changes only one value of calibration data, min or max. But SysUpdateCalibCacheInfo needs them two. How to get another one? There isn't a SysGetCalibCacheInfo.
So,NXT-G read it from file directly, then update the cache and write back the file.It's silly and slow.
User will get the same sensor value from NXC and NXT-G , if ScaleValue for light or sound is calibrated in NXC.