Help with writing a library

Hello,

I am trying to write a library to control the PCF8574 I/O expander from TI. I followed the tutorial on writing a library at http://playground.arduino.cc/Code/Library but I’m getting compiler errors. Specifically, when I compile the example sketch, I get errors (even without trying to use the class). I get several errors of the following type: ISO C++ forbids declaration of ‘writeData’ with no type (for many different functions in the library).

I have done some reading and some research, but I haven’t figured out what I’m doing wrong. Any help is greatly appreciated! I really want to understand what I’m doing wrong, rather than just fixing it.

Here is the code:

The header file Pcf.h

/*
  Pcf.h - Library for controlling a pcf8574 I/O port expander.

    Created by James Petersen, 2013.

    Released under the GPL.

*/



#ifndef Pcf_h

#define Pcf_h

#include "Arduino.h"

#include "Wire.h"



class Pcf

{

  public:

    Pcf(byte address);

    togglePin(byte pin);

    writePin(byte pin, boolean value);

    boolean readPin(byte pin);

    writeData(byte data);

    byte readData();

    clearData();

  private:

    byte _address;

};



#endif

Pcf.cpp

/*  Pcf.cpp - Library for controlling a pcf8574 I/O port expander.
    Created by James Petersen, 2013.
    Released under the GPL.
*/

#include "Arduino.h"
#include "pcf8574.h"
#include "Wire.h"

Pcf::Pcf(byte address)
{
  Wire.begin();
  _address = address;
  Wire.beginTransmission(_address);
  Wire.write((byte)0x00);
  Wire.endTransmission(_address);
}

void Pcf::toggle(byte pin) {
  Wire.requestFrom(_address,1);
  if(Wire.available()) {
    byte _data = Wire.read();
  }
  boolean bit = bitRead(_data, pin);
  bitWrite(_data, ~bit);
  Wire.beginTransmission(_address);
  Wire.write((byte)_data);
  Wire.endTransmission(_address);
}

void Pcf::clearData() {
  Wire.beginTransmission(_address);
  Wire.write((byte)0x00);
  Wire.endTransmission(_address);
}

void Pcf::writePin(byte pin, boolean value) {
  Wire.requestFrom(_address,1);
  if(Wire.available()) {
    byte _data = Wire.read();
  }
  boolean bit = bitRead(_data, pin);
  bitWrite(_data, value);
  Wire.beginTransmission(_address);
  Wire.write((byte)_data);
  Wire.endTransmission(_address);
}  

void Pcf::writeData(byte data) {
  Wire.beginTransmission(_address);
  Wire.write((byte)data);
  Wire.endTransmission(_address);
}

byte Pcf::readData() {
  Wire.requestFrom(_address,1);
  if(Wire.available()) {
    byte _data = Wire.read();
  }
  return _data;
}

boolean Pcf::readPin(byte pin) {
  Wire.requestFrom(_address,1);
  if(Wire.available()) {
    byte _data = Wire.read();
  }
  return bitRead(_data, pin);
}

And the example sketch that doesn’t compile:

/* Example sketch showing use of the Pcf Library.
 * Written by James Petersen, 2013.
 * Copyright under the GPL.
 */

#include <Pcf.h>
#include <Wire.h>

void setup(){
//  Pcf pcf = Pcf(32); //32 is the default address for the PCF8574 with all three input pins tied to ground.
}

void loop(){
  delay(500);
  
}

If the method isn't returning a value, it's needs to have void in place of its return type.

Arrch:
If the method isn't returning a value, it's needs to have void in place of its return type.

The only exception is the constructor - that is a special case, and probably the cause of the confusion.

Wonderful! Thanks for the help! I feel like I understand what's going on now.

I have finished tweaking the library, and it now compiles without a problem. I will be testing it next week (I don't have my hardware handy right now). Once I've made sure it works the way I'd like, I will be posting it to GitHub, and I'll add the link to this thread.

Thanks again for helping me understand!