/*
 * This file is part of the Pico Keys SDK distribution (https://github.com/polhenarejos/pico-keys-sdk).
 * Copyright (c) 2022 Pol Henarejos.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include "pico_keys.h"

#ifdef PICO_PLATFORM
#ifdef PICO_DEFAULT_LED_PIN
static uint8_t gpio = PICO_DEFAULT_LED_PIN;
#else
static uint8_t gpio = 0;
#endif

uint8_t pixel[][3] = {
    {1, 1, 1}, // 0: off
    {0, 1, 1}, // 1: red
    {1, 0, 1}, // 2: green
    {1, 1, 0}, // 3: blue
    {0, 0, 1}, // 4: yellow
    {0, 1, 0}, // 5: magenta
    {1, 0, 0}, // 6: cyan
    {0, 0, 0}  // 7: white
};

void led_driver_init_pimoroni() {
    if (phy_data.led_gpio_present) {
        gpio = phy_data.led_gpio;
    }
    gpio_init(gpio-1);
    gpio_set_dir(gpio-1, GPIO_OUT);
    gpio_init(gpio);
    gpio_set_dir(gpio, GPIO_OUT);
    gpio_init(gpio+1);
    gpio_set_dir(gpio+1, GPIO_OUT);
}

void led_driver_color_pimoroni(uint8_t color, uint32_t led_brightness, float progress) {
    if (progress < 0.5) {
        color = LED_COLOR_OFF;
    }
    gpio_put(gpio-1, pixel[color][0]);
    gpio_put(gpio, pixel[color][1]);
    gpio_put(gpio+1, pixel[color][2]);
}

led_driver_t led_driver_pimoroni = {
    .init = led_driver_init_pimoroni,
    .set_color = led_driver_color_pimoroni,
};

#endif
