/* * File: rgbled.h * Description: RGB LED library * Author: Jan Mrna * Date: 2021-10-11 * * * Copyright (c) 2024 Veles Labs s.r.o. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * */ #include "rgbled.h" static rgbled_context_t *rgbled_context; static uint8_t rgbled_intensity = RGBLED_MAX_INTENSITY; /* LED intensity from 0 (off) to RGBLED_MAX_INTENSITY (full power) */ int8_t rgbled_init(rgbled_context_t *context) { if (context == NULL) { return RGBLED_ERROR; } rgbled_context = context; return RGBLED_OK; } int8_t rgbled_set_color(uint8_t R, uint8_t G, uint8_t B) { uint8_t colors[3]; uint8_t i; colors[0] = (uint32_t)R * rgbled_intensity / RGBLED_MAX_INTENSITY; colors[1] = (uint32_t)G * rgbled_intensity / RGBLED_MAX_INTENSITY; colors[2] = (uint32_t)B * rgbled_intensity / RGBLED_MAX_INTENSITY; for (i = 0; i < 3; i++) { if (colors[i] > RGBLED_PERIOD) { return RGBLED_ERROR; } } for (i = 0; i < 3; i++) { switch (rgbled_context->channel_array[i]) { case 1: LL_TIM_OC_SetCompareCH1(rgbled_context->timer_array[i], (uint32_t)(RGBLED_PERIOD - colors[i])); break; case 2: LL_TIM_OC_SetCompareCH2(rgbled_context->timer_array[i], (uint32_t)(RGBLED_PERIOD - colors[i])); break; case 3: LL_TIM_OC_SetCompareCH3(rgbled_context->timer_array[i], (uint32_t)(RGBLED_PERIOD - colors[i])); break; case 4: LL_TIM_OC_SetCompareCH4(rgbled_context->timer_array[i], (uint32_t)(RGBLED_PERIOD - colors[i])); break; default: return RGBLED_WRONG_CHANNEL; } } return RGBLED_OK; } int8_t rgbled_set_intensity(uint8_t intensity) { if (intensity > RGBLED_MAX_INTENSITY) { return RGBLED_ERROR; } rgbled_intensity = intensity; return RGBLED_OK; } int8_t rgbled_get_intensity(uint8_t *intensity) { if (intensity == NULL) { return RGBLED_ERROR; } *intensity = rgbled_intensity; return RGBLED_OK; } int8_t rgbled_off(void) { rgbled_set_color(RGBLED_OFF); return RGBLED_OK; }