arduino / arduino-ide

Arduino IDE 2.x
https://www.arduino.cc/en/software
GNU Affero General Public License v3.0
2.21k stars 379 forks source link

Allow configuring board for IDE to select by default when sketch is opened #2438

Open matsekberg opened 11 years ago

matsekberg commented 11 years ago

Enable the use of some meta-comment in the code so that the appropriate board is automatically selected for that code.

Additional context

Additional requests

Related

mike-s123 commented 5 years ago

This has been open for too long. It shouldn't be that hard.

1) Allow different boards when there are multiple sketches open. It's really difficult to try to work with different boards at the same time.

2) Allow save/load of board setups. It shouldn't directly be part of an .ino, but should be saved in a sketchbook (sketch folder), so it's loaded automatically when opening a project and also gets archived with the sketch.

atrent commented 5 years ago

I'd like to "+1" to this RFE. A way to save board (and related settings) parsable info into the code or as a side file would be very useful.

mirage335 commented 5 years ago

+1

Arduino is very much appreciated and valuable software in real-world machines. Especially in the 3D printing industry, an entire cluster of machines may be using the same microcontroller for more than a decade. As Arduino's extensive hardware support makes it increasingly appealing for such applications, a lot of open-source developers will save a lot of time/risk being able to declare a specific 'Arduino' variant being programmed by a specific firmware version.

GarryG commented 5 years ago

How is it, that after several years of people noticing that there has to be some sort of persistent information in a project as to what the compile settings are, nothing is happening with this? Most compiler environments allow setting the options and keeping them WITH THE PROJECT, usually in a make-file or similar ... not having any way of setting the global compiler settings apart from the IDE itself, and it ignoring the fact that it is being used for dozens of boards, appears pretty ignorant ... I've not had much of a problem before, as I usually only compiled Wemos D1 compatible boards (apart from the odd ESP01 here and there), but have now gotten some Arduino Nano boards, which led to me noticing that the settings have to be changed every f***ing time I switch from one project to another ... really, this can't be that hard ... just scan the first and last 10-50 lines for comments, have some keywords and translate the stuff you found to the IDE settings ... this would also allow for the comment to be automagically added/updated to the source INO file on changing the settings through the IDE ... IMHO, this is a 100-200 line code update for the Arduino IDE ... (plus, I assume, additional lines to allow for multiple projects open with different settings ... this also is a must-have ...)

MarkusNaeher commented 5 years ago

For the time being, I can suggest a workaround that makes switching the board at least easier:

Edit boards.txt (the exact location depends on your OS) and comment out all boards you don't use. If you've added additional board types like ESP8266, they come with their own boards.txt. I suggest to edit them also. After that, I only have a small number of options in the board selection that's easy to handle.

tschundler commented 5 years ago

Another (interim?) solution is to use VSCode as a frontend to Arduino IDE - this plugin for VSCode from MSFT: https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.vscode-arduino saves board settings with a project. (You still use Arduino IDE to manage boards and libraries with that)

JamesNewton commented 4 years ago

I have no idea why I'm bothering to burn cycles on this again. I guess like @GarryG it just completely fry's me that after all these years, still there is no way to save board settings in the code.

Let me tell you what the #1 issue is that I run into teaching kids and adults to program Arduinos: They get the IDE installed (no small feat), They get the USB driver / connection for whatever Arduino compatible device they have working (no small feat), then they select some example code, e.g. "blink" and try to program it. It fails. Because they have the wrong device selected. And the error message does NOT say anything like "go to Tools / Board..." and select the board you have. And the example programs don't mention needing to do that either.

I could be wrong, but I seriously think that if you surveyed new users of the Arduino IDE, the point at which they most often give up and walk away is not knowing how to select the correct device for the sketch.

And then there are sketches which NEED to be on a specific board. E.g. anything for the ESP-8266 / ESP-32 that needs WiFi. Or code that needs the speed of the Zero. There should be a /standard/ way for the people who write and share sketches to include that requirement IN the .INO file. Not in a separate file, not in a "project", because people starting out with these things don't even understand projects. They just copy and paste in an .INO file. When they compile it for the first time, the IDE should popup a window saying "Hey, what board is this for? I notice in the .INO a comment that looks like I should set the Tools / Board... to xxxx... You want I should do that for ya boss?"

And then there are professional developers who are just tired of having to remember all the bloody menu settings for each project as they switch from one to the other. I'm currently working on two different customer projects, both with the requirement that the work be done in the Arduino IDE so that they can take over maintenance when I'm done. One is on the ESP-32, the other is Arduino Nano but with the 168. So there are multiple settings per projects in each case. Very, very, very annoying. I want to be able to put, in the main INO file, a set of comments which represent the menu settings. And when I open and compile that project for the first time in one session, it should ask me if I want to make those menu changes.

Very simple, very useful, very clean.

timkoers commented 4 years ago

Very simple, very useful, very clean.

Amen, if you really need a solution right now, you can clone my branch

It has got a little comment I talked about in this issue that specifies which board and settings are used.

I am willing to redo this feature, but I need guarantee that the PR will be merged :-)

Avamander commented 4 years ago

I have waited for this feature for nearly five years now :rofl:, would be nice to have someone from the official dev team would back this and help get this feature merged.

atrent commented 4 years ago

A PARTIAL solution is to save a copy of your working 'preferences.txt' (it's in ~/.arduino15) inside your (hopefully git) repository so that you have a future reference handy.

tschundler commented 4 years ago

@timkoers, have you submitted a pull request for that? Was it rejected? (If so, why?)

timkoers commented 4 years ago

@tschundler I submitted a pull request (https://github.com/arduino/Arduino/pull/7349) that has the feature that we are talking about implemented. I don't think it was rejected, it was not accepted.

JamesNewton commented 4 years ago

Sorry, I remove my comments.

per1234 commented 4 years ago

@JamesNewton the tone you're using is unproductive and will result in this thread being locked if it continues. If you and others are truly interested in participating in improving the Arduino software then take a different approach to your communication.

I'm surprised you can't see the need for this feature to be integrated at the level of the CLI. The association should work the same whether the user is using the IDE or the CLI. The Arduino IDE code is moving in the direction of being only for the GUI. All the underlying functionality is provided by the arduino-cli tool, which is shared between the Arduino IDE, Arduino Web Editor, Arduino Pro IDE, and 3rd party software. So by adding a feature to arduino-cli, it becomes available to all the software that uses that tool.

arduino-cli already supports the use of a metadata file (sketch.json) stored in the sketch folder to associate a board and port (though I'm not sure the port aspect is fully working yet). There is a dedicated CLI command for this: arduino-cli board attach. This feature is already available in the Arduino Web Editor. I can't say whether there are any plans to do the same for the Arduino IDE, but it seems clear to me that if it were to be done this would be the mechanism used.

JamesNewton commented 4 years ago

@per1234 Oh, I'm terribly sorry if my tone came over wrong. I seriously meant no offense to you or anyone else. I guess my frustration with prior interactions with non-contributors is boiling over here and I apologize for that. Since you actually have contributed to the project, I'll take the time to try to explain further:

Perhaps the word "interaction" is confusing. Let me clarify: This must go two ways. If you put something in the comments of the source .ino file which /always/ causes a change to settings, that violates least user surprise and breaks the CLI. If you pop up a dialog and /ask/ what the user wants, surprise is mitigated.

Popping up a dialog is generally not done in CLI's, at least as far as I've seen. Would you want the CLI to stop mid way and ask the user about which board to target? For the CLI, a separate (optional) settings file, or the port / board association makes perfect sense. Interaction doesn't happen in CLI's except via the command line. The GUI IDE can write a settings file or, more obviously, change the command line options when invoking the compiler. This is a GUI IDE feature only.

I guess it could be valuable for the CLI to issue a warning if the comment indicates one board and you're command line settings are compiling for another? But more than that would be problematic.

Now, you could argue that the port / board association or a separate settings file makes sense in the GUI, but it has a serious limitation: The user must know about it. As I've indicated above, most beginning users are not aware of the need to select a board. Even if they understand the need to select for the board they own, the .INO that they pasted in often needs a specific board and this is not noted in the sketch which is all they have. An easy way for authors to add that to the file, in the comments so it doesn't break the CLI, and have the GUI IDE point it out to you, is a help.

Interactions are typically associated with the GUI. We are specifically asking for a change to the GUI IDE ONLY for /interaction/ with the user. Again: It's like syntax highlighting or autocomplete. It simply works WITH the user /interactively/ to help a new user select the correct board for the project.

Interactive. GUI only.

Like autocomplete, or syntax highlighting, or copy and paste, or typing in the .INO file.

Very simple: The GUI IDE looks at the comments, finds a matching block, sees that the menus don't match and this is the first time this session, so it asks the user if they want the menus updated from the comment. The user can say "Yes" or "No" and that's the end of the interaction. If they said yes, the menus are updated to match the INO.

timkoers commented 4 years ago

@JamesNewton +100000! That is exactly what my PR (https://github.com/arduino/Arduino/pull/7349) does, except for the dialog to ask the user if they'd like to get the settings matched with the comments in the .INO file.

The only problem with my PR, is that it is outdated. But I am willing to fix that

tschundler commented 4 years ago

Where is sketch.json documented? Quickly search Google found example of it, but no docs. It sounds like maybe a solution for most(?) cases.

per1234 commented 4 years ago

If you put something in the comments of the source .ino file which /always/ causes a change to settings, that violates least user surprise and breaks the CLI.

But it doesn't always cause a change. Let's say you do this:

arduino-cli board attach arduino:avr:nano Foo

Now the default board for that sketch is the Nano. So if you do this:

arduino-cli compile Foo

it will compile for the Nano. However, if you do this:

arduino-cli compile --fqbn arduino:avr:uno Foo

then it will compile for the Uno. The only time you wouldn't use the --fqbn option is if you specifically wanted to use the default board associated with the sketch. So arduino-cli compiling for Nano in that case is not surprising to the user. Therefore there is no need for interaction in the CLI because the user is specifying exactly how they want arduino-cli to behave in advance in the command.

I guess it could be valuable for the CLI to issue a warning if the comment indicates one board and you're command line settings are compiling for another?

No need, since the command line settings, if present, override the default settings in sketch.json.

Now, you could argue that the port / board association or a separate settings file makes sense in the GUI, but it has a serious limitation: The user must know about it.

Not if the IDE has a GUI interface that allows them to set the associations. They don't need to know the mechanism behind how that information is stored. This similar to the Arduino IDE's File > Preferences interface. The preferences are stored to the preferences.txt file, but the user doesn't need to know that. They just set their preferences via the GUI. This is similar to how arduino-cli has a arduino-cli board attach command. Even the CLI user doesn't need to know about sketch.json.

Where is sketch.json documented?

I don't think it has been documented. It would be good to document this somewhere in the arduino-cli GitHub repository for people who might want to use that file directly UPDATE: it has now been documented in the Arduino sketch specification. However, arduino-cli's command line help does document the board attach command, which is the normal user's interface to that file:

>arduino-cli help board attach
Attaches a sketch to a board.

Usage:
  arduino-cli board attach <port>|<FQBN> [sketchPath] [flags]

Examples:
  C:\Users\per\go\bin\arduino-cli.exe board attach serial:///dev/tty/ACM0
  C:\Users\per\go\bin\arduino-cli.exe board attach serial:///dev/tty/ACM0 HelloWorld
  C:\Users\per\go\bin\arduino-cli.exe board attach arduino:samd:mkr1000

Flags:
  -h, --help             help for attach
      --timeout string   The timeout of the search of connected devices, try to high it if your board is not found (e.g. to 10s). (default "5s")

Global Flags:
      --additional-urls strings   Additional URLs for the board manager.
      --config-file string        The custom config file (if not specified the default will be used).
      --format string             The output format, can be [text|json]. (default "text")
      --log-file string           Path to the file where logs will be written.
      --log-format string         The output format for the logs, can be [text|json]. (default "text")
      --log-level string          Messages with this level and above will be logged. (default "info")
  -v, --verbose                   Print the logs on the standard output.
dsyleixa commented 4 years ago

IMO the most simple way would be to hardcode-config the targeted board type by configuration in Sketch (e.g. #pragma config...) such as

#pragma config board="Nano"
#pragma config board="NODE_MCU 1.0"
#pragma config board="Adafruit Feather ESP32"
Tsessebe commented 2 months ago

If I may add my 10 cents...

As I software developer by day; and Arduino enthusiast by night; may I make a suggestion: Why not create a config folder (Similar to Visual Studio or JetBrains IDEs), in the sketch folder, where the settings are recoded?

The config/ meta data can them be sodred in either a JSON file or YAML file. My understanding is that the Arduino-CLI makes use of a YAML file.

Visual Studio creates a .vs folder, where the meta data for the project is stored, similarly JetBrains IDE's creates a .idea folder. This is a standard practice in Modern IDEs.

This mechanism is also used in text editors like VS Code (.vscode folder).

It is a simple yet elegant way of recording the IDE metadata as well as user selected settings.

stef-ladefense commented 4 weeks ago

Hello,

I agree with you, it is missing to have the configuration saved in a file in the sketch directory.

it would be enough for the IDE to save the options as the user changes them and reloads them at the same time as the .ino.

it is a big plus when using several cards with as many projects, that it is configured as it was used at the last compilation. and to avoid transmitting the configuration in the .ino, a hidden folder with the configuration in it is one of the solutions.