Skip to content

Commit

Permalink
Merge pull request #319 from speed-axel/master
Browse files Browse the repository at this point in the history
added dynamic behaviour to rotary
  • Loading branch information
iltis42 authored Mar 26, 2024
2 parents 53b9612 + 1156ece commit 0197576
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
9 changes: 5 additions & 4 deletions main/ESPRotary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ bool ESPRotary::longPressed = false;

#define ROTARY_SINGLE_INC 0
#define ROTARY_DOUBLE_INC 1

static TaskHandle_t pid = NULL;

void ESPRotary::attach(RotaryObserver *obs) {
Expand Down Expand Up @@ -176,15 +177,15 @@ void ESPRotary::informObservers( void * args )
{
while( 1 ) {
if( Flarm::bincom ) {
vTaskDelay(20 / portTICK_PERIOD_MS);
vTaskDelay(100 / portTICK_PERIOD_MS);
continue;
}
int button = gpio_get_level((gpio_num_t)sw);
if( button == 0 ){ // Push button is being pressed
timer++;
released = false;
pressed = false;
if( timer > 20 ){ // > 400 mS
if( timer > 20/5 ){ // > 400 mS
if( !longPressed ){
sendLongPress();
sendRelease();
Expand All @@ -196,7 +197,7 @@ void ESPRotary::informObservers( void * args )
if( !released ){
// ESP_LOGI(FNAME,"timer=%d", timer );
longPressed = false;
if( timer < 20 ){ // > 400 mS
if( timer < 20/5 ){ // > 400 mS
if( !pressed ){
sendPress();
sendRelease();
Expand Down Expand Up @@ -241,6 +242,6 @@ void ESPRotary::informObservers( void * args )
}
if( uxTaskGetStackHighWaterMark( pid ) < 256 )
ESP_LOGW(FNAME,"Warning rotary task stack low: %d bytes", uxTaskGetStackHighWaterMark( pid ) );
vTaskDelay(20 / portTICK_PERIOD_MS);
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}
7 changes: 5 additions & 2 deletions main/ESPRotary.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#include "esp_system.h"
#include "driver/pcnt.h"



enum _event { NONE, PRESS, LONG_PRESS, RELEASE, UP, DOWN, ERROR, MAX_EVENT };

class RotaryObserver;
Expand All @@ -39,6 +37,10 @@ class ESPRotary {
static void sendDown( int diff );
static void sendEsc();
static bool readSwitch(); // returns true if pressed
static void setPollPeriod( const int16_t value ) {
if( value > 0 ) pollPeriod = value;
}
static int16_t getPollPeriod() { return pollPeriod; }

private:
static std::list<RotaryObserver *> observers;
Expand All @@ -48,6 +50,7 @@ class ESPRotary {
static int16_t r_enc_count;
static int16_t r_enc2_count;
static int timer;
static int16_t pollPeriod;
static bool released;
static bool longPressed;
static bool pressed;
Expand Down
1 change: 1 addition & 0 deletions main/SetupMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2206,6 +2206,7 @@ void SetupMenu::setup_create_root(MenuEntry *top ){

SetupMenuValFloat * afe = new SetupMenuValFloat( "Airfield Elevation", "", -1, 3000, 1, 0, true, &elevation );
afe->setHelp( "Airfield elevation in meters for QNH auto adjust on ground according to this elevation");
afe->setDynamic( 3.0 );
top->addEntry( afe );

// Clear student mode, password correct
Expand Down
34 changes: 23 additions & 11 deletions main/SetupMenuValFloat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ SetupMenuValFloat * SetupMenuValFloat::qnh_menu = 0;
SetupMenuValFloat * SetupMenuValFloat::meter_adj_menu = 0;
char SetupMenuValFloat::_val_str[20];

SetupMenuValFloat::SetupMenuValFloat( const char* title, const char *unit, float min, float max, float step, int (*action)( SetupMenuValFloat *p ), bool end_menu, SetupNG<float> *anvs, e_restart_mode_t restart, bool sync, bool live_update ) {
SetupMenuValFloat::SetupMenuValFloat( const char* title, const char *unit, float min, float max, float step, int (*action)( SetupMenuValFloat *p ), bool end_menu, SetupNG<float> *anvs, e_restart_mode_t restart, bool sync, bool live_update ):
_dynamic(1.0) {
// ESP_LOGI(FNAME,"SetupMenuValFloat( %s ) ", title.c_str() );
attach(this);
_title = title;
Expand Down Expand Up @@ -107,7 +108,7 @@ void SetupMenuValFloat::display( int mode ){

void SetupMenuValFloat::displayVal()
{
ESP_LOGI(FNAME,"displayVal %s", value() );
// ESP_LOGI(FNAME,"displayVal %s", value() );
xSemaphoreTake(spiMutex,portMAX_DELAY );
ucg->setPrintPos( 1, 70 );
ucg->setFont(ucg_font_fub25_hf, true);
Expand All @@ -133,14 +134,19 @@ void SetupMenuValFloat::down( int count ){
return;
// ESP_LOGI(FNAME,"val down %d times ", count );
_value = _nvs->get();
while( (_value > _min) && count ) {
// float start = _value;
int _count = std::pow( count, _dynamic );
while( (_value > _min) && _count ) {
_value -= step( _step );
_count --;
}

_value -= step( _step );
count --;
}
if( _value < _min )
_value = _min;
_nvs->set(_value );
_nvs->set( _value );

// ESP_LOGI(FNAME,"val down diff=%f", start-_value );

displayVal();
if( _action != 0 )
(*_action)( this );
Expand All @@ -151,12 +157,17 @@ void SetupMenuValFloat::up( int count ){
return;
// ESP_LOGI(FNAME,"val up %d times ", count );
_value = _nvs->get();
while( (_value < _max) && count ) {
// float start = _value;
int _count = std::pow( count, _dynamic );
while( (_value < _max) && _count ) {
_value += step( _step );
count--;
}
_count--;
}

if( _value > _max )
_value = _max;

// ESP_LOGI(FNAME,"val up diff=%f", _value-start );
_nvs->set(_value );
displayVal();
if( _action != 0 )
Expand All @@ -171,8 +182,9 @@ void SetupMenuValFloat::press(){
if( selected != this )
return;
ESP_LOGI(FNAME,"SetupMenuValFloat press %d", pressed );

if ( pressed ){
// ESP_LOGI(FNAME,"pressed, value: %f", _value );
ESP_LOGI(FNAME,"pressed, value: %f", _value );
_nvs->set( _value );
display( 1 );
if( bits._end_menu )
Expand Down
2 changes: 2 additions & 0 deletions main/SetupMenuValFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class SetupMenuValFloat: public MenuEntry {
void escape() {}; // ignore, base class takes care
void setStep( float val ) { _step = val; };
void setMax( float max ) { _max = max; };
void setDynamic( float value ) { _dynamic = value; }
float _value;
static SetupMenuValFloat * qnh_menu;
static SetupMenuValFloat * meter_adj_menu;
Expand All @@ -48,4 +49,5 @@ class SetupMenuValFloat: public MenuEntry {
const char * _unit;
int (*_action)( SetupMenuValFloat *p );
SetupNG<float> * _nvs;
float _dynamic;
};

0 comments on commit 0197576

Please sign in to comment.