Using the Wire Libary from a C source file

Hello all,

I am having some trouble programming. I am very comfortable with C and want to avoid using C++ to program the Arduino. I have written a decently large project in C and I wanted to use the wire library from my C source file. Unfortunately the wire library is written in C++ and the methods for the wire library are not static which means I can not call them from my C project. Does anyone know of a C library that implements the same functionality as the standard wire library or a solution to this problem? Thanks!

looks like the wire library uses a "twi.h" header file, which has a corresponding "twi.c" code file. Try to #include "twi.h" and see if the compiler complains.

You can find the source code for them in "...\Arduino\libraries\Wire\utility"

The wire c++ lib is largely a wrapper for twi.c, it's in the utility folder. In fact much of the Arduino core is a wrapper for avr-libc. See here for avr-libc docs: http://savannah.nongnu.org/projects/avr-libc/

bFlux: Does anyone know of a C library that implements the same functionality as the standard wire library or a solution to this problem? Thanks!

C++ is subset of C so if you know C then your code will work in C++ files. If you use the Arduino IDE and its libraries it is pretty much impossible not to use C++ Basically, you have two choices. Put your C code in C++ files (rename them .cpp) so it can call the C++ functions, or write C++ wrappers that use C linkage for the C++ routines you want to call.

Personally, i'd just move the C code into C++ files and be done with it, but you could write wrappers for all the C++ routines that can be called from C. You will have to put all the wrappers in a C++ file then put your wrappers in that file. Example:

extern "C"
{
    void WirebeginTransmission(uint8_t addr)
    {
        Wire.beginTransmission(addr);
    }
}

This creates a C callable function called WirebeginTransmission()

You would have to create a wrapper C linkage function for every C++ function you want to call. For the Wire library, many of the functions are overloaded so you will have come up with unique names for each of overloaded functions. It could be done, but I'd think it would be a lot easier to just move the C code to a C++ file so that it could call the C++ routines directly.

--- bill

C++ is subset of C

C++ is a superset of C.
C doesn’t have classes. C++ does.

bperrybap: Personally, i'd just move the C code into C++ files and be done with it, but you could write wrappers for all the C++ routines that can be called from C.

Yeah that sounds like the best idea. I wanted to avioid using as much C++ as possible because of dynamtic memory allocation on embedded systems is a nightmare. I looked at the Wire.h and Wire.c source files and like tf68 and Jiggy-Ninja said, they are mostly wrappers for twi.h. It seems weird to code the wire library in C++ since it doesn't instance variables which means the class is stateless. I will do what you suggested and report back on how it works out. Thanks for the suggestion.

PaulS:

C++ is subset of C

C++ is a superset of C.
C doesn’t have classes. C++ does.

oops… I totally spaced and used the wrong word here.
I was thinking “superset” and yet somehow I typed “subset”.

— bill

Changed the file extension to .cpp and included the extern "C" before the function implementations and it is working like a charm. Many thanks to bill, this problem is solved.