Use a FreeRTOS task to update strobe.

This commit is contained in:
Kevin Matz 2020-12-04 11:34:26 -05:00
parent 525970bbf9
commit efad085a2a
3 changed files with 50 additions and 44 deletions

View File

@ -71,53 +71,64 @@ bool Strobe::begin(uint8_t pin, uint8_t pwm) {
ledcSetup(m_pwm, 2400, 15); // 2.4KHz PWM, 15 bit resolutio
ledcWrite(m_pwm, m_level); // LED to 0%
// start update task
xTaskCreate(this->update, // function name
"StrobeUpdateThread", // task name
2048, // stack size (bytes)
this, // parameter to pass
1, // task priority
NULL); // task handle
return success;
}
void Strobe::update(void * args) {
// stop stobing on sACN loss of signal
// if (millis() - m_e131->stats.last_seen > 10000) {
// Serial.println("No Signal!");
// m_int = 0;
// }
void Strobe::update(void * param) {
Strobe *tThis = (Strobe *) param;
for (;;) {
// stop stobing on sACN loss of signal
// if (millis() - m_e131->stats.last_seen > 10000) {
// Serial.println("No Signal!");
// m_int = 0;
// }
// intensity changes start/stop the cycle
if (m_int == 0) {
// intensity off breaks the cycle
m_state = STROBE_INACTIVE;
} else {
if (m_state == STROBE_INACTIVE) {
// going active
m_state = STROBE_ACTIVE_ON;
// intensity changes start/stop the cycle
if (tThis->m_int == 0) {
// intensity off breaks the cycle
tThis->m_state = STROBE_INACTIVE;
} else {
if (tThis->m_state == STROBE_INACTIVE) {
// going active
tThis->m_state = STROBE_ACTIVE_ON;
}
}
}
// time based cycle changes
if (m_state == STROBE_ACTIVE_ON ||
m_state == STROBE_ACTIVE_OFF) {
uint32_t now, elapsed, period, durr;
now = micros();
// time based cycle changes
if (tThis->m_state == STROBE_ACTIVE_ON ||
tThis->m_state == STROBE_ACTIVE_OFF) {
uint32_t now, elapsed, period, durr;
now = micros();
elapsed = now - m_time;
period = hz_to_micros(m_rat);
durr = dr_to_micros(m_dur);
elapsed = now - tThis->m_time;
period = hz_to_micros(tThis->m_rat);
durr = dr_to_micros(tThis->m_dur);
if (elapsed > period || // hz period completed
now < m_time) { // micros() wraps 32 bits every ~70 min.
// cycle restarts
m_state = STROBE_ACTIVE_ON;
m_time = now;
} else if (elapsed > durr) { // durration period completed
// cycle enters dark phase
m_state = STROBE_ACTIVE_OFF;
if (elapsed > period || // hz period completed
now < tThis->m_time) { // micros() wraps 32 bits every ~70 min.
// cycle restarts
tThis->m_state = STROBE_ACTIVE_ON;
tThis->m_time = now;
} else if (elapsed > durr) { // durration period completed
// cycle enters dark phase
tThis->m_state = STROBE_ACTIVE_OFF;
}
}
}
// set LED output
if (m_state == STROBE_ACTIVE_ON) {
setLevel(m_int);
} else {
setLevel(0);
// set LED output
if (tThis->m_state == STROBE_ACTIVE_ON) {
tThis->setLevel(tThis->m_int);
} else {
tThis->setLevel(0);
}
}
}

View File

@ -44,10 +44,7 @@ class Strobe {
Strobe(uint16_t universe = 1, uint16_t address = 1);
bool begin(uint8_t pin, uint8_t pwm); // call in setup()
void update(void *args = NULL); // update task
const uint8_t profile = 4; // DMX profile length
static void update(void * param); // update task
void recvData(e131_packet_t *packet); // data recieved callback

View File

@ -150,8 +150,6 @@ void loop() {
return;
}
strobe->update();
// Be a remote trigger
//
bool val = !digitalRead(button);