Browse Source

unsuccessful rotaryencoder try

main
Hendrik Langer 7 years ago
parent
commit
a5986ccca2
  1. 25
      src/main.cpp
  2. 1
      src/main.h
  3. 6
      src/mp3.cpp
  4. 1
      src/mp3.h
  5. 97
      src/rotary.cpp
  6. 8
      src/rotary.h

25
src/main.cpp

@ -8,14 +8,14 @@
* GND O O GND * GND O O GND
* 5V O O 5V * 5V O O 5V
* 3V3 O O 3V3 * 3V3 O O 3V3
* GND O < 36 ROTARY_BTN * GND O < 36
* RX * * 17 * RX * * 17
* TX * < 38 ROTARY_A * TX * < 38 ROTARY_A
* RST * BUTTON < 39 ROTARY_B * RST * BUTTON < 39 ROTARY_B
* 0 * < 34 * 0 * BUTTON < 34
* 22 * < 35 * 22 * < 35
* BME280_SDO 19 * LoRa_MISO * 32 * BME280_SDO 19 * LoRa_MISO ? * 32
* BME280_CS 23 * * 33 * BME280_CS 23 * ? * 33
* 18 x LoRa_CS * 25 MAX98_DIN * 18 x LoRa_CS * 25 MAX98_DIN
* BME280_SCL/SCK 5 * LoRa_SCK LoRa_IRQ * 26 * BME280_SCL/SCK 5 * LoRa_SCK LoRa_IRQ * 26
* 15 * OLED_SCL LoRa_MOSI * 27 BME280_SDA/SDI * 15 * OLED_SCL LoRa_MOSI * 27 BME280_SDA/SDI
@ -25,6 +25,14 @@
* 16 * OLED_RST * 21 * 16 * OLED_RST * 21
*/ */
/* Rotary Encoder
* 1 LED
* pinA A 2 LED
* GND C 3 BTN -|
* pinB B 4 LED /
* 5 Vin -|
*/
//Include Basecamp in this sketch //Include Basecamp in this sketch
#include <Basecamp.hpp> #include <Basecamp.hpp>
#include <Configuration.hpp> #include <Configuration.hpp>
@ -64,7 +72,7 @@ uint32_t lastButtonPress = 0;
//Variables for the sensor and the battery //Variables for the sensor and the battery
static const int buttonPin = 0; static const int buttonPin = 0;
static const int ResetPin = 17; static const int ResetPin = 17;
static const int SensorPin = 32; static const int SensorPin = 0;
static const int BatteryPin = 34; static const int BatteryPin = 34;
static const int rotaryPinA = 38; static const int rotaryPinA = 38;
static const int rotaryPinB = 39; static const int rotaryPinB = 39;
@ -175,7 +183,7 @@ void setup() {
u8g2.setContrast(127); u8g2.setContrast(127);
rotary.registerCallback(nullptr); rotary.registerCallback(rotation);
rotary.begin(rotaryPinA, rotaryPinB, rotaryPinButton); rotary.begin(rotaryPinA, rotaryPinB, rotaryPinButton);
} }
@ -260,6 +268,11 @@ void suspendESP(uint16_t packetId) {
} }
void rotation(int i) {
Serial.println(i);
mp3.setVolume(i);
}
void loop() void loop()
{ {

1
src/main.h

@ -7,3 +7,4 @@ void transmitStatus();
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total); void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
void suspendESP(uint16_t packetId); void suspendESP(uint16_t packetId);
void loop(); void loop();
void rotation(int i);

6
src/mp3.cpp

@ -56,6 +56,12 @@ void MP3::start() {
} }
} }
void MP3::setVolume(int volume) {
if (playing && out)
if (volume > 0 && volume <= 100)
out->SetGain(((float)volume)/100.0);
}
void MP3::mp3_decoder_task(void *pvParameters) { void MP3::mp3_decoder_task(void *pvParameters) {
strcpy(titleStr, "..."); strcpy(titleStr, "...");
file = new AudioFileSourceICYStream(URL); file = new AudioFileSourceICYStream(URL);

1
src/mp3.h

@ -20,6 +20,7 @@ class MP3 {
//const char *URL="http://swr-swr1-bw.cast.addradio.de/swr/swr1/bw/mp3/64/stream.mp3"; //const char *URL="http://swr-swr1-bw.cast.addradio.de/swr/swr1/bw/mp3/64/stream.mp3";
void stop(void); void stop(void);
void start(void); void start(void);
void setVolume(int);
bool playing = false; bool playing = false;
private: private:
AudioGenerator *decoder; AudioGenerator *decoder;

97
src/rotary.cpp

@ -16,8 +16,11 @@
//using namespace std; //using namespace std;
TaskHandle_t Rotary::xTaskToNotify = NULL; TaskHandle_t Rotary::xTaskToNotify = NULL;
Rotary* Rotary::instance = NULL;
Rotary::Rotary() {} Rotary::Rotary() {
instance = this;
}
bool Rotary::begin(uint8_t pinA, uint8_t pinB, uint8_t pinButton) { bool Rotary::begin(uint8_t pinA, uint8_t pinB, uint8_t pinButton) {
this->pinA = pinA; this->pinA = pinA;
@ -44,82 +47,46 @@ void Rotary::cTaskWrapper(void* parameters) {
void Rotary::button_task(void *pvParameters) { void Rotary::button_task(void *pvParameters) {
xTaskToNotify = xTaskGetCurrentTaskHandle(); xTaskToNotify = xTaskGetCurrentTaskHandle();
attachInterrupt(digitalPinToInterrupt(pinA), doEncoderA, CHANGE); attachInterrupt(digitalPinToInterrupt(pinA), doEncoderA, RISING);
attachInterrupt(digitalPinToInterrupt(pinB), doEncoderB, CHANGE); attachInterrupt(digitalPinToInterrupt(pinB), doEncoderB, RISING);
// attachInterrupt(digitalPinToInterrupt(pinButton), doButton, CHANGE); // attachInterrupt(digitalPinToInterrupt(pinButton), doButton, FALLING);
uint32_t ulNotificationValue; uint32_t ulNotificationValue;
uint32_t ulStatusValue;
while(true) { while(true) {
rotating = true; // reset the debouncer
ulNotificationValue = xTaskNotifyWait(0x00, ULONG_MAX, &ulNotificationValue, portMAX_DELAY ); ulNotificationValue = xTaskNotifyWait(0x00, ULONG_MAX, &ulStatusValue, portMAX_DELAY );
Serial.printf("interrupt %d, rotating: %d\n", ulNotificationValue, rotating);
if( ulNotificationValue == 1 ) {
// debounce
if ( rotating ) delay (1); // wait a little until the bouncing is done
// Test transition, did things really change? // if ( rotating ) delay (1); // wait a little until the bouncing is done
if ( digitalRead(pinA) != A_set ) { // debounce once more
A_set = !A_set;
if( ulStatusValue == 1 ) {
// adjust counter + if A leads B // adjust counter + if A leads B
if ( A_set && !B_set )
encoderPos += 1; encoderPos += 1;
rotating = false; // no more debouncing until loop() hits again
changed(); changed();
} rotating = false; // no more debouncing until loop() hits again
} else if ( ulStatusValue == 2 ) {
} else if ( ulNotificationValue == 2 ) { // adjust counter -1 if B leads A
if ( rotating ) delay (1);
if ( digitalRead(pinB) != B_set ) {
B_set = !B_set;
// adjust counter - 1 if B leads A
if ( B_set && !A_set )
encoderPos -= 1; encoderPos -= 1;
rotating = false;
changed(); changed();
} rotating = false; // no more debouncing until loop() hits again
} else if ( ulNotificationValue == 4 ) { } else if ( ulStatusValue == 4 ) {
delay(1); Button_set = digitalRead(pinButton);
if ( digitalRead(pinButton) != Button_set ) {
Button_set = !Button_set;
if (! Button_set ) { if (! Button_set ) {
encoderPos = 0; encoderPos = 0;
} }
} }
}
} }
} }
/*
// main loop, work is done by interrupt service routines, this one only prints stuff
void loop() {
rotating = true; // reset the debouncer
if (lastReportedPos != encoderPos) {
Serial.print("Index:");
Serial.println(encoderPos, DEC);
lastReportedPos = encoderPos;
}
if (digitalRead(clearButton) == LOW ) {
encoderPos = 0;
}
}
*/
void Rotary::changed() { void Rotary::changed() {
if (lastReportedPos != encoderPos) { if (lastReportedPos != encoderPos) {
Serial.print("encoder: "); if (callback) callback(encoderPos);
Serial.println(encoderPos);
// if (callback) callback(encoderPos);
lastReportedPos = encoderPos; lastReportedPos = encoderPos;
} }
rotating = true; // reset the debouncer
} }
bool Rotary::registerCallback(std::function<void(int)> callback) { bool Rotary::registerCallback(std::function<void(int)> callback) {
@ -132,27 +99,43 @@ void Rotary::doButton() {
configASSERT( xTaskToNotify != NULL ); configASSERT( xTaskToNotify != NULL );
uint32_t intrBits = 4; uint32_t intrBits = 4;
xTaskNotifyFromISR( xTaskToNotify, intrBits, eSetValueWithOverwrite, &xHigherPriorityTaskWoken ); xTaskNotifyFromISR( xTaskToNotify, intrBits, eSetValueWithoutOverwrite, &xHigherPriorityTaskWoken );
portYIELD_FROM_ISR(); //portYIELD_FROM_ISR( xHigherPriorityTaskWoken ); portYIELD_FROM_ISR(); //portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
} }
// Interrupt on A changing state // Interrupt on A changing state
void Rotary::doEncoderA() { void Rotary::doEncoderA() {
BaseType_t xHigherPriorityTaskWoken = pdFALSE; BaseType_t xHigherPriorityTaskWoken = pdFALSE;
if (instance->skipA) {
instance->skipA = false;
instance->debounceA = millis();
return;
}
if (!digitalRead(instance->pinB) && ((millis() - instance->debounceA) > instance->debounceDelay)) {
instance->debounceA = millis();
instance->skipB = true;
configASSERT( xTaskToNotify != NULL ); configASSERT( xTaskToNotify != NULL );
uint32_t intrBits = 1; uint32_t intrBits = 1;
xTaskNotifyFromISR( xTaskToNotify, intrBits, eSetValueWithOverwrite, &xHigherPriorityTaskWoken ); xTaskNotifyFromISR( xTaskToNotify, intrBits, eSetValueWithoutOverwrite, &xHigherPriorityTaskWoken );
portYIELD_FROM_ISR(); //portYIELD_FROM_ISR( xHigherPriorityTaskWoken ); portYIELD_FROM_ISR(); //portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}
} }
// Interrupt on B changing state, same as A above // Interrupt on B changing state, same as A above
void Rotary::doEncoderB() { void Rotary::doEncoderB() {
BaseType_t xHigherPriorityTaskWoken = pdFALSE; BaseType_t xHigherPriorityTaskWoken = pdFALSE;
if (instance->skipB) {
instance->skipB = false;
instance->debounceB = millis();
return;
}
if (!digitalRead(instance->pinA) && ((millis() - instance->debounceB) > instance->debounceDelay)) {
instance->debounceB = millis();
instance->skipA = true;
configASSERT( xTaskToNotify != NULL ); configASSERT( xTaskToNotify != NULL );
uint32_t intrBits = 2; uint32_t intrBits = 2;
xTaskNotifyFromISR( xTaskToNotify, intrBits, eSetValueWithOverwrite, &xHigherPriorityTaskWoken ); xTaskNotifyFromISR( xTaskToNotify, intrBits, eSetValueWithoutOverwrite, &xHigherPriorityTaskWoken );
portYIELD_FROM_ISR(); //portYIELD_FROM_ISR( xHigherPriorityTaskWoken ); portYIELD_FROM_ISR(); //portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}
} }

8
src/rotary.h

@ -19,15 +19,21 @@ class Rotary {
static void IRAM_ATTR doEncoderA(void); static void IRAM_ATTR doEncoderA(void);
static void IRAM_ATTR doEncoderB(void); static void IRAM_ATTR doEncoderB(void);
static void IRAM_ATTR doButton(void); static void IRAM_ATTR doButton(void);
static Rotary* instance;
static TaskHandle_t xTaskToNotify; static TaskHandle_t xTaskToNotify;
uint32_t debounceA;
uint32_t debounceB;
uint32_t debounceDelay = 50;
private: private:
volatile unsigned int encoderPos = 0; // a counter for the dial volatile int encoderPos = 0; // a counter for the dial
unsigned int lastReportedPos = 1; // change management unsigned int lastReportedPos = 1; // change management
boolean rotating = false; // debounce management boolean rotating = false; // debounce management
// interrupt service routine vars // interrupt service routine vars
boolean A_set = false; boolean A_set = false;
boolean B_set = false; boolean B_set = false;
boolean Button_set = false; boolean Button_set = false;
boolean skipA = false;
boolean skipB = false;
uint8_t pinA; uint8_t pinA;
uint8_t pinB; uint8_t pinB;
uint8_t pinButton; uint8_t pinButton;

Loading…
Cancel
Save