diff --git a/src/Blinker.cpp b/src/Blinker.cpp new file mode 100644 index 0000000..a0e3a08 --- /dev/null +++ b/src/Blinker.cpp @@ -0,0 +1,84 @@ + +#include +#include "Blinker.h" + +//////////////////////////////// +// Blinker // +//////////////////////////////// + +Blinker::Blinker(int pin, int timerNum){ + this->pin=pin; + pinMode(pin,OUTPUT); + digitalWrite(pin,0); + + group=((timerNum/2)%2==0)?TIMER_GROUP_0:TIMER_GROUP_1; + idx=(timerNum%2==0)?TIMER_0:TIMER_1; + + timer_config_t conf; + conf.alarm_en=TIMER_ALARM_EN; + conf.counter_en=TIMER_PAUSE; + conf.intr_type=TIMER_INTR_LEVEL; + conf.counter_dir=TIMER_COUNT_UP; + conf.auto_reload=TIMER_AUTORELOAD_EN; + conf.divider=8000; // 80 MHz clock / 8,000 = 10 kHz clock (0.1 ms pulses) + + timer_init(group,idx,&conf); + timer_isr_register(group,idx,Blinker::isrTimer,(void *)this,0,NULL); + timer_enable_intr(group,idx); + +} + +void Blinker::isrTimer(void *arg){ + + Blinker *b=(Blinker *)arg; + + if(b->group){ + if(b->idx) + TIMERG1.int_clr_timers.t1=1; + else + TIMERG1.int_clr_timers.t0=1; + } else { + if(b->idx) + TIMERG0.int_clr_timers.t1=1; + else + TIMERG0.int_clr_timers.t0=1; + } + + if(!digitalRead(b->pin)){ + digitalWrite(b->pin,1); + timer_set_alarm_value(b->group,b->idx,b->onTime); + b->count--; + } else { + digitalWrite(b->pin,0); + if(b->count){ + timer_set_alarm_value(b->group,b->idx,b->offTime); + } else { + timer_set_alarm_value(b->group,b->idx,b->delayTime); + b->count=b->nBlinks; + } + } + + timer_set_alarm(b->group,b->idx,TIMER_ALARM_EN); +} + +void Blinker::start(int period, float dutyCycle){ + + start(period, dutyCycle, 1, period-dutyCycle*period); +} + +void Blinker::start(int period, float dutyCycle, int nBlinks, int delayTime){ + + period*=10; + onTime=dutyCycle*period; + offTime=period-onTime; + this->delayTime=delayTime*10; + this->nBlinks=nBlinks; + count=nBlinks; + timer_set_counter_value(group,idx,0); + timer_set_alarm_value(group,idx,0); + timer_start(group,idx); +} + +void Blinker::stop(){ + timer_pause(group,idx); +} diff --git a/src/Blinker.h b/src/Blinker.h new file mode 100644 index 0000000..7f57ccf --- /dev/null +++ b/src/Blinker.h @@ -0,0 +1,53 @@ + +#include + +//////////////////////////////// +// Blinker // +//////////////////////////////// + +class Blinker { + + timer_group_t group; + timer_idx_t idx; + int pin; + + int nBlinks; + int onTime; + int offTime; + int delayTime; + int count; + + static void isrTimer(void *arg); + + public: + + Blinker(int pin, int timerNum=0); + +// Creates a generic blinking LED on specified PIN controlled +// in background via interrupts generated by an ESP32 Alarm Timer. +// +// pin: Pin mumber to control. Blinker will set pinMode to OUTPUT automatically +// timerNum: ESP32 Alarm Timer to use. 0=Group0/Timer0, 1=Group0/Timer1, 2=Group1/Timer0, 3=Group1/Timer1 + + void start(int period, float dutyCycle=0.5); + +// Starts simple ON/OFF blinking. +// +// period: ON/OFF blinking period, in milliseconds +// dutyCycle: Fraction of period that LED is ON (default=50%) + + void start(int period, float dutyCycle, int nBlinks, int delayTime); + +// Starts ON/OFF blinking pattern. +// +// period: ON/OFF blinking period, in milliseconds, used for blinking portion of pattern +// dutyCycle: Fraction of period that LED is ON (default=50%) +// nBlinks: Number of blinks in blinking portion of pattern +// delayTime: delay, in milliseconds, during which LED is off before restarting blinking pattern + + + void stop(); + +// Stops current blinking pattern. + +};