Skip to content

Instantly share code, notes, and snippets.

@cmsj
Created September 7, 2017 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmsj/fcf5d5fcd3947601658f5201e006207f to your computer and use it in GitHub Desktop.
Save cmsj/fcf5d5fcd3947601658f5201e006207f to your computer and use it in GitHub Desktop.
- (void)fill:(int)button withColor:(NSColor*)fillColor {
int imageSideLength = 72;
NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(imageSideLength, imageSideLength)];
[image lockFocus];
[fillColor drawSwatchInRect:NSMakeRect(0, 0, imageSideLength, imageSideLength)];
[image unlockFocus];
//NSData *imageTiff = image.TIFFRepresentation;
//NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageTiff];
//NSData *data = [imageRep representationUsingType:NSBitmapImageFileTypeBMP properties:@{}];
NSData *data = [image bmpData];
int reportLength = 8191;
uint8_t reportMagic[] = {0x02, // Report ID
0x01, // Unknown (always seems to be 1)
0x01, // Image Page
0x00, // Padding
0x00, // Continuation Bool
button // Deck button to set
};
int imageLen = (int)data.length;
int halfImageLen = imageLen / 2;
const uint8_t *imageBuf = data.bytes;
// Prepare and send the first half of the image
NSMutableData *reportPage1 = [NSMutableData dataWithLength:reportLength];
[reportPage1 replaceBytesInRange:NSMakeRange(0, 6) withBytes:reportMagic];
[reportPage1 replaceBytesInRange:NSMakeRange(16, halfImageLen) withBytes:imageBuf];
const uint8_t *rawPage1 = (const uint8_t *)reportPage1.bytes;
IOHIDDeviceSetReport(self.device, kIOHIDReportTypeOutput, rawPage1[0], rawPage1, reportLength);
// Prepare and send the second half of the image
NSMutableData *reportPage2 = [NSMutableData dataWithLength:reportLength];
reportMagic[2] = 2;
reportMagic[4] = 1;
[reportPage2 replaceBytesInRange:NSMakeRange(0, 6) withBytes:reportMagic];
[reportPage2 replaceBytesInRange:NSMakeRange(16, halfImageLen) withBytes:imageBuf+halfImageLen];
const uint8_t *rawPage2 = (const uint8_t *)reportPage2.bytes;
IOHIDDeviceSetReport(self.device, kIOHIDReportTypeOutput, rawPage2[0], rawPage2, reportLength);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment