Skip to content

Instantly share code, notes, and snippets.

@imbushuo
Last active November 2, 2019 10:57
Show Gist options
  • Save imbushuo/2d18a9e6f771d0055223a78930a9171c to your computer and use it in GitHub Desktop.
Save imbushuo/2d18a9e6f771d0055223a78930a9171c to your computer and use it in GitHub Desktop.
Turn your Apple Magic Trackpad 2 into a vibrator
using HidSharp;
using System.Linq;
namespace ForceTouchTrigger
{
class Program
{
static void Main(string[] args)
{
// Force Touch Device: USB\VID_05AC&PID_0265&MI_02
// But I don't know if I have a better way to deal with multi collections
var hidDeviceList = DeviceList.Local.GetHidDevices(0x05ac, 0x0265).Where(i => i.DevicePath.Contains("mi_02"));
if (!hidDeviceList.Any()) return;
var dev = hidDeviceList.FirstOrDefault();
if (dev.TryOpen(out DeviceStream stream))
{
while (true)
{
// 14 bytes packed struct
// 0x53, 0x01: Magic (uint16_t)
// 0x15, 0x6c, 0x02, 0x00: Strength (uint32_t)
// Other fields remain unknown. I think they are X axis and Y axis data, and
// these values should be signed int32. No significant difference is obsereved
// when changing these fields. It's okay to write all zero for these fields.
//
// MacBook Pro will reject feature writes if you write it too fast
stream.Write(new byte[] { 0x53, 0x01, 0x15, 0x6C, 0x02, 0x00, 0x21, 0x2B, 0x06, 0x01, 0x00, 0x16, 0x41, 0x13 }, 0, 14);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment