neu-rah / ArduinoMenu

Arduino generic menu/interactivity system
GNU Lesser General Public License v2.1
929 stars 189 forks source link

Changing CHOOSE variable using a FIELD crashes program #436

Open Monskiller opened 3 months ago

Monskiller commented 3 months ago

I'm trying to manipulate a variable with either a CHOOSE menu or manual increments through a FIELD menu It works though various choose options, but editing it though a field crashes and resets my program Variable in question is rnds Am I doing something wrong?

#include <Wire.h>
#include <menu.h>
#include <menuIO/U8g2Out.h>
#include <menuIO/clickEncoderIn.h>
#include <menuIO/chainStream.h>
#include <EEPROM.h>

#define encA 7
#define encB 5
#define encBtn 3
#define encStep 4
#define MAX_DEPTH 2

#define fontName u8g2_font_7x13_mf
#define fontX 4
#define fontY 15
#define offsetX 3
#define offsetY 3
#define U8_Width 128
#define U8_Height 64

using namespace Menu;

U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
ClickEncoder rotaryEncoder = ClickEncoder(encA, encB, encBtn, encStep);
ClickEncoderStream encStream(rotaryEncoder, 1);

MENU_INPUTS(in,&encStream);
void IRAM_ATTR onTimer();

const colorDef<uint8_t> colors[6] MEMMODE={
  {{0,0},{0,1,1}},//bgColor
  {{1,1},{1,0,0}},//fgColor
  {{1,1},{1,0,0}},//valColor
  {{1,1},{1,0,0}},//unitColor
  {{0,1},{0,0,1}},//cursorColor
  {{1,1},{1,0,0}},//titleColor
};

int rnds=0;

result loadBBs(eventMask e,navNode& nav, prompt &item) {
  Serial.println(rnds);
  Serial.flush();
  return proceed;
}

CHOOSE(rnds,presetMenu,"Preset",doNothing,noEvent,noStyle
  ,VALUE("None",0,doNothing,noEvent)
  ,VALUE("Low Cap",30,doNothing,noEvent)
  ,VALUE("MP5 Mid Short",65,doNothing,noEvent)
  ,VALUE("M16 Polymer Mid",120,doNothing,noEvent)
  ,VALUE("M4 Mid",200,doNothing,noEvent)
  ,VALUE("PTS EPM1 Mid",250,doNothing,noEvent)
);

MENU(mainMenu,"Main menu",doNothing,noEvent,wrapStyle
  ,SUBMENU(presetMenu)
  ,FIELD(rnds,"Rounds"," Rnds",0,300,10,1,doNothing,noEvent,noStyle)
  ,OP("Load",loadBBs,enterEvent)
  ,OP("Manual",doNothing,enterEvent)
);

MENU_OUTPUTS(out,MAX_DEPTH
  ,U8G2_OUT(u8g2,colors,fontX,fontY,offsetX,offsetY,{0,0,U8_Width/fontX,U8_Height/fontY})
  ,NONE
);

NAVROOT(nav,mainMenu,MAX_DEPTH,in,out);

hw_timer_t *timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;

void setup() {
  Serial.begin(115200);

  Wire.begin();
  u8g2.begin();
  u8g2.setFont(fontName);
  //nav.idleTask=idle;

  timer = timerBegin(0, 80, true);
  timerAttachInterrupt(timer, &onTimer, true);
  timerAlarmWrite(timer, 1000, true);
  timerAlarmEnable(timer);

  pinMode(encA, INPUT_PULLUP);
  pinMode(encB, INPUT_PULLUP);
  rotaryEncoder.setAccelerationEnabled(true);
  rotaryEncoder.setDoubleClickEnabled(false);
}

void loop() {
  nav.doInput();
  if (nav.changed(0)) {
    u8g2.firstPage();
    do nav.doOutput(); while(u8g2.nextPage());
  }
}

void IRAM_ATTR onTimer()
{
  rotaryEncoder.service();
}

Hardware: