Library for MicroVGA adapter

Posted by Nick Gammon on Tue 17 Apr 2012 03:39 AM — 1 posts, 12,015 views.

Australia Forum Administrator #0
I recently got a MicroVGA output device for the Arduino:



From:

http://www.microvga.com/

Using SPI you can send text to a normal VGA monitor (most of us will have one of them lying around).

Example output ...



To help use it I wrote a small library. First, to connect:

Wiring


uVGA
Pin
1 GND     Arduino GND
2 +5V     Arduino +5V
3 +3V3 output NOT CONNECTED
4 /SS     Arduino Digital 10
5 SCK     Arduino Digital 13
6 /RDY    Arduino Digital 9
7 MISO    Arduino Digital 12
8 MOSI    Arduino Digital 11 


Switch to SPI mode


Plug in a PS2 keyboard (it only goes into one socket), power up the device, and short the "setup" pad (on the edge near the keyboard socket, see photo) with a screwdriver. It should enter "setup" mode.

  • Select "Communication -> SPI Mode"
  • Hit Enter
  • (Note, if you go back in 1000000 baud is still selected, that is the default, not the current mode)
  • Select "Save settings"
  • Wait for confirmation
  • Power device off (unplug Arduino from power)


Library


Download from:

http://gammon.com.au/Arduino/uVGA.zip

Unzip contents into your "libraries" folder.


Sketch showing output



#include <SPI.h>
#include "uvga.h"

uVGA uvga;

void setup ()
  {
  uvga.begin ();
  uvga.clrscr ();
  uvga.println ("uVGA test.");
  }  // end of setup
 
void loop ()
  {
  for (int color = BLACK; color <= WHITE; color++)
    {
    uvga.textcolor (color);
    uvga.print ("Color: ");
    uvga.println (color);
    uvga.println (micros ());
    delay (1000);
    }
  }  // end of loop


The class is based on the Stream class which lets you do the usual things you can with stuff like Serial. That is print, println, test if input is available, and so on.

You can also call these functions to do useful things with the screen like clearing it, changing colours and so on:

  • clrscr
  • clreol
  • cursoron
  • cursoroff
  • textcolor
  • textbackground
  • textattr
  • gotoxy



Sketch showing input


// Demonstrates text input

#include <SPI.h>
#include "uvga.h"

uVGA uvga;

void setup ()
  {
  uvga.begin ();
  uvga.clrscr ();
  uvga.println ("uVGA input test.");
  }  // end of setup
 
 // callback handler for function keys 
boolean fkey (const int key)
  {
  uvga.print ("Function key: ");
  uvga.print (key, HEX);
  uvga.println (" pressed.");
  if (key == KB_ESC)
    return true;
  return false;
  }

// get user input  
void loop ()
  {
  char buf [20];

  uvga.textcolor (WHITE);
  uvga.print ("Enter something ... ");
  if (uvga.getline (buf, sizeof buf, fkey))
    uvga.println ("Cancelled");
  uvga.textcolor (RED);
  uvga.print ("You entered: ");
  uvga.println (buf);
  uvga.println ();
  }  // end of loop


The getline function lets you query for input. This is a blocking call. If you want to do it non-blocking just use uvga.available() and put things into a buffer (which is what getline does).

You can optionally supply a callback function to handle things like F3 being pressed, or the ESC key.

Sketch showing cursor positioning



// gotoxy test

#include <SPI.h>
#include "uvga.h"

uVGA uvga;

void setup ()
  {
  uvga.begin ();
  }  // end of setup
 
void loop ()
  {
  uvga.clrscr ();
  uvga.cursoroff ();
  for (int x = 1; x < 80; x += 10)
    {
    uvga.textcolor (x / 10 + 8);  
    if (x == 1)
      uvga.textcolor (GREEN);
    for (int y = 1; y <= 24; y++)
      {
      uvga.gotoxy (x, y);
      uvga.print (x);
      uvga.print (",");
      uvga.print (y);
      }
    }
  delay (5000);
  }  // end of loop


The above sketch demonstrates the gotoxy function. With that you can move the cursor around on the screen.