/*
 * rgbprog_basic.c
 * Created Nov 1, 2024
 */

#include <stdint.h>
#include "rgbled.h"

#include "user_config.h"


static uint8_t timeout;


const uint8_t * rgb_solid_desc(uint8_t idx)
{
    switch (idx) {
        case 0:
        case 1:  return RGB_STR_UNUSED;
        case 2:  return ">7 = blank";
        case 3:  return "Bitfield";
        case 4:
        case 5:  return RGB_STR_DELAY;
        case 10:
        case 11: return "Override Hue";
        case 12:
        case 13: return "Override Sat";
        case 14:
        case 15: return "Override Val";
        default:
            return RGB_STR_RESERVED;
    }
}

void rgb_solid(uint8_t *a, uint16_t tick)
{
    // 1=bitfield, 2=timeout-set, 4=state, 5=hue, 6=sat, 7=val
    // bitfield:
    // 7: blank / off
    // 3: override altcolor (if bit 1 is not set)
    // 1: override hsv
    // 0: flash

    int i;
    color_hsv hsv = {0, 0, 0};

    // timeout
    if (!timeout || (timeout > a[2])) {
        timeout = a[2];
    } else {
        timeout--;
        return;
    }

    if (a[1] & 0x80) {
        // override off
        hsv.v = 0;
    } else {
        // color select
        if (a[1] & 0x02) {
            hsv.h = a[5] * 6;
            hsv.s = a[6];
            hsv.v = a[7];
        } else if (a[1] & 0x08) {
            hsv.h = uconf.altcolor_hue * 6;
            hsv.s = uconf.altcolor_sat;
            hsv.v = uconf.altcolor_val;
        } else {
            hsv.h = uconf.favcolor_hue * 6;
            hsv.s = uconf.favcolor_sat;
            hsv.v = uconf.favcolor_val;
        }

        // flash
        if (a[1] & 0x01) {
            a[4] ^= 0x01;
            if (a[4] & 0x01) hsv.v = 0;
        }
    }

    // update
    hsv_out[0].h = hsv.h;
    hsv_out[0].s = hsv.s;
    hsv_out[0].v = hsv.v;

    for (i = 1; i < RGBLED_COUNT; i++) {
        hsv_out[i].h = hsv_out[0].h;
        hsv_out[i].s = hsv_out[0].s;
        hsv_out[i].v = hsv_out[0].v;
    }
}
