Sunday 30 August 2015

Converting .ino file into .cpp

I found myself wanting to have a program folder made up solely of C++ files and the existence of the .ino file irked me for some reason (slightly OCD?)

After a bit of googling it was apparent that the .ino file only differed from regular C++ files in that a preprocessor would run on it before compilation to insert a header include.

So lo and behold a file that looks like this:
myApp.ino
-------
void setup()
{
   //do some setup
}
void loop()
{
   //do cool stuff here
}
-------
can be converted into:
main.cpp
-------
#include "application.h"
void setup()
{
   //do some setup
}
void loop()
{
   //do cool stuff here
}
-------

by simply renaming the file and adding the application.h include.

It is a little strange not to have a main method in this file but I assume the set up is similar to arduino where the main method is hidden away and you are only given the hooks into it by implementing setup() and loop()

2 comments:

  1. hi. have u tried it before? like converting from blink test .ino to .cpp?

    ReplyDelete
    Replies
    1. Yes I have tried it with the blink test.

      Create a file blink.cpp with the following contents:

      #######################################

      #include "application.h"

      int led1 = D0;
      int led2 = D7;

      void setup() {

      pinMode(led1, OUTPUT);
      pinMode(led2, OUTPUT);

      }

      void loop() {

      digitalWrite(led1, HIGH);
      digitalWrite(led2, HIGH);

      delay(1000);

      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);

      delay(1000);

      }

      #######################################

      Notice the only difference with the .ino version is the additional include statement.

      You can compile and flash this program as usual through the CLI.
      Then you can see the light blinking away as usual.

      More info here -> http://codefrieze.blogspot.co.uk/2015/08/using-particle-cli-on-ubuntu-1404.html


      Delete