/* strobe_esp32.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 #include #include #include #include "strobe_esp32.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 */ Strobe::Strobe(uint16_t address) { address_ = address; } /* call durring setup() */ bool Strobe::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 "StrobeUpdateThread", // task name 2048, // stack size (bytes) this, // parameter to pass 1, // task priority NULL); // task handle return success; } void Strobe::update(void * param) { Strobe *tThis = (Strobe *) 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 uint16_t intensity = tThis->profile_.intensity << 8 | tThis->profile_.intensity >> 8; if (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(intensity); } else { tThis->setLevel(0); } // yield task vTaskDelay(xDelay); } vTaskDelete( NULL ); } /* Write a 15bit level to PWM */ void Strobe::setLevel(uint16_t level) { ledcWrite(pwm_, ((level >> 1) & 0xffff)); // write at 15 bit PWM } void Strobe::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)); }