How to translate Arduino code into C/C++ or hex?

How to translate Arduino code into C/C++ or hex?

#define BITLEN        0x80000000 //Enforces 9 bytes sent
#define CHECK         0xBDE31    //Check String
#define MASK10        0x3FF      //mask for first 10-bits of result
#define MASK15        0x4000
#define FPOS          21     //append flag to result for LED number
#define NUM_SAMPLES    8     //number of samples per LED ON interval

int _pd   = 0;
int _gout = 12;
int _rout = 11;
//int pdata = 0;
int pdata, gdata, rdata, ndata;
char ix;

void sRES(char lflag)
{
  int ready;
  ledMode(1);
  gdata = 0;
  for(ix = 0; ix < NUM_SAMPLES; ix++) 
    gdata += analogRead(_pd);
  
  /*ledMode(0);
  ndata = 0;
  for(ix = 0; ix < NUM_SAMPLES; ix++)
    ndata += analogRead(_pd);
   */ 
  ledMode(2);
  rdata = 0;
  for(ix = 0; ix < NUM_SAMPLES; ix++)
    rdata += analogRead(_pd);
  
  ledMode(0);
  //pdata = gdata | (ndata << 11) | (rdata << 20) | BITLEN;
  //lflag == 1 ? pdata |= 1 << FPOS : pdata = pdata;
  gdata |= MASK15;
  ndata |= MASK15;
  rdata |= MASK15;
  //while(Serial.available() == 0);
  if(Serial.available() > 0) {
    if(Serial.read() > 0) {
      //Serial.print(CHECK);
      Serial.print(gdata);
      Serial.print(ndata);
      Serial.print(rdata);
    }
  }
  //Serial.print(pdata);
  

}

void ledMode(char mode) 
{
  if (mode == 0) {
    digitalWrite(_gout,LOW);
    digitalWrite(_rout,LOW);
  }
  else if (mode == 1) {
    digitalWrite(_rout,LOW);  //red OFF
    digitalWrite(_gout,HIGH); //green ON
  }
  else if (mode == 2) {
    digitalWrite(_gout,LOW);
    digitalWrite(_rout,HIGH);
  }
};

void setup() 
{
  Serial.begin(115200);
  ADCSRA |= 1<<ADPS2 | 1<<ADPS1;
  ADCSRA &= ~(1<<ADPS0);
  pinMode(_gout, OUTPUT);
  pinMode(_rout, OUTPUT);
  ledMode(0);
};

void loop()
{
  //digitalWrite(_out,HIGH);
  sRES(1);
  //digitalWrite(_out,LOW);
  //sRES(0);
};

Please inform.
Thanks.
CLUECU

p.s. We have a varied knowledge of programming languages.

How to translate Arduino code into C/C++

It is already in C/C++.

or hex?

"od -x myprog.pde" ?

The Arduino uses C so there is no need to translate anything.

The Arduino uses C so there is no need to translate anything.

Presumably you're refering to .pde file(s), they are most certainly not C.

The contents of a PDE file is standard C with abstractions. Any standard C command will work in the Arduino IDE. The IDE use AVR-GCC

Verify/Compile the Arduino code then look in the 'Applet' folder for filename.cpp and filename.hex .

Don

The contents of a PDE file is standard C with abstractions. Any standard C command will work in the Arduino IDE. The IDE use AVR-GCC

Wrong, wrong, and mostly wrong.

rFree:

Why don't you think a PDE file is standard C? Are you saying "wrong" because you don't think the dialect of C isn't standard?

Do you have an example C command that doesn't work?

If the IDE doesn't use AVR-GCC, what compiler does it use?

Valid C:

int *new = malloc(sizeof('a'));

I presumed from context he was meaning the GNU C Compiler, aka gcc.
He may have meant GNU Compiler Collection, but that doesn't strike me as likely or making all that much sense.

Your project's .pde is compiled with the AVR port of g++.

rFree, in that piece of code, what are you trying to do?
new is a keyword in C++ but not in C. Also malloc returns a void*
is this what you intend in that example, if so it does compile without problem:
int newPointer = (int)malloc(sizeof('a'));

Like I said, the Arduino IDE uses standard C. Any C command will work and you can import any C library.

Like I said, the Arduino IDE uses standard C. Any C command will work and you can import any C library.

You may have said but it doesn't appear you have read.

I still have no idea what you are on about. The Arduino IDE uses C. The language everyone programs an Arduino in is C. If you want to learn to program an Arduino get a C book.

I am beginning to doubt this is going anywhere constructive, but it may help if rFree clarified why he thinks the .pde files are not valid C. Bear in mind that Arduino uses the AVR-GCC compiler. This is a C++ compiler so note the usual caveats about C++ keywords in C code. There are also restrictions due to the AVR environment documented for that compiler. These caveats and restrictions are not Arduino specific.

...but it may help if rFree clarified why he thinks the .pde files are not valid C.

Because they are not?
I think of the .pde files as valid (not complete/compiling) C++, with some additional abstractions (Function prototypes and #includes).

:slight_smile:

If the point being made was that some of the abstractions (libraries for example) are C++ then perhaps that should have been stated more clearly.

It's a lot more helpful to say something like: the .pde combines valid C with calls to C and C++ abstractions, rather then saying that standard C command won't work in the Arduino IDE (which is I think what was being communicated in reply #6).

Just to be clear, I totally agree with all of the statements above.

[edit]The code in #8 is valid C but will not compile in the IDE, this I presume is because the IDE compiles sketches as C++ ?[/edit]

mem:

The intention of that line of code was to demonstrate a line of standard C which would break your .pde file. It however is fine in a .c file.
(One can incorporate .c files in your sketch, the C compiler will be used for these which is why previously I said Mike Mc was "mostly wrong")

There are 3 incompatibilities demonstrated in that line, two of them you noticed.

Now all this is relevant because if you go around telling people they can just copy and paste ANSI C into a .pde without concern for porting it to C++, then you'd be lying and they'd be frustrated and annoyed.

The title of this thread is about translating arduino code to C/C++ and the OPs initial post refers to C/C++, as does the initial reply.

To respond with a post that the pde files are most certainly not C seems at best not helpful, and could be considered nitpicking.

Yes, reply #2 left off the C++ part, but come on guys, adding information that helps the OP understand how Arduino relates to C/C++ would be more helpful then jumping on a point of detail.

Does the "build process" description help: http://arduino.cc/en/Hacking/BuildProcess

Turning on build.verbose in your preferences file will give you a clearer picture of how things work.

Building/uploading an arduino sketch will leave .hex files, along with other stuff like the pre-processed source and .elf files, in a temporary folder. /applet, usually...