Thursday, February 21, 2013

Right-Alignment Demo with Arduino and GLO-416Y

Most of Seetron's serial displays have a right-alignment feature (ctrl-R) that makes it easy to create and update text fields with minimal programming. You can write code to duplicate this feature by padding and formatting strings, but why bother?


Using the right-alignment feature requires these simple steps:
  • Position the cursor at the end of the text field. 
  • Send the right-align instruction followed by a single text digit ('2'-'9') specifying the width of the text field. 
  • Send the string (usually a number, but text works, too) to be right-aligned. 
In code (using the Arduino Streaming library for C++ brevity) that looks like this:

mySerial <<MOVETO <<END <<RTALIGN <<'5' <<printMe;

The right-alignment feature clears the text field (5 characters wide in the example above) by padding the incoming string with leading spaces. In order for this to work, the display must be able to figure out the actual length of the string, which it determines by accumulating incoming characters until
  • The number of characters equals the specified width. 
  • A non-printing character (e.g., control code) is received. 
  • The '.' (decimal point) character is received. 
That last condition allows for lining up columns of values on the decimal point, tidy as a spreadsheet. Arduino example code for the GLO-416Y

/*
Demonstrating right-alignment instruction with 
GLO-416Y serial OLED module. 
seetrontech.blogspot.com
seetron.com
 */

// SoftwareSerial lets us use any I/O pin for serial output 
// to the BPP-440L, saving the hardware UART for the bootloader  
#include <SoftwareSerial.h>  // or other critical comms.

// Streaming allows items for serial output to be grouped
#include <Streaming.h>  // logically on a single line

#define rxPin 255       // Not used, so set to invalid pin #
#define txPin 3         // Hook SER input to Arduino pin 3.
#define inverted 1      // Inverted serial: like RS-232 w/TTL levels
#define noninverted 0  // Noninverted serial (like UART output)

// Change the definition of SPOL to inverted or noninverted 
// depending on your display configuration. On GLO-416Ys, 
// if the SPOL jumper is intact (factory setting), you want 
// "inverted." If it's cut, use "noninverted." 
#define SPOL inverted

// For inverted serial, the output pin must be LOW when data is 
// not being sent; for noninverted, HIGH. 
#if SPOL
#define STOPBIT LOW
#else
#define STOPBIT HIGH
#endif

//=====================================================================
//          CONSTANTS FOR OLED INSTRUCTIONS
//
// Rather than scatter a bunch of magic numbers throughout the 
// program, I've defined some self-explanatory names for the 
// small subset of GLO-416Y instructions. 
#define HOME (char) 1           // Moveto character position 0.
#define FLARGER (char) 2        // Increase font size.
#define FRESET (char) 3        // Reset font to default/small. 
#define CLS (char) 12          // Clear screen, moveto 0.
#define MOVETO (char) 16       // Position instruction
#define END (char) (16 + 64) // Position of end of data field
#define RTALIGN (char) 18      //Ctrl-R right-align instruction

//=====================================================================
// Set up a new serial output using the definitions above. 
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin, SPOL);

// Create the string object that will be used for our output data.
String printMe = "" ;  

//=====================================================================
// SETUP: 
//=====================================================================
// Put the serial-output pin in the stop-bit state and 
// set the bit rate to 9600bps. Then print the banner and the 
// fixed label. 

void setup()  {
  delay(500) ;
// define pin modes for tx, rx:
  digitalWrite(txPin, STOPBIT);  // Preset pin to stop-bit
  mySerial.begin(9600);          // Set the data rate.
  delay(10) ;                    // wait.

// Print 'demo' banner and label.
  mySerial <<CLS <<FRESET ;
  mySerial <<"Right-align demo";
  mySerial <<"----------------" ;
  mySerial <<FLARGER <<FLARGER <<FLARGER <<"?=";
}  //end of setup

//=====================================================================
// LOOP: 
//=====================================================================
// The program loop prints random numbers right-aligned in big
// (2-line tall) characters at bottom-right of the screen. 

void loop() {
  delay(250) ;                      // Slow so text is legible. 
  printMe = String(randigits(5));  // Get a 5-digit random #. 
  
// The next line is the whole procedure for printing right-
// aligned text to a fixed-width field on the screen:  
// Move to the position of the _end_ of the field, send the 
// right-align instruction followed by a char-number for the 
// width of the field, then send the text (in this case our 
// random-number string). The right-alignment is triggered when: 
//   (1) The specified # of characters is received. 
//   (2) A dot '.' (decimal point) is received. 
//   (3) Or a control character is received. 
// In this case, the trigger is usually the control character
// <<MOVETO from the _next_ iteration of the loop. 

  mySerial <<MOVETO <<END <<RTALIGN <<'5' <<printMe;  
}

//=====================================================================
// FUNCTION: randigits
//=====================================================================
// This function returns a random number
// with a random # of digits, up to the # of 
// digits specified. This is to ensure that a
// short run of the demo program will show all 
// possible lengths of of values, so that you can  
// see that the program updates the screen   
// smoothly and erases previous values. 
unsigned int randigits(int numdigs) {
  numdigs = random(numdigs) ;
 switch (numdigs) {
   case 0:
     return random(10) ;
     break ;
    case 1:
     return random(100) ;
     break ;
    case 2:
     return random(1000) ;
     break ;
    case 3:
     return random(10000) ;
     break ;
    case 4:
     return random(65535) ;
     break ;
    default:
      return 42 ;
  } 
}


Arduino-compatible serial OLED: $49 at seetron.

5 comments:

  1. Nice. Are you doing any promo codes for this display still?

    ReplyDelete
    Replies
    1. Better--$10 price cut, no promo code required.

      Delete
    2. Ahh, very good, thank you. I've been debating this one over another brand which is a little less expensive, but this one seems to integrate a whole lot easier with Arduino and take up less resources. If the physical dimensions work for my project, I'm sure I'll be ordering one of these soon!

      Delete
    3. Thanks for the kind words--it's a great display. Since you're on the fence, I've set up promo code NUDGE that will take another 10% off the purchase of the GLO-416Y. It's only good for a few days...

      Delete
  2. You're killing me! Haha. I'll measure it up and make the decision. I appreciate it.

    ReplyDelete