From 019fe61f966f27b721a9053275c98528028a4bff Mon Sep 17 00:00:00 2001 From: Dominic DiTaranto Date: Sat, 17 Jan 2026 20:35:24 -0500 Subject: [PATCH] modularize --- Out.cpp | 37 +++++++++--- Out.h | 19 +++--- globals.h | 7 +++ master_clock.ino | 147 ++++++++++++++++++++++++++++------------------- 4 files changed, 137 insertions(+), 73 deletions(-) create mode 100644 globals.h diff --git a/Out.cpp b/Out.cpp index c5068c9..c2f8ea4 100644 --- a/Out.cpp +++ b/Out.cpp @@ -1,40 +1,63 @@ // Out.cpp +#include "Arduino.h" #include "Out.h" +#include "globals.h" -Out::Out(unsigned byte pin) { - pin = pin; +Out::Out(byte pin) { + this->pin = pin; state = LOW; - + div = 1; cycle = 0; dur = 0; - width = 25; + width = 50; // this needs to dynamically change with width - len = period * (width / 100); + + len = 0; pinMode(pin, OUTPUT); } +void Out::setLen(unsigned long currentPeriod) { + // Fix the integer division by using floating-point math temporarily + // The result 'len' is likely a duration in milliseconds, so keep it an integer type + len = (unsigned long)((double)currentPeriod * (width / 100.0) / 1000.0); +} + void Out::turnOn() { cycle += 1; if (cycle == div) { state = HIGH; + digitalWrite(pin, state); dur = millis(); cycle = 0; - } + }; } void Out::turnOff() { + if (state == HIGH && millis() - dur >= len) { + Serial.println(len); state = LOW; digitalWrite(pin, state); dur = 0; - } + }; } +void Out::setDiv(int newDiv) { + div = newDiv; +}; + +void Out::setWidth(int newWidth) { + width = newWidth; + len = (unsigned long)((double)period * (width / 100.0) / 1000.0); + +}; + int Out::getState() { return state; } + diff --git a/Out.h b/Out.h index 9c36a71..363446e 100644 --- a/Out.h +++ b/Out.h @@ -6,19 +6,24 @@ class Out { private: - unsigned byte pin; + byte pin; unsigned char state; - unsigned int div; - unsigned int cycle; + int cycle; unsigned long dur; - unsigned byte width; - unsigned long len; + unsigned long len; + public: - LED(unsigned byte pin); + Out(byte pin); void turnOn(); void turnOff(); + void setLen(unsigned long currentPeriod); + void setDiv(int newDiv); + void setWidth(int newWidth); int getState(); -} + byte width; + int div; + +}; #endif diff --git a/globals.h b/globals.h new file mode 100644 index 0000000..02df380 --- /dev/null +++ b/globals.h @@ -0,0 +1,7 @@ +#ifndef GLOBALS_H +#define GLOBALS_H + +#include + +extern unsigned long period; +#endif // GLOBALS_H diff --git a/master_clock.ino b/master_clock.ino index 69d9e35..8bfd94e 100644 --- a/master_clock.ino +++ b/master_clock.ino @@ -3,6 +3,7 @@ #include #include #include +#include "globals.h" #include "Out.h" @@ -11,9 +12,10 @@ #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C + Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); -int BPM = 100; +int BPM = 140; unsigned long period = 60000000L / BPM; volatile boolean beatToggle = false; @@ -25,34 +27,34 @@ String modes[] = { "CLK DIV" }; -Out out1(2); -Out out2(3); -Out out3(4); -Out out4(5); -Out out5(6); -Out out6(7); -Out out7(8); -Out out8(9); +Out out1(4); +Out out2(5); +Out out3(6); +Out out4(7); +Out out5(8); +Out out6(9); +Out out7(10); +Out out8(11); + +byte btnPin = 13; +byte btnState = 0; +byte play = 1; // the setup function runs once when you press reset or power the board void setup() { + pinMode(btnPin, INPUT_PULLUP); + Serial.begin(9600); - initDisp(); - Timer1.initialize(1000000); - Timer1.attachInterrupt(clock); - Timer1.setPeriod(period); -} - -void initDisp() { + // init display if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); for (;;); // Don't proceed, loop forever } display.clearDisplay(); - display.setTextSize(1.5); + display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(0, 0); @@ -60,63 +62,90 @@ void initDisp() { String bpmCalculatedString = bpmString + BPM; display.println(bpmCalculatedString); - String modeString = "MODE: "; - String currentModeString = modes[mode]; - display.println(modeString + currentModeString); + // String modeString = "MODE: "; + // String currentModeString = modes[mode]; + // display.println(modeString + currentModeString); display.display(); + + //init timer + Timer1.initialize(1000000); + Timer1.attachInterrupt(clock); + Timer1.setPeriod(period); + + out1.setLen(period); + out2.setLen(period); + out3.setLen(period); + out4.setLen(period); + out5.setLen(period); + out6.setLen(period); + out7.setLen(period); + out8.setLen(period); + + // manual setup + out1.setDiv(1); + out1.setWidth(25); + + out2.setDiv(2); + out2.setWidth(25); + + out3.setDiv(4); + out3.setWidth(25); + + out4.setDiv(8); + out4.setWidth(25); + + out5.setDiv(16); + out5.setWidth(25); + + out6.setDiv(32); + out6.setWidth(25); + + out7.setDiv(64); + out7.setWidth(25); + + out8.setDiv(1); + out8.setWidth(10); } void clock(void) { - beatToggle = true; + if (play == 1) { + beatToggle = true; + } } void loop() { if (beatToggle) { - out1.turnOn(); + out2.turnOn(); + out3.turnOn(); + out4.turnOn(); + out5.turnOn(); + out6.turnOn(); + out7.turnOn(); + out8.turnOn(); beatToggle = false; } - out1.turnOff(); + out1.turnOff(); + out2.turnOff(); + out3.turnOff(); + out4.turnOff(); + out5.turnOff(); + out6.turnOff(); + out7.turnOff(); + out8.turnOff(); + + checkBtn(); } -void clockDivider() { - clockDivCount += 1; - - turnOnLed(0); - - if (clockDivCount % 2 == 0) { - turnOnLed(1); - }; - - if (clockDivCount % 3 == 0) { - turnOnLed(2); - }; - - if (clockDivCount % 4 == 0) { - turnOnLed(3); - }; - - if (clockDivCount % 8 == 0) { - turnOnLed(4); - }; - - if (clockDivCount % 16 == 0) { - turnOnLed(5); - }; - - if (clockDivCount % 32 == 0) { - turnOnLed(6); - }; - - if (clockDivCount % 64 == 0) { - turnOnLed(6); - }; - - if (clockDivCount == 64) { - clockDivCount = 0; +void checkBtn() { + byte currentBtnState = digitalRead(btnPin); + if (currentBtnState != btnState) { + if (currentBtnState == 1) { + play = (play == 1) ? 0 : 1; }; } -} + btnState = currentBtnState; +}; \ No newline at end of file