3

I'm not sure if I should split this question in two. I had to edit some libraries and I plan to make some myself but didn't try yet.

Editing a library seems very laborious comared to editing a sketch.

I have to use an external editor and then compile on the Arduino IDE with the help of a test sketch.

I wish to use the Arduino IDE to edit the libraries but it seems it is not easy, maybe in future reales it is supported.

The libraries used to be on %home%/Arduino/libraries/etc. but now I have to edit some located at %appdata%\Arduino15\packages\esp8266\hardware\esp8266\1.6.5-947-g39819f0\libraries and it seems these packages might get updated so maybe it is not a good idea to edit them there but I am not sure.

What about GitHub and conflicts? I clone the library and edit it but then I have two versions and you can't choose one of them. So I have to delete the original from the arduino instalation or wherever it is to avoid the conflict.

I also wish to post the progress to GitHub and maybe receive collaboration there. Do you have any hint about the clone process or something to avoid any trouble with it?

And what about the .h/.o? Arduino precompile the headers on startup so if I edit a .h I have to find and delete the .o or restart the IDE.

How do you do all of this? Is there any trick that helps?

Thank you.

  • I know this playground.arduino.cc/Main/CustomizeArduinoIDE (very raw information, just a specification) and this forum.arduino.cc/index.php?topic=37294.0 (old). – aalku Aug 14 '15 at 12:48
  • I'm not accepting any answer because they are only focused on versioning/git/github fork->edit->share and I am very interested on all the things that I have to do to make the development. Any good editor if Arduino IDE is not an option? Why is not an option? Where should I place the git clone to override any other version of the library? or should I delete them? That kind of things. Thank you. – aalku Aug 19 '15 at 10:57
1

There is one component of your question that remains unanswered:

I wish to use the Arduino IDE to edit the libraries but it seems it is not easy, maybe in future reales it is supported.

Any good editor if Arduino IDE is not an option? Why is not an option?

The rest of your questions already have good answers so I'll limit myself to explaining how the Arduino IDE 1.6.5-r4 and newer can be used to edit libraries:

Copy the library you want to modify to {sketchbook folder}/libraries or create a new folder at that location if you're writing a library from scratch. You can find the location of {sketchbook folder} at File > Preferences > Sketchbook location.

Create a file that matches the folder name that contains the source files with the .ino extension. For example:

{sketchbook folder}
|_libraries
  |_FooBar
    |_FooBar.ino
    |_FooBar.h
    |_FooBar.cpp

or:

{sketchbook folder}
|_libraries  
  |_FooBar
    |_library.properties
    |_src  
      |_src.ino
      |_FooBar.h
      |_FooBar.cpp

This dummy .ino file is necessary in order to open the library source files in the Arduino IDE but will not actually be used as part of the library. You can leave it blank. I like to use this file as a "to-do" list for my development work. I add this filename to .git/info/exclude so that it will not be tracked as part of the repository. It's also possible to use this file as a sketch to test the library with. Just remember to use the local file include style:

#include "FooBar.h"

Note that you will only be able to edit the files that are in the same folder as the .ino file via the Arduino IDE. Luckily most Arduino libraries place all their source files in a single folder so this is not much of a limitation. If you wanted to edit a library that has source files in multiple folders you could always put a dummy .ino file in each folder and open them in separate IDE windows.

Add a file named .development to the library root folder. This is necessary because normally the Arduino IDE does not allow saving to the library folders or the examples folder to prevent accidental modification of the example sketches. Using Windows Explorer (and possibly others), you will find that you are not allowed to create files that start with a dot. You can create the .development file using a text editor or the command line. The contents of the file don't matter, only the presence of the file. Note that you should not publish the .development file to repositories that will be included in the Arduino IDE Library Manager as the file will cause the library to be skipped by the job that fetches releases. I also add this filename to .git/info/exclude.

If the library does not already contain one, add a library.properties file to the library root folder. You can find the correct format of this file in the Arduino Library Specification:

https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5:-Library-specification#libraryproperties-file-format

Now the structure of the example libraries above would look like this:

{sketchbook folder}
|_libraries
  |_FooBar
    |_.development
    |_FooBar.ino
    |_FooBar.h
    |_FooBar.cpp
    |_library.properties

or:

{sketchbook folder}
|_libraries  
  |_FooBar
    |_.development
    |_library.properties
    |_src  
      |_src.ino
      |_FooBar.h
      |_FooBar.cpp

Restart the Arduino IDE if it's running.

After doing this, when you open any of the source files of the library with the Arduino IDE, all source files in the folder will be opened and you can edit and save them as you would a sketch.

4

I'm not accepting any answer because they are only focused on versioning/git/github fork->edit->share and I am very interested on all the things that I have to do to make the development.


I will run through the basics of making a library. It's really quite simple, and they persist through upgrades to the IDE.

Let's imagine I have a useful function (or lots of functions) that I want to use in lots of sketches. For example, something that blinks an LED:

void blinkLED (const byte which, const int times)
  {
  for (int i = 0; i < times; i++)
    {
    digitalWrite (which, HIGH);
    delay (500);
    digitalWrite (which, LOW);
    delay (500);
    }  // end of for loop
  } // end of blinkLED

And some test code:

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();
  pinMode (13, OUTPUT);
  blinkLED (13, 5);
  }  // end of setup

void loop ()
  {
  }  // end of loop

Make a library

To turn that into a library we need three steps:

  1. Make a folder for the library. This should be placed inside the libraries folder in your sketchbook folder. This folder:

    Sketchbook folder

    In my case: /home/nick/sketchbook/libraries. It does not go into anywhere associated with the IDE download.

    but now I have to edit some located at %appdata%\Arduino15\packages\esp8266\hardware\esp8266\1.6.5-947-g39819f0\libraries

    No, not there.

    I will call my library blinkLED. So I make that folder, which is now: /home/nick/sketchbook/libraries/blinkLED


  1. Make a function prototype for any functions your library is going to "expose" to the end-user, and put that into a .h file, like this:

    blinkLED.h

    #include <Arduino.h>
    
    void blinkLED (const byte which, const int times);
    

    Important: You must include <Arduino.h> as in the example, or you will get compile errors.


  1. Put the C++ code into a .cpp file, like this:

    blinkLED.cpp

    #include <blinkLED.h>
    
    void blinkLED (const byte which, const int times)
      {
      for (int i = 0; i < times; i++)
        {
        digitalWrite (which, HIGH);
        delay (500);
        digitalWrite (which, LOW);
        delay (500);
        }  // end of for loop
      } // end of blinkLED
    

    Important: You must include the .h file created in step 2, as in the example, or you will get compile errors.


That's it! Restart the IDE now so it notices the new folder with the library in it.

Now our test sketch can just use the new library.

#include <blinkLED.h>

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();
  pinMode (13, OUTPUT);
  blinkLED (13, 5);
  }  // end of setup

void loop ()
  {
  }  // end of loop

Testing

For testing I just keep the library files open in a separate text editor (I am using Geany these days, but whatever your favourite editor is).

You can make changes to the library source and save them. You do not need to restart the IDE if you do this. It will always pick up the latest version.

In the IDE you alter your calling code, upload and test. It is really quite fast and simple.


Git

Once you have put a bit of work into your library you probably want to put it under source control. Using a terminal/command window, navigate to the library folder and put it into a Git repository:

git init
git add *.cpp
git add *.h
git commit -m "Initial commit"
1

I am going to split my answer into 2 parts

1. Editing an Arduino library:

It doesn't matter how you edit your library, you can do it in notepad for all I care. It just doesn't matter.

2. Version Control:

Git is a version control software used to track software/code changes and ensure that people don't screw up the files by adding code that's not tested.

If you want to contribute to a certain library here is what you need to do

Step 1: create a github.com account since most people host it there

Step 2: fork the required library, this basically creates a clone of the library in your account.

Step 3: clone the forked repo from your account to your computer. So now you have the files on your computer to mess around

Step 4: create a branch a branch is your personal workspace to mess around as you wish. The advantage of have a branch is that if you screw up. You could always go to the main branch (master) and start over :)

Step 5: once you are done editing and testing and are sure the file is perfect, you commit your changes and then you create **pull request ** with the original author so that he can review your work and if he is happy will accept your change and merge it to master.

Note: read about adding remote to your git repo and fetching from remote repo, once you understand these basics you will understand the power of git :)

1

This is the common process for all open source projects, not just arduino (example using github svn)

1. Fork a project

When you fork a project, you create a separate "project" of your own from the original project on the SVN.

2. Clone your project

Clone your fork down to your local directory so you can make changes and use it.

git clone https://github.com/<your-repo>/<project>

3. Push changes up

Push your changes up to your repo regularly. At this point your changes are not affecting the original project in any way.

git push -u origin <branch>

4. Pull updates from the original project

The original author(s) can make changes and improve his/her code. You can make sure your fork is up-to-date by pulling/merging those updates into your fork.

git pull https://github.com/<original-author>/<repo> <branch>

Some time there will be conflicts, which are normal since your changes and the author's may collide. You'll be prompted to fix the conflicts before committing your changes.

<<<<<<< HEAD
    # this portion is your code at the current HEAD
=======
    # this portion is the one from the original branch master
>>>>>>> master

You'll have to fix the conflict manually by either accepting your change and deleting the master's or deleting your change, and get rid of the marks before committing to your svn.

4. Creat a pull request

After using your supercharged version of the project for a while, you think it might be useful for others. To contribute, you create a pull request to the original author. It works similar to you pulling in his changes, just he will review your changes then merge in.

All the while, others can do all these with your project too!

To edit the project/make changes, you can use any editor to open your local project, code away, save it and commit. It is not ideal to use Arduino IDE since it is an environment to run the code (but otherwise possible).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.