Skip to content

Commit

Permalink
#5 What I thought was pretty much perfect clock code doesn't work
Browse files Browse the repository at this point in the history
  • Loading branch information
wraybowling committed Aug 30, 2020
1 parent 6240456 commit 029c09b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
1 change: 1 addition & 0 deletions equations/knob to 24ppqn multipliers.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
y=3\; \cdot \; 2^{\left( d-1 \right)}\; ,\; a=1023,\; c=5,\; d=\mbox{floor}\left( \frac{c\cdot x\; -\; 512}{a} \right)
1 change: 1 addition & 0 deletions equations/knob to 5 steps.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
y=\; \mbox{floor}\left( \frac{\left( x\; \cdot \; 5\; +\; 512 \right)}{1023} \right)
38 changes: 28 additions & 10 deletions src/clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,40 @@ bool Clock::isHigh(int sync_voltage, int division_knob, uint32_t time) {

// predict milliseconds of period
// ignoring clock division
predicted_period = predictor.Predict(DELTA);

time_accumulation = 0;
period_prediction = predictor.Predict(DELTA);
period_accumulation = 0;
return true;
} else {
time_accumulation += DELTA;
period_accumulation += DELTA;
}

// get multiplier from knob
const int STEP = (division_knob * 10 + 512) / 1023 - 5;
const unsigned int FACTOR = abs(STEP) == 5
? 24
: 1 << abs(STEP);
return false;
const int8_t KNOB_STEP = (division_knob * 5 + 512) >> 10; // quick divide by 1023

// if no division, duration of 1 pulse at 24ppqn is the same as period
if(KNOB_STEP == 0){
slot_duration = period_prediction;
} else if (KNOB_STEP == 1) {
// 16ppqn * 3/2 = 24ppqn
slot_duration = period_prediction * 1.5;
} else {
// 8ppqn, 4ppqn, 2ppqn, 1ppqn
const uint16_t MULTIPLIER = pow(2, KNOB_STEP - 2) * 3;
slot_duration = period_prediction * MULTIPLIER;
}

// which pulse are we on
const uint8_t PULSE_NUMBER = period_accumulation / slot_duration;
if(pulse_counter < 24 && PULSE_NUMBER > pulse_counter){
return true;
pulse_counter++;
} else {
return false;
}

}

void Clock::reset() {
clock_skips = 0;
pulse_counter = 0;
period_accumulation = 0;
}
24 changes: 5 additions & 19 deletions src/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,18 @@ class Clock {
DebouncedBoolean sync_debounce;

PatternPredictor<32, 8> predictor;
uint16_t predicted_period;

uint8_t time_accumulation;



// uint32_t prev_modulo;
// uint32_t next_modulo;
uint32_t clock_skips;
// uint8_t clock_counter;
// // uint32_t clock_slip;
// // byte clock_debounce = 0;

// // uint32_t millis_next_clock_on;
// // bool clock_is_on;
uint32_t millis_next_clock_off;
void predictNextQuarterNote();
uint8_t convertVoltageToMultiplier();
uint16_t period_prediction;
uint16_t period_accumulation;
uint8_t slot_duration;

uint8_t pulse_counter = 0;

public:
Clock() {
predictor.Init();
}
bool isHigh(int sync_voltage, int division_knob, uint32_t time);
void reset();
// void doTimeStuff();
};

#endif
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ void setup()

// const bool slave_mode = getMux(host_vs_slave);
Grid leftGrid;
Grid topGrid;
Grid rightGrid;
Clock clock;
unsigned long clock_until = 0;
unsigned long left_until = 0;
Expand Down

0 comments on commit 029c09b

Please sign in to comment.