sk6812d/src/sk6812.c

43 lines
1.0 KiB
C

#include <stdlib.h>
#include <stdint.h>
#include "logger.h"
#include "hat_spi.h"
#include "sk6812.h"
int sk6812_init(struct sk6812_ctx *ctx, struct hat_spi_ctx *hat_ctx, uint8_t strip_id, uint32_t num_modules)
{
ctx->hat_ctx = hat_ctx;
ctx->num_modules = num_modules;
ctx->strip_id = strip_id;
ctx->pixeldata = malloc(4*num_modules * sizeof(ctx->pixeldata[0]));
if(!ctx->pixeldata) {
LOG(LVL_ERR, "sk6812: malloc for pixeldata failed.");
return -1;
}
return 0;
}
void sk6812_shutdown(struct sk6812_ctx *ctx)
{
free(ctx->pixeldata);
}
void sk6812_set_colour(struct sk6812_ctx *ctx, uint32_t module, uint8_t red, uint8_t green, uint8_t blue, uint8_t white)
{
// despite the info in the datasheet, red and green are swapped :/
ctx->pixeldata[4*module + 0] = green;
ctx->pixeldata[4*module + 1] = red;
ctx->pixeldata[4*module + 2] = blue;
ctx->pixeldata[4*module + 3] = white;
}
int sk6812_send_update(struct sk6812_ctx *ctx)
{
return hat_spi_set_data(ctx->hat_ctx, ctx->strip_id, ctx->num_modules*4, ctx->pixeldata);
}