adc121c021: Updated JavaScript example for adc121c021 I2C ADC sensor

This patch fixes the JS example to be more like the C++ example.

Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Zion Orent 2015-01-21 18:21:49 -05:00 committed by Mihai Tudor Panu
parent a665b5d6f1
commit fec3f22765

View File

@ -3,7 +3,7 @@
/*global */ /*global */
/* /*
* Author: Zion Orent <zorent@ics.com> * Author: Zion Orent <zorent@ics.com>
* Copyright (c) 2014 Intel Corporation. * Copyright (c) 2015 Intel Corporation.
* *
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the
@ -28,37 +28,18 @@
// Load ADC121C021 module // Load ADC121C021 module
var I2C_ADC = require('jsupm_adc121c021'); var I2C_ADC = require('jsupm_adc121c021');
// Instantiate an ADC121C021 on I2C bus 0 // Instantiate an ADC121C021 on I2C bus 0
var myI2C_ADC = new I2C_ADC.ADC121C021(I2C_ADC.ADC121C021_I2C_BUS, var busID = I2C_ADC.ADC121C021_I2C_BUS;
I2C_ADC.ADC121C021_DEFAULT_I2C_ADDR); var I2CAddr = I2C_ADC.ADC121C021_DEFAULT_I2C_ADDR;
var g_sum, g_iteration, g_intervalObj; var myI2C_ADC = new I2C_ADC.ADC121C021(busID, I2CAddr);
var g_total_iteration_count = 30;
// An analog sensor, such as a Grove light sensor,
// must be attached to the adc
main();
// this function will be called by itself // get the data every 50 milliseconds
function main() setInterval(function()
{ {
g_sum = 0; var val = myI2C_ADC.value();
g_iteration = 0; var voltsVal = myI2C_ADC.valueToVolts(val);
// Sum the value every 50 milliseconds for g_total_iteration_count times console.log("ADC value: " + val + " Volts = " + voltsVal);
// Then print the average value and corresponding voltage }, 50);
g_intervalObj = setInterval(function(I2CADC_val)
{
g_sum += I2CADC_val;
if (g_iteration >= g_total_iteration_count)
{
g_iteration = 0;
var val = (g_sum / g_total_iteration_count);
console.log("ADC value: " + val + " Volts = " +
myI2C_ADC.valueToVolts(val));
clearInterval(g_intervalObj);
// Now that we're done with this iteration, go to the next iteration
main();
}
g_iteration++;
}, 50, myI2C_ADC.value());
}
// Print message when exiting // Print message when exiting
process.on('SIGINT', function() process.on('SIGINT', function()