#include "bsp_scheduler.h"

/* ļƣbsp_scheduler.c */
/* ļãȣϵͳFREERTOS */

/* Զ */
uint8_t task_num;   //ȫֱڴ洢

/* ************************************* */
typedef struct{//һṹ壬а/ִ/ϴڼ
     void(*task_func)(void);//趨:
     uint32_t rate_ms;  //趨:ִms
     uint32_t last_run;  //趨:һڼms
}    task_t;   


/* ̬飬ÿִmsϴʱms */
static task_t scheduler_task[] =
{
    {key_proc,10,0}, //һΪ Key_Proc,ִΪ10msʼϴʱΪ0ms
    {led_ui_proc,10,0}, //һΪ Led_UI_Proc,ִΪ10msʼϴʱΪ0ms
    {adc_proc,500,0}, //һΪ adc_Proc,ִΪ100msʼϴʱΪ0ms
    {beep_proc,100,0}, //һΪ Beep_Proc,ִΪ100msʼϴʱΪ0ms
    {water_proc,200,0}, //һΪ Water_Proc,ִΪ200msʼϴʱΪ0ms
    {oled_proc,100,0}, //һΪ OLED_Proc,ִΪ100msʼϴʱΪ0ms
    {warning_proc,100,0}, //һΪ Warning_Proc,ִΪ100msʼϴʱΪ0ms
    {led_proc,100,0}, //һΪ Led_Proc,ִΪ100msʼϴʱΪ0ms
};

/* ƣscheduler_init() */
/* ãȻʼ */
/*           Ԫظ洢 task_num  */
void scheduler_init(void)
{
     task_num = sizeof(scheduler_task) / sizeof(task_t);//Ԫظ洢 task_num 
}

/* ƣscheduler_run() */
/* ãȻк */
/* 飬ǷҪִСǰʱѾִڣִ񲢸ϴʱ */
void scheduler_run(void)
{   
    //
    //OLEDSprintf(4,1,"updataOK");
    //е
    for(uint8_t i =0;i < task_num;i++)
    {
        //Խnow_timeȡǰϵͳʱ䣨룩
        uint32_t now_time = HAL_GetTick();
        
        //鵱ǰʱǷﵽִʱ
        if(now_time >= scheduler_task[i].rate_ms + scheduler_task[i].last_run)
        {
            //ϴʱΪǰʱ
            scheduler_task[i].last_run = now_time;
            
            //ִ
            scheduler_task[i].task_func();
        }
    }
}

