Skip to content

Commit

Permalink
rotaryEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
yangcan1 committed May 13, 2024
1 parent 30f50a3 commit d687739
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"files.associations": {
"cmath": "cpp"
"cmath": "cpp",
"__locale": "cpp",
"string": "cpp",
"string_view": "cpp"
}
}
22 changes: 22 additions & 0 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "ext_analysis.h"
#include "storage.h"
#include "globals.h"
#include "AiEsp32RotaryEncoder.h"


FASTLED_USING_NAMESPACE
Expand Down Expand Up @@ -75,6 +76,8 @@ uint8_t manual_pattern_idx = 0;

/// Contains if manual control is enabled or not.
volatile bool manual_control_enabled = false;
uint8_t rotary_encoder_pattern_idx = 0;


/// History of all currently-running patterns.
Strip_Buffer histories[PATTERN_LIMIT];
Expand All @@ -91,6 +94,8 @@ extern Pattern mainPatterns[];
#include "api.h"
#endif

AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);

void setup();
void loop();
void audio_analysis();
Expand All @@ -107,6 +112,21 @@ void setup() {
Serial.begin(115200);
while (!Serial) { ; }

//Initialize rotary encoder
rotaryEncoder.begin();
rotaryEncoder.setup(readEncoderISR);
bool circleValues = true;
rotaryEncoder.setBoundaries(0, 12, circleValues); //minValue, maxValue, circleValues true|false (when max go to min and vice versa)

/*Rotary acceleration introduced 25.2.2021.
in case range to select is huge, for example - select a value between 0 and 1000 and we want 785
without accelerateion you need long time to get to that number
Using acceleration, faster you turn, faster will the value raise.
For fine tuning slow down.
*/
//rotaryEncoder.disableAcceleration(); //acceleration is now enabled by default - disable if you dont need it
rotaryEncoder.setAcceleration(250); //or set the value - larger number = more accelearation; 0 or 1 means disabled acceleration

// Reindex mainPatterns, to make sure it is consistent.
for (int i = 0; i < NUM_PATTERNS; i++) {
mainPatterns[i].index = i;
Expand Down Expand Up @@ -359,6 +379,8 @@ void print_buffer(CRGB *buf, uint8_t len) {
/// LED strip.
void loop() {
begin_loop_timer(config.loop_ms); // Begin timing this loop
rotary_loop();
delay(50);

// Reset buffers if pattern settings were changed since
// last program loop.
Expand Down
7 changes: 7 additions & 0 deletions main/nanolux_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ typedef void (*SimplePatternList[])();
// Button Input
#define BUTTON_PIN 33

// Rotary Encoder
#define ROTARY_ENCODER_A_PIN 23
#define ROTARY_ENCODER_B_PIN 22
#define ROTARY_ENCODER_BUTTON_PIN 33
#define ROTARY_ENCODER_VCC_PIN -1
#define ROTARY_ENCODER_STEPS 4

// MAX - MIN | Freq Volume
#define MAX_FREQUENCY 4000.0
#define MIN_FREQUENCY 50.0
Expand Down
32 changes: 32 additions & 0 deletions main/nanolux_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "nanolux_types.h"
#include "nanolux_util.h"
#include "storage.h"
#include "AiEsp32RotaryEncoder.h"

/// The current config of the device, defined in main.ino.
/// Used to check if serial printing is allowed.
Expand All @@ -34,6 +35,9 @@ long loop_start_time = 0;

/// Holds the expected end time of the current program loop.
long loop_end_time = 0;
extern uint8_t rotary_encoder_pattern_idx;
extern AiEsp32RotaryEncoder rotaryEncoder;


/************************************************
*
Expand Down Expand Up @@ -266,3 +270,31 @@ long timer_overrun(){
// the expected loop end time.
return (millis() < loop_end_time) ? 0 : millis() - loop_end_time + 1;
}

void rotary_onButtonClick() {
static unsigned long lastTimePressed = 0; // Soft debouncing
if (millis() - lastTimePressed < 500){
return;
}
lastTimePressed = millis();
Serial.print("button pressed ");
Serial.print(millis());
Serial.println(" milliseconds after restart");
}

void rotary_loop() {
//dont print anything unless value changed
if (rotaryEncoder.encoderChanged()){
Serial.print("Value: ");
rotary_encoder_pattern_idx = rotaryEncoder.readEncoder();
Serial.println(rotaryEncoder.readEncoder());
nextPattern();
}
if (rotaryEncoder.isEncoderButtonClicked()) {
rotary_onButtonClick();
}
}

void IRAM_ATTR readEncoderISR() {
rotaryEncoder.readEncoder_ISR();
}
4 changes: 4 additions & 0 deletions main/nanolux_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ void bound_byte(uint8_t * val, int lower, int upper);
void process_reset_button();
void nanolux_serial_print(char * msg);

// Rotary encoder codes
void rotary_onButtonClick();
void rotary_loop();
void IRAM_ATTR readEncoderISR();
#endif

0 comments on commit d687739

Please sign in to comment.