#include <FastLED.h>
#include <Wire.h>//alows communications with I2C devices
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>//library for Monochrome OLEDs based on SSD1306 drivers
#include <EncoderButton.h>
//#include "EncoderTool.h"
//using namespace EncoderTool;
//Define Screen
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
//Encoder and buttons setting (Buttons will be replaced with midi inputs)
//EncoderButton encoder(53,51,49);
//constexpr uint8_t pinA = 0, pinB = 1, pinBtn = 2;
// PAD DATA PINS
#define SNAREPIN 2
#define BASSPIN 3
#define TOM1PIN 4
#define TOM2PIN 5
#define TOM3PIN 6
#define HIHATPIN 7
#define CRASHPIN 8
#define CRASH2PIN 9
#define RIDEPIN 10
// NUMBER OF LEDS PER PAD
#define SNARELEDS 54
#define BASSLEDS 67
#define TOM1LEDS 34
#define TOM2LEDS 34
#define TOM3LEDS 34
#define HIHATLEDS 30
#define CRASHLEDS 37
#define CRASH2LEDS 37
#define RIDELEDS 37
//TOTAL
#define NUM_LEDS TOM1LEDS + TOM2LEDS + TOM3LEDS + SNARELEDS + BASSLEDS + HIHATLEDS + CRASHLEDS + CRASH2LEDS + RIDELEDS
#define NUM_STRIPS 9
// VARIABLES AND DEFAULTS
//Encoder and buttons setting (Buttons will be replaced with midi inputs)
EncoderButton eb1(14,15,13);
EncoderButton b1(31);
EncoderButton b2(33);
EncoderButton b3(35);
EncoderButton b4(37);
EncoderButton b5(39);
EncoderButton b6(41);
EncoderButton b7(43);
EncoderButton b8(45);
EncoderButton b9(47);
//char text[i] = "NOTHING";
byte brightness = 255;
byte minBrightness = 10;
byte snareColor = 0;
byte bassColor = 210;
byte tom1Color = 64;
byte tom2Color = 160;
byte tom3Color = 96;
byte hihatColor = 64;
byte crashColor = 160;
byte crash2Color = 160;
byte rideColor = 96;
byte snareBrightness = 255;
byte bassBrightness = 255;
byte tom1Brightness = 255;
byte tom2Brightness = 255;
byte tom3Brightness = 255;
byte hihatBrightness = 255;
byte crashBrightness = 255;
byte crash2Brightness = 255;
byte rideBrightness = 255;
byte Eb1pos = 0;
// CREATE LED STRIPS
CRGB snarestrip[SNARELEDS];
CRGB bassstrip[BASSLEDS];
CRGB tom1strip[TOM1LEDS];
CRGB tom2strip[TOM2LEDS];
CRGB tom3strip[TOM3LEDS];
CRGB hihatstrip[HIHATLEDS];
CRGB crashstrip[CRASHLEDS];
CRGB crash2strip[CRASH2LEDS];
CRGB ridestrip[RIDELEDS];
CRGB leds[NUM_LEDS];
CLEDController *controllers[NUM_STRIPS];
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // Address the OLED 128x64 I2C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//////////// FUNCTIONS ////////////////
// ENCODER HANDLER FUNCTION
void onEb1Encoder(EncoderButton& eb) {
Serial.print("eb1 incremented by: ");
Serial.println(eb.increment());
Serial.print("eb1 position is: ");
Serial.println(eb.position());
//Eb1pos=eb.position();
displayVariable("ENCODER 1", "ROTATED", "FUCK");
}
void onB1Clicked(EncoderButton& eb) {
Serial.print("eb1 clickCount: ");
Serial.println(eb.clickCount());
fill_solid(snarestrip, SNARELEDS, CHSV(snareColor, 255, snareBrightness));
FastLED.show(255);
displayText(" FLASH 1 ");
Serial.println(" FLASH 1");
delay(500);
FastLED.clear();
FastLED.show();
}
//DISPLAY TEXT FUNCTION
void displayText(const char* text) {
display.clearDisplay();
// Define line height
int lineHeight = 10; // Increase to add spacing between lines
// Dynamically calculate the maximum number of lines based on screen height and line height
const int maxLines = SCREEN_HEIGHT / lineHeight;
// Split the text into lines
char lines[maxLines][SCREEN_WIDTH / 6 + 1]; // Estimate maxLineLength based on width
int lineCount = 0;
int charCount = 0;
int textLen = strlen(text);
int lastSpace = -1;
for (int i = 0; i < textLen; i++) {
lines[lineCount][charCount] = text[i];
if (text[i] == ' ') {
lastSpace = charCount;
}
charCount++;
if (charCount >= SCREEN_WIDTH / 6 || text[i] == '\n') {
if (lastSpace != -1 && text[i] != '\n') {
// Wrap at the last space
lines[lineCount][lastSpace] = '\0';
i -= (charCount - lastSpace - 1);
charCount = lastSpace + 1;
} else {
// Wrap at the current character
lines[lineCount][charCount] = '\0';
}
lineCount++;
charCount = 0;
lastSpace = -1;
if (lineCount >= maxLines) break; // Stop if maximum lines reached
}
}
lines[lineCount][charCount] = '\0'; // Null-terminate the last line
lineCount++;
// Calculate vertical starting point for centered text
int totalTextHeight = lineHeight * lineCount;
int startY = (SCREEN_HEIGHT - totalTextHeight) / 2;
// Draw the border
display.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SSD1306_WHITE);
// Draw each line of text
for (int i = 0; i < lineCount; i++) {
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(lines[i], 0, 0, &x1, &y1, &w, &h);
int16_t x = (SCREEN_WIDTH - w) / 2; // Center horizontally
int16_t y = startY + (i * lineHeight); // Vertical position for each line
display.setCursor(x, y);
display.print(lines[i]);
}
// Display the content
display.display();
}
void displayVariable(const char* label, const char* text, const char* variable) {
display.clearDisplay();
// Define line height
int lineHeight = 10; // Increase to add spacing between lines
// Calculate vertical starting point for label
int labelY = 5;
int labelX = (SCREEN_WIDTH - strlen(label) * 6) / 2; // Center horizontally
display.setCursor(labelX, labelY);
display.print(label);
// Calculate vertical starting point for variable
int varY = SCREEN_HEIGHT - lineHeight - 5; // 5 pixels from the bottom
int varX = (SCREEN_WIDTH - strlen(variable) * 6) / 2; // Center horizontally
display.setCursor(varX, varY);
display.print(variable);
// Dynamically calculate the maximum number of lines based on screen height and line height
const int maxLines = (varY - labelY - lineHeight) / lineHeight;
// Split the text into lines
char lines[maxLines][SCREEN_WIDTH / 6 + 1]; // Estimate maxLineLength based on width
int lineCount = 0;
int charCount = 0;
int textLen = strlen(text);
int lastSpace = -1;
for (int i = 0; i < textLen; i++) {
lines[lineCount][charCount] = text[i];
if (text[i] == ' ') {
lastSpace = charCount;
}
charCount++;
if (charCount >= SCREEN_WIDTH / 6 || text[i] == '\n') {
if (lastSpace != -1 && text[i] != '\n') {
// Wrap at the last space
lines[lineCount][lastSpace] = '\0';
i -= (charCount - lastSpace - 1);
charCount = lastSpace + 1;
} else {
// Wrap at the current character
lines[lineCount][charCount] = '\0';
}
lineCount++;
charCount = 0;
lastSpace = -1;
if (lineCount >= maxLines) break; // Stop if maximum lines reached
}
}
lines[lineCount][charCount] = '\0'; // Null-terminate the last line
lineCount++;
// Calculate vertical starting point for centered text
int totalTextHeight = lineHeight * lineCount;
int startY = (varY + labelY + lineHeight - totalTextHeight) / 2;
// Draw each line of text
for (int i = 0; i < lineCount; i++) {
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(lines[i], 0, 0, &x1, &y1, &w, &h);
int16_t x = (SCREEN_WIDTH - w) / 2; // Center horizontally
int16_t y = startY + (i * lineHeight); // Vertical position for each line
display.setCursor(x, y);
display.print(lines[i]);
}
// Display the content
display.display();
}
void setup() {
//Debug
Serial.begin(115200);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.fillScreen(SSD1306_WHITE);
// DISPLAY BLOCK
delay(1000); // wait two seconds for initializing
//Set text size and color
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
//display.fillScreen();
display.fillScreen(SSD1306_WHITE);
display.display();
delay(500);
display.clearDisplay();
display.display();
delay(500);
display.fillScreen(SSD1306_WHITE);
display.display();
delay(500);
display.clearDisplay();
display.display();
delay(500);
display.fillScreen(SSD1306_WHITE);
display.display();
delay(500);
display.clearDisplay();
display.display();
delay(2000);
displayText("I am ALIVE and ready to send MIDI Stuff");
delay(2000);
Serial.println("I am ALIVE and ready to send MIDI Stuff");
FastLED.addLeds<WS2812B, SNAREPIN, GRB>(snarestrip, SNARELEDS);
FastLED.addLeds<WS2812B, BASSPIN, GRB>(bassstrip, BASSLEDS);
FastLED.addLeds<WS2812B, TOM1PIN, GRB>(tom1strip, TOM1LEDS);
FastLED.addLeds<WS2812B, TOM2PIN, GRB>(tom2strip, TOM2LEDS);
FastLED.addLeds<WS2812B, TOM3PIN, GRB>(tom3strip, TOM3LEDS);
FastLED.addLeds<WS2812B, HIHATPIN, GRB>(hihatstrip, HIHATLEDS);
FastLED.addLeds<WS2812B, CRASHPIN, GRB>(crashstrip, CRASHLEDS);
FastLED.addLeds<WS2812B, CRASH2PIN, GRB>(crash2strip, CRASH2LEDS);
FastLED.addLeds<WS2812B, RIDEPIN, GRB>(ridestrip, RIDELEDS);
displayText(" Testing Strips They should flash 3 times ");
Serial.println(" Testing Strips They should flash 3 times. ");
delay(2500);
fill_solid(snarestrip, SNARELEDS, CHSV(snareColor, 255, snareBrightness));
fill_solid(bassstrip, BASSLEDS, CHSV(bassColor, 255, bassBrightness));
fill_solid(tom1strip, TOM1LEDS, CHSV(tom1Color, 255, tom1Brightness));
fill_solid(tom2strip, TOM2LEDS, CHSV(tom2Color, 255, tom2Brightness));
fill_solid(tom3strip, TOM3LEDS, CHSV(tom3Color, 255, tom3Brightness));
fill_solid(hihatstrip, HIHATLEDS, CHSV(hihatColor, 255, hihatBrightness));
fill_solid(crashstrip, CRASHLEDS, CHSV(crashColor, 255, crashBrightness));
fill_solid(crash2strip, CRASH2LEDS, CHSV(crash2Color, 255, crash2Brightness));
fill_solid(ridestrip, RIDELEDS, CHSV(rideColor, 255, rideBrightness));
FastLED.show(255);
displayText(" FLASH 1 ");
Serial.println(" FLASH 1");
delay(500);
FastLED.clear();
FastLED.show();
delay(500);
fill_solid(snarestrip, SNARELEDS, CHSV(snareColor, 255, snareBrightness));
fill_solid(bassstrip, BASSLEDS, CHSV(bassColor, 255, bassBrightness));
fill_solid(tom1strip, TOM1LEDS, CHSV(tom1Color, 255, tom1Brightness));
fill_solid(tom2strip, TOM2LEDS, CHSV(tom2Color, 255, tom2Brightness));
fill_solid(tom3strip, TOM3LEDS, CHSV(tom3Color, 255, tom3Brightness));
fill_solid(hihatstrip, HIHATLEDS, CHSV(hihatColor, 255, hihatBrightness));
fill_solid(crashstrip, CRASHLEDS, CHSV(crashColor, 255, crashBrightness));
fill_solid(crash2strip, CRASH2LEDS, CHSV(crash2Color, 255, crash2Brightness));
fill_solid(ridestrip, RIDELEDS, CHSV(rideColor, 255, rideBrightness));
FastLED.show(255);
displayText(" FLASH 2 ");
Serial.println(" FLASH 2");
delay(500);
FastLED.clear();
FastLED.show(0);
delay(500);
fill_solid(snarestrip, SNARELEDS, CHSV(snareColor, 255, snareBrightness));
fill_solid(bassstrip, BASSLEDS, CHSV(bassColor, 255, bassBrightness));
fill_solid(tom1strip, TOM1LEDS, CHSV(tom1Color, 255, tom1Brightness));
fill_solid(tom2strip, TOM2LEDS, CHSV(tom2Color, 255, tom2Brightness));
fill_solid(tom3strip, TOM3LEDS, CHSV(tom3Color, 255, tom3Brightness));
fill_solid(hihatstrip, HIHATLEDS, CHSV(hihatColor, 255, hihatBrightness));
fill_solid(crashstrip, CRASHLEDS, CHSV(crashColor, 255, crashBrightness));
fill_solid(crash2strip, CRASH2LEDS, CHSV(crash2Color, 255, crash2Brightness));
fill_solid(ridestrip, RIDELEDS, CHSV(rideColor, 255, rideBrightness));
FastLED.show(255);
//FastLED[0].showLeds(255);
//FastLED[1].showLeds(255);
//FastLED[2].showLeds(255);
//FastLED[3].showLeds(255);
//FastLED[4].showLeds(255);
//FastLED[5].showLeds(255);
//FastLED[6].showLeds(255);
//FastLED[7].showLeds(255);
Serial.println(" FLASH 3");
displayText(" FLASH 3 ");
delay(500);
FastLED.clear();
FastLED.show();
display.clearDisplay();
display.display();
// encoder handler
eb1.setEncoderHandler(onEb1Encoder);
b1.setClickHandler(onB1Clicked);
//Eb1pos=(eb1.position())
}
void loop() {
eb1.update();
b1.update();
}Loading
ssd1306
ssd1306