#include "delay.h"
#include "sys.h"
#include "adc.h"
#include "key.h"
#include "oled.h"
#include "usart.h"
#include "control.h"
#include "dht11.h"
#include "hx711.h"
#include "uln2003.h"
#include "timer.h"
#include "ds1302.h"
uint16_t timeone = 0;
uint16_t timetwo = 0;
uint8_t food_flag = 0;
uint8_t water_flag = 0;
uint8_t led_flag = 0;
uint8_t auto_mode = 0;
int temp_door=30;
int hum_door=20;
int level_door = 0;
int weight_door = 500;
int light_door = 3000;
uint8_t option = 0;
uint8_t keu_num = 0;
uint8_t page_num = 1;
void Key_event_handle(void);
void get_all_sensor_data();
void Sensor_Data_Handle();
void OLED_Show_FirstPage();
void OLED_Show_SecondPage();
void OLED_Show_ThirdPage();
int value;
float weight;
int32_t reset;
u8 bufff[30];
u16 light;
float Weights=38642.0;
int32_t Weights_100=8493860;
int main(void) {
delay_init();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
OLED_Init();
uart3_init(9600);
uart1_init(115200);
HX711_Init();
reset = HX711_GetData();
KEY_Init();
DHT11_Init();
Adc_Init();
Control_Init();
DS1302_Init(&SysDate);
DS1302_DateSet(&SysDate);
LED=0;
Uln2003_Init();
TIME_Config();
while(1) {
if(Time_Period[0]>1000){
get_all_sensor_data();
Time_Period[0]=0;
printf("温度:%d℃,湿度:%d%%RH\r\n",temp,hum);
printf("水位:%d%%,光照:%dLux\r\n",Water_Value,Light_Value);
printf("食物重量:%dg\r\n",(int)weight);
}
Key_event_handle();
if(page_num == 1){
OLED_Show_FirstPage();
}else if(page_num == 2){
OLED_Show_SecondPage();
}else if(page_num == 3){
OLED_Show_ThirdPage();
}
Sensor_Data_Handle();
}
}
void Sensor_Data_Handle() {
if(auto_mode==0){
if(water_flag==1){
water = 1;
}else water = 0;
if(led_flag==1){
LED = 1;
}else LED = 0;
if(food_flag==1){
Stepper_RotateByLoop(1,1,1);
food_flag=0;
}
if(SysDate.hour==timeone&&SysDate.min==timetwo&&SysDate.sec==0)food_flag=1;
}else if(auto_mode==1){
if(weight<weight_door)Stepper_RotateByLoop(1,1,1);
if(Water_Value<level_door)water=1;
else water=0;
if(Light_Value<light_door)LED=1;
else LED=0;
}
}
void get_all_sensor_data() {
DHT11_Read_Data(&temp,&hum);
value = HX711_GetData();
weight=(float)(value-reset)*Weights/(float)(Weights_100-reset);
Get_Water_Value(&Water_Value);
Get_Light_Value(&Light_Value);
DS1302_DateRead(&SysDate);
}
void Key_event_handle(void) {
keu_num = KEY_Scan(0);
switch(keu_num) {
case 1:
if(page_num==1){
page_num=2;
OLED_Clear();OLED_Show_SecondPage();
}else if(page_num==2){
option++;
if(option==6){
option=0;
page_num=3;
OLED_Clear();OLED_Show_ThirdPage();
}else OLED_Show_SecondPage();
}else if(page_num==3){
option++;
if(option==4){
option=0;
page_num=1;
OLED_Clear();OLED_Show_FirstPage();
}else OLED_Show_ThirdPage();
}
break;
case 2:
if(page_num==1){
auto_mode = 1;
}else if(page_num==2){
if(option==1){ level_door++; }
else if(option==2){ weight_door+=10; }
else if(option==3){ light_door+=15; }
else if(option==4){ timeone++; if(timeone==24)timeone=0; }
else if(option==5){ timetwo++; if(timetwo==60)timetwo=0; }
}else if(page_num==3){
if(option==1){ led_flag=1; }
else if(option==2){ water_flag=1; }
else if(option==3){ food_flag=1; }
}
break;
case 3:
if(page_num==1){
auto_mode = 0;
}else if(page_num==2){
if(option==1){ if(level_door>0)level_door--; }
else if(option==2){ if(weight_door>0)weight_door-=10; }
else if(option==3){ if(light_door>0)light_door-=15; }
else if(option==4){ timeone--; if(timeone>24)timeone=23; }
else if(option==5){ timetwo--; if(timetwo>60)timetwo=59; }
}else if(page_num==3){
if(option==1){ led_flag=0; }
else if(option==2){ water_flag=0; }
else if(option==3){ food_flag=1; }
}
break;
case 4:
break;
}
}
void OLED_Show_FirstPage(){
OLED_ShowChar(8*8,0,SysDate.year/10+'0',8,1);
OLED_ShowChar(8*9,0,SysDate.year%10+'0',8,1);
OLED_ShowChar(8*10,0,'/',8,1);
OLED_ShowChar(8*11,0,SysDate.mon/10+'0',8,1);
OLED_ShowChar(8*12,0,SysDate.mon%10+'0',8,1);
OLED_ShowChar(8*13,0,'/',8,1);
OLED_ShowChar(8*14,0,SysDate.day/10+'0',8,1);
OLED_ShowChar(8*15,0,SysDate.day%10+'0',8,1);
OLED_ShowChar(8*8,8,SysDate.hour/10+'0',8,1);
OLED_ShowChar(8*9,8,SysDate.hour%10+'0',8,1);
OLED_ShowChar(8*10,8,':',8,1);
OLED_ShowChar(8*11,8,SysDate.min/10+'0',8,1);
OLED_ShowChar(8*12,8,SysDate.min%10+'0',8,1);
OLED_ShowChar(8*13,8,':',8,1);
OLED_ShowChar(8*14,8,SysDate.sec/10+'0',8,1);
OLED_ShowChar(8*15,8,SysDate.sec%10+'0',8,1);
OLED_ShowChar(8*0,0,'T',16,1);
OLED_ShowChar(8*1,0,':',16,1);
OLED_ShowNum(16*1,0,temp,2,16,1);
OLED_ShowChinese(16*2,0,21,16,1);
OLED_ShowChar(8*0,16,'H',16,1);
OLED_ShowChar(8*1,16,':',16,1);
OLED_ShowNum(16*1,16,hum,2,16,1);
OLED_ShowChar(16*2,16,'%',16,1);
OLED_ShowChinese(16*3+8,16,32,16,1);
OLED_ShowChinese(16*4+8,16,33,16,1);
OLED_ShowChar(16*5+8,16,':',16,1);
OLED_ShowNum(16*6,16,Light_Value,4,16,1);
OLED_ShowChinese(16*0,32,5,16,1);
OLED_ShowChinese(16*1,32,6,16,1);
OLED_ShowChar(16*2,32,':',16,1);
OLED_ShowNum(16*2+8,32,Water_Value,2,16,1);
OLED_ShowChinese(16*0,48,10,16,1);
OLED_ShowChinese(16*1,48,29,16,1);
OLED_ShowChar(16*2,48,':',16,1);
OLED_ShowNum(16*2+8,48,weight,4,16,1);
OLED_ShowChar(16*4+8,48,'g',16,1);
if(auto_mode==1){
OLED_ShowChinese(16*6,48,43,16,1);
}else{
OLED_ShowChinese(16*6,48,44,16,1);
}
OLED_ShowChinese(16*7,48,45,16,1);
OLED_Refresh();
}
void OLED_Show_SecondPage(){
OLED_ShowChinese(16*0,0,5,16,1);
OLED_ShowChinese(16*1,0,6,16,1);
OLED_ShowChinese(16*2,0,41,16,1);
OLED_ShowChinese(16*3,0,42,16,1);
OLED_ShowChar(16*4,0,':',16,1);
if(option==1)OLED_ShowNum(16*4+8,0,level_door,3,16,0);
else OLED_ShowNum(16*4+8,0,level_door,3,16,1);
OLED_ShowChinese(16*0,16,10,16,1);
OLED_ShowChinese(16*1,16,29,16,1);
OLED_ShowChinese(16*2,16,41,16,1);
OLED_ShowChinese(16*3,16,42,16,1);
OLED_ShowChar(16*4,16,':',16,1);
if(option==2)OLED_ShowNum(16*4+8,16,weight_door,4,16,0);
else OLED_ShowNum(16*4+8,16,weight_door,4,16,1);
OLED_ShowChinese(16*0,32,32,16,1);
OLED_ShowChinese(16*1,32,33,16,1);
OLED_ShowChinese(16*2,32,41,16,1);
OLED_ShowChinese(16*3,32,42,16,1);
OLED_ShowChar(16*4,32,':',16,1);
if(option==3)OLED_ShowNum(16*4+8,32,light_door,4,16,0);
else OLED_ShowNum(16*4+8,32,light_door,4,16,1);
OLED_ShowChinese(16*0,48,35,16,1);
OLED_ShowChinese(16*1,48,17,16,1);
OLED_ShowChinese(16*2,48,9,16,1);
OLED_ShowChinese(16*3,48,10,16,1);
OLED_ShowChar(16*4,48,':',16,1);
if(option==4)OLED_ShowNum(16*4+8,48,timeone,2,16,0);
else OLED_ShowNum(16*4+8,48,timeone,2,16,1);
OLED_ShowChar(16*5+8,48,':',16,1);
if(option==5)OLED_ShowNum(16*6,48,timetwo,2,16,0);
else OLED_ShowNum(16*6,48,timetwo,2,16,1);
OLED_Refresh();
}
void OLED_Show_ThirdPage(){
if(option==1){
OLED_ShowChinese(16*0,0,11,16,0);
OLED_ShowChinese(16*1,0,32,16,0);
}else{
OLED_ShowChinese(16*0,0,11,16,1);
OLED_ShowChinese(16*1,0,32,16,1);
}
OLED_ShowChar(16*2,0,':',16,1);
if(led_flag==1)OLED_ShowChinese(16*2+8,0,19,16,1);
else OLED_ShowChinese(16*2+8,0,20,16,1);
if(option==2){
OLED_ShowChinese(16*0,16,11,16,0);
OLED_ShowChinese(16*1,16,5,16,0);
}else{
OLED_ShowChinese(16*0,16,11,16,1);
OLED_ShowChinese(16*1,16,5,16,1);
}
OLED_ShowChar(16*2,16,':',16,1);
if(water_flag==1)OLED_ShowChinese(16*2+8,16,19,16,1);
else OLED_ShowChinese(16*2+8,16,20,16,1);
if(option==3){
OLED_ShowChinese(16*0,32,9,16,0);
OLED_ShowChinese(16*1,32,10,16,0);
}else{
OLED_ShowChinese(16*0,32,9,16,1);
OLED_ShowChinese(16*1,32,10,16,1);
}
OLED_Refresh();
}