串口工具点灯
#define MAX 6
int cmp_func(const char *str, char *commnd)
{
int i;
for (i = 0; str[i] != 0; i++)
{
if (str[i] != commnd[i])
return -1;
}
if (str[i] == commnd[i])
return 0;
else
return -1;
}
//pe10--->led1
//pf10--->led2
//pf8--->led3
cmd_t cmd[MAX] = {
[0] = {
.cmd_arr = "led1on",
.gpio = GPIOE,
.pin = GPIO_PIN_10,
.status = GPIO_SET,
.gpio_write_p = hal_gpio_write,
},
[1] = {
.cmd_arr = "led1off",
.gpio = GPIOE,
.pin = GPIO_PIN_10,
.status = GPIO_RESET,
.gpio_write_p = hal_gpio_write,
},
[2] = {
.cmd_arr = "led2on",
.gpio = GPIOF,
.pin = GPIO_PIN_10,
.status = GPIO_SET,
.gpio_write_p = hal_gpio_write,
},
[3] = {
.cmd_arr = "led2off",
.gpio = GPIOF,
.pin = GPIO_PIN_10,
.status = GPIO_RESET,
.gpio_write_p = hal_gpio_write,
},
[4] = {
.cmd_arr = "led3on",
.gpio = GPIOE,
.pin = GPIO_PIN_8,
.status = GPIO_SET,
.gpio_write_p = hal_gpio_write,
},
[5] = {
.cmd_arr = "led3off",
.gpio = GPIOE,
.pin = GPIO_PIN_8,
.status = GPIO_RESET,
.gpio_write_p = hal_gpio_write,
},
};
cmd_t *find_command(const char *str)
{
for (int i = 0; i < MAX; i++)
{
if (!(cmp_func(str, cmd[i].cmd_arr)))
{
return &cmd[i];
}
}
return 0; //不能返回NULL,因为是裸机开发,没有开辟空间
}
main
#include "led.h"
#include "uart4.h"
#include "command.h"
int main(int argc, const char *argv[])
{
char *uart_str;
hal_led_init();
hal_uart4_init();
commed_t *cmd_p;
while (1)
{
hal_send_string("please input ");
uart_str = hal_recv_string(buffer);
cmd_p = find_command(uart_str);
if (cmd_p != 0) {
cmd_p->hal_gpio_write_p(cmd_p->gpios, cmd_p->pins, cmd_p->statu);
} else {
hal_send_string("command not found!\n");
}
}
return 0;
}