140 lines
2.9 KiB
C

#include <rtthread.h>
#include <rtdevice.h>
#include <drv_gpio.h>
#include <assistant.h>
#include <sim.h>
#include <motor.h>
#include "sensor.h"
#include "status.h"
struct rt_event robot_event; // 机器人的控制事件
#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 灯引脚 */
#define PIN_LED_B GET_PIN(E, 12)
#define LED_ON PIN_HIGH
#define LED_OFF PIN_LOW
/* 配置蜂鸣器引脚 */
#define PIN_BEEP GET_PIN(B, 0) // PA1: BEEP --> BEEP (PB1)
int light_status = 0;
int fan_status = 0;
void fan_on(void)
{
fan_status = 1;
LOG3("fan on!");
motor_speed(100);
}
void fan_off(void)
{
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)
{
// LOG3("light off!");
rt_pin_mode(PIN_LED_B, PIN_MODE_OUTPUT);
rt_pin_write(PIN_LED_B, LED_OFF);
}
void beep_on(void)
{
rt_pin_mode(PIN_BEEP, PIN_MODE_OUTPUT);
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);
rt_pin_write(PIN_BEEP, PIN_LOW);
}
MSH_CMD_EXPORT_ALIAS(beep_off, P2, show beep_off);
void danger_status(void)
{
rt_event_send(&robot_event, EVENT_DELAY);
char *str = "aa";
serial_send(str);
sim_call("17318112360");
fan_on();
light_on();
// LED_BreathMore(0,LED_NUM-1,LED_RED);
}
MSH_CMD_EXPORT_ALIAS(danger_status, danger, show danger_status);
void normal_status(void)
{
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)
{
while (1)
{
if (rt_event_recv(&robot_event, EVENT_DELAY, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER, NULL) == RT_EOK)
{
rt_thread_mdelay(10000);
if(cnt_warning>0)
{
beep_on();
}
}
}
}
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;
}