modularize

This commit is contained in:
Dominic DiTaranto 2026-01-17 20:35:24 -05:00
parent 5ccc0ef4e5
commit 019fe61f96
4 changed files with 137 additions and 73 deletions

37
Out.cpp
View file

@ -1,40 +1,63 @@
// Out.cpp // Out.cpp
#include "Arduino.h"
#include "Out.h" #include "Out.h"
#include "globals.h"
Out::Out(unsigned byte pin) { Out::Out(byte pin) {
pin = pin; this->pin = pin;
state = LOW; state = LOW;
div = 1; div = 1;
cycle = 0; cycle = 0;
dur = 0; dur = 0;
width = 25; width = 50;
// this needs to dynamically change with width // this needs to dynamically change with width
len = period * (width / 100);
len = 0;
pinMode(pin, OUTPUT); 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() { void Out::turnOn() {
cycle += 1; cycle += 1;
if (cycle == div) { if (cycle == div) {
state = HIGH; state = HIGH;
digitalWrite(pin, state); digitalWrite(pin, state);
dur = millis(); dur = millis();
cycle = 0; cycle = 0;
} };
} }
void Out::turnOff() { void Out::turnOff() {
if (state == HIGH && millis() - dur >= len) { if (state == HIGH && millis() - dur >= len) {
Serial.println(len);
state = LOW; state = LOW;
digitalWrite(pin, state); digitalWrite(pin, state);
dur = 0; 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() { int Out::getState() {
return state; return state;
} }

19
Out.h
View file

@ -6,19 +6,24 @@
class Out { class Out {
private: private:
unsigned byte pin; byte pin;
unsigned char state; unsigned char state;
unsigned int div; int cycle;
unsigned int cycle;
unsigned long dur; unsigned long dur;
unsigned byte width; unsigned long len;
unsigned long len;
public: public:
LED(unsigned byte pin); Out(byte pin);
void turnOn(); void turnOn();
void turnOff(); void turnOff();
void setLen(unsigned long currentPeriod);
void setDiv(int newDiv);
void setWidth(int newWidth);
int getState(); int getState();
} byte width;
int div;
};
#endif #endif

7
globals.h Normal file
View file

@ -0,0 +1,7 @@
#ifndef GLOBALS_H
#define GLOBALS_H
#include <Arduino.h>
extern unsigned long period;
#endif // GLOBALS_H

View file

@ -3,6 +3,7 @@
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
#include <TimerOne.h> #include <TimerOne.h>
#include "globals.h"
#include "Out.h" #include "Out.h"
@ -11,9 +12,10 @@
#define OLED_RESET -1 #define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C #define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int BPM = 100; int BPM = 140;
unsigned long period = 60000000L / BPM; unsigned long period = 60000000L / BPM;
volatile boolean beatToggle = false; volatile boolean beatToggle = false;
@ -25,34 +27,34 @@ String modes[] = {
"CLK DIV" "CLK DIV"
}; };
Out out1(2); Out out1(4);
Out out2(3); Out out2(5);
Out out3(4); Out out3(6);
Out out4(5); Out out4(7);
Out out5(6); Out out5(8);
Out out6(7); Out out6(9);
Out out7(8); Out out7(10);
Out out8(9); 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 // the setup function runs once when you press reset or power the board
void setup() { void setup() {
pinMode(btnPin, INPUT_PULLUP);
Serial.begin(9600); Serial.begin(9600);
initDisp();
Timer1.initialize(1000000); // init display
Timer1.attachInterrupt(clock);
Timer1.setPeriod(period);
}
void initDisp() {
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed")); Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever for (;;); // Don't proceed, loop forever
} }
display.clearDisplay(); display.clearDisplay();
display.setTextSize(1.5); display.setTextSize(2);
display.setTextColor(WHITE); display.setTextColor(WHITE);
display.setCursor(0, 0); display.setCursor(0, 0);
@ -60,63 +62,90 @@ void initDisp() {
String bpmCalculatedString = bpmString + BPM; String bpmCalculatedString = bpmString + BPM;
display.println(bpmCalculatedString); display.println(bpmCalculatedString);
String modeString = "MODE: "; // String modeString = "MODE: ";
String currentModeString = modes[mode]; // String currentModeString = modes[mode];
display.println(modeString + currentModeString); // display.println(modeString + currentModeString);
display.display(); 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) { void clock(void) {
beatToggle = true; if (play == 1) {
beatToggle = true;
}
} }
void loop() { void loop() {
if (beatToggle) { if (beatToggle) {
out1.turnOn(); out1.turnOn();
out2.turnOn();
out3.turnOn();
out4.turnOn();
out5.turnOn();
out6.turnOn();
out7.turnOn();
out8.turnOn();
beatToggle = false; beatToggle = false;
} }
out1.turnOff(); out1.turnOff();
out2.turnOff();
out3.turnOff();
out4.turnOff();
out5.turnOff();
out6.turnOff();
out7.turnOff();
out8.turnOff();
checkBtn();
} }
void clockDivider() { void checkBtn() {
clockDivCount += 1; byte currentBtnState = digitalRead(btnPin);
if (currentBtnState != btnState) {
turnOnLed(0); if (currentBtnState == 1) {
play = (play == 1) ? 0 : 1;
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;
}; };
} }
} btnState = currentBtnState;
};