Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Transitions #6

Open
8bitbuddhist opened this issue Dec 2, 2018 · 0 comments
Open

Feature: Transitions #6

8bitbuddhist opened this issue Dec 2, 2018 · 0 comments
Assignees

Comments

@8bitbuddhist
Copy link
Owner

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 8bitbuddhist self-assigned this Dec 2, 2018
@8bitbuddhist 8bitbuddhist changed the title New feature: Transitions Feature: Transitions Dec 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant