Projekt med Mindstorm robottens display

Robotten er i stand til at vise en slide tekst som man kan indtaste via et indbygget “keyboard”.
I admin panelet kan man slette og oprette nye slide tekster.

[lyte id=”COFTrCMpQqU” /]

///////////////////////////////////////
///////////// Global Vars /////////////

string Text;
string Letter;
int TextLinePos;
int LibrarySlot;
bool ExitButtonMng_KeyboardMode;
int IsKeyBoardRunning;   // 0 = Keyboard function is not running. 1 = The keyboard functions is running. 2 = menu function is running.
int Slider_TextLength;
int Slider_yCoord;
bool Keyboard_Capslock;

///////////////////////////////////////
//////////// Global Arrays ////////////

string Library[101];
string TextLine[17];

///////////////////////////////////////

int GetSliderTime()
{
	// Calculates the time it takes for the text to reach the end of the screen.
	Slider_TextLength = strlen(Text);   // The "strlen" command checks the length of a string and outputs the result as a number.
	int SliderTime =  105 + (Slider_TextLength * 6);
	return SliderTime;
}

void RanSlider_yCoord()
{
// Randomizes the y coordinate for the sliding text.
// It "retries" every time the result is below 8, as the text will not be fully visible if it's placed at the Y coordinate of "8" or below.
	bool yCoord_Approved = false;

	while (yCoord_Approved == false)
	{

		Slider_yCoord = random(62);  // Random number between the 0 and 62.

		if (Slider_yCoord > 8)
		{
			yCoord_Approved = true;
		}

		wait1Msec(10);
	}

}

void TextSlide()
{
	eraseDisplay();

	// Start the text slide.
	int Slider_TextLoopCounter = 0;
	int Slider_xCoord = 99;
	Slider_yCoord = 32;

	while (true)
	{

		while (Slider_TextLoopCounter < GetSliderTime())
		{
			nxtDisplayStringAt(Slider_xCoord, Slider_yCoord, Text);
			wait1Msec(50);
			Slider_xCoord--;
			Slider_TextLoopCounter++;
		}

		Slider_TextLoopCounter = 0;
		Slider_xCoord = 99;
		RanSlider_yCoord();
	}

}

task ExitButtonMng()
{
// This task "manages" the usability of the grey button on the NXT.
// It is normally used to exit a running program, but you can overwrite that with "nNxtButtonClicks".
	while (true)
	{

		if (ExitButtonMng_KeyboardMode == true)   // If the keyboard is active.
		{
			nNxtExitClicks = 32767;   // Sets the number of clicks required to exit the program. The program will close at the 32767th click on the exit button. That gives me 32767 clicks to use in the program itself.
		}

		if (ExitButtonMng_KeyboardMode == false)   // If the keyboard is inactive.
		{
			nNxtExitClicks = 1;   // Set the number of clicks required to quit the program to "1".
		}

	}

}

void GenerateText()
{
	// This function takes the content from all the positions of the "TextLine" array and trows it into the "Text" string.
	int GenText_Pos = 1;
	int GenText_TextLength;
	Text = "";

	while (GenText_Pos < 17)
	{
		Text = Text + TextLine[GenText_Pos];
		GenText_Pos++;
	}

	if (StringFind(Text, "_"))   // If "_" is found within the "Text" string.
	{
		GenText_TextLength = strlen(Text) + 1;   // Checks the length of "Text" and gives the number to the "GenText_TextLength" integrier. "+1" because "StringDelete" starts at 0 and not 1.
		StringDelete(Text, GenText_TextLength, GenText_TextLength);   // Delete the last character of the "Text" string. Syntax: (String, From Pos, To Pos).
	}

	nxtDisplayCenteredTextLine(2, Text);
}

void ClearTextLine()
{
	// Clears the "TextLine" array.
	int ClearText_Pos = 1;

	while (ClearText_Pos < 17)
	{
		TextLine[ClearText_Pos] = "";
		ClearText_Pos++;
	}

}

void Keyboard_ResetText()
{
	TextLinePos = 1;
	Text = "";
	ClearTextLine();   // Calls the "clearTextLine" function.
}

task Keyboard_ShowLetterChoice()
{

	while (IsKeyBoardRunning == true)
	{
		Letter = Library[LibrarySlot];
		nxtDisplayCenteredTextLine(4, Letter);
		wait1Msec(50);
	}

}

task Keyboard_LibraryLoop()
{

	while (true)
	{

		if (nNxtButtonPressed == 2)   // If "left arrow" button has been pressed.
		{

			if ((Keyboard_Capslock == true) && (LibrarySlot == 0))
			{
				LibrarySlot = 49;
			}

			if ((Keyboard_Capslock == false) && (LibrarySlot == 50))
			{
				LibrarySlot = 99;
			}

		}

		if (nNxtButtonPressed == 1)   // If "right arrow" button has been pressed.
		{

			if ((Keyboard_Capslock == true) && (LibrarySlot == 50))
			{
				LibrarySlot = 1;
			}

			if ((Keyboard_Capslock == false) && (LibrarySlot == 100))
			{
				LibrarySlot = 51;
			}

		}

		wait1Msec(100);
	}

}

void Keyboard(bool FirstStartup)
{
	IsKeyBoardRunning = 1;

	if (FirstStartup == true)
	{
		Keyboard_ResetText();
		LibrarySlot = 1;
		Keyboard_Capslock = true;
	}

	// "Booting sequence"...
	eraseDisplay();
	nxtDisplayCenteredTextLine(2, Text);
	StartTask(Keyboard_ShowLetterChoice);
	StartTask(Keyboard_LibraryLoop);
	ExitButtonMng_KeyboardMode = true;
	wait1Msec(500);

	// The configuration of the buttons.
	while (IsKeyBoardRunning == 1)
	{

		if (nNxtButtonPressed == 2)   // If "left arrow" button has been pressed.
		{

			if ((Keyboard_Capslock == true) && (LibrarySlot >= 0))
			{
				LibrarySlot--;
				wait1Msec(200);
			}

			if ((Keyboard_Capslock == false) && (LibrarySlot >= 0))
			{
				LibrarySlot--;
				wait1Msec(200);
			}

		}

		if (nNxtButtonPressed == 1)   // If "right arrow" button has been pressed.
		{

			if ((Keyboard_Capslock == true) && (LibrarySlot <= 49))
			{
				LibrarySlot++;
				wait1Msec(200);
			}

			if ((Keyboard_Capslock == false) && (LibrarySlot <= 99))
			{
				LibrarySlot++;
				wait1Msec(200);
			}

		}

		if (nNxtButtonPressed == 0) // If "grey" button has been pressed.
		{

			if ((TextLinePos <= 17) && (TextLinePos >= 1))
			{
				TextLinePos--;
				TextLine[TextLinePos] = "";
				GenerateText();   // Goes through the "TextLine" array and trows it into the "Text" string.
				Text = Text + "_";
				nxtDisplayCenteredTextLine(2, Text);
				wait1Msec(200);
			}

		}

		if (nNxtButtonPressed == 3)   //If "orange" button has been pressed.
		{

			if ((LibrarySlot == 49) || (LibrarySlot == 99))   // If "[Continue]" is marked.
			{

				if (TextLinePos >= 2)
				{
					wait1Msec(100);
					break;
				}

			}
			else
			{

				switch (LibrarySlot)
				{

				case 46:   // If "[Quit]" is marked. (Caps ON).

					StopAllTasks();   // Stops the program.

				case 96:   // If "[Quit]" is marked. (Caps OFF).

					StopAllTasks();   // Stops the program.

				case 47:   // If "[Clear]" is marked. (Caps ON).

					Keyboard_ResetText();
					nxtDisplayCenteredTextLine(2, Text);
					break;

				case 97:   // If "[Clear]" is marked. (Caps OFF).

					Keyboard_ResetText();
					nxtDisplayCenteredTextLine(2, Text);
					break;

				case 48:   // If "[CapsLock]" is marked. (Caps ON).

					if (Keyboard_Capslock == true)
					{
						Keyboard_Capslock = false;
						LibrarySlot = LibrarySlot + 50;
						wait1Msec(10);
					}

					break;

				case 98:   // If "[CapsLock]" is marked. (Caps OFF).

					if (Keyboard_Capslock == false)
					{
						Keyboard_Capslock = true;
						LibrarySlot = LibrarySlot - 50;
						wait1Msec(10);
					}

					break;

				default:   // If anything else is marked.

					if (TextLinePos <= 16)
					{
						// Pick and print chosen letter.
						TextLine[TextLinePos] = Letter;
						GenerateText();
						TextLinePos++;
					}

					break;
				}

			}

			wait1Msec(200);
		}

		wait1Msec(50);
	}

	StopTask(Keyboard_ShowLetterChoice);
	StopTask(Keyboard_LibraryLoop);
	ExitButtonMng_KeyboardMode = false;
	IsKeyBoardRunning = 0;
}

void Menu()
{
	IsKeyBoardRunning = 2;
	eraseDisplay();
	wait1Msec(100);

	// Draw UI for "Use text?" page.
	nxtFillRect(0, 62, 99, 52);  // Draws a solid rectangle above the user-created text.
	GenerateText();   // "Refreshes" the "Text" string to remove "_" from deleting text.
	nxtDisplayCenteredTextLine(2, Text);   // Shows the text written with the keyboard.
	nxtFillRect(0, 36, 99, 25);  // Draws a solid rectangle below the user-created text.
	nxtDisplayCenteredTextLine(5, "Use this text?");

	nxtDisplayStringAt(64, 10, "Deny");   // Prints the text at (X, Y).
	nxtDisplayStringAt(9, 10, "Accept");
	nxtDrawLine(0, 14, 99, 14);   // Draws the UI horizontal line for the top of the button layout. (Syntax: From [x,y] to [x,y])
	nxtDrawLine(50, 14, 50, 0);   // Draws the UI vertical line for the button layout.
	nxtDrawRect(0, 0, 99, 63);   // Draws the UI full screen frame of the display.

	while (true)   // Setting a while loop to "true" will make it run forever.
	{
		// The configuration of the buttons.
		if (nNxtButtonPressed == 2)   // If "left arrow" button has been pressed.
		{
			TextSlide();
			break;   // The "break" command "breaks out" of the current loop.
		}

		if (nNxtButtonPressed == 1)   // If "right arrow" button has been pressed.
		{
			wait1Msec(50);
			Keyboard(false);
			break;
		}

		wait1Msec(50);
	}

}

task main()
{
	// Configure keyboard "Library" array.
	// "Library[0]" is being used to jump to "Library[49]" if CapsLock is on.
	Library[1] = "A";
	Library[2] = "B";
	Library[3] = "C";
	Library[4] = "D";
	Library[5] = "E";
	Library[6] = "F";
	Library[7] = "G";
	Library[8] = "H";
	Library[9] = "I";
	Library[10] = "J";
	Library[11] = "K";
	Library[12] = "L";
	Library[13] = "M";
	Library[14] = "N";
	Library[15] = "O";
	Library[16] = "P";
	Library[17] = "Q";
	Library[18] = "R";
	Library[19] = "S";
	Library[20] = "T";
	Library[21] = "U";
	Library[22] = "V";
	Library[23] = "W";
	Library[24] = "X";
	Library[25] = "Y";
	Library[26] = "Z";
	Library[27] = " ";
	Library[28] = "0";
	Library[29] = "1";
	Library[30] = "2";
	Library[31] = "3";
	Library[32] = "4";
	Library[33] = "5";
	Library[34] = "6";
	Library[35] = "7";
	Library[36] = "8";
	Library[37] = "9";
	Library[38] = ".";
	Library[39] = "-";
	Library[40] = "=";
	Library[41] = ":";
	Library[42] = "/";
	Library[43] = "!";
	Library[44] = "?";
	Library[45] = "&";
	Library[46] = "[Quit]";
	Library[47] = "[Clear]";
	Library[48] = "[CapsLock]";
	Library[49] = "[Continue]";
	// "Library[50]" is being used to jump to "Library[1]" if CapsLock is on.
	// "Library[50]" is being used to jump to "Library[99]" if CapsLock is off.
	Library[51] = "a";
	Library[52] = "b";
	Library[53] = "c";
	Library[54] = "d";
	Library[55] = "e";
	Library[56] = "f";
	Library[57] = "g";
	Library[58] = "h";
	Library[59] = "i";
	Library[60] = "j";
	Library[61] = "k";
	Library[62] = "l";
	Library[63] = "m";
	Library[64] = "n";
	Library[65] = "o";
	Library[66] = "p";
	Library[67] = "q";
	Library[68] = "r";
	Library[69] = "s";
	Library[70] = "t";
	Library[71] = "u";
	Library[72] = "v";
	Library[73] = "w";
	Library[74] = "x";
	Library[75] = "y";
	Library[76] = "z";
	Library[77] = " ";
	Library[78] = "0";
	Library[79] = "1";
	Library[80] = "2";
	Library[81] = "3";
	Library[82] = "4";
	Library[83] = "5";
	Library[84] = "6";
	Library[85] = "7";
	Library[86] = "8";
	Library[87] = "9";
	Library[88] = ".";
	Library[89] = "-";
	Library[90] = "=";
	Library[91] = ":";
	Library[92] = "/";
	Library[93] = "!";
	Library[94] = "?";
	Library[95] = "&";
	Library[96] = "[Quit]";
	Library[97] = "[Clear]";
	Library[98] = "[CapsLock]";
	Library[99] = "[Continue]";
	// "Library[100]" is being used to jump to "Library[51]" if CapsLock is off.

	StartTask(ExitButtonMng);
	Keyboard(true);

// Checks if the keyboard function is running. It will start the "menu" function if the keyboard function stops.
	while (true)
	{

		if (IsKeyBoardRunning == 0)
		{
			Menu();
		}

		wait1Msec(100);
	}

}

Skriv et svar