Skip to content

Commit

Permalink
#5 clock class in use but acts out of turn
Browse files Browse the repository at this point in the history
  • Loading branch information
wraybowling committed Aug 29, 2020
1 parent 88dcd24 commit 6b8dc92
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 80 deletions.
75 changes: 40 additions & 35 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[env]

platform = atmelavr
board = ATmega328
framework = arduino
Expand All @@ -12,50 +13,54 @@ upload_flags =
-c
usbtiny
upload_command = avrdude $UPLOAD_FLAGS -U flash:w:$SOURCE:i
test_ignore =
examples
test_desktop

; this will allow for unit tests, but i'm not ready to
; totally rewrite this file yet
;
; [env:native]
; platform = native
[env:native]
platform = native
test_ignore = examples ;enables test_desktop folder

[env:USBtiny ISP]
; build_flags=-std=c++11
; board = ATmega328
; framework = arduino
upload_protocol = usbtiny

[env:AVRISP mkII]
upload_protocol = stk500v2
upload_flags =
-Pusb
; [env:AVRISP mkII]
; upload_protocol = stk500v2
; upload_flags =
; -Pusb

[env:Arduino ISP]
upload_protocol = arduinoisp
; [env:Arduino ISP]
; upload_protocol = arduinoisp

[env:Arduino as ISP]
upload_protocol = stk500v1
upload_flags =
-P$UPLOAD_PORT
-b$UPLOAD_SPEED
; [env:Arduino as ISP]
; upload_protocol = stk500v1
; upload_flags =
; -P$UPLOAD_PORT
; -b$UPLOAD_SPEED

; edit these lines
upload_port = SERIAL_PORT_HERE
upload_speed = 19200
; ; edit these lines
; upload_port = SERIAL_PORT_HERE
; upload_speed = 19200

[env:USBasp]
upload_protocol = usbasp
upload_flags =
-Pusb
; [env:USBasp]
; upload_protocol = usbasp
; upload_flags =
; -Pusb

[env:Parallel Programmer]
upload_protocol = dapa
upload_flags =
-F
; [env:Parallel Programmer]
; upload_protocol = dapa
; upload_flags =
; -F

[env:BusPirate as ISP]
upload_protocol = buspirate
upload_flags =
-P$UPLOAD_PORT
-b$UPLOAD_SPEED
; [env:BusPirate as ISP]
; upload_protocol = buspirate
; upload_flags =
; -P$UPLOAD_PORT
; -b$UPLOAD_SPEED

; edit these lines
upload_port = SERIAL_PORT_HERE
upload_speed = 115200
; ; edit these lines
; upload_port = SERIAL_PORT_HERE
; upload_speed = 115200
31 changes: 12 additions & 19 deletions src/DebouncedBoolean.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,23 @@
* isFresh() only returns true if no past value is true
*/

#ifndef DEBOUNCE_H
#define DEBOUNCE_H
#pragma once

class DebouncedBoolean {
public:
DebouncedBoolean() {}

bool state[2] = {false};
void set(bool value);
bool isFresh();
void reset();
};

void DebouncedBoolean::set(bool value) {
state[0] = state[1];
state[1] = value;
}

bool DebouncedBoolean::isFresh() {
return state[0] == false && state[1] == true;
}
void set(bool value){
state[0] = state[1];
state[1] = value;
}

void DebouncedBoolean::reset() {
state[2] = {false};
}

#endif
bool isFresh(){
return state[0] == false && state[1] == true;
}
void reset(){
state[2] = {false};
}
};
18 changes: 10 additions & 8 deletions src/clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@
*/

#include "clock.h"
#include "preferences.h"

void Clock::clockIn(int sync_voltage) {
sync_debounce = (sync_debounce << 1) + (sync_voltage > 900);
void Clock::clockIn(int sync_voltage, unsigned long time) {
sync_debounce.set(sync_voltage > LOGIC_HIGH);
sync_counter++;
if (sync_debounce == 1) {
const uint32_t NEW_PERIOD = millis() - millis_prev_sync;
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) {
bool Clock::clockOut(int division_voltage, unsigned long time) {
const int MULTIPLIER_STEP = (division_voltage * 10 + 512) / 1023 - 5;
const unsigned int MULTIPLIER = abs(MULTIPLIER_STEP) == 5
const unsigned int MULTIPLIER_FACTOR = abs(MULTIPLIER_STEP) == 5
? 24
: 1 << abs(MULTIPLIER_STEP);

Expand All @@ -45,9 +46,10 @@ bool Clock::clockOut(int division_voltage) {

// if (something) {
clock_skips++;
if (clock_skips >= MULTIPLIER)
if (clock_skips >= MULTIPLIER_FACTOR)
{
clock_skips = 0;
return true;
}
// }
} else { // divide period (faster)
Expand All @@ -60,7 +62,7 @@ bool Clock::clockOut(int division_voltage) {
return false;
}

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

Expand Down
12 changes: 6 additions & 6 deletions src/clock.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#ifndef CLOCK_H
#define CLOCK_H

#include <Arduino.h>
#include "peaks_pattern_predictor.h"
#include "DebouncedBoolean.h"

class Clock {
private:
PatternPredictor<32, 8> predictor;
byte sync_debounce = 0;
uint32_t millis_prev_sync = 0;
DebouncedBoolean sync_debounce;
uint32_t millis_prev_sync;
uint32_t sync_period;
uint32_t sync_counter;
uint32_t prev_modulo;
Expand All @@ -26,9 +26,9 @@ class Clock {
Clock() {
predictor.Init();
}
void clockIn(int sync_voltage);
bool clockOut(int division_voltage);
void reTrigger();
void clockIn(int sync_voltage, unsigned long time);
bool clockOut(int division_voltage, unsigned long time);
void reset();
// void doTimeStuff();
};

Expand Down
17 changes: 8 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ void setup()

// const bool slave_mode = getMux(host_vs_slave);
Grid leftGrid;
unsigned long prev_time = millis();
unsigned long clock_until = millis();
unsigned long left_until = millis();
Clock clock;
unsigned long clock_until = 0;
unsigned long left_until = 0;
int prev_clock_in = 0;

// const int steps = RESOLUTION;
Expand All @@ -57,21 +57,21 @@ bool this_leftPressed = false;

short pwm = 0;

void loop()
{
void loop() {
// local vars
int this_clock_in = getTrigerMux(clock_div_cv);
bool this_left = digitalRead(left_button);
int clock_div_knob__val = getTrigerMux(clock_div_knob);

unsigned long time = millis();
// unsigned int delta = time - prev_time;

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

// clock advancement
if(prev_clock_in <= NOISE_FLOOR && this_clock_in > NOISE_FLOOR) // rising
{
clock.clockIn(this_clock_in, time);

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

// write true only on the current step.
// hold the button to clear subsequent steps
Expand All @@ -83,7 +83,6 @@ void loop()
this_leftPressed = false; //reset

//advance clock
prev_time = time;
clock_until = time + 25;

//advance outputs
Expand Down
6 changes: 4 additions & 2 deletions src/multiplexer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once

#ifndef MULTIPLEXER_H
#define MULTIPLEXER_H
/**
* multiplexed analog signals
* all are read on A6 via the getMux() function
Expand Down Expand Up @@ -30,3 +30,5 @@ bool getHostMode()
return 0; //slave
return 1; //normal
}

#endif
3 changes: 3 additions & 0 deletions src/peaks_pattern_predictor.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#define PATTERN_PREDICTOR_H

#include <Arduino.h>
// #include <cstdint>
// #include <stddef.h>
// #include <stdint.h>

template <size_t history_size = 32, uint8_t max_candidate_period = 8>
class PatternPredictor
Expand Down
3 changes: 2 additions & 1 deletion src/preferences.h
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#define NOISE_FLOOR 128
#define NOISE_FLOOR 128 // out of 1024
#define LOGIC_HIGH 900 // out of 1024

0 comments on commit 6b8dc92

Please sign in to comment.