motor_control: refactor: cache next_tick_half_micros
This commit is contained in:
parent
6efea4f89d
commit
6ca0fb07ea
|
@ -33,13 +33,20 @@ void TickController::CalculatePeriod() {
|
||||||
period_with_bend = (unsigned int) (((float) period_half_micros) * pgm_read_float_near(midi_pitch_bend_scale + _bend));
|
period_with_bend = (unsigned int) (((float) period_half_micros) * pgm_read_float_near(midi_pitch_bend_scale + _bend));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int TickController::GetTruePeriod() {
|
unsigned long TickController::NextTick() {
|
||||||
return period_with_bend;
|
return NextTickWithTs(timer2.get_count());
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long TickController::NextTickWithTs(unsigned long cur_half_micros) {
|
||||||
|
if (period_with_bend == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return cur_half_micros + period_with_bend;
|
||||||
}
|
}
|
||||||
|
|
||||||
MotorControl::MotorControl(int pin_dir, int pin_step) :
|
MotorControl::MotorControl(int pin_dir, int pin_step) :
|
||||||
pin_dir(pin_dir), pin_step(pin_step),
|
pin_dir(pin_dir), pin_step(pin_step),
|
||||||
last_tick_half_micros(0)
|
next_tick_half_micros(0)
|
||||||
{
|
{
|
||||||
// No actual constructor logic -- initialization is in Init()
|
// No actual constructor logic -- initialization is in Init()
|
||||||
}
|
}
|
||||||
|
@ -54,8 +61,7 @@ void MotorControl::Init() {
|
||||||
|
|
||||||
void MotorControl::TickOn(unsigned long period_half_micros) {
|
void MotorControl::TickOn(unsigned long period_half_micros) {
|
||||||
tick_ctrl.SetPeriod(period_half_micros);
|
tick_ctrl.SetPeriod(period_half_micros);
|
||||||
// Force a tick
|
next_tick_half_micros = tick_ctrl.NextTick();
|
||||||
last_tick_half_micros = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MotorControl::TickAtPitch(unsigned int midi_pitch) {
|
void MotorControl::TickAtPitch(unsigned int midi_pitch) {
|
||||||
|
@ -75,22 +81,23 @@ void MotorControl::TickPitchBend(int bend) {
|
||||||
if (bend < 0 || bend >= 16384) return;
|
if (bend < 0 || bend >= 16384) return;
|
||||||
|
|
||||||
tick_ctrl.SetBend(bend);
|
tick_ctrl.SetBend(bend);
|
||||||
|
// We don't need to re-calculate next tick here, because if a note is currently on,
|
||||||
|
// it will eventually call NextTick() anyway
|
||||||
}
|
}
|
||||||
|
|
||||||
void MotorControl::TickOff() {
|
void MotorControl::TickOff() {
|
||||||
tick_ctrl.SetPeriod(0);
|
tick_ctrl.SetPeriod(0);
|
||||||
|
next_tick_half_micros = tick_ctrl.NextTick();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MotorControl::Tick(unsigned long cur_half_micros) {
|
void MotorControl::Tick(unsigned long cur_half_micros) {
|
||||||
unsigned long period = tick_ctrl.GetTruePeriod();
|
if (next_tick_half_micros == 0) {
|
||||||
|
|
||||||
if (period == 0) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (last_tick_half_micros == 0 || cur_half_micros >= last_tick_half_micros + period) {
|
if (cur_half_micros >= next_tick_half_micros) {
|
||||||
DoTick();
|
DoTick();
|
||||||
last_tick_half_micros = cur_half_micros;
|
next_tick_half_micros = tick_ctrl.NextTickWithTs(cur_half_micros);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,14 +16,15 @@ class TickController {
|
||||||
void SetBend(unsigned int bend);
|
void SetBend(unsigned int bend);
|
||||||
// In units of half microseconds
|
// In units of half microseconds
|
||||||
// 0 = disabled
|
// 0 = disabled
|
||||||
unsigned int GetTruePeriod();
|
unsigned long NextTick();
|
||||||
|
unsigned long NextTickWithTs(unsigned long cur_half_micros);
|
||||||
};
|
};
|
||||||
|
|
||||||
class MotorControl {
|
class MotorControl {
|
||||||
private:
|
private:
|
||||||
// Control pins of the stepper motor
|
// Control pins of the stepper motor
|
||||||
int pin_dir, pin_step;
|
int pin_dir, pin_step;
|
||||||
unsigned long last_tick_half_micros;
|
unsigned long next_tick_half_micros;
|
||||||
TickController tick_ctrl;
|
TickController tick_ctrl;
|
||||||
|
|
||||||
void DoTick();
|
void DoTick();
|
||||||
|
|
Loading…
Reference in New Issue