增加blink(通用LED/马达/蜂鸣器)控制器.

This commit is contained in:
魔罗技术 2020-06-21 14:31:50 +08:00
parent b4cadbb5b7
commit f6dfd4fa00
2 changed files with 120 additions and 0 deletions

80
framework/blink.c Normal file
View File

@ -0,0 +1,80 @@
/*******************************************************************************
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˸<EFBFBD><EFBFBD><EFBFBD><EFBFBD>(dev, motor, buzzer)<EFBFBD><EFBFBD><EFBFBD>(dev, motor, buzzer)<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* Change Logs:
* Date Author Notes
* 2019-04-01 Morro Initial version.
*******************************************************************************/
#include "public.h"
#include "blink.h" /*get_tick()*/
#include <stddef.h>
#include <string.h>
#ifdef __CC_ARM /* ARM Compiler */
extern blink_dev_t __section_blink_dev$$Base;
extern blink_dev_t __section_blink_dev$$Limit;
#define blink_tbl_start &__section_blink_dev$$Base
#define blink_tbl_end &__section_blink_dev$$Limit
#elif defined (__ICCARM__) /* for IAR Compiler */
#pragma section="__section_blink_dev"
#define blink_tbl_start __section_begin("__section_blink_dev")
#define blink_tbl_end __section_end("__section_blink_dev")
#endif
/*
* @brief
* @param[in] name - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param[in] ontime - <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ0<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ùر<EFBFBD>)
* @param[in] offtime- <EFBFBD>ر<EFBFBD>ʱ<EFBFBD><EFBFBD>
* @param[in] repeats- <EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(0<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><EFBFBD>)
* @return none
*/
void blink_dev_ctrl(blink_dev_t *dev, int ontime, int offtime, int repeats)
{
dev->ontime = ontime;
dev->offtime = offtime + ontime;
dev->repeats = repeats;
dev->tick = get_tick();
dev->count = 0;
if (ontime == 0) {
dev->ioctrl(false);
dev->enable = false;
}
}
/*
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param[in] none
* @return none
*/
void blink_dev_process(void)
{
blink_dev_t *s, *e;
s = (blink_dev_t *)blink_tbl_start;
e = (blink_dev_t *)blink_tbl_end;
for (; s < e; s++) {
if (s->ontime == 0) {
continue;
} else if (get_tick() - s->tick < s->ontime) {
if (!s->enable) {
s->enable = true;
s->ioctrl(true);
}
} else if(get_tick() - s->tick < s->offtime) {
if (s->enable) {
s->enable = false;
s->ioctrl(false);
}
} else {
s->tick = get_tick();
if (s->repeats) {
if (++s->count >= s->repeats) {
s->ontime = 0;
s->ioctrl(false);
s->enable = false;
}
}
}
}
}

40
framework/blink.h Normal file
View File

@ -0,0 +1,40 @@
/*******************************************************************************
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˸<EFBFBD><EFBFBD><EFBFBD><EFBFBD>(led, motor, buzzer)<EFBFBD><EFBFBD><EFBFBD>(led, motor, buzzer)<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* Change Logs:
* Date Author Notes
* 2019-04-01 Morro Initial version.
*******************************************************************************/
#ifndef _BLINK_H_
#define _BLINK_H_
#include "framework.h"
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/*Blink <20><EFBFBD><E8B1B8><EFBFBD><EFBFBD>*/
typedef struct {
unsigned short ontime;
unsigned short offtime;
unsigned short repeats;
unsigned char count;
unsigned char enable;
unsigned int tick;
void (*ioctrl)(bool enable);
}blink_dev_t;
#define blink_dev_register(name, ioctrl) \
(blink_dev_t name) \
SECTION("__section_blink_dev") UNUSED = \
{0,0,0,0,ioctrl}
void blink_dev_ctrl(blink_dev_t *dev, int ontime, int offtime, int repeat);
void blink_dev_process(void);
#ifdef __cplusplus
}
#endif
#endif