Saturday, 12 January 2013

Arduino display and keyboard

Bought an LCD display and keyboard this week, so had a go at hooking them up this afternoon. The display is connected to an I2C bus and also reads the keys pressed on a keypad. Here is the script that I have used - it is just a quick check that everything works. At the moment I'm not sure what I'll use it for but need to sort out some better cables, mine were a bit cobbled together.

#include <bv4618_I.h>
#include <Wire.h>

BV4618_I di(0x31,9); // 0x62 I2C address (8 bit)

void setup()
{
}

void loop()
{
const char kb[]={0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77};
char tmp;
  // set up display geometry
  di.setdisplay(4,20);
  // set up keyboard scan codes, alter the above constant array
  // this will depend on how the keypad has been wired
  di.setkeycodes(kb);
  // clear screen
  di.cls();
  di.puts("Display Test");
  di.rowcol(2,1);
  di.backlight(1);
  
  // This requires a wire from the INT pin of the BV4618 to pin
  // D9 on the Ardino. The pin is specified in the constructor
//  while(1) {
//    if(!di.keyint()) {
//      tmp=di.key();
//      if(tmp > 9) tmp+='A'-10;
//      else tmp+='0';
//      di.putch(tmp);
//      di.putch(' ');
//    }
//   }
  // This does not require a wire as it poles keys for a value
  // other then 0. di.keys returns the number of keys in the 
  // buffer
  while(1) { // do forever
    if(di.keys()) {
      tmp=di.key();
      // Take the position in the array and convert it into the key that is pressed.
      if(tmp < 3) tmp+=49;
      else if (tmp == 3) tmp+=62;
      else if (tmp < 7) tmp+=48;
      else if (tmp == 7) tmp+=59;
      else if (tmp < 11) tmp+=47;
      else if (tmp == 11) tmp+=56;
      else if (tmp == 12) {
        tmp+=30;
        di.backlight(0);  // Just messing
      }
      else if (tmp == 13) tmp+=35;
      else if (tmp == 14) {    //# Key moves down a line
        tmp+=21;
        di.crdown();
        di.backlight(1);
      }
      else if (tmp == 15) tmp+=53;
      di.putch(tmp);
      di.putch(' ');
      // a delay releases bus - no keys will be lost as the BV4218
      // will capture every keypress for when the cpu comes back
      delay(500); 
    }
   }
}

It's really just a variation of the example file provided.