From 5ccc0ef4e5b0b66818aee444644c1a721cb24df7 Mon Sep 17 00:00:00 2001 From: Dominic DiTaranto Date: Fri, 16 Jan 2026 17:41:08 -0500 Subject: [PATCH] OOO --- Out.cpp | 40 ++++++++++++++++ Out.h | 24 ++++++++++ master_clock.ino | 122 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 186 insertions(+) create mode 100644 Out.cpp create mode 100644 Out.h create mode 100644 master_clock.ino diff --git a/Out.cpp b/Out.cpp new file mode 100644 index 0000000..c5068c9 --- /dev/null +++ b/Out.cpp @@ -0,0 +1,40 @@ +// Out.cpp +#include "Out.h" + +Out::Out(unsigned byte pin) { + pin = pin; + state = LOW; + + div = 1; + cycle = 0; + + dur = 0; + width = 25; + // this needs to dynamically change with width + len = period * (width / 100); + + pinMode(pin, OUTPUT); +} + +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) { + state = LOW; + digitalWrite(pin, state); + dur = 0; + } +} + +int Out::getState() { + return state; +} diff --git a/Out.h b/Out.h new file mode 100644 index 0000000..9c36a71 --- /dev/null +++ b/Out.h @@ -0,0 +1,24 @@ +// Out.h +#ifndef Out_h +#define Out_h + +#include + +class Out { + private: + unsigned byte pin; + unsigned char state; + unsigned int div; + unsigned int cycle; + unsigned long dur; + unsigned byte width; + unsigned long len; + + public: + LED(unsigned byte pin); + void turnOn(); + void turnOff(); + int getState(); +} + +#endif diff --git a/master_clock.ino b/master_clock.ino new file mode 100644 index 0000000..69d9e35 --- /dev/null +++ b/master_clock.ino @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include + +#include "Out.h" + +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 64 +#define OLED_RESET -1 +#define SCREEN_ADDRESS 0x3C + +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); + +int BPM = 100; +unsigned long period = 60000000L / BPM; +volatile boolean beatToggle = false; + +int clockDivCount = 0; + +int mode = 0; + +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); + + +// the setup function runs once when you press reset or power the board +void setup() { + Serial.begin(9600); + initDisp(); + + Timer1.initialize(1000000); + Timer1.attachInterrupt(clock); + Timer1.setPeriod(period); +} + +void initDisp() { + 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.setTextColor(WHITE); + display.setCursor(0, 0); + + String bpmString = "BPM: "; + String bpmCalculatedString = bpmString + BPM; + display.println(bpmCalculatedString); + + String modeString = "MODE: "; + String currentModeString = modes[mode]; + display.println(modeString + currentModeString); + + display.display(); +} + +void clock(void) { + beatToggle = true; +} + +void loop() { + if (beatToggle) { + + out1.turnOn(); + + beatToggle = false; + } + + out1.turnOff(); +} + +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; + }; + } +}