Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
marcmerlin committed Jan 23, 2012
0 parents commit 8e72ab0
Show file tree
Hide file tree
Showing 129 changed files with 16,737 additions and 0 deletions.
198 changes: 198 additions & 0 deletions I2CIO.cpp
@@ -0,0 +1,198 @@
// ---------------------------------------------------------------------------
// Created by Francisco Malpartida on 20/08/11.
// Copyright 2011 - Under creative commons license 3.0:
// Attribution-ShareAlike CC BY-SA
//
// This software is furnished "as is", without technical support, and with no
// warranty, express or implied, as to its usefulness for any purpose.
//
// Thread Safe: No
// Extendable: Yes
//
// @file I2CIO.h
// This file implements a basic IO library using the PCF8574 I2C IO Expander
// chip.
//
// @brief
// Implement a basic IO library to drive the PCF8574* I2C IO Expander ASIC.
// The library implements basic IO general methods to configure IO pin direction
// read and write uint8_t operations and basic pin level routines to set or read
// a particular IO port.
//
// This library is only compatible with Arduino's SDK version 1.0
//
// @version API 1.0.0
//
// @author F. Malpartida - fmalpartida@gmail.com
// ---------------------------------------------------------------------------
#if (ARDUINO < 100)
#include <WProgram.h>
#else
#include <Arduino.h>
#endif

#include <inttypes.h>
#include <../Wire/Wire.h>
#include <I2CIO.h>

// CLASS VARIABLES
// ---------------------------------------------------------------------------


// CONSTRUCTOR
// ---------------------------------------------------------------------------
I2CIO::I2CIO ( )
{
_i2cAddr = 0x0;
_dirMask = 0xFF; // mark all as INPUTs
_shadow = 0x0; // no values set
_initialised = false;
}

// PUBLIC METHODS
// ---------------------------------------------------------------------------

//
// begin
int I2CIO::begin ( uint8_t i2cAddr )
{
_i2cAddr = i2cAddr;

Wire.begin ( );

_initialised = Wire.requestFrom ( _i2cAddr, (uint8_t)1 );

#if (ARDUINO < 100)
_shadow = Wire.receive ();
#else
_shadow = Wire.read (); // Remove the byte read don't need it.
#endif

return ( _initialised );
}

//
// pinMode
void I2CIO::pinMode ( uint8_t pin, uint8_t dir )
{
if ( _initialised )
{
if ( OUTPUT == dir )
{
_dirMask &= ~( 1 << pin );
}
else
{
_dirMask |= ( 1 << pin );
}
}
}

//
// portMode
void I2CIO::portMode ( uint8_t dir )
{

if ( _initialised )
{
if ( dir == INPUT )
{
_dirMask = 0xFF;
}
else
{
_dirMask = 0x00;
}
}
}

//
// read
uint8_t I2CIO::read ( void )
{
uint8_t retVal = 0;

if ( _initialised )
{
Wire.requestFrom ( _i2cAddr, (uint8_t)1 );
#if (ARDUINO < 100)
retVal = ( _dirMask & Wire.receive ( ) );
#else
retVal = ( _dirMask & Wire.read ( ) );
#endif

}
return ( retVal );
}

//
// write
int I2CIO::write ( uint8_t value )
{
int status = 0;

if ( _initialised )
{
// Only write HIGH the values of the ports that have been initialised as
// outputs updating the output shadow of the device
_shadow = ( value & ~(_dirMask) );

Wire.beginTransmission ( _i2cAddr );
#if (ARDUINO < 100)
Wire.send ( _shadow );
#else
Wire.write ( _shadow );
#endif
status = Wire.endTransmission ();
}
return ( (status == 0) );
}

//
// digitalRead
uint8_t I2CIO::digitalRead ( uint8_t pin )
{
uint8_t pinVal = 0;

// Check if initialised and that the pin is within range of the device
// -------------------------------------------------------------------
if ( ( _initialised ) && ( pin <= 7 ) )
{
// Remove the values which are not inputs and get the value of the pin
pinVal = this->read() & _dirMask;
pinVal = ( pinVal >> pin ) & 0x01; // Get the pin value
}
return (pinVal);
}

//
// digitalWrite
int I2CIO::digitalWrite ( uint8_t pin, uint8_t level )
{
uint8_t writeVal;
int status = 0;

// Check if initialised and that the pin is within range of the device
// -------------------------------------------------------------------
if ( ( _initialised ) && ( pin <= 7 ) )
{
// Only write to HIGH the port if the port has been configured as
// an OUTPUT pin. Add the new state of the pin to the shadow
writeVal = ( 1 << pin ) & ~_dirMask;
if ( level == HIGH )
{
_shadow |= writeVal;

}
else
{
_shadow &= ~writeVal;
}
status = this->write ( _shadow );
}
return ( status );
}

//
// PRIVATE METHODS
// ---------------------------------------------------------------------------
150 changes: 150 additions & 0 deletions I2CIO.h
@@ -0,0 +1,150 @@
// ---------------------------------------------------------------------------
// Created by Francisco Malpartida on 20/08/11.
// Copyright 2011 - Under creative commons license 3.0:
// Attribution-ShareAlike CC BY-SA
//
// This software is furnished "as is", without technical support, and with no
// warranty, express or implied, as to its usefulness for any purpose.
//
// Thread Safe: No
// Extendable: Yes
//
// @file I2CIO.h
// This file implements a basic IO library using the PCF8574 I2C IO Expander
// chip.
//
// @brief
// Implement a basic IO library to drive the PCF8574* I2C IO Expander ASIC.
// The library implements basic IO general methods to configure IO pin direction
// read and write uint8_t operations and basic pin level routines to set or read
// a particular IO port.
//
// This library is only compatible with Arduino's SDK version 1.0
//
// @version API 1.0.0
//
// @author F. Malpartida - fmalpartida@gmail.com
// ---------------------------------------------------------------------------

#ifndef _I2CIO_H_
#define _I2CIO_H_

#include <inttypes.h>

#define _I2CIO_VERSION "1.0.0"

/*!
@class
@abstract I2CIO
@discussion Library driver to control PCF8574 based ASICs. Implementing
library calls to set/get port through I2C bus.
*/

class I2CIO
{
public:
/*!
@method
@abstract Constructor method
@discussion Class constructor constructor.
*/
I2CIO ( );

/*!
@method
@abstract Initializes the device.
@discussion This method initializes the device allocating an I2C address.
This method is the first method that should be call prior to calling any
other method form this class. On initialization all pins are configured
as INPUT on the device.
@param i2cAddr: I2C Address where the device is located.
@result 1 if the device was initialized correctly, 0 otherwise
*/
int begin ( uint8_t i2cAddr );

/*!
@method
@abstract Sets the mode of a particular pin.
@discussion Sets the mode of a particular pin to INPUT, OUTPUT. digitalWrite
has no effect on pins which are not declared as output.
@param pin[in] Pin from the I2C IO expander to be configured. Range 0..7
@param dir[in] Pin direction (INPUT, OUTPUT).
*/
void pinMode ( uint8_t pin, uint8_t dir );

/*!
@method
@abstract Sets all the pins of the device in a particular direction.
@discussion This method sets all the pins of the device in a particular
direction. This method is useful to set all the pins of the device to be
either inputs or outputs.
@param dir[in] Direction of all the pins of the device (INPUT, OUTPUT).
*/
void portMode ( uint8_t dir );

/*!
@method
@abstract Reads all the pins of the device that are configured as INPUT.
@discussion Reads from the device the status of the pins that are configured
as INPUT. During initialization all pins are configured as INPUTs by default.
Please refer to pinMode or portMode.
@param none
*/
uint8_t read ( void );

/*!
@method
@abstract Read a pin from the device.
@discussion Reads a particular pin from the device. To read a particular
pin it has to be configured as INPUT. During initialization all pins are
configured as INPUTs by default. Please refer to pinMode or portMode.
@param pin[in] Pin from the port to read its status. Range (0..7)
@result Returns the pin status (HIGH, LOW) if the pin is configured
as an output, reading its value will always return LOW regardless of its
real state.
*/
uint8_t digitalRead ( uint8_t pin );

/*!
@method
@abstract Write a value to the device.
@discussion Writes to a set of pins in the device. The value is the binary
representation of all the pins in device. The value written is masked with
the configuration of the direction of the pins; to change the state of
a particular pin with this method, such pin has to be configured as OUTPUT
using the portMode or pinMode methods. If no pins have been configured as
OUTPUTs this method will have no effect.
@param value[in] value to be written to the device.
@result 1 on success, 0 otherwise
*/
int write ( uint8_t value );

/*!
@method
@abstract Writes a digital level to a particular pin.
@discussion Write a level to the indicated pin of the device. For this
method to have effect, the pin has to be configured as OUTPUT using the
pinMode or portMode methods.
@param pin[in] device pin to change level. Range (0..7).
@para level[in] logic level to set the pin at (HIGH, LOW).
@result 1 on success, 0 otherwise.
*/
int digitalWrite ( uint8_t pin, uint8_t level );



private:
uint8_t _shadow; // Shadow output
uint8_t _dirMask; // Direction mask
uint8_t _i2cAddr; // I2C address
bool _initialised; // Initialised object

};

#endif

0 comments on commit 8e72ab0

Please sign in to comment.