2020-07-11 14:44:09 +08:00

95 lines
2.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/******************************************************************************
* @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˸<EFBFBD><CBB8><EFBFBD><EFBFBD>(dev, motor, buzzer)<29><><EFBFBD>豸(dev, motor, buzzer)<29><><EFBFBD><EFBFBD>
*
* Copyright (c) 2019, <master_roger@sina.com>
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-04-01 Morro Initial version
******************************************************************************/
#include "module.h" /*get_tick()*/
#include "blink.h"
#include <stddef.h>
#include <string.h>
static blink_dev_t *head = NULL; /*ͷ<><CDB7><EFBFBD><EFBFBD> -*/
/*
* @brief <20><><EFBFBD><EFBFBD>blink<6E>
* @param[in] dev - <20>
* @param[in] ioctrl - IO<49><4F><EFBFBD>ƺ<EFBFBD><C6BA><EFBFBD>
* @return none
*/
void blink_dev_create(blink_dev_t *dev, void (*ioctrl)(bool enable))
{
blink_dev_t *tail = head;
memset(dev, 0, sizeof(blink_dev_t));
dev->ioctrl = ioctrl;
dev->next = NULL;
if (head == NULL) {
head = dev;
return;
}
while (tail->next != NULL)
tail = tail->next;
tail->next = dev;
}
/*
* @brief blink <20><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
* @param[in] name - <20><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
* @param[in] ontime - <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ0<CEAA><30><EFBFBD><EFBFBD><EFBFBD>ùر<C3B9>)
* @param[in] offtime- <20>ر<EFBFBD>ʱ<EFBFBD><CAB1>
* @param[in] repeats- <20>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>(0<><30>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD>)
* @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 blink<6E><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
* @param[in] none
* @return none
*/
void blink_dev_process(void)
{
blink_dev_t *dev;
for (dev = head; dev != NULL; dev = dev->next) {
if (dev->ontime == 0) {
continue;
} else if (get_tick() - dev->tick < dev->ontime) {
if (!dev->enable) {
dev->enable = true;
dev->ioctrl(true);
}
} else if(get_tick() - dev->tick < dev->offtime) { /**/
if (dev->enable) {
dev->enable = false;
dev->ioctrl(false);
}
} else {
dev->tick = get_tick();
if (dev->repeats) {
if (++dev->count >= dev->repeats) {
dev->ontime = 0;
dev->ioctrl(false);
dev->enable = false;
}
}
}
}
}