Help with Wire.write() type.

Hey all!

So I’ve been all through this code, trying different things, and it always throws the following error:

pinbrains.cpp: In function ‘void write_register(byte, byte, byte)’:
pinbrains.cpp:60:14: error: expected primary-expression before ‘register’
pinbrains.cpp: In function ‘byte read_register(byte, byte)’:
pinbrains.cpp:67:14: error: expected primary-expression before ‘register’
In file included from pinbrains.cpp:1:0:
/usr/share/arduino/libraries/Wire/Wire.h:58:13: note: candidate 1: uint8_t TwoWire::requestFrom(int, int)
/usr/share/arduino/libraries/Wire/Wire.h:56:13: note: candidate 2: uint8_t TwoWire::requestFrom(uint8_t, uint8_t)

when I had the code of the read_register() and write_register() funtions in my class itself, (that is duplicating the code each time I wanted to use it) it worked fine, but in a function like this, it doesn’t work. And… if I hard code in the Wire.write() values like so: wire.write(0x12) it works fine.

#include <Wire.h>

void write_register(byte address, byte register, byte value);
byte read_register(byte address, byte register);

class mcp23017{
  public:
    byte address, porta_dir, portb_dir;
    mcp23017(byte addr, byte pa_dir, byte pb_dir){
      address = addr;
      porta_dir = pa_dir;
      portb_dir = pb_dir;
      write_register(address, 0x00, porta_dir); //set Port A DIR
      write_register(address, 0x01, portb_dir); //set Port B DIR
    }
    
    boolean get_bit(byte port, byte index);
    
    void set_bit(byte port, byte index, boolean value);
    
    byte get_port(byte port){
      return read_register(address, port);
    }
    
    void set_port(byte port, byte value);
};

class inputPin {
  public:
    byte chip, port, pin;
    void init(byte c, byte p, byte b){
      chip = c;
      port = p;
      pin = b;
    }
    void on();
    void off();
};

void setup(){
  Wire.begin();
  Serial.begin(9600);
}


void loop(){
  mcp23017* i_o_chip = new mcp23017(0x20, 0xff, 0x00);
  while (true){
    Serial.println(i_o_chip->get_port(0x12));
  }
}

void write_register(byte address, byte register, byte value){
  Wire.beginTransmission(address);
  Wire.write(register);
  Wire.write(value);
  Wire.endTransmission();
}

byte read_register(byte address, byte register){
  Wire.beginTransmission(address); //begin transmit to specified address
  Wire.write(register);	//write register address
  Wire.endTransmission(); //end transmit
  Wire.requestFrom(address, 1); //request 1 byte responce
  return Wire.read(); //read and return response
}

Point being, I’m pretty confused. Can anybody see where I’m going wrong??

Thanks!

~Mark Baldridge

register is a (never used) C keyword. Just like you can’t have a variable named for or int, you can’t have a variable named register

Also, the function prototypes are not needed. You are, in effect, defining both a global function and a class method with the same names. That way lies madness.