151 lines
No EOL
2.6 KiB
C++
151 lines
No EOL
2.6 KiB
C++
#include <SPI.h>
|
|
#include <Wire.h>
|
|
#include <Adafruit_SSD1306.h>
|
|
#include <Adafruit_GFX.h>
|
|
#include <TimerOne.h>
|
|
#include "globals.h"
|
|
|
|
#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 = 140;
|
|
unsigned long period = 60000000L / BPM;
|
|
volatile boolean beatToggle = false;
|
|
|
|
int clockDivCount = 0;
|
|
|
|
int mode = 0;
|
|
|
|
String modes[] = {
|
|
"CLK DIV"
|
|
};
|
|
|
|
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);
|
|
|
|
// 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(2);
|
|
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();
|
|
|
|
//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) {
|
|
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();
|
|
out2.turnOff();
|
|
out3.turnOff();
|
|
out4.turnOff();
|
|
out5.turnOff();
|
|
out6.turnOff();
|
|
out7.turnOff();
|
|
out8.turnOff();
|
|
|
|
checkBtn();
|
|
}
|
|
|
|
void checkBtn() {
|
|
byte currentBtnState = digitalRead(btnPin);
|
|
if (currentBtnState != btnState) {
|
|
if (currentBtnState == 1) {
|
|
play = (play == 1) ? 0 : 1;
|
|
};
|
|
}
|
|
btnState = currentBtnState;
|
|
}; |