/* * drv_led.c * * Created on: Mar 31, 2023 * Author: gxms0 */ #include "drv_led.h" static leddef *root = NULL; leddef red; leddef green; leddef blue; leddef led_m1; leddef led_m2; leddef led_m3; leddef led_m4; void LED_Init(void) { LED_install(&red,"red",GPIOA,GPIO_Pin_7,0); LED_install(&green,"green",GPIOC,GPIO_Pin_4,0); LED_install(&blue,"blue",GPIOC,GPIO_Pin_5,0); LED_install(&led_m1,"led_m1",GPIOA,GPIO_Pin_5,0); LED_install(&led_m2,"led_m2",GPIOA,GPIO_Pin_15,0); LED_install(&led_m3,"led_m3",GPIOD,GPIO_Pin_3,0); LED_install(&led_m4,"led_m4",GPIOA,GPIO_Pin_4,0); } void LED_install(leddef *e,char *name,GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin,uint8_t State) { GPIO_InitTypeDef GPIO_InitStructure = {0}; uint32_t RCC_Periph; if(GPIOx == GPIOA)RCC_Periph = RCC_APB2Periph_GPIOA; else if(GPIOx == GPIOB)RCC_Periph = RCC_APB2Periph_GPIOB; else if(GPIOx == GPIOC)RCC_Periph = RCC_APB2Periph_GPIOC; else if(GPIOx == GPIOD)RCC_Periph = RCC_APB2Periph_GPIOD; else if(GPIOx == GPIOE)RCC_Periph = RCC_APB2Periph_GPIOE; RCC_APB2PeriphClockCmd(RCC_Periph, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOx, &GPIO_InitStructure); e->next_ptr = NULL; e->name = name; e->GPIO_Pin = GPIO_Pin; e->GPIOx = GPIOx; e->State = 0x00; e->timeout = 1000; if(State)//1 on 0 off { GPIO_WriteBit(e->GPIOx, e->GPIO_Pin, Bit_RESET); e->State = 0x01; } else { GPIO_WriteBit(e->GPIOx, e->GPIO_Pin, Bit_SET); e->State = 0x00; } if (root) { leddef *last = root; while (last->next_ptr) { last = last->next_ptr; } last->next_ptr = e; e->channel = last->channel + 1; } else { root = e; } } void LED_setOn(uint16_t channel) { if (root) { leddef *e = root; while (e) { if(e->channel == channel) { GPIO_WriteBit(e->GPIOx, e->GPIO_Pin, Bit_RESET); e->State = 0x01; break; } else { e = e->next_ptr; } } } } void LED_setOff(uint16_t channel) { if (root) { leddef *e = root; while (e) { if(e->channel == channel) { GPIO_WriteBit(e->GPIOx, e->GPIO_Pin, Bit_SET); e->State = 0x00; break; } else { e = e->next_ptr; } } } } void LED_setToggle(uint16_t channel) { if (root) { leddef *e = root; while (e) { if(e->channel == channel) { if(e->State == 0x00) { GPIO_WriteBit(e->GPIOx, e->GPIO_Pin, Bit_RESET); e->State = 0x01; } else { GPIO_WriteBit(e->GPIOx, e->GPIO_Pin, Bit_SET); e->State = 0x00; } break; } else { e = e->next_ptr; } } } }