Files
CH32V208WBU6/DRV/drv_led.c
T
2023-06-11 18:46:26 +08:00

149 lines
3.0 KiB
C

/*
* drv_led.c
*
* Created on: Mar 31, 2023
* Author: gxms0
*/
#include "drv_led.h"
static leddef *root = NULL;
leddef red;
leddef green;
leddef blue;
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);
}
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;
}
}
}
}