PixelMaestro icon indicating copy to clipboard operation
PixelMaestro copied to clipboard

Feature: Transitions

Open 8bitbuddhist opened this issue 6 years ago • 0 comments

Timer-based component that gradually changes any numeric variable from a start value to a target value. Gets initialized when calling any of the set() functions that changes a numeric value.

Required varibles:

  • var: a reference to the variable that will be changed. To start, this is restricted to certain ints.
  • value: the value that the variable will transition to
  • interval: the amount of time until the transition is finished

When calling a set() function with these arguments, a new Transition object is allocated. It calculates the difference between the variable's current value and target value using the Maestro's refresh interval and stores it as step_. Transitions use internal Timers to store the interval. On update(), step_ gets added to var until the Timer fires. Once the Transition ends, its object gets deleted.

Example: cutting a Maestro's brightness in half over 5 seconds:

# main.cpp
int brightness = 127;
int interval = 5000;
maestro.set_brightness(brightness, interval);

# transition.cpp
void Transition::Transition(...) {
	this->step_ = (value - *var) / (float)maestro_->get_refresh->get_interval();
	this->target_value_ = value;
	this->timer_ = new Timer(interval);
}

bool Transition::update(const uint32_t& current_time) {
	if (this->timer_->update(current_time)) {
		*var = this->target_value_;
		return true;
	}
	else {
		*var += this->step_;
		return false;
	}
}

# maestro.cpp
void Maestro::set_brightness(uint8_t target_brightness, uint16_t interval) {
	this->transition_ = new Transition(&this->brightness_, target_brightness, interval);
}

void Maestro::update(const uint32_t& current_time, bool force) {
	...
	if (this->transition_->update()) {
		delete this->transition_;
	}
	...
}

8bitbuddhist avatar Dec 02 '18 22:12 8bitbuddhist