Sketch info streamlined  

Page 1 / 2
  RSS

Pugwash
(@pugwash)
Prominent Member
Joined: 10 months ago
Posts: 969
2020-02-16 12:06 pm  

Until now I have been using the following code for sketch templates:

#include <string.h> // not really needed!
#define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)

void setup() {
//
// Print filename
//
Serial.begin(9600);
Serial.print("*Source file: ");
Serial.println(__FILENAME__); // Source file name
Serial.print("Compile date: ");
Serial.println(__DATE__);
Serial.print("Compile time: ");
Serial.println(__TIME__);
Serial.println();

// Serial.println(__FILE__); // prints the full path
}

void loop() {
  // put your main code here, to run repeatedly:

}

But I have decided this is too cluttered and have changed to this;

#include <FileHeaderInfo.h>

// true shows file info, false surpresses file info
FileInfo fileInfo(true);

void setup() {
  // starts the serial monitor interface and shows sketch details when "true"
  fileInfo.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:

}

This requires the following 3 files to be copied to a new folder in the Arduino/libraries folder, I have called it simply "FileHeaderInfo".

This topic was modified 2 months ago by Pugwash

SteveC - Topple Rudd Poltman


Quote
Topic Tags
Pugwash
(@pugwash)
Prominent Member
Joined: 10 months ago
Posts: 969
2020-02-16 1:11 pm  

Replace keywords.txt file with this version for correct syntax colouring!

 

SteveC - Topple Rudd Poltman


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 10 months ago
Posts: 384
2020-02-16 2:36 pm  

@pugwash

FileHeaderInfo.h

Pic a suitable name, and then wrap all the code in your header file between "include/inclusion guards":

#ifndef ABOUT_PROGRAM_INFORMATION_2020
#define ABOUT_PROGRAM_INFORMATION_2020 

// [...CODE...]

#endif

If you do that, then you'll never have a problem with even multiple inclusions:

#include <FileHeaderInfo.h>
#include <FileHeaderInfo.h>
#include <FileHeaderInfo.h>

...adding three in a row will not cause a problem when guards are added.

FileHeaderInfo.cpp

The function "strrchr(..., ...)" is implemented in the string library, and so to ensure that it always works, you'll need to include:

#include <string.h>

As explained in another post recently, your constructor should also make use of an initialisation list:

FileInfo::FileInfo(bool displayMsg) : _msg(displayMsg) {
  // Anything you need when instantiating your object goes here
 }

These are all minor changes, so shouldn't cause you too much heartache 🙂

By the way... you know that I always prefer to use C++ functions and objects where possible, and so will only use legacy C functions and associated coding style when memory actually becomes a problem.  Having said that, I would have probably implemented something like the following:

const String getFilename(const String& filename) {
  return filename.substring(filename.lastIndexOf('\\') + 1);
 }
 
void setup() {
  Serial.println(getFilename(__FILE__));
 }

String is also included by default, so you avoid including an extra library too.

Cheers! ?


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 10 months ago
Posts: 969
2020-02-16 4:25 pm  

Revision 1, includes suggestions from last post.

SteveC - Topple Rudd Poltman


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 10 months ago
Posts: 969
2020-02-16 5:55 pm  

To my shame and embarrassment, this library is about as much use as a chocolate watch ? ? ? .

I have just found the deliberate(not) mistake, "of course, it won't work" a small voice in my head is screaming "it's showing the path to the .cpp file, not the .ino file"!

Never mind, back to the drawing board!!

SteveC - Topple Rudd Poltman


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 10 months ago
Posts: 384
2020-02-17 3:08 am  

@pugwash

Posted by: @pugwash

Never mind, back to the drawing board!!

LOL... all is not lost, you can just change your member function signature to:

void FileInfo::begin(const char* filename, int baudRate) {

Then change the following line in the member function to:

Serial.println((strrchr(filename, '\\') ? strrchr(filename, '\\') + 1 : filename));

Then in your INO file:

fileInfo.begin(__FILE__);         // Using default baud rate, or...
fileInfo.begin(__FILE__, 115200); // Use a different baud rate

Not exactly what you wanted, I know... but still better than the original.

Also, maybe not a bad idea to extend the following?

#if (ARDUINO >=100)
  #include "Arduino.h"
#elif (ARDUINO < 100)
  #include "WProgram.h"
#else
  #error "This library does not support your processor!"
#endif

Cheers!


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 10 months ago
Posts: 969
2020-02-17 10:18 am  

@frogandtoad

Ralph Bacon came up with an interesting point in his video on simple libraries and that was about selective compiling.

As we all know the Arduino has limited space for programs, and if you pack all of your favourite functions in a library, but only need one then there is apparently a lot of extra unwanted baggage being compiled into the sketch. Ralph meant, when he talks about selective compiling, that the compiler only compiles the function required by the main sketch, ignoring the rest.

I don't know whether there is such a compiler available, my guess is that the Arduino IDE just uses the standard GNU C++ compiler that comes with every UNIX clone OS. And therefore the whole referenced library is compiled into the finished binary file.

The way I see it is that if you are only writing a single standalone sketch that repetitively needs a piece of code then a function within the sketch would suffice, but if this code could be used in a multitude of different sketches, then putting that code in a library would make a lot more sense.

I do think that putting code in a library just for the sake of putting code in a library is just a waste of time and memory space.

Your thoughts??

This post was modified 2 months ago by Pugwash

SteveC - Topple Rudd Poltman


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 10 months ago
Posts: 969
2020-02-17 12:03 pm  

As an addition to the above post, in Python parts of imported modules can be selected individually, I guess this is not possible in C++!

SteveC - Topple Rudd Poltman


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 10 months ago
Posts: 384
2020-02-17 12:07 pm  

@pugwash

Happy to give you my thoughts, but I must first know exactly what you mean, i.e:- what you read from Ralph Bacon to make sure I understand the question properly... do you have a link so I can check it out?

Cheers!


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 10 months ago
Posts: 384
2020-02-17 12:08 pm  

@pugwash

Again, a simple example if you could?
Did you mean like the import statement?

Cheers!


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 10 months ago
Posts: 969
2020-02-17 12:13 pm  

@frogandtoad

Python example:

from hashlib import md4..............to load just the md4 part of hashlib

import hashlib...............................to load all the hashlib module

This is what I mean!!

SteveC - Topple Rudd Poltman


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 10 months ago
Posts: 384
2020-02-17 12:59 pm  

@pugwash

Posted by: @pugwash

Python example:

from hashlib import md4..............to load just the md4 part of hashlib

import hashlib...............................to load all the hashlib module

This is what I mean!!

I thought so, but the closest I think we can get to that right now[1] is separating functionality into separate header files and namespaces, and including what we need.

[1] - The new C++ standard due for release this year will support python style imports as they are planning on doing away with header files, and compiler creators expected to implement most new features... I'd guess that some compilers may have already done so ahead of time based on the ISO draft.

Cheers!


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 10 months ago
Posts: 969
2020-02-17 1:12 pm  

@frogandtoad

This is the link to Ralph Bacon's library video, 30 mins long but he only mentioned "selective compilation" in passing!

SteveC - Topple Rudd Poltman


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 10 months ago
Posts: 384
2020-02-17 1:15 pm  

@pugwash

OK, thanks for the link... checking it out now!

 


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 10 months ago
Posts: 384
2020-02-17 2:52 pm  

@pugwash

Checked it out... although I didn't quite hear him talk about selective compiling, I did hear him talk about compiler optimisation, if that's what you meant?  This is true, good compilers can do a great job of optimising away variables etc... and offer compiler options on how aggressive to allow the compiler to optimise the final executable.

Now I know where all that code from two different people here came from! 🙂

By the way... the reason that he is unable to run Serial.begin(9600); and see a valid output in the constructor body, is because the class object hasn't been validly created until the execution of the constructor has actually completed. 

I don't know enough about the specification of the Serial object's parent, but there may be a way to do it in the constructor initialisation list, which you've heard me say for a while now, runs before the body of the constructor, and this is one of those cases where it can be valuable, if the Serial object or parent offers a way to initialise the baud rate in the initialisation list... remembering that the Serial object is already previously constructed and globally available.

Cheers!


ReplyQuote
Page 1 / 2

Please Login or Register