libraries / Visuino_LiquidCrystal / Visuino_I2CIO.hon commit Added link to project report (97a3ba0)
   1// ---------------------------------------------------------------------------
   2// Created by Francisco Malpartida on 20/08/11.
   3// Copyright 2011 - Under creative commons license 3.0:
   4//        Attribution-ShareAlike CC BY-SA
   5//
   6// This software is furnished "as is", without technical support, and with no 
   7// warranty, express or implied, as to its usefulness for any purpose.
   8//
   9// Thread Safe: No
  10// Extendable: Yes
  11//
  12// @file I2CIO.h
  13// This file implements a basic IO library using the PCF8574 I2C IO Expander
  14// chip.
  15// 
  16// @brief 
  17// Implement a basic IO library to drive the PCF8574* I2C IO Expander ASIC.
  18// The library implements basic IO general methods to configure IO pin direction
  19// read and write uint8_t operations and basic pin level routines to set or read
  20// a particular IO port.
  21//
  22// @version API 1.0.0
  23//
  24// @author F. Malpartida - fmalpartida@gmail.com
  25// ---------------------------------------------------------------------------
  26
  27#ifndef _I2CIO_H_
  28#define _I2CIO_H_
  29
  30#include <inttypes.h>
  31
  32#define _I2CIO_VERSION "1.0.0"
  33
  34/*!
  35 @class
  36 @abstract    I2CIO
  37 @discussion  Library driver to control PCF8574 based ASICs. Implementing
  38 library calls to set/get port through I2C bus.
  39 */
  40
  41class I2CIO  
  42{
  43public:
  44   /*!
  45    @method     
  46    @abstract   Constructor method
  47    @discussion Class constructor constructor. 
  48    */
  49   I2CIO ( );
  50   
  51   /*!
  52    @method
  53    @abstract   Initializes the device.
  54    @discussion This method initializes the device allocating an I2C address.
  55    This method is the first method that should be call prior to calling any
  56    other method form this class. On initialization all pins are configured
  57    as INPUT on the device.
  58    
  59    @param      i2cAddr: I2C Address where the device is located.
  60    @result     1 if the device was initialized correctly, 0 otherwise
  61    */   
  62   int begin ( uint8_t i2cAddr );
  63   
  64   /*!
  65    @method
  66    @abstract   Sets the mode of a particular pin.
  67    @discussion Sets the mode of a particular pin to INPUT, OUTPUT. digitalWrite
  68    has no effect on pins which are not declared as output.
  69    
  70    @param      pin[in] Pin from the I2C IO expander to be configured. Range 0..7
  71    @param      dir[in] Pin direction (INPUT, OUTPUT).
  72    */   
  73   void pinMode ( uint8_t pin, uint8_t dir );
  74   
  75   /*!
  76    @method
  77    @abstract   Sets all the pins of the device in a particular direction.
  78    @discussion This method sets all the pins of the device in a particular
  79    direction. This method is useful to set all the pins of the device to be
  80    either inputs or outputs.
  81    @param      dir[in] Direction of all the pins of the device (INPUT, OUTPUT).
  82    */
  83   void portMode ( uint8_t dir );
  84   
  85   /*!
  86    @method
  87    @abstract   Reads all the pins of the device that are configured as INPUT.
  88    @discussion Reads from the device the status of the pins that are configured
  89    as INPUT. During initialization all pins are configured as INPUTs by default.
  90    Please refer to pinMode or portMode.
  91    
  92    @param      none
  93    */   
  94   uint8_t read ( void );
  95   
  96   /*!
  97    @method
  98    @abstract   Read a pin from the device.
  99    @discussion Reads a particular pin from the device. To read a particular
 100    pin it has to be configured as INPUT. During initialization all pins are
 101    configured as INPUTs by default. Please refer to pinMode or portMode.
 102    
 103    @param      pin[in] Pin from the port to read its status. Range (0..7)
 104    @result     Returns the pin status (HIGH, LOW) if the pin is configured
 105    as an output, reading its value will always return LOW regardless of its
 106    real state.
 107    */
 108   uint8_t digitalRead ( uint8_t pin );
 109   
 110   /*!
 111    @method
 112    @abstract   Write a value to the device.
 113    @discussion Writes to a set of pins in the device. The value is the binary
 114    representation of all the pins in device. The value written is masked with 
 115    the configuration of the direction of the pins; to change the state of
 116    a particular pin with this method, such pin has to be configured as OUTPUT 
 117    using the portMode or pinMode methods. If no pins have been configured as
 118    OUTPUTs this method will have no effect.
 119    
 120    @param      value[in] value to be written to the device.
 121    @result     1 on success, 0 otherwise
 122    */   
 123   int write ( uint8_t value );
 124   
 125   /*!
 126    @method
 127    @abstract   Writes a digital level to a particular pin.
 128    @discussion Write a level to the indicated pin of the device. For this 
 129    method to have effect, the pin has to be configured as OUTPUT using the
 130    pinMode or portMode methods.
 131    
 132    @param      pin[in] device pin to change level. Range (0..7).
 133    @para       level[in] logic level to set the pin at (HIGH, LOW).
 134    @result     1 on success, 0 otherwise.
 135    */   
 136   int digitalWrite ( uint8_t pin, uint8_t level );
 137   
 138   
 139   
 140private:
 141   uint8_t _shadow;      // Shadow output
 142   uint8_t _dirMask;     // Direction mask
 143   uint8_t _i2cAddr;     // I2C address
 144   bool    _initialised; // Initialised object
 145   
 146};
 147
 148#endif