Libraries and Example of mbed parallel bus using I2C port expanders

Dependencies:   HDSP253X mbed PCF8574_Bus

Keyboard.cpp

Committer:
wim
Date:
2011-08-26
Revision:
5:38b853bb1afa
Parent:
4:745fbbd5e4e5

File content as of revision 5:38b853bb1afa:

/* Keyboard - Keyboard and Fire Switch control
 * Copyright (c) 2011 Wim Huiskamp
 *
 * Released under the MIT License: http://mbed.org/license/mit
 *
 * version 0.2 Initial Release
*/
#include "mbed.h"
#include "PCF8574_DataBus.h"
#include "PCF8574_EnableBus.h"
#include "MBED_ControlBus.h"
#include "Keyboard.h"


/** Create a Keyboard object connected to the specified buses
 *
 * @param  PCF8574_DataBus databus to connect to 
 * @param  PCF8574_EnableBus enable enablebus to connect to 
 * @param  MBED_ControlBus control controlbus to connect to 
*/
Keyboard::Keyboard (PCF8574_DataBus &databus, PCF8574_EnableBus &enablebus, MBED_ControlBus &controlbus) : 
                                     _databus(databus), _enablebus(enablebus), _controlbus(controlbus) {
   _init();
}    

/** Init Keyboard
 * @param
 * @returns 
 */
void Keyboard::_init(void)
{
   _KeyReady = false;  
   _KeyCode = KEY_NONE;        
}


/** Check Keypressed or Fire switch pressed
 *
 * @param none
 * @returns bool keypressed 
*/
bool Keyboard::readable () {
  uint8_t data = 0;

  // Init to No keypressed
  _KeyReady = false;
  
  if (_controlbus.CDINT() == HIGH) {
    // CDA Key pressed  
    _KeyReady = true;
        
    // Get the switchvalue and decode it
    _KeyCode = getswitch();
  }

  if (_controlbus.FIRE() == HIGH) {
    // Manual Fire Key pressed  
    _KeyReady = true;
        
    _KeyCode = KEY_FIRE;
  }

  return _KeyReady;
}


/** Read keycode
 *
 * @param none
 * @returns keycode 
*/
Key_Code Keyboard::getkey() {
  
  if (_KeyReady) {
    _KeyReady = false;  
    return _KeyCode;  
  }
  else {
    return KEY_NONE;
  }
}


/** Get keycode for pressed switch
 *
 * @param none
 * @returns keycode 
*/
Key_Code Keyboard::getswitch() {
  uint8_t data = 0;
  Key_Code code;

  // Get the switchvalue and decode it
  data = _read() & D_KEYBOARD_MASK;       
  switch (data) {
    case D_KEYBOARD_MODE:      code = KEY_MODE;
                               break;
    case D_KEYBOARD_EDIT_PATH: code = KEY_EDIT_PATH;
                               break;
    case D_KEYBOARD_BRIGHT:    code = KEY_BRIGHT;
                               break;                               
    case D_KEYBOARD_GRAT_RT:   code = KEY_GRAT_RT;
                               break;
    case D_KEYBOARD_F_L_UP:    code = KEY_F_L_UP;
                               break;
    default:                   code = KEY_NONE;
                               break;                               
  }
   
  return code;
}


/** Read switch value
 *
 * @param none
 * @returns switch register value 
*/
uint8_t Keyboard::_read() {
    uint8_t data = 0;

    // Switch databus to inputs
    _databus.busdir(READ);        
    // Switch databus buffer to inputs
    _controlbus.busdir(READ);      
    
    
    // Set CE low and wait
    _enablebus.chipselect(CS_SWITCH, LOW);
    wait_ms(1.0);

    // Set RD low and wait
    _controlbus.RD(LOW);
    wait_ms(1.0);
    
    // Read the data byte from databus
    data = _databus.read();

    // set RD high and wait
    _controlbus.RD(HIGH);
    wait_ms(1.0);

    // Set CE high and wait
    _enablebus.chipselect(CS_SWITCH, HIGH);
    wait_ms(1.0);

    // Switch databus buffer back to outputs
    _controlbus.busdir(WRITE);        
    // Switch databus to outputs
    _databus.busdir(WRITE);   
    
    // Return read data to caller
    return data;
}