本帖最后由 cang 于 2023-12-9 20:51 编辑
简短教程
本来是写了不少的, 但是因为这个编辑器的问题, 因为提前填好了验证码,验证码栏尾部也呈现 绿色√
, 然后再写的帖子,
结果发表帖子的时候提示 验证码
错了, 然后就强制返回到编辑页面, 也只有标题还在了, 内容全部消失了.................
- 搭建环境
- 让小安派 Ai-M61-32SU 进入烧录模式
- 长按不放开 Type-C 右侧的烧录按钮 -[IO2] (中文名:烧录|英文名:Flashing)
- 按一下 Type-C 左侧的重置按钮 -[EN] (中文名:重置|英文名:Reset)
- 松开先前长按的烧录按钮, 则进入烧录模式了
- 图片来源:[[持续更新]小安派S1&M61从编译到烧录]
- 修改 //AiPi-Open-Kits/aithinker_Ai-M6X_SDK/examples/peripherals/gpio/gpio_input_output 里的 main.c, 将 main.c 的内容替换为下面贴出的
核心代码
- 开始烧录程序到
小安派
上
- terminal 进入 //AiPi-Open-Kits/aithinker_Ai-M6X_SDK/examples/peripherals/gpio/gpio_input_output 目录
- terminal 输入以下内容
- make clean
- make
- make flash CHIP=bl616 COMX=/dev/ttyUSB0
- 其中 COMX=/dev/ttyUSB0 则需要替换 [/dev/ttyUSB0] 为你电脑上小安派实际接口
- Linux 环境下输入 ls /dev/tty* 可用查看所有端口, 例如: [COMX=/dev/ttyUSB0]
- Windows 环境下用设备管理器查看小安派实际 COM, 例如: [COMX=COM5]
- 烧录结束之后, 点击小安派的 Reset 按钮一下, 烧录的程序则开始运作.
效果图
核心代码
#include "bflb_gpio.h"
#include "board.h"
#include "log.h"
struct bflb_device_s *gpio;
uint32_t gpio_group[] = { GPIO_PIN_12, GPIO_PIN_14, GPIO_PIN_15 };
char *gpio_color[] = { "红色", "绿色", "蓝色" };
void debounce_delay(uint32_t delay_ms)
{
bflb_mtimer_delay_ms(delay_ms);
}
void start_show_rgb(uint32_t delayms)
{
LOG_I("| START RGB | Delay ms: %d |\r\n", delayms);
for (size_t i = 0; i < sizeof(gpio_group) / sizeof(gpio_group[0]); i++) {
if (bflb_gpio_read(gpio, GPIO_PIN_2)) {
// 说明用户再次点击了按钮, 则需要退出show rgb
LOG_W("| Interrupted | Exit to show RGB_%d | Color: %s |\r\n", gpio_group[i], gpio_color[i]);
break;
}
LOG_I("| Showing | gpio_%d | Color: %s |\r\n", gpio_group[i], gpio_color[i]);
bflb_gpio_set(gpio, gpio_group[i]);
debounce_delay(delayms);
bflb_gpio_reset(gpio, gpio_group[i]);
debounce_delay(delayms / 2);
}
}
void stop_show_rgb()
{
LOG_W("| STOP | Stop to show RGB |\r\n");
for (size_t i = 0; i < sizeof(gpio_group) / sizeof(gpio_group[0]); i++) {
LOG_W("| Stop | gpio_%d | Color: %s |\r\n", gpio_group[i], gpio_color[i]);
bflb_gpio_reset(gpio, gpio_group[i]);
}
}
int main(void)
{
LOG_I("| START | Board Start Init |\r\n");
board_init();
gpio = bflb_device_get_by_name("gpio");
bflb_gpio_init(gpio, GPIO_PIN_2, GPIO_INPUT | GPIO_PULLDOWN);
bflb_gpio_init(gpio, GPIO_PIN_12, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_0);
bflb_gpio_init(gpio, GPIO_PIN_14, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_0);
bflb_gpio_init(gpio, GPIO_PIN_15, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_0);
bflb_gpio_reset(gpio, GPIO_PIN_12);
bflb_gpio_reset(gpio, GPIO_PIN_14);
bflb_gpio_reset(gpio, GPIO_PIN_15);
LOG_I("| INIT | Board Inited |\r\n");
uint32_t delayms = 1200;
bool show_rgb = false;
bool button_pressed = false;
while (1) {
LOG_RD("| START |\r\n");
bool button_state = bflb_gpio_read(gpio, GPIO_PIN_2);
LOG_RI("| Reading | button_state: %d | button_pressed: %d | show_rgb: %d |\r\n", button_state, button_pressed, show_rgb);
// Debounce the button state
debounce_delay(10); // Adjust the delay according to your requirements
bool debounced_state = bflb_gpio_read(gpio, GPIO_PIN_2);
if (button_state == debounced_state) {
if (button_state) {
if (button_pressed) {
// Second button press
button_pressed = false;
show_rgb = false; // Turn off RGB
} else {
// First button press
button_pressed = true;
show_rgb = true; // Turn on RGB
}
}
}
LOG_RD("| Processed | button_state: %d | button_pressed: %d | show_rgb: %d |\r\n", button_state, button_pressed, show_rgb);
if (show_rgb) {
start_show_rgb(delayms);
// Use non-blocking delays or timers instead of blocking delay
} else {
stop_show_rgb();
}
// Use non-blocking delays or timers instead of blocking delay
bflb_mtimer_delay_ms(1000);
LOG_RD("| END | Delay 1000 ms |\r\n");
}
LOG_I("| DONE | Board DONE |\r\n");
}
补充