Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update HAGIWO_Display.ino #12

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <avr/io.h>//for fast PWM
#include "fix_fft.h"//spectrum analyze
#include <EEPROM.h>

//OLED display setting
#include <SPI.h>//for OLED display
Expand All @@ -14,8 +15,11 @@
#define OLED_RESET 13
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
//EEPROM
#define ENCODER_DIR_ADDR 0 // EEPROM address to store the encoder direction
#define MENUTIMER_DIR_ADDR 2 // EEPROM address to store the menuTimer direction

//rotery encoder setting
//rotary encoder setting
#define ENCODER_OPTIMIZE_INTERRUPTS //contermeasure of rotery encoder noise
#include <Encoder.h>
Encoder myEnc(2, 4);//rotery encoder digitalRead pin
Expand Down Expand Up @@ -44,7 +48,34 @@ bool hide = 0; //1=hide,0=not hide

char data[128], im[128] , cv[128]; //data and im are used for spectrum , cv is used for oscilo.


bool encoderPressed = false;

//Global Configs
bool secretMenuActive = false;
unsigned long enterPressStartTime = 0; // Timer for entering the secret menu
unsigned long exitPressStartTime = 0; // Timer for exiting the secret menu
byte secretMenuOption = 1; // 1 for encoderDirection, 2 for menuTimer
unsigned int menuTimer = 5; // Default value for menuTimer, set to 100
int encoderDirection = 1; // 1 for normal, -1 for reversed



void setup() {

// Load the saved encoder direction from EEPROM
EEPROM.get(ENCODER_DIR_ADDR, encoderDirection);
delay(10);
if (encoderDirection != 1 && encoderDirection != -1) {
encoderDirection = 1; // Default to normal direction if EEPROM contains invalid data
}

EEPROM.get(MENUTIMER_DIR_ADDR, menuTimer);
delay(10);
if (menuTimer < 1 || menuTimer > 30) {
menuTimer = 5; // Default to 5 if menuTimer is invalid
}

//display setting
display.begin(SSD1306_SWITCHCAPVCC);
display.clearDisplay();
Expand All @@ -65,13 +96,112 @@ void setup() {
//fast ADC setting
ADCSRA = ADCSRA & 0xf8;//fast ADC *8 speed
ADCSRA = ADCSRA | 0x04;//fast ADC *8 speed

};

void loop() {
old_SW = SW;
old_mode = mode;

SW = digitalRead(5);
old_SW = SW;
old_mode = mode;
SW = digitalRead(5);

// Handle entering the secret menu
if (SW == LOW && !encoderPressed && !secretMenuActive) { // Button just pressed
encoderPressed = true;
enterPressStartTime = millis(); // Start timing the press
} else if (SW == HIGH && encoderPressed && !secretMenuActive) { // Button released
encoderPressed = false;
}

if (encoderPressed && millis() - enterPressStartTime >= 3000 && !secretMenuActive) {
// Enter the secret menu after holding for 3 seconds
secretMenuActive = true;
enterPressStartTime = 0;
encoderPressed = false; // Reset press state
secretMenuOption = 1; // Reset to first option
oldPosition = newPosition = myEnc.read(); // Reset encoder position
}

if (secretMenuActive) {
int newDirection = encoderDirection;
newPosition = myEnc.read();

// Check if the encoder was turned
if (newPosition > (oldPosition + 3)) {
oldPosition = newPosition;
if (secretMenuOption == 1) {
newDirection = 1; // Normal direction
} else if (secretMenuOption == 2) {
menuTimer += encoderDirection;
menuTimer = constrain(menuTimer, 1, 60);
}
} else if (newPosition < (oldPosition - 3)) {
oldPosition = newPosition;
if (secretMenuOption == 1) {
newDirection = -1; // Reversed direction
} else if (secretMenuOption == 2) {
menuTimer -= encoderDirection;
menuTimer = constrain(menuTimer, 1, 60);
}
}

// Update encoder direction if changed
if (newDirection != encoderDirection) {
encoderDirection = newDirection;
}

// Display the secret menu
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
display.setTextColor(WHITE);
display.println(" MENU");
display.setTextColor(secretMenuOption == 1 ? BLACK : WHITE, secretMenuOption == 1 ? WHITE : BLACK);
display.println("1. Enc. Direction:");
display.setTextColor(WHITE);
display.println(encoderDirection == 1 ? "Normal" : "Reversed");
display.println("");
display.setTextColor(secretMenuOption == 2 ? BLACK : WHITE, secretMenuOption == 2 ? WHITE : BLACK);
display.println("2. Menu Timer:");
display.setTextColor(WHITE);

display.println(menuTimer);
display.display();

// Handle encoder button press within secret menu
if (SW == LOW && !encoderPressed) {
encoderPressed = true;
exitPressStartTime = millis(); // Start timing the press for exit
} else if (SW == HIGH && encoderPressed) {
if (millis() - exitPressStartTime < 3000) {
// Short press: switch between encoderDirection and menuTimer
secretMenuOption = secretMenuOption == 1 ? 2 : 1;
}
encoderPressed = false;
exitPressStartTime = 0;
}

// Check if button has been held down for 3 seconds to exit the secret menu
if (encoderPressed && millis() - exitPressStartTime >= 3000) {
EEPROM.put(ENCODER_DIR_ADDR, encoderDirection); // Save the new direction to EEPROM
delay(50);
EEPROM.put(MENUTIMER_DIR_ADDR, menuTimer); // Save the new direction to EEPROM
delay(50);
secretMenuActive = false; // Exit the secret menu
display.clearDisplay();
display.display(); // Clear the screen or return to the main menu display
encoderPressed = false;
exitPressStartTime = 0;
delay(500); // Debounce delay
}

} else {

//START OF MAIN LOGIC

// Use encoder in the chosen direction
newPosition = encoderDirection * myEnc.read();

//select mode by push sw
if (old_SW == 0 && SW == 1 && param_select == param) {
param_select = 0;
Expand All @@ -93,7 +223,8 @@ void loop() {
param = constrain(param, 1, 3);

//rotery encoder input
newPosition = myEnc.read();
newPosition = encoderDirection * myEnc.read();

if ( (newPosition - 3) / 4 > oldPosition / 4) {
oldPosition = newPosition;
hideTimer = millis();
Expand Down Expand Up @@ -181,15 +312,15 @@ void loop() {
}

//OLED parameter hide while no operation
if ( hideTimer + 5000 >= millis() ) {//
if ( hideTimer + (menuTimer * 1000) >= millis() ) {//
hide = 1;
}
else {
hide = 0;
}

//LFO mode--------------------------------------------------------------------------------------------
if (mode == 1) {
if (mode == 1) {
param = constrain(param, 1, 3);
param1 = constrain(param1, 1, 8);
param2 = constrain(param2, 1, 8);
Expand Down Expand Up @@ -238,6 +369,8 @@ void loop() {
}
}



//wave mode---------------------------------------------------------------------------------------------
else if (mode == 2) {
param = constrain(param, 1, 3);
Expand Down Expand Up @@ -306,7 +439,7 @@ void loop() {
}
}

//shot mode---------------------------------------------------------------------------------------------
//shot mode---------------------------------------------------------------------------------------------
else if (mode == 3) {
param = constrain(param, 1, 2);
param1 = constrain(param1, 1, 4);
Expand Down Expand Up @@ -411,4 +544,5 @@ void loop() {
}

display.display();
}
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
#include <FlexiTimer2.h>
#include "Periphery.h"
#include <avr/io.h>



#define PWMOut 10
#define SyncIn 3
#define WaveformIn 3
#define AmpIn 0
#define PhaseIn 1
#define ModIn 5

unsigned int frq = 60000; // PWM周波数。60kHzあたりまで機能するが、マージンとって50kHzとする。
float duty = 0.5; // duty比率
int count = 0;
Expand Down