// set up I2C
var i2c = new I2C();
i2c.setup({ scl : D25, sda: D26 });
var P = {
HRM_SCL : D25,
HRM_SDA : D26
};
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();
}
getHRM();
in the device (MAx30102):
connected 3.3V,GND
SCL-D25
SDA-D26
after I uplaod the code I get this error:
>Starting HRM...
Uncaught Error: WHO_AM_I failed
at line 45 col 60
...new Error("WHO_AM_I failed");
^
in function "init" called from line 103 col 12
hrm.init();
^
in function "getHRM" called from line 143 col 8
getHRM();
why?
what did I do wrong ?
I disconnected the max3012 and connect it to the arduino - it's working (so now hardware issue ) . did it just to be sure
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.
this is what I did:
in the device (MAx30102):
connected 3.3V,GND
SCL-D25
SDA-D26
after I uplaod the code I get this error:
why?
what did I do wrong ?
I disconnected the max3012 and connect it to the arduino - it's working (so now hardware issue ) . did it just to be sure