I have a menu - several strings, an array of pointers for that strings, and an index to that array. I also have a function returning the movement of an encoder since last call of the function (a signed number). Something along these lines:
char a1[] = "Menu1"; char a2[] = "Menu2"; char a3[] = "Menu3"; char a4[] = "Menu4"; char a5[] = "Menu5"; char a6[] = "Menu6"; char a7[] = "Menu7"; char a8[] = "Menu8"; char a9[] = "Menu9"; char * menu[] = {a1, a2, a3, a4, a5, a6, a7, a8, a9}; volatile uint8_t menuIndex; #define SIZE_A (sizeof(menu) / sizeof(char *)) int16_t EncoderMovement(void); // defined elsewhere void ProcessMenu(void) { int16_t tmp; tmp = EncoderMovement(); tmp = menuIndex + tmp; tmp = tmp % SIZE_A; if (tmp < 0) tmp = tmp + SIZE_A; menuIndex = tmp; DisplayMenu(tmp); }
When Menu1 is displayed and I move the encoder one tick backwards, it jumps onto Menu6; moving it further backwards it goes nicely to Menu5-Menu4...Menu1 when it jumps again to Menu6; moving it forward goes as supposed to Menu7 (then Menu8-Menu9-Menu1-Menu2...
Hands up those who know right off where's the gotcha.
I supposedly should be ashamed for this kind of error:
Developers forgetting about the [root cause of my problem] is not something that can be addressed by a guideline recommendation.
JW