Example Code's in C++, want to convert to C!

Hey!
So I've got a Pololu3pi robot and I'm trying to program a line follower task into it, however I need to write the code in C, not C++.
I have gotten a hold of a sample code that is perfect for my needs, but the syntax of some of the code is in C++ and I'm wondering is there a way to just tweak it so it runs as C, or at least looks like C?

Ive attached the arduino file with the aforementioned code. An example of what I mean is :

" OrangutanLCD::clear(); "

The "::" is something I've never seen in C. My coding knowledge only extends to C and so this is why Im asking you guys for help!

Line_Follower_Task.ino (1.7 KB)

Why on earth? You can write in C, only the library API is in C++. Only the library calls will look a little different. Just learn the object call syntax. It's a hundred times easier than rewriting the library yourself (which, ironically, you might have to learn C++ to do).

Im very much a beginner, so I apologise, but I would I begin to learn the object call syntax? Is there a resource somewhere?

tomtom412:
Im very much a beginner, so I apologise, but I would I begin to learn the object call syntax? Is there a resource somewhere?

Well, yes, there are object oriented language and C++ references all over the web. Of course, they are not all good. You have to understand that it's a mature technology that has gone through an evolution. C and C++ are "workaday" languages, i.e., they are designed and have evolved to fit the needs of real world applications. Since the needs are complex, they have become and are somewhat complex.

You might be more comfortable starting in C. But C++ evolved partly to overcome a lot of the stupid limitations and practical programming difficulties in C. You will not be able to avoid those pitfalls as a beginner. So learning C++ from the beginning might not be such a crazy idea.

Right now, you have chosen an incredibly difficult conversion task. My advice again, is to try and figure out what the example code does and learn from it. It's actually easier than the conversion. But if you want to write your own C code from scratch, it is possible to incorporate external C++ resources without writing anything yourself in C++.

Thank you very much. From a closer look at the code, I think the conversion won't be that difficult as a lot of the predefined functions in the C++ version, are the same in the C version. I get what you're saying in terms of what language I should've started out with, but I didnt really have a choice as the course I was doing required we start at C.

Thanks again.

The :: syntax is a scope operator use access the method/function in class OrangutanLCD. It's the library on the top where #include <OrangutanLCD.h>

tomtom412:
Thank you very much. From a closer look at the code, I think the conversion won't be that difficult as a lot of the predefined functions in the C++ version, are the same in the C version. I get what you're saying in terms of what language I should've started out with, but I didnt really have a choice as the course I was doing required we start at C.

Thanks again.

Mmmmm... what are these versions you speak of? You didn't mention that before. If they gave you a C version, isn't your job already done? What is it you need to do with this code, anyway?

mistergreen:
The :: syntax is a scope operator use access the method/function in class OrangutanLCD. It's the library on the top where #include <OrangutanLCD.h>

He's saying:

The :: syntax is a scope operator used to access a method/function in class OrangutanLCD. It's used in the library that the #include <OrangutanLCD.h> line at the top of your program refers to.

The :: syntax is a scope operator

that is: use the "clear()" function that is in the "OrangutanLCD" class/library, rather than some other "clear()" that might be wandering around ("clear" being a common word, after all.)

For a conversion to "pure" C, you'll need to find (or write) C libraries that do the equivalent of all the C++ classes in the Pololu libraries. It looks to me like they've jumped on the C++ bandwagon pretty hard, so that may be difficult.

I have gotten a hold of a sample code that is perfect for my needs, but the syntax of some of the code is in C++ and I'm wondering is there a way to just tweak it so it runs as C, or at least looks like C?

Why do you want to convert it to C when the Arduino IDE uses C++ ?

westfw:
that is: use the "clear()" function that is in the "OrangutanLCD" class/library, rather than some other "clear()" that might be wandering around ("clear" being a common word, after all.)

Ah I understand now. My previous understanding was that as long as I had the libraries included at the top, that any predefined functions that I write in the main code, are pulled from those libraries. So what you're saying is that a "clear()" function in the "OrangutanLCD" library, could be completely different to a "clear()" function from a different library thats included at the top? And so the only way I can specify the "clear()" I want, is to use the :: syntax?

And sorry to be a noob about this, but is that C++ or C? This project I'm doing is to programme a pololu3pi robot to do various tasks. One is a lie follower, which is what this code relates to.

tomtom412:
Ah I understand now. My previous understanding was that as long as I had the libraries included at the top, that any predefined functions that I write in the main code, are pulled from those libraries. So what you're saying is that a "clear()" function in the "OrangutanLCD" library, could be completely different to a "clear()" function from a different library thats included at the top? And so the only way I can specify the "clear()" I want, is to use the :: syntax?

And sorry to be a noob about this, but is that C++ or C? This project I'm doing is to programme a pololu3pi robot to do various tasks. One is a lie follower, which is what this code relates to.

I think if you have the object method OrangutanLCD::clear() in your library, the way you would call it is by first creating an instantiation of an OrangutanLCD like:

OrangutanLCD myLCD;

Then you access the members of your instantiation like this:

myLCD.clear();

In a similar way, the library could have other object definitions that have a clear function like:

GorillaLCD::clear() ...

and you could make a myGorilla object and clear it. The code could be different to drive different hardware.
But in C, if you create a bare clear() function, you can't create another one, as it would be a duplicate.
So you would create functions like clearGorillaLCD() and clearOrangutanLCD(). But don't forget that
many definitions allow multiple objects, as in:

GorillaLCD PanelLCD;
GorillaLCD LidLCD;

and you now have two displays, driven by the same software.
Get to know it, it's beautiful stuff.

tomtom412:
Hey!
So I've got a Pololu3pi robot and I'm trying to program a line follower task into it, however I need to write the code in C, not C++.
I have gotten a hold of a sample code that is perfect for my needs, but the syntax of some of the code is in C++ and I'm wondering is there a way to just tweak it so it runs as C, or at least looks like C?

Ive attached the arduino file with the aforementioned code. An example of what I mean is :

" OrangutanLCD::clear(); "

The "::" is something I've never seen in C. My coding knowledge only extends to C and so this is why Im asking you guys for help!

There's an easier way to do it (without "converting" anything). In fact, I don't see why the example code does it in such a pain-in-the-rear manner.

Just do this: At the beginning of your sketch, create an instance of the "OrangutanLCD" object. Give it any name you wish, for example "lcd" and "mot" and "pb" (one for each):

static OrangutanLCD lcd;
static OrangutanMotors mot;
static OrangutanPushbuttons pb;

Then just use them as such:

  while (!pb.isPressed(BUTTON_B))
  {

    lcd.clear();
    lcd.gotoXY(0, 1);
    lcd.print("Press B To Start");

    delay(100);
  }

See?

That would still be C++, rather than C.

westfw:
That would still be C++, rather than C.

Yup. I'm trying to show him how to use it so that he STAYS using C++.

aarg:
I think if you have the object method OrangutanLCD::clear() in your library, the way you would call it is by first creating an instantiation of an OrangutanLCD like:

OrangutanLCD myLCD;

Then you access the members of your instantiation like this:

myLCD.clear();

In a similar way, the library could have other object definitions that have a clear function like:

GorillaLCD::clear() ...

and you could make a myGorilla object and clear it. The code could be different to drive different hardware.
But in C, if you create a bare clear() function, you can't create another one, as it would be a duplicate.
So you would create functions like clearGorillaLCD() and clearOrangutanLCD(). But don't forget that
many definitions allow multiple objects, as in:

GorillaLCD PanelLCD;

GorillaLCD LidLCD;



and you now have two displays, driven by the same software.
Get to know it, it's beautiful stuff.

Krupski:
There's an easier way to do it (without "converting" anything). In fact, I don't see why the example code does it in such a pain-in-the-rear manner.

Just do this: At the beginning of your sketch, create an instance of the "OrangutanLCD" object. Give it any name you wish, for example "lcd" and "mot" and "pb" (one for each):

static OrangutanLCD lcd;
static OrangutanMotors mot;
static OrangutanPushbuttons pb;

Then just use them as such:

  while (!pb.isPressed(BUTTON_B))

{

lcd.clear();
   lcd.gotoXY(0, 1);
   lcd.print("Press B To Start");

delay(100);
 }




See?

Thank you so much, this is what I was looking for. Thank you all for answering in such detail! My engineering degree isn't at a loss yet! :slight_smile: :smiley:

tomtom412:
My engineering degree isn't at a loss yet! :slight_smile: :smiley:

Not so sure what mine's worth considering that some of my labs involved graphing (by hand - on paper) the grid-plate transfer characteristics of a 6C4 triode vacuum tube.

Back then "computers" were huge "mainframes" that filled entire false-floor rooms and dialed into with KSR-33 teletype terminals (or the absolutely awesome Anderson/Jacobson terminals that worked at a whopping 134.5 baud, printed on paper with a typeball (like an IBM selectric) and had an IBM selectric style keyboard).

Imagine... running BASIC, typing "list" and then "clack-clack-clackety-clack" as the printer generated the listing, line by line and spit out paper by the ream (which I will tell you was INFINITELY better than those dang IBM batch cards!).