6 ms·
How to write a great Arduino library
- news_to_me 7y agoDoes anyone have recommendations for writing Arduino code without the Processing-based Arduino IDE? I'm sure there must be a not-too-difficult makefile-based compile/upload configuration, but I'm not aware of any (nor do I trust myself to write one from scratch). At this point, the Arduino IDE is my least favorite part of the whole ecosystem.
- faster 7y agoI switched to VS Code with the PlatformIO extension. A little bit of conversion is required, 'ino' to 'cpp' and some shuffling between directories.
- monocasa 7y agoI've used this in the past to get started. https://github.com/mkleemann/cmake-avr https://github.com/mkleemann/cmake-avr
- tridentlead 7y agoI don’t know if this is what you’re looking for but I frankenstiened this from some stuff on the net: https://github.com/dstutman/avr-template https://github.com/dstutman/avr-template
- etaioinshrdlu 7y agoI had good luck with Atmel Studio which is a variant of MS visual studio. This was on the ARM based arduino parts. It overwrote the arduino boot loader. The debugger was actually useful!
- leonvv 7y agoThe Arduino IDE has a verbose mode which prints out all the commands it uses to upload your program.
- jlubawy 7y agoThis plus VS Code and CMake is what I'd use to start any new project, saves time having to figure out all the options yourself.
- tuankiet65 7y agoI used to rely on this Makefile template https://github.com/sudar/Arduino-Makefile https://github.com/sudar/Arduino-Makefile.
- inamberclad 7y agoI use PlatformIO's CLI, which is pretty good for build management. It also has its own library system.
- anonsivalley652 7y agoNot Fritzing. Generally: Arduino CLI Mac + Xcode: EmbedXCode. Windows: Arduino add-on for VS. I would keep the Arduino IDE because no work-alike is going to have every feature.
- jpindar 7y agoAtmel Studio. It was very easy to set up, and can call AVRdude for uploading. It has the ability to import typical Arduino projects, so you can either keep using the Arduino libraries or convert the project to pure C/C++.
- craftinator 7y agoI really wanted to use Atmel Studio, but couldn't get it working correctly on Arch Linux + wine. Can't imagine why such a major company wouldn't support Linux native.
- feistypharit 7y agoLikewise, I use https://github.com/plerup/makeEspArduino https://github.com/plerup/makeEspArduino
- miccah 7y agoI wrote a very, very simple Makefile awhile back utilizing the arduino CLI options. https://github.com/mcastorina/laptop-remote/blob/master/Makefile https://github.com/mcastorina/laptop-remote/blob/master/Make... Haven't used it since then, so YMMV.
- carlosperate 7y agoArduino has recently modularised their IDE, and the arduino-cli can be used to crate a sketch, compile it and flash the board: https://github.com/arduino/arduino-cli https://github.com/arduino/arduino-cli
- mcshicks 7y agoI have used emacs on linux before and it's fine. However I don't know how well it works with non standard arduinos like atttiny. I can't remember the exact write-up I used but I just googled emacs arduino.
- inetknght 7y agoMy understanding is that GCC can cross-compile to Arduino if you target AVR. See also https://www.arduino.cc/en/Reference/UsingAVR https://www.arduino.cc/en/Reference/UsingAVR I haven't done it personally... but I also haven't yet poked much into Arduino either. I'm still having fun with Raspberry Pis. Arduinos... are a little more in-depth.
- fake-name 7y agoPlatformio[0] is probably the best option, here. It supports the arduino-style code munging, though realistically if you're at least remotely familiar with C++ it's super easy to not use it. It also supports basically every embedded platform out there to some extent. [0]: https://platformio.org/ https://platformio.org/
- craftinator 7y agoPlatformIO plugin for VSCode. There's a bit of a learning curve, especially for folder structuring requirements, but overall it makes you stay organized in a consistent and repeatable way. It's also great if you're working with more than just Arduino framework (Espressif, Kendryte, STM32), as you can keep your code modular and easily apply it to different microcontrollers.
- klingonopera 7y agoI've managed to setup Code::Blocks[0] to use WinAVR to program Arduinos. Here's a guide[1] I found for that. It works, but I probably forgot to add some optimization flags, because the code ran slower than when compiled with the Arduino IDE. And then I realized, you can just take any .c or .cpp written for the AVR, rename it to .ino, put it in a folder with the same name, and use the Arduino IDE to compile and flash. So I never really use C::B for the Arduino anymore, I just write the code in Notepad++[2] and use the Arduino IDE to upload. [0]: http://www.codeblocks.org/ http://www.codeblocks.org/ [1]: https://uetianblogger.blogspot.com/2015/05/winavr-and-codeblocks-for-atmega-code.html https://uetianblogger.blogspot.com/2015/05/winavr-and-codebl... [2]: https://notepad-plus-plus.org/ https://notepad-plus-plus.org/ EDIT: > you can just take any .c or .cpp file Eh... actually only .cpp, as the Arduino IDE is C++. Or a .c file that will also compile in C++.
- franky47 7y agoAs others mentioned, PlatformIO is a good start. For those who are interested in building a library, here's my TravisCI workflow for unit testing & running examples on various platforms: https://github.com/FortySevenEffects/arduino_midi_library/blob/master/.travis.yml https://github.com/FortySevenEffects/arduino_midi_library/bl...
- lonelygirl15a 7y agohttps://marketplace.visualstudio.com/items?itemName=vsciot-vscode.vscode-arduino https://marketplace.visualstudio.com/items?itemName=vsciot-v...
- smcameron 7y agoHow about write in C, rather than C++, so you don't force C++ on your users. I mean, if you're going to go so far as to recommend avoiding pointers in your api because your users can't understand them, why in the hell would you then go write a library in C++?
- deleted 7y ago[deleted]
- jcelerier 7y agoUsing values everywhere has less gotchas in c++ - return value optimization is applied much more consistently for instance. Also you can make c++ look not far from python in terms of cognitive overhead for beginners. C ? Not so much
- eska 7y agoIn C return value optimization is not a problem to begin with. There are many more side effects in C++ that could inhibit (have inhibited) return value optimization, by e.g. overloading operators. The standard actually changes the behavior of side effects to allow for return value optimization, i.e. code acts differently depending on whether it's return value optimized or not. I don't feel like any of this is consistent or simpler than C.
- jcelerier 7y ago> In C return value optimization is not a problem to begin with of course it is - https://gcc.godbolt.org/z/welWvd https://gcc.godbolt.org/z/welWvd C gives you O(N) memcpy, C++ gives you O(1) do-stuff-in-place for the exact same code > overloading operators has exactly zero things to do with RVO > I don't feel like any of this is consistent or simpler than C. it produces vastly simpler user code. In years and years of programming in C++ never has "code acts differently depending on whether it's return value optimized or not" been an issue for anyone I know.
- pjmlp 7y agoBecause it is safer than using plain old C, which WG14 refuses to improve its security story. I don't like my IoT devices to be easily p0wned, just because an array went out of bounds.
- pjmlp 7y ago> Efficiency is not paramount; readability is How I wish that this would drive C++ design decisions of lately.
- franky47 7y agoOne thing I've learned from maintaining an Arduino library [1] for the last 10+ years: make the API as simple as possible. Lots of people discover programming with Arduino, making the experience enjoyable for them requires abstracting a lot of things away. Things can be as complex as reasonable in the implementation, but providing helpers, macros & shortcuts (as well as good documentation and examples as stated in the blog post) makes the learning curve less steep. [1] https://github.com/FortySevenEffects/arduino_midi_library https://github.com/FortySevenEffects/arduino_midi_library