164 lines
3.4 KiB
C
Raw Normal View History

2025-03-14 22:57:49 +08:00
#include <rtthread.h>
#include <rtdevice.h>
#include <drv_gpio.h>
#include <assistant.h>
#include <sim.h>
#include <motor.h>
2025-03-21 22:10:18 +08:00
#include "sensor.h"
#include "status.h"
2025-03-14 22:57:49 +08:00
2025-03-21 22:10:18 +08:00
struct rt_event robot_event; // 机器人的控制事件
2025-03-14 22:57:49 +08:00
#define THREAD_PRIORITY 25
#define THREAD_STACK_SIZE 4096
#define THREAD_TIMESLICE 5
#define LOG_TAG "status"
#define DBG_LVL DBG_LOG
// #define DBG_LVL DBG_INFO
#define USE_LOG1
#define USE_LOG2
#define USE_LOG3
// #define USE_LOG4
#define USE_LOG5
// #define USE_LOG6
// #define USE_LOG_D
#include "logn.h"
/* 配置 LED 灯引脚 */
2025-03-21 22:10:18 +08:00
#define PIN_LED_B GET_PIN(E, 12)
#define LED_ON PIN_HIGH
#define LED_OFF PIN_LOW
2025-03-21 22:10:18 +08:00
/* 配置蜂鸣器引脚 */
#define PIN_BEEP GET_PIN(B, 0) // PA1: BEEP --> BEEP (PB1)
int light_status = 0;
int fan_status = 0;
void fan_on(void)
{
2025-03-21 22:10:18 +08:00
fan_status = 1;
LOG3("fan on!");
motor_speed(100);
}
void fan_off(void)
{
2025-03-21 22:10:18 +08:00
fan_status = 0;
// LOG3("fan off!");
motor_speed(0);
}
void light_on(void)
{
LOG3("light on!");
rt_pin_mode(PIN_LED_B, PIN_MODE_OUTPUT);
rt_pin_write(PIN_LED_B, LED_ON);
}
void light_off(void)
{
2025-03-21 22:10:18 +08:00
// LOG3("light off!");
rt_pin_mode(PIN_LED_B, PIN_MODE_OUTPUT);
rt_pin_write(PIN_LED_B, LED_OFF);
2025-03-15 16:07:17 +08:00
}
2025-03-21 22:10:18 +08:00
void beep_on(void)
{
rt_pin_mode(PIN_BEEP, PIN_MODE_OUTPUT);
2025-04-20 22:02:43 +08:00
LOG3("beep on!");
2025-03-21 22:10:18 +08:00
rt_pin_write(PIN_BEEP, PIN_HIGH);
rt_thread_mdelay(100);
}
MSH_CMD_EXPORT_ALIAS(beep_on, beep, show beep_on);
void beep_off(void)
{
rt_pin_mode(PIN_BEEP, PIN_MODE_OUTPUT);
2025-04-20 22:02:43 +08:00
// LOG3("beep off!");
2025-03-21 22:10:18 +08:00
rt_pin_write(PIN_BEEP, PIN_LOW);
}
MSH_CMD_EXPORT_ALIAS(beep_off, P2, show beep_off);
2025-03-15 16:07:17 +08:00
void danger_status(void)
{
fan_on();
light_on();
}
MSH_CMD_EXPORT_ALIAS(danger_status, danger, show danger_status);
void people_danger_status(void)
2025-03-15 16:07:17 +08:00
{
2025-04-20 22:02:43 +08:00
// static int need_send = 1;
// rt_event_send(&robot_event, EVENT_DELAY);
2025-03-15 16:07:17 +08:00
char *str = "aa";
2025-04-20 22:02:43 +08:00
// if (need_send)
2025-03-15 16:07:17 +08:00
serial_send(str);
2025-04-20 22:02:43 +08:00
// need_send = !need_send;
// fan_on();
2025-03-15 16:07:17 +08:00
light_on();
2025-03-21 22:10:18 +08:00
// LED_BreathMore(0,LED_NUM-1,LED_RED);
2025-03-15 16:07:17 +08:00
}
MSH_CMD_EXPORT_ALIAS(people_danger_status, rdanger, show people danger_status);
2025-03-15 16:07:17 +08:00
void normal_status(void)
{
2025-03-21 22:10:18 +08:00
if (fan_status == 0)
fan_off();
if (light_status == 0)
light_off();
beep_off();
// LED_SetMore(0,LED_NUM-1,LEDI_OFF);
}
void collect_entry(void *parameter)
{
2025-04-20 22:02:43 +08:00
int cnt_sec = 0;
2025-03-21 22:10:18 +08:00
while (1)
{
if (rt_event_recv(&robot_event, EVENT_DELAY, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
2025-04-20 22:02:43 +08:00
RT_WAITING_FOREVER, NULL) == RT_EOK)
2025-03-21 22:10:18 +08:00
{
2025-04-20 22:02:43 +08:00
// LOG3("delay event!(%d)", cnt_sec);
// cnt_sec++;
// if (cnt_sec >= 10)
// {
beep_on();
// if(cnt_sec % 10==0)
// {
sim_call("17318112360");
sim_message("17318112360", "");
// }
// }
2025-03-21 22:10:18 +08:00
}
2025-04-20 22:02:43 +08:00
// else
// {
// cnt_sec = 0;
// // LOG3("NO RECIEVE(%d)", cnt_sec);
// }
2025-03-21 22:10:18 +08:00
}
}
int status_init(void)
{
rt_err_t result;
rt_thread_t collect_tid;
result = rt_event_init(&robot_event, "robotE", RT_IPC_FLAG_PRIO);
if (result != RT_EOK)
{
rt_kprintf("init robot_event failed.\n");
return -1;
}
collect_tid = rt_thread_create("collecT", collect_entry, NULL, 2048, 12, 10);
if (collect_tid)
{
rt_thread_startup(collect_tid);
}
else
{
LOG_E("create collect thread failed!");
return -RT_ENOMEM;
}
return RT_EOK;
}