I am declaring an int with bigger scope than a problematic while() loop. At the end of each single iteration of the while() loop, that variable seems to get reset to 0 without my doing. I am using no forking/multithreading here. Here is the symptomatic code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
// Change the brightness setting
void change_brightness () {
int new_brightness = brightness;
int keypress = 0;
while (keypress != SUBMIT && keypress != CANCEL) {
crystal_clear();
crystal_write("Brightness:", TOP, 0);
string brightness_string = "[";
for (int i = 0; i < 10; i++) {
if (i < new_brightness) brightness_string += "-";
else brightness_string += " ";
}
brightness_string += "]";
crystal_write(brightness_string, BOTTOM, 2);
int keypress = crystal_keystroke();
cerr << "Key pressed: " << keypress << endl;
if (keypress == UP || keypress == RIGHT) {
new_brightness++;
if (new_brightness > 10) new_brightness = 10;
crystal_set_brightness(new_brightness * 5);
}
if (keypress == DOWN || keypress == LEFT) {
new_brightness--;
if (new_brightness < 0) new_brightness = 0;
crystal_set_brightness(new_brightness * 5);
}
cerr << "Key pressed was " << keypress << endl;
if (keypress == SUBMIT) break;
if (keypress == CANCEL) break;
}
cerr << "And now, key pressed was " << keypress << endl;
if (keypress == SUBMIT) brightness = new_brightness;
if (keypress == CANCEL) crystal_set_brightness(brightness * 5);
}
|
A brief overview of what this is supposed to do, if necessary: I am communicating with a "CrystalFontz"-branded LCD control pad (some of you might recognize me from another post regarding another problem with the same project, which has been mostly resolved) and this function is intended to provide the user with a way of changing the brightness of the display's backlight. brightness is a global variable, and new_brightness is initialized here to keep track of the user's potential change (I allow the user to submit the change or cancel, whichever is desired). keypress is declared to monitor the keypresses of the user. SUBMIT, CANCEL, etc. are enums corresponding to the appropriate keycodes back from the device. The while loop is, clearly, expected to execute until either Submit or Cancel is pressed. crystal_clear and crystal_write are just functions I've written to affect the LCD screen, and are working properly. A decent-looking ASCII representation of the brightness level is concocted and sent to the screen. Then it awaits a keystroke from the device and stores the key code in keypress. Then I have a temporary debug statement to tell me which key was pressed. Then, if an arrow key on the device was pressed, the brightness is adjusted accordingly, and then I have another debug output to confirm to me that the keypress variable has not changed.
Everything up to that point works perfectly. Here's where the fun begins. Why do I have those "if....break" instructions there at the very end of the loop, you ask? They are equivalent to the while loop condition, so why are they necessary? Because it never leaves the while loop, otherwise! The reason why is because the keypress variable's value gets sent back to 0 for whatever reason at the end of each iteration, a fact confirmed to me by the third debug statement. That is, the error output looks like this, where 11 is the code the device sends for the "Submit" key having been pressed:
Key pressed: 11
Key pressed was 11
And now, key pressed was 0
|
?!?! I'm giving no instruction to change that variable, the while loop shouldn't be messing with it, I'm not multithreading so nothing else should be messing with it either (in my perception). All I can theorize is that maybe somewhere else in my project, I have some subtly bad code inciting undefined behavior. However, every time I test the program, I get perfectly predictable results, so I'm hoping that it's something less serious than that. Can anyone help me with this or provide insight? It would be much appreciated if so. TIA!