• Final working code:

    let debug = 0;
    let code = 0;
    let timeout;
    let lastTime;
    
    const VOLUME_UP    = 16712445;
    const VOLUME_DOWN  = 16750695;
    const PLAY_PAUSE   = 16754775;
    const MUTE         = 16720605;
    const NEXT         = 16748655;
    const PREV         = 16769055;
    const STOP         = 16756815;
    
    E.setUSBHID({
      reportDescriptor : [
      0x05, 0x0c,  // USAGE_PAGE (Consumer Devices)
      0x09, 0x01,  // USAGE (Consumer Control)
      0xa1, 0x01,  // COLLECTION (Application)
                   // -------------------- common global items
      0x15, 0x00,  //   LOGICAL_MINIMUM (0)
      0x25, 0x01,  //   LOGICAL_MAXIMUM (1)
      0x75, 0x01,  //   REPORT_SIZE (1)    - each field occupies 1 bit
                   // -------------------- misc bits
      0x95, 0x05,  //   REPORT_COUNT (5)
      0x09, 0xb5,  //   USAGE (Scan Next Track)
      0x09, 0xb6,  //   USAGE (Scan Previous Track)
      0x09, 0xb7,  //   USAGE (Stop)
      0x09, 0xcd,  //   USAGE (Play/Pause)
      0x09, 0xe2,  //   USAGE (Mute)
      0x81, 0x06,  //   INPUT (Data,Var,Rel)  - relative inputs
                   // -------------------- volume up/down bits
      0x95, 0x02,  //   REPORT_COUNT (2)
      0x09, 0xe9,  //   USAGE (Volume Up)
      0x09, 0xea,  //   USAGE (Volume Down)
      0x81, 0x02,  //   INPUT (Data,Var,Abs)  - absolute inputs
                   // -------------------- padding bit
      0x95, 0x01,  //   REPORT_COUNT (1)
      0x81, 0x01,  //   INPUT (Cnst,Ary,Abs)
      0xc0         // END_COLLECTION
      ]
    });
    
    function p(c, cb) {
      E.sendUSBHID([c]);
      setTimeout(function() {
        E.sendUSBHID([0]);
        cb();
      },100);
    }
    
    next = function (cb) {
      p(0x1, cb);
      console.log('next');
    };
    prev = function (cb) {
      p(0x2, cb);
      console.log('prev');
    };
    stop = function (cb) {
      p(0x4, cb);
      console.log('stop');
    };
    playpause = function (cb) {
      p(0x8, cb);
      console.log('playpause');
    };
    mute = function (cb) {
      p(0x10, cb);
      console.log('mute');
    };
    volumeUp = function (cb) {
      p(0x20, cb);
      console.log('volumeUp');
    };
    volumeDown = function (cb) {
      p(0x40, cb);
      console.log('volumeDown');
    };
    
    const cb = () => {};
    
    function handleCode() {
      timeout = undefined;
      if (debug) print(code);
      LED2.write(true);
    
      if (code === PLAY_PAUSE)  playpause(cb);
      if (code === VOLUME_UP)   volumeUp(cb);
      if (code === VOLUME_DOWN) volumeDown(cb);
      if (code === MUTE)        mute(cb);
      if (code === NEXT)        next(cb);
      if (code === PREV)        prev(cb);
      if (code === STOP)        stop(cb);
    
      code = 0;
      LED2.write(false);
    }
    
    // When an IR pulse is detected
    function onPulseOn(e) {
      code = (code * 2) | (e.time - lastTime > 0.0008);
      if (timeout !== undefined) clearTimeout(timeout);
      timeout = setTimeout(handleCode, 20);
      lastTime = e.time;
    }
    
    // When IR pulse stops
    function onPulseOff(e) {
      lastTime = e.time;
    }
    
    setWatch(onPulseOff, A5, { repeat: true, edge: "rising" });
    setWatch(onPulseOn, A5, { repeat: true, edge: "falling" });
    
    setWatch(
      // Press the button on the pico to enable debug mode and
      // see the IR code for your own remote
      function (e) {
        debug = !debug;
        console.log(`Debug mode ${debug ? "on" : "off"}`);
      },
      BTN,
      { repeat: true }
    );
    
    
About

Avatar for Stoaty @Stoaty started