• Thanks - I hadn't realised I could alter the LED intensity without rebuilding the firmware - I'll try that first, then have a look at using the analogue output as you suggest.

  • This code can be used to view the data, might be useful. It is using analogRead from old method of reading pin directly instead of talk through i2c. Could be interesting to compare outputs. Pass hrm.raw into instead of val to c.onHRM to see difference. (You have to adjust graph constraints too, hrm.raw are huge numbers between 0 - 10000 ).
    EDIT: It is using wrong pin (29), its not hrm data

    var c = E.compiledC(`
      // void onHRM(int,int,int)
      const unsigned char windowSize = 25;
      typedef struct inputs {
        short * vals;
      } inputs_t;
      void onHRM(unsigned int &tick,int val,inputs_t *argss){
        argss->vals[tick % windowSize] = val;
    let newUint32FlatArray = len => {
      let fs = E.toFlatString((new Uint32Array(len)).buffer);
      if ( !fs ) throw new Error("Required to be a flatstring");
      let obj = new Uint32Array(E.toArrayBuffer(fs));
      return {"obj" : obj, "ref" : E.getAddressOf(obj,true) };
    let newInt16FlatArray = len => {
      let fs = E.toFlatString((new Int16Array(len)).buffer);
      if ( !fs ) throw new Error("Required to be a flatstring");
      let obj = new Int16Array(E.toArrayBuffer(fs));
      return {"obj" :  obj, "ref" : E.getAddressOf(obj,true) };
      hrmGreenAdjust: false
    let windowSize = 25;
    var vals = newInt16FlatArray(windowSize);
    var inputs = newUint32FlatArray(6);
    inputs.obj[0] = vals.ref;
    let tickFlat = newUint32FlatArray(1);
    tickFlat.obj[0] = 0;
    let tickCountOld = 0;
    //This is called by idleLoop. Not an interrupt.
    Bangle.on('HRM-raw', function(hrm) {
      let val = Math.round(analogRead(29)* 16383);
    //25Hz, Bangle.js 2
    //50Hz, Bangle.js 1
    setInterval(function hmm(){
        let tick = tickFlat.obj[0];
        tickCountOld = tick;
        let nonCircVals = [];
        //circular to non-circular.
        for ( let i = 0; i < windowSize; i++ ) {
          nonCircVals.push(vals.obj[(tick + 1 + i) % windowSize]);
        require("graph").drawLine(g, nonCircVals,{axes:false,miny:-50,maxy:50­,gridy:25});
  • Thank you very much for this example, d3nd3-o0. I have never seen inline C code in javascript before, so this is a bit of an education for me!
    I will have a go with your example tomorrow because my attempt to implement the PineTime HRM graph app in javascript is not being very successful - I was assuming that the 'on hrm-raw' functions etc. were interrupt driven, but I was taking too long to draw the graph and blocking it, so need to be more careful.

    I see from the source code that there is another register that is supposed to adjust the LED current - but I find that if I try to set this, then the HRM stops updating after about 50 samples. I can see the LED intensity changing with register 0x17 as you suggested though.
    But so far, I have not seen a stunning effect on the measured signal - will do some more digging tomorrow - thank you all for your suggestions!


Avatar for d3nd3-o0 @d3nd3-o0 started