C example for Unicorn hat HD

We don’t normally include C examples for any of our libraries, since it’s a can of worms that we don’t have the time or manpower to support. That said, it should be pretty trivial to drive Unicorn HAT HD from C.

The only relevant parts of the Python library are setting up the SPI: https://github.com/pimoroni/unicorn-hat-hd/blob/master/library/unicornhathd/init.py#L19-L21
And sending the buffer: https://github.com/pimoroni/unicorn-hat-hd/blob/master/library/unicornhathd/init.py#L127

The buffer is just a block of 768 (RGB * 16 * 16) bytes blasted straight out to Unicorn HAT HD, with a Start Of Frame byte preceding them: 0x72

Your buffer, in C, might as well just be a flat array of 768 bytes, or even 769 with the SOF byte already dropped in place. Something like (code typed from memory and not tested!):

uint8_t buffer[769];
buffer[0] = 0x72;

void set_pixel(uint8_t x, uint8_t y, uint8_t r, uint8_t g, uint8_t b) {
    uint16_t offset = (x * 16) + y;
    buffer[offset + 0] = r;
    buffer[offset + 1] = g;
    buffer[offset + 2]  = b;
}

And you could use the BCM2835 SPI library: http://www.airspayce.com/mikem/bcm2835/group__spi.html

Or WiringPi’s SPI library: http://wiringpi.com/reference/spi-library/