cond.cpp#include "Look_cond.h"
#include "utils/debug.h"
#include "numicro/sfr/gpio"
int Sum = 0;
flag_t Flag(0);
mutex_t Mutex;
cond_t Cond;
// eint_t 类提供了 INT0/INT1 的接口
// 当 INT0/INT1 发生时,对象将发送相应的 int 消息到 mbox。
class eint_t : public interrupt_t {
public:
__INLINE eint_t();
protected:
bool isr(int vector);
void dsr(int vector, uintptr_t count);
};
// eint 构造函数
__DEBUG//debug调试不优化便于调试
__INLINE eint_t::eint_t()
{
using namespace sfr::gpio;
attach(EINT0_IRQn);//绑定外部中断0
attach(EINT1_IRQn);//绑定外部中断1
GPIOB.IEN(0).IF_EN15(1).IF_EN14(1);//开启Key1,Key2中断
vector_t::enable(EINT0_IRQn);//使能外部中断0即Key1中断
vector_t::enable(EINT1_IRQn);//使能外部中断1即Key2中断
}
// eint 中断服务例程
__DEBUG//debug调试不优化便于调试
bool eint_t::isr(int vector)
{
using namespace sfr::gpio;
GPIOB.ISRC = GPIOB.ISRC; // 清中断 flag
return true;
}
// eint 中断滞后服务例程
__DEBUG//debug调试不优化便于调试
void eint_t::dsr(int vector, uintptr_t count)
{
using namespace sfr::gpio;
if (vector == EINT0_IRQn)//Key2中断
{
Flag.do_set_bits(2);//在中断中唤醒任务2
}
else if (vector == EINT1_IRQn)//Key1中断
{
Flag.do_set_bits(1);//在中断中唤醒任务1
}
}
eint_t eint; // 创建 eint 对象
instance_task1_Look_cond_t task1_Look_cond(1); // 任务实例
// 任务类 task1_Look_cond_t 的例程
__DEBUG//debug调试不优化便于调试
void task1_Look_cond_t::routine()
{
// TODO: 在此编写 task1_Look_cond_t 例程的内容
using namespace sfr::gpio;
while (true) {
// TODO: 在此编写 task1_Look_cond_t 例程的内容
int flag = Flag.wait(1, flag_t::ANY_CONSUME);//阻塞等待Key1中断
if (flag)
{
GPIOA.DOUT().DOUT2 ^= 1;//LED1闪烁
Mutex.lock();
Sum++;
if (Sum < 3) Mutex.unlock();
else{
Mutex.unlock();
Cond.signal();
}
}
}
}
instance_task2_Look_cond_t task2_Look_cond(2); // 任务实例
// 任务类 task1_Look_cond_t 的例程
__DEBUG//debug调试不优化便于调试
void task2_Look_cond_t::routine()
{
// TODO: 在此编写 task2_Look_cond_t 例程的内容
using namespace sfr::gpio;
while (true) {
// TODO: 在此编写 task2_Look_cond_t 例程的内容
int flag = Flag.wait(2, flag_t::ANY_CONSUME);//阻塞等待Key2中断
if (flag)
{
GPIOA.DOUT().DOUT3 ^= 1;//LED2闪烁
Sum++;
if (Sum < 3) Mutex.unlock();
else{
Mutex.unlock();
Cond.signal();
}
}
}
}
instance_task3_Look_cond_t task3_Look_cond(3); // 任务实例
// 任务类 task3_Look_cond_t 的例程
__DEBUG//debug调试不优化便于调试
void task3_Look_cond_t::routine()
{
// TODO: 在此编写 task3_Look_cond_t 例程的内容
using namespace sfr::gpio;
while (true) {
// TODO: 在此编写 task3_Look_cond_t 例程的内容
Mutex.lock();
while (Sum < 3){//Key1或Key2按键合计次数为三次时激活
Cond.wait(Mutex);
Mutex.unlock();
Sum = 0;
GPIOA.DOUT().DOUT4(0).DOUT5(0);//LED3,LED4亮
delay(LOOK_TICKS_PER_SEC / 2);//延时亮0.5S
GPIOA.DOUT().DOUT4(1).DOUT5(1);//LED3,LED4灭
break;
}
}
}
|