Skip to content

Commit

Permalink
Ensure consistent behaviour of MicroBitDisplay::print() operations
Browse files Browse the repository at this point in the history
  - Simplify behaviour of print() and printAsync() methods.
  - print(char) print(ManagedString) and print(MicroBitImage) now all have same semantics:

    - If provided, the delay parameter is always respected. Screen is cleared after that time.

    - discrete print operations (character, string of length 1 and MicroBitImage) with no
      delay parameter are printed, and return immeditely. The screen is not cleared.

    - print (string of length > 1) with no delay parameter defaults to a delay
      of MICROBIT_DEFAULT_PRINT_SPEED milliseconds. The screen is cleared on
      completion.
  • Loading branch information
finneyj committed Apr 21, 2020
1 parent 0fd4a80 commit 3bccdd6
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 16 deletions.
44 changes: 41 additions & 3 deletions inc/drivers/MicroBitDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,33 @@ class MicroBitDisplay : public MicroBitComponent
* @param s The string to display.
*
* @param delay The time to delay between characters, in milliseconds. Must be > 0.
* Defaults to: MICROBIT_DEFAULT_PRINT_SPEED.
*
* @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
*
* @code
* display.printAsync("abc123",400);
* @endcode
*/
int printAsync(ManagedString s, int delay = MICROBIT_DEFAULT_PRINT_SPEED);
int printAsync(ManagedString s, int delay);

/**
* Prints the given ManagedString to the display, one character at a time.
* Returns immediately, and executes the animation asynchronously.
*
* If the string is greater than one charcter in length, the screen
* will be cleared after MICROBIT_DEFAULT_PRINT_SPEED milliseconds.
* Otherwise, that character will be left on the screen indefinitely.
*
* @param s The string to display.
*
* @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
*
* @code
* display.printAsync("abc123");
* @endcode
*/
int printAsync(ManagedString s);


/**
* Prints the given image to the display, if the display is not in use.
Expand Down Expand Up @@ -352,7 +370,27 @@ class MicroBitDisplay : public MicroBitComponent
* display.print("abc123",400);
* @endcode
*/
int print(ManagedString s, int delay = MICROBIT_DEFAULT_PRINT_SPEED);
int print(ManagedString s, int delay);

/**
* Prints the given string to the display, one character at a time.
*
* Blocks the calling thread until all the text has been displayed.
*
* If the string is greater than one charcter in length, the screen
* will be cleared after MICROBIT_DEFAULT_PRINT_SPEED milliseconds.
* Otherwise, that character will be left on the screen indefinitely.
*
* @param s The string to display.
*
* @return MICROBIT_OK, MICROBIT_CANCELLED or MICROBIT_INVALID_PARAMETER.
*
* @code
* display.print("abc123");
* @endcode
*/
int print(ManagedString s);


/**
* Prints the given image to the display.
Expand Down
78 changes: 65 additions & 13 deletions source/drivers/MicroBitDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ MicroBitDisplay::animationUpdate()

if(animationMode == ANIMATION_MODE_PRINT_CHARACTER)
{
image.print(' ');
animationMode = ANIMATION_MODE_NONE;
this->sendAnimationCompleteEvent();
}
Expand Down Expand Up @@ -520,13 +521,13 @@ int MicroBitDisplay::printCharAsync(char c, int delay)
*/
int MicroBitDisplay::printAsync(ManagedString s, int delay)
{
if (s.length() == 1)
return printCharAsync(s.charAt(0));

//sanitise this value
if (delay <= 0 )
if (delay < 0 || (delay == 0 && s.length() > 1))
return MICROBIT_INVALID_PARAMETER;

if (s.length() == 1)
return printCharAsync(s.charAt(0), delay);

if (animationMode == ANIMATION_MODE_NONE || animationMode == ANIMATION_MODE_STOPPED)
{
printingChar = 0;
Expand All @@ -544,6 +545,33 @@ int MicroBitDisplay::printAsync(ManagedString s, int delay)
return MICROBIT_OK;
}

/**
* Prints the given ManagedString to the display, one character at a time.
* Returns immediately, and executes the animation asynchronously.
*
* If the string is greater than one charcter in length, the screen
* will be cleared after MICROBIT_DEFAULT_PRINT_SPEED milliseconds.
* Otherwise, that character will be left on the screen indefinitely.
*
* @param s The string to display.
*
* @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
*
* @code
* display.printAsync("abc123");
* @endcode
*/
int MicroBitDisplay::printAsync(ManagedString s)
{
int delay = MICROBIT_DEFAULT_PRINT_SPEED;

if(s.length() == 1)
delay = 0;

return printAsync(s, delay);
}


/**
* Prints the given image to the display, if the display is not in use.
* Returns immediately, and executes the animation asynchronously.
Expand Down Expand Up @@ -646,7 +674,7 @@ int MicroBitDisplay::printChar(char c, int delay)
int MicroBitDisplay::print(ManagedString s, int delay)
{
//sanitise this value
if(delay <= 0 )
if(delay < 0 || (delay == 0 && s.length() > 1))
return MICROBIT_INVALID_PARAMETER;

// If there's an ongoing animation, wait for our turn to display.
Expand All @@ -656,15 +684,11 @@ int MicroBitDisplay::print(ManagedString s, int delay)
// If someone called stopAnimation(), then we simply skip...
if (animationMode == ANIMATION_MODE_NONE)
{
int ret = this->printAsync(s, delay);
if (s.length() == 1)
{
return printCharAsync(s.charAt(0));
}
else
{
this->printAsync(s, delay);
fiberWait();
}
return ret;

fiberWait();
}
else
{
Expand All @@ -674,6 +698,34 @@ int MicroBitDisplay::print(ManagedString s, int delay)
return MICROBIT_OK;
}

/**
* Prints the given string to the display, one character at a time.
*
* Blocks the calling thread until all the text has been displayed.
*
* If the string is greater than one charcter in length, the screen
* will be cleared after MICROBIT_DEFAULT_PRINT_SPEED milliseconds.
* Otherwise, that character will be left on the screen indefinitely.
*
* @param s The string to display.
*
* @return MICROBIT_OK, MICROBIT_CANCELLED or MICROBIT_INVALID_PARAMETER.
*
* @code
* display.print("abc123");
* @endcode
*/
int MicroBitDisplay::print(ManagedString s)
{
int delay = MICROBIT_DEFAULT_PRINT_SPEED;

if(s.length() == 1)
delay = 0;

return print(s, delay);
}


/**
* Prints the given image to the display.
* Blocks the calling thread until all the image has been displayed.
Expand Down

0 comments on commit 3bccdd6

Please sign in to comment.