NXT 2.0 Hanging in this function...
Posted: 02 Nov 2013, 16:51
Hello,
can somebody shed some light on this for me, please? I've done as much banging my head against it as I can - I imagine it's something quite straight forward, embarrassingly...
Sorry for the big, messy function, but somewhere in here, although it complies fine, it causes my NXT 2.0 to hang (batteries out/batteries in again to fix it!)
What I'm trying to do is this - I want a general purpose function that will put a menu of text options on the NXT LCD screen. Because I can't seem to pass an array to a function (I keep getting the error "Math Factor expected"), I'm instead passing a string and then parsing it to build an array inside the function itself. As you can see, however, all the string parsing code is commented out, because it doesn't work... I can't see what's wrong with it, but the array it creates is beyond 5 elements in size (how?!) and always contains only the last text option in every element... Anyway, whilst trying to debug this conundrum, I decided to hard-code the array (strArrOptions[]) with the options I'd be passing in as strOptions. The result was just strPrompt on the screen, as I'd forgotten to set intOptions to be greater than 0; once I did this, however, although the code happily compiles and uploads to the NXT, when I run it, the NXT freezes.
I imagine I've put it into some sort of non-ending loop, but I can't see why
Any thoughts on this would be gratefully received,
Thanks,
monxc_
can somebody shed some light on this for me, please? I've done as much banging my head against it as I can - I imagine it's something quite straight forward, embarrassingly...
Code: Select all
string optMenu(string strPrompt, string strOptions)
{
int intIndex = 0;
int intOptions = 0;
int intOptStart = 0;
int intOptEnd = 0;
int intLineT = 40;
int intLineS = 40;
int intSelected = 0;
int strArrOptions[5]; //never more than five options, so set it here rather than try to ArrayInit it etc...
//create the options array from the string passed in
/* while(intIndex < StrLen(strOptions)){
if(SubStr(strOptions,intIndex,1) == ","){
intOptEnd = intIndex - 1; //define the end of this option
strArrOptions[intOptions] = SubStr(strOptions,intOptStart,intOptEnd - intOptStart);
intOptStart = intIndex + 1; //define the start of the next option (if there is one)
intOptions++; //increment the number of options found
}
intIndex++; //increment the string pointer
}
//add the last option (will be missed by the above loop, as no comma is encountered
strArrOptions[intOptions] = SubStr(strOptions,intOptStart,StrLen(strOptions) - intOptStart;
intOptions++;
*/
strArrOptions[0] = "Turn";
strArrOptions[1] = "Move";
strArrOptions[2] = "Set WPT";
strArrOptions[3] = "Options...";
strArrOptions[4] = "";
intOptions = 4;
while(!ButtonPressed(BTN4,FALSE)){
TextOut(0,56,strPrompt/*,DRAW_OPT_CLEAR_WHOLE_SCREEN*/);
for(intIndex = 0; intIndex < intOptions; intIndex++){
TextOut(8,intLineT,">" + strArrOptions[intIndex]);
intLineT -= 8;
}
intLineT = 40;
}
return strArrOptions[intSelected];
}
What I'm trying to do is this - I want a general purpose function that will put a menu of text options on the NXT LCD screen. Because I can't seem to pass an array to a function (I keep getting the error "Math Factor expected"), I'm instead passing a string and then parsing it to build an array inside the function itself. As you can see, however, all the string parsing code is commented out, because it doesn't work... I can't see what's wrong with it, but the array it creates is beyond 5 elements in size (how?!) and always contains only the last text option in every element... Anyway, whilst trying to debug this conundrum, I decided to hard-code the array (strArrOptions[]) with the options I'd be passing in as strOptions. The result was just strPrompt on the screen, as I'd forgotten to set intOptions to be greater than 0; once I did this, however, although the code happily compiles and uploads to the NXT, when I run it, the NXT freezes.
I imagine I've put it into some sort of non-ending loop, but I can't see why
Any thoughts on this would be gratefully received,
Thanks,
monxc_