Notifications
Clear all

Sketch info streamlined

19 Posts
4 Users
0 Reactions
4,970 Views
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

   
Quote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

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

 


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@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)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

Revision 1, includes suggestions from last post.


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

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!!


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@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)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

@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??


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

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++!


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@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)
Member
Joined: 5 years ago
Posts: 1458
 

@pugwash

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

Cheers!


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

@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!!


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@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)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

@frogandtoad

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


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@pugwash

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

 


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@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