You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

349 lines
6.8 KiB

#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "wifi.h"
#include "lcd1602.h"
#include "adc.h"
#include "timer.h"
#include "string.h"
#include "ds18b20.h"
#include "Lcd_Driver.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "DS1302.h"
#include "oled.h"
#include "key.h"
#include "Sim900a.h"
#define TIMERSIZE 3
#define DRUGTYPE 3
struct Drug
{
char name[30];
int num;
};
/**
* @description: 定时任务
*/
struct Crontab
{
struct timing timer;
struct Drug Drugs[DRUGTYPE];
};
struct Crontab crontab[TIMERSIZE];
struct timing nowTime;
u8 Settime[6]={0x22,0x04,0x30,0x16,0x16,0x30}; // 设置时间 22年 04月 30日 16:16:30
// 函数声明
int getValue(void);
int getFirstIndexes(const char str);
int getLastIndexes(const char str);
int getTimerResult(void);
struct timing getTimer(void);
void Sys_Init(void);
void DisplayTask(void);
void TaskProcessing(void);
void DataProcessing(void);
/**
* @description: 设置药的数量
* @param {Crontab*} tab
* @param {int} i
* @param {int} num
* @return {*}
*/
void setDrugs(struct Crontab* tab, int i, int num){
if(i == 0) sprintf(tab->Drugs[i].name, "1.Amikin:%d ", num);
else if(i == 1) sprintf(tab->Drugs[i].name, "2.Rulid:%d ", num);
else if(i == 2) sprintf(tab->Drugs[i].name, "3.Brufen:%d ", num);
tab->Drugs[i].num = num;
}
/**
* @description: 定时初始化
* @param {*}
* @return {*}
*/
void TimerInit(){
// 定时一
setDrugs(&crontab[0], 0, 10);
setDrugs(&crontab[0], 1, 10);
setDrugs(&crontab[0], 2, 10);
crontab[0].timer.hour = 7;
crontab[0].timer.min = 0;
crontab[0].timer.sec = 0;
// 定时二
setDrugs(&crontab[1], 0, 0);
setDrugs(&crontab[1], 1, 20);
setDrugs(&crontab[1], 2, 20);
crontab[1].timer.hour = 12;
crontab[1].timer.min = 0;
crontab[1].timer.sec = 0;
// 定时三
setDrugs(&crontab[2], 0, 30);
setDrugs(&crontab[2], 1, 30);
setDrugs(&crontab[2], 2, 30);
crontab[2].timer.hour = 18;
crontab[2].timer.min = 0;
crontab[2].timer.sec = 0;
}
/**
* @description: main函数
* @param {*}
* @return {*}
*/
int main(void)
{
Sys_Init();
delay_ms(1000);
BEEP = 1;
while(1)
{
DS1302_Readtime();
TaskProcessing();
DisplayTask();
delay_ms(200);
}
}
/**
* @description: 系统初始化
* @param {*}
* @return {*}
*/
void Sys_Init(void)
{
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
LED_Init();
OLED_Init();
SIM900aInit();
DS1302_init(Settime);
//DS1302_SetTime(Settime);
OLED_Clear();
TimerInit();
}
/**
* @description: 报警
* @param {*}
* @return {*}
*/
void Warning(){
BEEP = 0;
delay_ms(100);
BEEP = 1;
}
/**
* @description: 显示
* @param {*}
* @return {*}
*/
void DisplayTask(void)
{
char str[30];
sprintf(str, "%2d:%2d:%2d", nowTime.hour, nowTime.min, nowTime.sec);
OLED_ShowString(0, 0, str, 16);
};
/**
* @description:
* @param {int} i
* @return {*}
*/
void MedicalKit(int i){
LED0 = 0, LED1 = 0, LED2 = 0, LED3 = 0;
if(crontab[i].Drugs[0].num > 0) LED0 = 1;
if(crontab[i].Drugs[1].num > 0) LED1 = 1;
if(crontab[i].Drugs[2].num > 0) LED2 = 1;
}
/**
* @description: 任务处理
* @param {*}
* @return {*}
*/
void TaskProcessing()
{
static int flag = 0;
char std[30];
int result = getTimerResult();
if(result != -1) {
Warning();
MedicalKit(result);
strcpy(std, crontab[result].Drugs[0].name);
strcat(std, crontab[result].Drugs[1].name);
strcat(std, crontab[result].Drugs[2].name);
OLED_ShowString(0, 2, std, 16);
if(flag == 0){
SIM900aSend(std);
flag = 1;
}
}else{
OLED_ShowString(0, 2, " ", 16);
LED0 = 0, LED1 = 0, LED2 = 0, LED3 = 0;
flag = 0;
}
}
/**
* @description: 判断时间是否到达
* @param {*}
* @return {*}
*/
int getTimerResult(void)
{
static int flag = 0;
int i = 0;
u8 result = 1;
//检查那个定时达到
for(i = 0; i < TIMERSIZE; i++){
if(crontab[i].timer.hour != nowTime.hour) result = 0;
else if(crontab[i].timer.min != nowTime.min) {
result = 0;
flag = 0;
}
else if(crontab[i].timer.sec != nowTime.sec){
if(flag == 0) result = 0;
}
if(result){
flag = 1;
return i; // 返回定时时间的索引
}
result = 1;
}
return -1;
}
/**
* @description: 解析数据
* @param {*}
* @return {*}
*/
int getValue(void)
{
int target1 = 0, target2 = 0,i = 0;
int result = 0;
target1 = (char *)strstr((const char *)USART_RX_BUF, "(") - (char *)USART_RX_BUF;
target2 = (char *)strstr((const char *)USART_RX_BUF, ")") - (char *)USART_RX_BUF;
result = 0;
for(i = target1 + 1; i < target2; i++){
result = result * 10 + (*(USART_RX_BUF + i) - 0x30);
}
return result;
}
/**
* @description: 获取字符出现的第一个索引
* @param {char} str
* @return {*}
*/
int getFirstIndexes(const char str){
int i = 0;
int length = USART_RX_STA & 0x3fff;
for(i = 0; i < length; i++){
if(str == USART_RX_BUF[i]) return i;
}
return 0;
}
/**
* @description: 获取字符出现的最后一个索引
* @param {char} str
* @return {*}
*/
int getLastIndexes(const char str){
int result = 0, i = 0;
int length = USART_RX_STA & 0x3fff;
for(i = 0; i < length; i++){
if(str == USART_RX_BUF[i]) result = i;
}
return result;
}
/**
* @description: 解析Timer
* @param {*}
* @return {*}
*/
struct timing getTimer()
{
struct timing timer;
int target1 = 0, target2 = 0,i = 0 , result = 0;
target1 = (char *)strstr((const char *)USART_RX_BUF, "(") - (char *)USART_RX_BUF;
target2 = getFirstIndexes('-');
for(i = target1 + 1; i < target2; i++){
result = result * 10 + (*(USART_RX_BUF + i) - 0x30);
}
timer.year = result;
result = 0;
target1 = getFirstIndexes('-');
target2 = getLastIndexes('-');
for(i = target1 + 1; i < target2; i++){
result = result * 10 + (*(USART_RX_BUF + i) - 0x30);
}
timer.mon = result;
result = 0;
target1 = getLastIndexes('-');
target2 = getFirstIndexes(' ');
for(i = target1 + 1; i < target2; i++){
result = result * 10 + (*(USART_RX_BUF + i) - 0x30);
}
timer.day = result;
result = 0;
target1 = getFirstIndexes(' ');
target2 = getFirstIndexes(':');
for(i = target1 + 1; i < target2; i++){
result = result * 10 + (*(USART_RX_BUF + i) - 0x30);
}
timer.hour = result;
result = 0;
target1 = getFirstIndexes(':');
target2 = getLastIndexes(':');
for(i = target1 + 1; i < target2; i++){
result = result * 10 + (*(USART_RX_BUF + i) - 0x30);
}
timer.min = result;
result = 0;
target1 = getLastIndexes(':');
target2 = getLastIndexes(')');
for(i = target1 + 1; i < target2; i++){
result = result * 10 + (*(USART_RX_BUF + i) - 0x30);
}
timer.sec = result;
return timer;
}
/**
* @description: 接收数据
* @param {*}
* @return {*}
*/
void DataProcessing(void)
{
// 接受到数据
if(USART_RX_STA & 0x8000){
memset(USART_RX_BUF, 0, sizeof(USART_RX_BUF));
USART_RX_STA = 0x00;
}
}