Use arduino as library in my application

I need to compile a sketch and upload it to the board in my java application.
I want to use arduino as the library for it.

What is the best way to do this?

Are there any examples?

It's not clear what you want to do. Please provide more details.

I developing own java application and I have a arduino sketch.

I want to upload sketch to Atmega by button click in my aplication.

I may use arduino-cli fo that, but I want do this in my application directly.

For this I want using arduino source code.

When you have an ATmega board selected and press the "Upload" button in the Arduino IDE four things happen:

  • The Arduino IDE passes the board configuration selections from the Tools menu to the arduino-builder tool, which is written in the Go programming language.
  • arduino-builder does a bit of preprocessing to turn the sketch into valid C++ code and determines the necessary include paths to resolve all library dependencies. Actually in the next Arduino IDE release the preprocessing will be handled by a separate application called arduino-preprocessor. arduino-preprocessor is written in C++.
  • The sketch is compiled to a binary file using the AVR-GCC toolchain.
  • The binary file is uploaded to the Arduino board using AVRDUDE.

So to do all that in your application directly you would need to reproduce more than just the Arduino IDE's functionality. In fact the Java coded Arduino IDE has almost no role in this process. The Arduino IDE is just the GUI interface to all these other tools. Trying to reproduce all that functionality in your application would be a lot of work.

If you provide a compiled binary file you can skip the first three steps. But you will need to compile the code for a specific microcontroller and clock speed (e.g. ATmega328P @ 16 MHz for the Arduino UNO board). At that point you will have no need for the Arduino IDE or AVR-GCC. You would only need to use AVRDUDE or else write your own replacement for AVRDUDE.

If you can provide more details of what you want to do then we might be able to give more assistance.

I need compile Marlin firmware Marlin/Marlin at 1.1.x · MarlinFirmware/Marlin · GitHub and upload it to MKS GEN v.1.4 board.
MKS GEN use Atmega2560 chip.

I want to develop application for setup Marlin configuration in GUI and upload it to the board.

Now user usually uses Arduino for change firmware settings and upload it. But it's very difficult for newbie users and I want to make it easier, that user do not need download arduino, include libraries, setup settings in C++ code.

Also I want to translate settings description to other language, because many users don't know English

OK, if you want to allow the configuration to be modified then you are going to need to compile the code so you can't just skip straight to the upload via AVRDUDE.

I think you can easily do away with the Arduino IDE. If you want to get rid of arduino-builder/arduino-preprocessor that wouldn't be too bad in this case since Marlin doesn't require any sketch preprocessing. Trying to reproduce avr-gcc would be a monumental task. Reproducing AVRDUDE would probably be doable since only a small subset of the functionality of that application is used to upload to an ATmega2560.

If I were you I would try to use as many of the existing tools as possible but I can see wanting to get rid of the Arduino IDE. You can definitely go straight to arduino-builder. There is documentation for it here:

If you turn on File > Preferences > Show verbose output during > compilation > upload in the Arduino IDE, upload Marlin, then examine the contents of the black console window at the bottom of the Arduino IDE window you will see all the commands that were run during the compilation and upload.

A more simple project would be to make an application that allows the user to select configuration options via a GUI and then generates the Configuration.h and Configuration_adv.h files for Marlin. The user then replaces the stock Marlin files with the generated files and then uses the Arduino IDE only for the upload. I seem to remember there is something like that for the Repetier firmware (I'm not really a 3D printer person).

As another variant I may to include arduino to my distributive. Arduino version without installation. (Windows ZIP file for non admin install)

And use arduino-cli commands for compile and upload app

e.x.:
arduino_debug --install-library "U8glib:1.19.1"
arduino_debug --upload E:\Dropbox\Marlin\Marlin.ino --board arduino:avr:mega:cpu=atmega2560 --port COM9

Am I right?

Looks good to me at a quick glance.

Thank you very much for useful information! I am going to use arduino-cli instead my first idea. This is easier :slight_smile:

I think it's a reasonable decision. It will allow you to focus your efforts on the UI aspect that's the most important part of the project. After that's finished, you can proceed remove the requirement for the Arduino IDE if you like. Actually one of the reasons for the creation of the arduino-builder and arduino-preprocessor tools was to allow for 3rd party applications to avoid being dependent on the entire IDE. Years ago all that code was inside the IDE code.

You can also call the Arduino IDE from the command line - I think it is easier than using the Builder, though maybe not as flexible.

...R