WiFlasher/wiflash_esp32/src/lib/fixture/EspStrobe.cpp

168 lines
5.0 KiB
C++

/*
EspStrobe.cpp
Part of WiFlash_esp32
Copyright (c) 2020 Kevin Matz (kevin.matz@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <Arduino.h>
#include <AsyncUDP.h>
#include <algorithm>
#include <iterator>
#include "EspStrobe.h"
/*
calculate period from DMX value
*/
uint32_t hz_to_micros(uint8_t dmx) {
if (dmx == 0) {
return 0;
}
// ( millis / ( percent * scalor ) + offset ) * micros
return (1000 / (((dmx / 255.0) * 24.5) + 0.5)) * 1000;
}
/*
calculate duration from DMX value
*/
uint32_t dr_to_micros(uint8_t dmx) {
// (( percent * scalor) + offset) * micros
return (((dmx / 255.0) * .5 ) + .01 ) * 1000 * 1000;
}
/*
Constructor
*/
EspStrobe::EspStrobe(uint16_t address) {
address_ = address;
}
/*
call durring setup()
*/
bool EspStrobe::begin(uint8_t pin, uint8_t pwm) {
bool success = true;
// set state
pwm_ = pwm;
state_ = Strobe_INACTIVE;
// configure hardware
ledcAttachPin(pin, pwm_); // Attach GPIO to PWM timer
ledcSetup(pwm_, 2400, 15); // 2.4KHz PWM, 15 bit resolutio
ledcWrite(pwm_, 0); // LED to 0%
// start update task
xTaskCreate(this->update, // function name
"EspStrobeUpdateThread", // task name
2048, // stack size (bytes)
this, // parameter to pass
1, // task priority
NULL); // task handle
return success;
}
void EspStrobe::update(void * param) {
EspStrobe *tThis = (EspStrobe *) param;
uint32_t last_time = 0;
TickType_t xDelay;
for (;;) {
xDelay = (1000 / 40.0) / portTICK_PERIOD_MS; // sleep 1 dmx frame
// intensity changes start/stop the cycle
if (tThis->profile_.intensity == 0) {
// intensity off breaks the cycle
tThis->state_ = Strobe_INACTIVE;
} else {
if (tThis->state_ == Strobe_INACTIVE) {
// going active
tThis->state_ = Strobe_ACTIVE_ON;
}
}
// time based cycle changes
if (tThis->state_ == Strobe_ACTIVE_ON ||
tThis->state_ == Strobe_ACTIVE_OFF) {
uint32_t now, elapsed, period, dur;
now = micros();
elapsed = now - last_time;
period = hz_to_micros(tThis->profile_.rate);
dur = dr_to_micros(tThis->profile_.duration);
if (elapsed > period || // hz period completed
now < last_time) { // micros() wraps 32 bits every ~70 min.
// cycle restarts
tThis->state_ = Strobe_ACTIVE_ON;
last_time = now;
// yield task until end of duration
xDelay = std::min(xDelay, dur / portTICK_PERIOD_MS);
} else if (elapsed > dur) { // durration period completed
// cycle enters dark phase
tThis->state_ = Strobe_ACTIVE_OFF;
// yield task until end of period
xDelay = std::min(xDelay, (period - dur) / portTICK_PERIOD_MS);
} else {
if (tThis->state_ == Strobe_ACTIVE_ON) {
// yield task for remainder of duration
xDelay = std::min(xDelay, (dur - elapsed) / portTICK_PERIOD_MS);
} else {
// yield task for remainder of period
xDelay = std::min(xDelay, (period - elapsed) / portTICK_PERIOD_MS);
}
}
}
// set LED output
if (tThis->state_ == Strobe_ACTIVE_ON) {
tThis->setLevel(tThis->profile_.intensity);
} else {
tThis->setLevel(0);
}
// yield task
vTaskDelay(xDelay);
}
vTaskDelete( NULL );
}
/*
Write a 15bit level to PWM
*/
void EspStrobe::setLevel(uint16_t level) {
ledcWrite(pwm_, ((level >> 1) & 0xffff)); // write at 15 bit PWM
}
void EspStrobe::recvData(Universe * univ) {
if ( sizeof(profile_.dmx) + address_ > sizeof(univ->data()->data)) {
// address is higher than allowable
Serial.println("Bad Address");
return;
}
std::copy(univ->data()->data + address_,
univ->data()->data + address_ + sizeof(profile_.dmx),
std::begin(profile_.dmx));
profile_.intensity = htons(profile_.intensity);
}