All the source code for other modules is available from their respective pages so it's pretty easy to see what is done and work from there.
As it happens, I have some beta MAX30102 code that hasn't been added as a module yet:
var hrm = (function() { // MAX30102
var R = {
INTR_STATUS_1 : 0x00,
INTR_STATUS_2 : 0x01,
INTR_ENABLE_1 : 0x02,
INTR_ENABLE_2 : 0x03,
FIFO_WR_PTR : 0x04,
OVF_COUNTER : 0x05,
FIFO_RD_PTR : 0x06,
FIFO_DATA : 0x07,
FIFO_CONFIG : 0x08,
MODE_CONFIG : 0x09,
SPO2_CONFIG : 0x0A,
LED1_PA : 0x0C,
LED2_PA : 0x0D,
PILOT_PA : 0x10,
MULTI_LED_CTRL1 : 0x11,
MULTI_LED_CTRL2 : 0x12,
TEMP_INTR : 0x1F,
TEMP_FRAC : 0x20,
TEMP_CONFIG : 0x21,
PROX_INT_THRESH : 0x30,
REV_ID : 0xFE,
PART_ID : 0xFF
};
var i = new I2C();
i.setup({scl:P.HRM_SCL,sda:P.HRM_SDA});
function r(a,n) {
i.writeTo(87,a);
return i.readFrom(87,n);
}
function w(a,d) {
i.writeTo(87,[a,d]);
}
function init() {
if (r(R.PART_ID,1)!=0x15) throw new Error("WHO_AM_I failed");
w(R.MODE_CONFIG,0x40); // reset all
w(R.INTR_ENABLE_1,0xc0); // INTR setting
w(R.INTR_ENABLE_2,0x00);
w(R.FIFO_WR_PTR,0x00); //FIFO_WR_PTR[4:0]
w(R.OVF_COUNTER,0x00); //OVF_COUNTER[4:0]
w(R.FIFO_RD_PTR,0x00); //FIFO_RD_PTR[4:0]
w(R.FIFO_CONFIG,0b10001111); //sample avg = 4, fifo rollover=false, fifo almost full = 17
w(R.MODE_CONFIG,0x02); //0x02 for Red only, 0x03 for SpO2 mode 0x07 multimode LED
w(R.SPO2_CONFIG,0b00101111); // SPO2_ADC range = 4096nA, SPO2 sample rate (400 Hz), LED pulseWidth (411uS)
w(R.LED1_PA,0x3f); //Choose value for ~ 7mA for LED1
w(R.LED2_PA,0x3f); // Choose value for ~ 7mA for LED2
w(R.PILOT_PA,0x3f); // Choose value for ~ 8mA for Pilot LED (?)(
}
function kill() {
w(R.MODE_CONFIG,0x40);
}
function read() {
var d = r(R.FIFO_DATA,6);
return {
red : (d[0]<<16)|(d[1]<<8)|d[2],
ir : (d[3]<<16)|(d[4]<<8)|d[5]
};
}
function readFifoRaw() {
var fifo = r(R.FIFO_WR_PTR,3); // wr,overflow,rd
var s = fifo[0]-fifo[2];
if (s<=0) s+=32;
if (fifo[1]) s=32; //overflowed
var result = [];
return r(R.FIFO_DATA,3*s);
}
function decodeRawData(d, red) {
var l = d.length;
for (var i=0,n=0;i<l;i+=3,n++) {
red[n] = (d[i+0]<<16)|(d[i+1]<<8)|d[i+2];
}
}
var h = {
r:r,w:w,
init:init,
kill:kill,
read:read, // read one
readFifoRaw:readFifoRaw, // read entire fifo as raw data (32 elements max, 6 bytes each)
decodeRawData:decodeRawData // decode data read with readFifoRaw
};
// IRQ seems not to work at the moment
//pinMode(P.HRM_INT,"input_pullup");
//setWatch(function() {
// h.emit('data',read());
//}, P.HRM_INT, {repeat:true,edge:-1});
// could use with hrm.on('data',x=>print("hrm",x));
return h;
})();
function getHRM() {
print("Starting HRM...");
hrm.init();
var cnt = 100;
var rawdata = new Uint8Array(cnt*3);
var dcnt = 0;
print("Waiting for HRM... wait 1 second");
var i = setInterval(function() {
hrm.readFifoRaw();
});
setTimeout(function() {
print("Reading HRM... wait 10 seconds");
clearInterval(i);
hrm.readFifoRaw();
var t = getTime();
var i = setInterval(function() {
d = hrm.readFifoRaw();
rawdata.set(d, dcnt*3);
dcnt += d.length/3;
if (dcnt>=cnt) {
drawHRM(rawdata);
dcnt=0;
}
}, 1000);
}, 1000);
}
function drawHRM(rawdata) {
var cnt = rawdata.length/3;
var dred = new Uint32Array(cnt);
hrm.decodeRawData(rawdata, dred);
var g = Graphics.createArrayBuffer(cnt,60,1);
var red = E.sum(dred)/cnt;
g.clear();
g.drawString("Red",2,2);
g.moveTo(-100,0);
for (var i=0;i<cnt;i++)
g.lineTo(i,30+(dred[i]-red)/10);
g.dump();
}
If you want to find out about temperature sensors that are supported out of the box with Espruino, just type 'temperature' into the search box: http://www.espruino.com/Search?kw=temperature
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Then you can write your own module for it, eg: http://www.espruino.com/Writing+Modules
All the source code for other modules is available from their respective pages so it's pretty easy to see what is done and work from there.
As it happens, I have some beta MAX30102 code that hasn't been added as a module yet:
If you want to find out about temperature sensors that are supported out of the box with Espruino, just type 'temperature' into the search box: http://www.espruino.com/Search?kw=temperature