Skip to content

Commit

Permalink
#5 reduced clock to a single function instead of in and out
Browse files Browse the repository at this point in the history
  • Loading branch information
wraybowling committed Aug 30, 2020
1 parent 6b8dc92 commit 6240456
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 100 deletions.
103 changes: 20 additions & 83 deletions src/clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,97 +23,34 @@
#include "clock.h"
#include "preferences.h"

void Clock::clockIn(int sync_voltage, unsigned long time) {
sync_debounce.set(sync_voltage > LOGIC_HIGH);
sync_counter++;
if (sync_debounce.isFresh()) {
const uint32_t NEW_PERIOD = time - millis_prev_sync;
sync_period = predictor.Predict(NEW_PERIOD);
sync_counter = 0;
}
}

bool Clock::clockOut(int division_voltage, unsigned long time) {
const int MULTIPLIER_STEP = (division_voltage * 10 + 512) / 1023 - 5;
const unsigned int MULTIPLIER_FACTOR = abs(MULTIPLIER_STEP) == 5
? 24
: 1 << abs(MULTIPLIER_STEP);
bool Clock::isHigh(int sync_voltage, int division_knob, uint32_t time) {
// measure time delta
const uint32_t DELTA = time - prev_time;
prev_time = time;

if (MULTIPLIER_STEP <= 0) {
// less knob = multiply period (slower)
//= this.sync_period * MULTIPLIER;
// actually just skip
// reset if sync is rising
sync_debounce.set(sync_voltage > LOGIC_HIGH);
if (sync_debounce.isFresh()) {

// if (something) {
clock_skips++;
if (clock_skips >= MULTIPLIER_FACTOR)
{
clock_skips = 0;
return true;
}
// }
} else { // divide period (faster)
//const NEXT_MODULO =
//prev_modulo
// predict milliseconds of period
// ignoring clock division
predicted_period = predictor.Predict(DELTA);

// millis_next_clock_on = now + periodPrediction / multiplier;
time_accumulation = 0;
return true;
} else {
time_accumulation += DELTA;
}
// millis_next_clock_off = millis_next_clock_on + 25;

// 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;
}

void Clock::reset() {
clock_skips = 0;
}


// void
// doTimeStuff()
// {

// // y = floor((x10+512)/1023-5)+5

// // int multiplier = getMux(clock_div_knob) * 24 / 512 - 24;
// lights(0, multiplier);

// const uint32_t now = millis();

// bool clockIsHigh = getMux(clock_div_cv) > 512;

// if (clockIsHigh)
// {
// if (!prevClockWasHigh)
// { //new pulse
// uint32_t newPeriod = now - prevClockMillis;
// prevClockMillis = now;

// periodPrediction = bpm.Predict(newPeriod);
// // lights(0, byte(periodPrediction)); //debug

// prevClockOffAtMillis = nextClockOffAtMillis;

// if (multiplier_step <= 0)
// { // multiply period, go slower
// nextClockOffAtMillis = now + periodPrediction * multiplier + 25;
// }
// else
// { // divide period, go faster
// nextClockOffAtMillis = now + periodPrediction / multiplier + 25;
// }
// }
// prevClockWasHigh = true;
// }
// else
// {
// prevClockWasHigh = false;
// }

// if (now < prevClockOffAtMillis)
// {
// digitalWrite(clock_out, HIGH);
// }
// else
// {
// digitalWrite(clock_out, LOW);
// }
// }
32 changes: 19 additions & 13 deletions src/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,34 @@

class Clock {
private:
PatternPredictor<32, 8> predictor;
uint32_t prev_time;
DebouncedBoolean sync_debounce;
uint32_t millis_prev_sync;
uint32_t sync_period;
uint32_t sync_counter;
uint32_t prev_modulo;
uint32_t next_modulo;

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;
// 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_on;
// // bool clock_is_on;
uint32_t millis_next_clock_off;
void predictNextQuarterNote();
uint8_t convertVoltageToMultiplier();

public:
Clock() {
predictor.Init();
}
void clockIn(int sync_voltage, unsigned long time);
bool clockOut(int division_voltage, unsigned long time);
bool isHigh(int sync_voltage, int division_knob, uint32_t time);
void reset();
// void doTimeStuff();
};
Expand Down
6 changes: 2 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,13 @@ void loop() {
bool this_left = digitalRead(left_button);
int clock_div_knob__val = getTrigerMux(clock_div_knob);

unsigned long time = millis();
uint32_t time = millis();

// set state
if(this_left) this_leftPressed = true;

// clock advancement
clock.clockIn(this_clock_in, time);

if( clock.clockOut(clock_div_knob__val, time) ) {
if( clock.isHigh(this_clock_in, clock_div_knob__val, time) ) {

// write true only on the current step.
// hold the button to clear subsequent steps
Expand Down

0 comments on commit 6240456

Please sign in to comment.