diff --git a/Firmware/src/drivers/led.c b/Firmware/src/drivers/led.c index d7588b2..671a763 100644 --- a/Firmware/src/drivers/led.c +++ b/Firmware/src/drivers/led.c @@ -1,137 +1,138 @@ /* * Copyright (C) 2017 EPFL-LSRO (Laboratoire de Systemes Robotiques). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "led.h" #include "../communication.h" #define N_LEDS 4 #define LED_TIMER TIM1 #define LED_PORT GPIOA #define LED_MAX_DUTY 255 #define LED_PWM_FREQ 32000 // [Hz]. typedef struct { uint16_t const pin, pinSource; volatile uint32_t *const duty; } led_Led; led_Led led_leds[N_LEDS] = { { GPIO_Pin_8, GPIO_PinSource8, &LED_TIMER->CCR1 }, { GPIO_Pin_9, GPIO_PinSource9, &LED_TIMER->CCR2 }, { GPIO_Pin_10, GPIO_PinSource10, &LED_TIMER->CCR3 }, { GPIO_Pin_11, GPIO_PinSource11, &LED_TIMER->CCR4 }, }; void setLed0(float32_t brightness) { led_Set(0, brightness); }; void setLed1(float32_t brightness) { led_Set(1, brightness); }; void setLed2(float32_t brightness) { led_Set(2, brightness); }; void setLed3(float32_t brightness) { led_Set(3, brightness); }; float32_t getLed0(void) { return led_Get(0); }; float32_t getLed1(void) { return led_Get(1); }; float32_t getLed2(void) { return led_Get(2); }; float32_t getLed3(void) { return led_Get(3); }; /** * @brief Initializes the LEDs module. */ void led_Init(void) { int i; GPIO_InitTypeDef GPIO_InitStruct; TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct; TIM_OCInitTypeDef TIM_OCInitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); // Initialize each pin. for(i=0; i= 0 && ledIndex < N_LEDS) return ((float32_t)(*led_leds[ledIndex].duty)) / (float32_t)LED_MAX_DUTY; else return 0.0f; } /** * @brief Sets the intensity of a single LED. * @param ledIndex: LED index (0, 1, 2 or 3). * @param brightness: the LED intensity [0.0-1.0]. */ void led_Set(int ledIndex, float32_t brightness) { if(ledIndex >= 0 && ledIndex < N_LEDS && brightness >= 0.0f && brightness <= 1.0f) { *led_leds[ledIndex].duty = (uint32_t)(brightness * (float32_t)LED_MAX_DUTY); } }