#include "include.h"
#include "sensor_hal.h"
#include "auxiliary.h"
// 软件定时器设定
static Timer timer_detect_sensor;
static Timer timer_recv_data;
static Timer timer_MAX30102;
static Timer timer_1s;
static Timer timer_key;
/*本地函数*/
void HardWare_Init(void); // 硬件初始化
void SoftWare_Init(void); // 软件初始化
void UserBoot_Init(void); // 用户初始化
void detect_sensor(void); // 周期采集传感器信息
void user_send_data(const unsigned char *src, int len); // 串口发送函数
void uart_recv_IdValue(const char *id, const char *value); // 处理串口接收到的指令
void service_connect(void)
{
const char *mqtt_service = "423423.iotcloud.tencentdevices.com"; // mqtt服务器地址
const char *mqtt_port = "1883"; // mqtt 端口
const char *mqtt_client_ID = "1334342345"; // mqtt client 名称ID
const char *mqtt_device_key = "AF8234234234FSF234AA"; // mqtt client连接秘钥
const char *mqtt_client_topic = "50FK234Fef234V3cfs"; // mqtt订阅主题
user_send_data((unsigned char *)mqtt_service, strlen(mqtt_service));
user_send_data((unsigned char *)mqtt_port, strlen(mqtt_port));
user_send_data((unsigned char *)mqtt_client_ID, strlen(mqtt_client_ID));
user_send_data((unsigned char *)mqtt_device_key, strlen(mqtt_device_key));
user_send_data((unsigned char *)mqtt_client_topic, strlen(mqtt_client_topic));
}
void wireless_config()
{
const char *cfg_wifi_ssid = "{\"cfg_wifi_ssid\":\"wifi\"}";
const char *cfg_wifi_passwd = "{\"cfg_wifi_passwd\":\"12345678\"}";
// config wifi and gprs/4g
user_send_data((unsigned char *)cfg_server_host, strlen(cfg_server_host));
// wifi only
user_send_data((unsigned char *)cfg_wifi_passwd, strlen(cfg_wifi_passwd));
user_send_data((unsigned char *)cfg_wifi_ssid, strlen(cfg_wifi_ssid));
service_connect(); // 连接mqtt服务器
}
void wireless_config_refresh()
{
static unsigned int tick_count = 0;
tick_count++;
if (tick_count % 20 == 3)
{
wireless_config(); // 初始化无线配置
}
}
/*****************************************/
// 硬件部分初始化
/*****************************************/
void HardWare_Init(void)
{
NVIC_Configuration(); /* 配置NVIC中断模式 */
delay_init(); /* 延时函数初始化 */
UART2_Init(38400); // 串口2初始化
// UART1_Init(9600); // 串口1初始化 -- 解析串口数据
// UART3_Init(9600); // 串口1初始化 -- 解析串口数据
TIM4_Int_Init(1000, 72); /* 开TIM4定时中断 计数到为 1ms */
// 5个引脚全为普通引脚,但不能再用JTAG&SWD仿真器调试,只能用st-link调试
// GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);
// 如果你用PB3,PB4,PA15做普通IO,PA13&14用于SWD调试
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
}
void func_timer_recv_data()
{
protocol_pipe_get_IdValue(&protocol_pipe_uart, uart_recv_IdValue); // 解析串口2的控制指令
protocol_pipe_get_IdValue(&protocol_pipe_uart_2, uart_recv_IdValue_sensor); // 解析串口 传感器数据
protocol_pipe_get_IdValue(&protocol_pipe_uart_3, uart_recv_IdValue_sensor3); // 解析串口 传感器数据
parse_uart_sensor(); // 解析串口1传感器数据
parse_uart_sensor3(); // 解析串口1传感器数据
if (GlobalVar_flag_is_inited_zhendong_DO)
{
process_zhendong_DO_100ms();
}
if (GlobalVar_flag_liuliang_inited)
{
process_liuliang_100ms();
}
}
void SoftWare_Init(void)
{
// 通讯协议初始化
protocol_pipe_init(&protocol_pipe_uart, protocol_pipe_uart_buff, sizeof(protocol_pipe_uart_buff));
protocol_send_init(&send_protocol_uart, send_protocol_uart_buff, sizeof(send_protocol_uart_buff));
protocol_pipe_init(&protocol_pipe_uart_2, protocol_pipe_uart_buff_2, sizeof(protocol_pipe_uart_buff_2));
protocol_pipe_init(&protocol_pipe_uart_3, protocol_pipe_uart_buff_3, sizeof(protocol_pipe_uart_buff_3));
sensor_hal_protocol_init(&send_protocol_uart); // 初始化
// 定时器初始化
timer_init(&timer_detect_sensor, detect_sensor, 2000, 1); // 周期采集传感器信息
timer_init(&timer_recv_data, func_timer_recv_data, 100, 1); // 周期解析串口数据指令
timer_init(&timer_MAX30102, func_timer_MAX30102, 1000, 1); // MAX30102
timer_init(&timer_1s, func_timer_1s, 1000, 1); // 1秒定时器
timer_init(&timer_key, func_timer_key, 10, 1); // 按键检测
timer_start(&timer_detect_sensor);
timer_start(&timer_recv_data);
timer_start(&timer_MAX30102);
timer_start(&timer_1s);
timer_start(&timer_key);
// TJC4824_Screen_Directive_send(user_send_data); // 串口显示屏发送函数注册
key_button_register(key_button_process); // 注册按键处理函数
RINGBUF_Init(&Uart_sensor_RB, uart_sensor_buff, sizeof(uart_sensor_buff)); // 初始化串口环形缓冲器
RINGBUF_Init(&Uart_sensor_RB3, uart_sensor_buff3, sizeof(uart_sensor_buff3)); // 初始化串口环形缓冲器
}
/**************************************************************************************/
/***********************************main 程序入口**************************************/
/**************************************************************************************/
int main(void)
{
SoftWare_Init(); // 软件初始化
HardWare_Init(); // 硬件初始化
UserBoot_Init(); // 用户初始化
while (1)
{
timer_loop(); // 定时器执行
}
}
/**************************************************************************************/
// 定时器 硬件定时器震荡源
/**************************************************************************************/
void TIM4_IRQHandler(void) /* TIM4中断 */
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) /* 检查指定的TIM中断发生与否:TIM 中断源 */
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update); /* 清除TIMx的中断待处理位:TIM 中断源 */
timer_ticks(); // 1ms tick to uodate timer
if (GlobalVar_flag_key_inited)
{
key_button_scan_1ms();
}
}
}
/////////////////////////////////////////////////////////////////////////////////
//
//
// 核心逻辑函数
//
//
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
//
//
// 核心逻辑函数
//
//
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
//
//
// 核心逻辑函数
//
//
/////////////////////////////////////////////////////////////////////////////////
// 上电初始化
void UserBoot_Init(void)
{
// SetSensor_Jidianqi_1(0);
// SetSensor_Jidianqi_2(0);
// SetSensor_Jidianqi_3(0);
// SetSensor_Jidianqi_4(0);
// SetSensor_Beep_1(0);
// SetSensor_Beep_interval_1(0);
// SetSensor_Deng_1(0);
// SetSensor_Deng_2(0);
// SetSensor_Deng_3(0);
// SetSensor_Deng_4(0);
// SetSensor_Fan_1(0);
// SetSensor_Fan_2(0);
// SetSensor_Ws2812b_rgb(0, 0, 0);
// SetSensor_MOTOR(2, 5);
// Getsensor_16key();
// Set_GPIO_Init_Mode_DO_PB15(1);
}
// 自动调节函数
// 开始自动调节判断(如高温开风扇、天黑开灯)
// 1:首先判断是否开启了自动调节(如是否开启了天黑开灯的功能)
// 2:如果开启了自动调节的功能,那么进入自动调节判断(如 光强低于设定的阈值,那么开灯,否则关灯)
void Adjust_Sensor(void)
{
static unsigned int time_value = 0;
unsigned char wen_value = 0, dia_value = 0, zao_value = 0, r_value = 0, g_value = 0;
static unsigned char beep_value = 0;
static char oled_buff[40];
memset(oled_buff, 0, sizeof(oled_buff));
sprintf(oled_buff, ":%d ", GlobalVar_sensor_ds18b20_wendu);
SetSensor_OLED_Sring(32, 2, (unsigned char *)oled_buff, 16);
memset(oled_buff, 0, sizeof(oled_buff));
sprintf(oled_buff, ":%d ", GlobalVar_sensor_dianliu);
SetSensor_OLED_Sring(96, 2, (unsigned char *)oled_buff, 16);
memset(oled_buff, 0, sizeof(oled_buff));
sprintf(oled_buff, ":%d ", GlobalVar_sensor_distance);
SetSensor_OLED_Sring(48, 4, (unsigned char *)oled_buff, 16);
memset(oled_buff, 0, sizeof(oled_buff));
sprintf(oled_buff, ":%d ", GlobalVar_sensor_zhendong_AO);
SetSensor_OLED_Sring(32, 6, (unsigned char *)oled_buff, 16);
// 温度电流噪音超声波
SetSensor_OLED_Chinese(0, 2, 0);
SetSensor_OLED_Chinese(16, 2, 1);
SetSensor_OLED_Chinese(64, 2, 2);
SetSensor_OLED_Chinese(80, 2, 3);
SetSensor_OLED_Chinese(0, 6, 4);
SetSensor_OLED_Chinese(16, 6, 5);
SetSensor_OLED_Chinese(0, 4, 6);
SetSensor_OLED_Chinese(16, 4, 7);
SetSensor_OLED_Chinese(32, 4, 8);
// GlobalVar_Threshold_alert_data2
wen_value = GlobalVar_sensor_ds18b20_wendu > GlobalVar_Threshold_alert_wendu ? 1 : 0;
dia_value = GlobalVar_sensor_dianliu > GlobalVar_Threshold_alert_data1 ? 1 : 0;
zao_value = GlobalVar_sensor_distance < GlobalVar_Threshold_alert_distance ? 1 : 0;
if (wen_value || dia_value)
{
time_value = 0;
beep_value = 1;
}
else if (time_value < GlobalVar_Threshold_alert_data2)
{
time_value++;
}
else
{
beep_value = 0;
}
SetSensor_Beep_1(beep_value +zao_value);
SetSensor_Jidianqi_1(!beep_value);
if (beep_value)
{
r_value = 255;
}
else
{
g_value = 255;
}
if (zao_value)
{
r_value = 255;
g_value = 255;
}
SetSensor_Ws2812b_rgb(r_value, g_value, 0);
}
void detect_sensor(void)
{
//---------------------------------------/////
wireless_config_refresh(); // 配置wifi或无线模块
//---------------------------------------/////
protocol_send_start(&send_protocol_uart); //
// ################################################################
GetSensor_Time("online1"); // 离线检查 开机时间
GetSensor_date_time(); // 获取当前时间需要接收time
GetSensor_Ds18b20_float("wendu1"); // ds18b20 三位小数
GetSensor_Dianliu("dianliu1"); // dianliu A1 10mA
GetSensor_Zhendong_AO("zhendong1"); // 震动DO接口数据 鼓型震动传感器 声音 ---默认用这个
GetSensor_Chaoshengbo("chaoshengbo1"); // 超声波
GetSensor_GPS("x1", "y1");
// GetSensor_IdValue_int("dianliang", int_value);//直接上传IDvalue value为int
// GetSensor_IdValue_str("xxx" , "xxx");//直接上传IDvalue value为string
// ################################################################
Adjust_Sensor(); // 传感器调节判断
protocol_send_complete(&send_protocol_uart, user_send_data);
}
void uart_recv_IdValue(const char *id, const char *value)
{
int value_len = protocol_valuelen(value); // 获取协议的数值长度
int num_value;
GlobalVar_sensor_online_response++;
if (protocol_compare(id, "time"))
{
SetSensor_time_update(value); // 更新时间
}
else if (protocol_compare(id, "l_wen1"))
{
num_value = protocol_atoi(value);
GlobalVar_Threshold_alert_wendu = num_value;
}
else if (protocol_compare(id, "l_dia1"))
{
num_value = protocol_atoi(value);
GlobalVar_Threshold_alert_data1 = num_value;
}
else if (protocol_compare(id, "l_cha1"))
{
num_value = protocol_atoi(value);
GlobalVar_Threshold_alert_distance = num_value;
}
else if (protocol_compare(id, "l_shi1"))
{
num_value = protocol_atoi(value);
GlobalVar_Threshold_alert_data2 = num_value;
}
}
对此代码进行分析