Edited, again for spelling. Sigh!
Yup – that’s about all you need to do. There’s no learning about libraries other than:
- How to write one;
- How to use one.
That’s it. You can delve into the source code if you wish, but that way, many rabbit holes lie. You could be down there for days!
Well, the whole Arduino concept was originally to get students building stuff quickly to get a handle on what they were learning. It sort of grew from that - in a manner very similar to the Raspberry Pi’s original design. It too was because kids were doing tech courses at University and couldn’t program a computer. IT learning back then was using Microsoft Office. Eben Upton was at Oxford University and dismayed at the standard of abilities of prospective students.
See my brief description of pinMode()
further back. Lots of stuff happening below the quilt covers to stop you doing something daft and smoking the board. But it saves you having to read the data sheet to determine that you must:
- Write a 1 bit to the pin position in the appropriate DDR register to make a pin an OUTPUT;
- Write a 0 bit to the pin position in the appropriate DDR register, AND write a 0 bit to the pin position in the appropriate PORT register to make a pin an INPUT;
- Write a 0 bit to the pin position in the appropriate DDR register, AND write a 1 bit to the pin position in the appropriate PORT register to make a pin an INPUT_PULLUP;
That’s all there is to it, but what is easier pinMode(3, INPUT_PULLUP)
or:
// Make D3 an INPUT Pin.
DDRD &= ~(1 << DDD3);
// Turn on PULLUP resistor.
PORTD |= (1 << PORTD3);
I know which one I prefer, but I know which one is by far the more efficient.
By the way, in both the Arduino IDE and PlatformIO, all the core files for the appropriate platform are compiled into a library (usually core.a
is it’s name) and then linked with your code. Only the modules you have used are linked into your final firmware file ready for uploading.
They don’t, well they do but …
When you or someone write a library for the Arduino, for example, it is written in a particular way. An examples
directory/folder, if supplied, will cause the examples for that library to be installed when you add the library. The layout is:
MyLibrary
src
myLibrary.h
myLibrary.cpp
someOtherFile.ino
examples
MyLibrary_example_1
MyLibraryExample_1.ino
MyLibrary_example_2
MyLibraryExample_2.ino
MyLibrary_example_2
MyLibraryExample_2.ino
keywords.txt
library.properties
MyLibraryName
It is possible to have other folders in there as well, my libraries have a docs
folder – to describe how to install and use the library and a bit about the examples. There will usually be screen shots for the breadboard setup and so on. Those additional folders are ignored by the Arduino IDE.
The good news is, PlatformIO can use Arduino libraries in this format. Most useful indeed. And the code in use should also be the same if you are using the framework = arduino
option in platformio.ini
.
Because you don’t need to download them. using lib_deps
does that for you. However, if your internet is flaky, then having a download is helpful.
I’m developing a bunch of libraries for Arduino and PlatformIO. I have a layout like this:
PlatformIO_Libraries
AVRwdt
... as above.
AVRadc
... as above.
AVRandSoOn...
Each library folder is as described above. In my platformio.ini
I have something like this:
; Where to find my various PlatformIO libraries. This is a
; relative path from the project directory to the directory
; named "PlatformIO_Libraries" under which, each library is
; to be found in it's own sub-directory.
; In each subdirectory is the source and header files. There's
; no need for "lib_deps" in this case.
lib_extra_dirs = ../../../../PlatformIO_Libraries/
So, I don’t have to code a bit, test a bit, publish the library – which may not be complete – to platformio.org and/or GitHub then mess about changing versions, adding and removing bugs and features, and so on, then having to republish everywhere. I do what I want locally, and when I’m finished I’ll publish.
In my code I just have the appropriate headers:
#include "AVR_wdt.h"
Then use the code:
#include "AVR_wdt.h"
// LED pin for the WDT to flash.
#define WDT_LED 7
void wdtInterrupt() {
digitalWrite(WDT_LED, !digitalRead(WDT_LED));
}
void setup() {
// Initialise Serial.
Serial.begin(9600);
// Do the pins. Pin 7 is for the WDT interrupt.
pinMode(LED_BUILTIN, OUTPUT);
pinMode(WDT_LED, OUTPUT);
// Enable the WDT to fire every second.
AVRwdt.begin(AVR_wdt::WDT_TIMEOUT_1S, wdtInterrupt, false);
// Inform the user as to what will happen.
Serial.print("WDT will reset: ");
Serial.println((AVRwdt.willReset()) ? "YES" : "NO");
Serial.print("WDT will interrupt: ");
Serial.println((AVRwdt.willInterrupt()) ? "YES" : "NO");
}
void loop() {
// Flash the builtin LED every 5 seconds.
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(5000);
}
Job done. The built in LED is flashed every 5 seconds in the loop()
and the LED on D7 is flashed every second by the watchdog.
Long story (too long!) short, I have lots of libraries in a safe place, and I can use them for my AVR code in PlatformIO and/or the Arduino IDE without having to lib_deps
them all - just do the above with the very top level folder and either it’s full path or a relative one. All the libraries in that folder and sub-folders will be able to be used as required.
So, you can still use your USB and/or Downloads folder for your libraries with a little organisation of the folder structure.
Me too! I love to know how things work. I have two mottos in life:
- Don’t think, find out!
- How the f*ck does that work!
Well, it’s called “junk” DNA, but nobody knows, yet, if it really is junk. I love the fact that an onion has more DNA than a human. Makes perfect sense to me. Onions are complex creatures.
I’m tempted, but what I need isn’t printable!
Cheers,
Norm.