• Awesome tips -thx! Responses:

    Is OpenEmu running on the same Mac you're developing with?

    yup!

    Maybe try NRF.sendHIDReport([buttons, x, y], () => { inside a try { ... } catch...

    Done

    I'd remove the print statements.

    Ahh - didn't realize that could cause issues. Removed.

    You could add a little LED flash in your code so you can see if update is still executing correctly

    Done - great suggestion. I have it flashing blue when it starts the sendHID function, red when NRF.sendHIDReport() is successful, and green when it's sending a button press.

    With those changes it does expose something weird. When I connect via the IDE, I can flash the device and see both the red and blue LEDs blinking. When I disconnect from the IDE and only connect via the mac bluetooth menu, I only ever see the blue led blinking. So NRF.sendHIDReport() never seems to be successful, unless I connect via the IDE...

    Does that add up to anything obviously incorrect?

    //https://github.com/espruino/BangleApps/blob/master/apps/boot/hid_info.txt
    var joystick_report = new Uint8Array([
      0x05, 0x01,       // Usage Page (Generic Desktop)
      0x09, 0x04,       // Usage (Joystick)
      0xA1, 0x01,       // Collection (Application)
    	0x09, 0x01,       // Usage (Pointer)
    	0xA1, 0x00,       // Collection (Physical)
      // Buttons
      0x05, 0x09,       // Usage Page (Buttons)
      0x19, 0x01,       // Usage Minimum (1)
      0x29, 0x05,       // Usage Maximum (5)
      0x15, 0x00,       // Logical Minimum (0)
      0x25, 0x01,       // Logical Maximum (1)
      0x95, 0x05,       // Report Count (5)
      0x75, 0x01,       // Report Size (1)
      0x81, 0x02,       // Input (Data, Variable, Absolute)
    
      // padding bits
      0x95, 0x03,       // Report Count (3)
      0x75, 0x01,       // Report Size (1)
      0x81, 0x03,       // Input (Constant)
    
      // Stick
      0x05, 0x01,       // Usage Page (Generic Desktop)
      0x09, 0x30,       // Usage (X)
      0x09, 0x31,       // Usage (Y)
      0x15, 0x81,       // Logical Minimum (-127)
      0x25, 0x7f,       // Logical Maximum (127)
      0x75, 0x08,       // Report Size (8)
      0x95, 0x02,       // Report Count (2)
      0x81, 0x02,       // Input (Data, Variable, Absolute)
    	0xC0,              // End Collection (Physical)
      0xC0              // End Collection (Application)
    ]);
    
    NRF.setServices(undefined, { hid : joystick_report });
    
    Puck.accelOn(); // default is 12.5Hz, with gyro
    
    var sendInProgress = false; // Only send one message at a time, do not flood
    const sendHid = function (x, y, btn1, btn2, btn3, btn4, btn5, cb) {
      try {
        const buttons = (btn5<<4) | (btn4<<3) | (btn3<<2) | (btn2<<1) | (btn1<<0);
        if (!sendInProgress) {
          sendInProgress = true;
          try {
            NRF.sendHIDReport([buttons, x, y], () => 
            {
              sendInProgress = false;
              //print ([buttons, x, y]);
              digitalPulse(LED1,1,1);
              if (buttons == 1) {
                digitalPulse(LED2,1,1);
              }
            }
            );
          } catch(e) {
            print(e);
          }
        }
      } catch(e) {
        print(e);
      }
    };
    
    function update() {
      const btn1 = BTN1.read();
      const accel = Puck.accel();
      var x = accel.acc.x/128000;
      var y = accel.acc.y/128000;
      
      // rescale to approximately [-127:127]
      x *= 10;
      y *= 10;
      // I assume the joystick requires ints
      x = Math.floor( x * 127 );
      y = Math.floor( y * 127); 
      
      // check limits
      if (x > 127) x = 127;
      else if (x < -127) x = -127;
      if (y > 127) y = 127;
      else if (y < -127) y = -127;
      
      sendHid(x & 0xff, y & 0xff, btn1, false, false, false, false);
      //print ("x", x, "y", y, "btn1", btn1);
    }
    
    setInterval(update, 100); // 10 Hz
    
About

Avatar for DrewS @DrewS started