Save MIDI channel to EPROM, add EPROM save indicator

This commit is contained in:
2025-06-14 03:10:02 +02:00
parent f1163c3469
commit 34968af713
4 changed files with 70 additions and 14 deletions
+10
View File
@@ -53,4 +53,14 @@ const unsigned char midich [] PROGMEM = {
0x78, 0xf1, 0xe3, 0xc7, 0xff, 0xf1, 0xe0, 0x60, 0x18, 0x60, 0x78, 0xf1, 0xe3, 0xc7, 0xff, 0xf1, 0x78, 0xf1, 0xe3, 0xc7, 0xff, 0xf1, 0xe0, 0x60, 0x18, 0x60, 0x78, 0xf1, 0xe3, 0xc7, 0xff, 0xf1,
0xe0, 0x7f, 0x98, 0x66, 0x78, 0xf1, 0xe3, 0xc7, 0xff, 0xe1, 0xe0, 0x3f, 0x98, 0x66, 0x00, 0x00, 0xe0, 0x7f, 0x98, 0x66, 0x78, 0xf1, 0xe3, 0xc7, 0xff, 0xe1, 0xe0, 0x3f, 0x98, 0x66, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'savetorom', 32x12px
#define SAVETOROM_WIDTH 32
#define SAVETOROM_HEIGHT 12
const unsigned char savetorom [] PROGMEM = {
0x00, 0x05, 0x55, 0x54, 0x00, 0x0f, 0xff, 0xfe, 0x01, 0x0c, 0x00, 0x02, 0x01, 0x8c, 0x00, 0x02,
0x01, 0xcd, 0x89, 0xb2, 0xbf, 0xed, 0x55, 0x54, 0x5f, 0xed, 0x95, 0x14, 0x01, 0xcd, 0x49, 0x12,
0x01, 0x8c, 0x00, 0x02, 0x01, 0x0c, 0x00, 0x02, 0x00, 0x0f, 0xff, 0xfe, 0x00, 0x05, 0x55, 0x54
}; };
+59 -14
View File
@@ -1,8 +1,10 @@
#define FW_VER 1.0 #define FW_VER 1.0
#define USE_OLED true // OLED displays #define USE_OLED true // OLED displays
#define USE_ENCODER true #define USE_ENCODER true
#define EEPROM_MIDI_CHANNEL_ADDR 0 // Address to save MIDI channel in EEPROM
// include libraries // include libraries
#include <EEPROM.h>
#include <Adafruit_MCP23X17.h> #include <Adafruit_MCP23X17.h>
#include <MIDI.h> #include <MIDI.h>
@@ -23,22 +25,24 @@
int lastClk = HIGH; int lastClk = HIGH;
#endif #endif
unsigned long lastChannelChangeTime = 0;
bool channelChanged = false;
const unsigned long saveDelay = 2000; // 2 seconds
bool showSavedMessage = false;
unsigned long savedMessageStart = 0;
const unsigned long savedMessageDuration = 3000; // 1 second
// Instantiate libraries // Instantiate libraries
Adafruit_MCP23X17 mcp1; // First MCP23017 Adafruit_MCP23X17 mcp1; // First MCP23017
Adafruit_MCP23X17 mcp2; // Second MCP23017 Adafruit_MCP23X17 mcp2; // Second MCP23017
// Default hardware MIDI. SoftwareSerial is too slow... // Default hardware MIDI. SoftwareSerial is too slow...
MIDI_CREATE_DEFAULT_INSTANCE(); MIDI_CREATE_DEFAULT_INSTANCE();
// A7 A6 A5 A4 A3 A2 A1 A0 <-- GPIO A
// MCP pins 28 27 26 25 24 23 22 21 20 19 18 17 16 15
// 8 = pin 1 ╔══════════════════════════════════════════╗
// 9 = pin 2 ╠ MCP23017 ║
// 10 = pin 3 ╚══════════════════════════════════════════╝
// 01 02 03 04 05 06 07 08 9 10 11 12 13 14
// B0 B1 B2 B3 B4 B5 B6 B7 <-- GPIO B
// Default MIDI channel // Default MIDI channel
byte currentMidiChannel = MIDI_CHANNEL_OMNI; //8; // Default MIDI channel byte currentMidiChannel = MIDI_CHANNEL_OMNI; //8; // Default MIDI channel
byte savedChannel = EEPROM.read(EEPROM_MIDI_CHANNEL_ADDR);
// Struct for 2 MCP chips. // Struct for 2 MCP chips.
struct MCPOutput { struct MCPOutput {
@@ -95,7 +99,7 @@ MCPOutput noteToMCP[32] = {
void updateOLED() { void updateOLED() {
#if USE_OLED #if USE_OLED
oled.clearDisplay(); oled.clearDisplay();
oled.drawBitmap(0, 13, midich, MIDICH_WIDTH, MIDICH_HEIGHT, 1); oled.drawBitmap(0, 12, midich, MIDICH_WIDTH, MIDICH_HEIGHT, 1);
oled.setCursor(86, 32); oled.setCursor(86, 32);
oled.setTextColor(SSD1306_WHITE); oled.setTextColor(SSD1306_WHITE);
if (currentMidiChannel == MIDI_CHANNEL_OMNI) { if (currentMidiChannel == MIDI_CHANNEL_OMNI) {
@@ -103,6 +107,11 @@ void updateOLED() {
} else { } else {
oled.print(currentMidiChannel); oled.print(currentMidiChannel);
} }
if (showSavedMessage) {
oled.drawBitmap(88, 0, savetorom, SAVETOROM_WIDTH, SAVETOROM_HEIGHT, 1);
}
oled.display(); oled.display();
#endif #endif
} }
@@ -111,23 +120,39 @@ void checkEncoder() {
#if USE_ENCODER #if USE_ENCODER
int newClk = digitalRead(ENCODER_CLK); int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk && newClk == LOW) { if (newClk != lastClk && newClk == LOW) {
int newChannel = currentMidiChannel;
if (digitalRead(ENCODER_DT) == LOW) { if (digitalRead(ENCODER_DT) == LOW) {
currentMidiChannel++; newChannel++;
} else { } else {
currentMidiChannel--; newChannel--;
} }
// Wraparound: OMNI = 0, channels 116 if (newChannel > 16) newChannel = 0;
if (currentMidiChannel > 16) currentMidiChannel = 0; if (newChannel < 0) newChannel = 16;
if (currentMidiChannel < 0) currentMidiChannel = 16;
updateOLED(); if (newChannel != currentMidiChannel) {
currentMidiChannel = newChannel;
updateOLED();
// Mark that channel changed and restart delay timer
lastChannelChangeTime = millis();
channelChanged = true;
}
} }
lastClk = newClk; lastClk = newClk;
#endif #endif
} }
void setup() { void setup() {
// Validate saved midi channel in EEPROM
if (savedChannel <= 16) {
currentMidiChannel = savedChannel;
} else {
currentMidiChannel = MIDI_CHANNEL_OMNI;
}
#if USE_OLED #if USE_OLED
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // I2C addr 0x3C if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // I2C addr 0x3C
// Don't halt if missing, just skip display // Don't halt if missing, just skip display
@@ -174,6 +199,26 @@ void loop() {
#if USE_ENCODER #if USE_ENCODER
checkEncoder(); checkEncoder();
#endif #endif
// Save channel if change timeout has passed
if (channelChanged && (millis() - lastChannelChangeTime >= saveDelay)) {
EEPROM.update(EEPROM_MIDI_CHANNEL_ADDR, currentMidiChannel);
channelChanged = false;
// Show "Saved" message
showSavedMessage = true;
savedMessageStart = millis();
#if USE_OLED
updateOLED();
#endif
}
if (showSavedMessage && (millis() - savedMessageStart >= savedMessageDuration)) {
showSavedMessage = false;
#if USE_OLED
updateOLED(); // Return to normal channel display
#endif
}
// LED feedback // LED feedback
if (ledActive && millis() - ledOnTime >= ledDuration) { if (ledActive && millis() - ledOnTime >= ledDuration) {
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

@@ -0,0 +1 @@
{"hostname":"Aletheia","username":"sgilissen"}