Start a new topic

Another (light) Arduino library and some formatted printing helper

 Guys,


I would like to donate a tiny Nextion library for use on Arduinos I built from scratch for fun. It is by far not as capable as the "official" library, but it has a very small footprint.


The library has been developed and tested on both an Arduino Zero Pro and an Arduino Uno. It works on both, although 32bit handling of EEPROM values does not completely satisfy me on the Uno yet.


You are left to your own with it - I deny any responsibility for whatever you may do with it. It is all your fault :-)


A few words of documentation can be found in the NextionLightLib.h file. Additional insight may come from the example INO file I also attached. The demo will need the attached HMI file to work properly.


The Nextion Light Library and the demo INO both make heavy use of another small library: mini-printf, that is also attached. This library provides a very small implementation of the snprintf() function (named "mini_snprinf()") to write formatted text into char buffers. As an add-on, a "mini_uprintf()" variant is given, that is able to write formatted text to a Serial interface directly. 


Again no formal documentation exists - see the mini-printf.h file for some hints.


Have fun with it!



zip
zip
HMI
ino

3 people like this idea

Thanks Michael for your contribution to the community.

Might I ask for you to post some specifics numbers as to its footprint?

Perhaps as some relatively close comparison to the official?

Placed a link to this thread in Tools, Tips, Tricks and How Tos.

 Please find attached a slightly updated version:

  • getObjname() and setObjname() removed, as that simply makes no sense :-)
  • maxX(), maxY(), is_enhanced_version() and type() added to report the dimensions etc. of the currently used device


As for the sizes: I do not think a comparison with the ITEAD Arduino library is sensible, as the ITEAD lib is able to do so much more.


All I can say is that the demo INO with both libs integrated compiles to an 18kB HEX file. If you need even less, I would suggest to throw out all the attribute helper functions from NextionLightLib.h. Those are all declared "inline" and should shine up only if used, but I suspect the Arduino IDE integrates them as code anyway.


If the helper functions are removed, any attribute can be set/gotten with "setAtt()" and "getAtt()".


Any suggestions on the libs are welcome, by the way, one never is at the final end of development :-)

zip

Hi Michael,


"although 32bit handling of EEPROM values does not completely satisfy me on the Uno yet."


To get things working on the 8 bit Uno's and others, a lot of your 'int' variables will need 'upgrading' to uint32_t. It's a compiler thing, where the 8 bit AVR's get a default 2 byte address for 'int' or 'unsigned int'  


Especially important when bit shifting more than 16 bits and some of the printf stuff like %u. As the background function variables also get set as 2 byte by default. So %u would need a case for %lu or %ld


http://forum.arduino.cc/index.php?topic=156850.0

https://www.arduino.cc/en/Reference/Int


That said, I don't now want you to go away and fix it just for me, as I don't use any such libraries in my Nextion communications. I'm only highlighting issues I've had myself when porting 32 bit code to 8 bit.


Other than that, nice job :) 


Steve. 

Hi Steve,

thank you for looking that deep inside! :-)

I know that the "long" versions of int need to be used on the Uno, but since I am doing development lately only on the Zero I confess I was too  lazy to put a distinction in. Especially as I was bitten more than once with the odd treatment of #ifdef and #define in the Arduino IDE...

Thanks anyway!

 

Update to reflect the changes in Nextion Editor 0.42 (get_key() and get_type() added). mini_printf now knows the "%C" and "%S" formats, replacing characters below 0x20 by '.' for safe printing without side effects.

%u still does not work correctly on 8bit Atmels - use 32bit ARMs instead! ;-)

 

zip
zip
Michael, I'm not a 'C' programmer, but can read it eventually, but I'm having trouble understanding the PREPROCESSOR commands that you have at the beginning of CPP file. I understand about getting and setting of attributes, but the mechanics of the preprocessing is not something I'm familiar with. What do these substitutions create? (I'm thinking on translating your LIB into a LIB for another MCU.)

 

// Some preprocessor magic to encode all set/get functions
// Functions will be named "set_att()" / "get_att()", "att" standing for all Nextion attributes
// like bco, font, maxlen etc.
// SETGETVAL(att) always includes GETVAL(att)
#define GETVAL(_att) inline NextionError get_##_att(const char *obj, unsigned int *val) { return getAtt(obj, (const char *)#_att, val); }
#define SETGETVAL(_att) GETVAL(_att) \
   inline NextionError set_##_att(const char *obj, unsigned int val) { return setAtt(obj, (const char *)#_att, val); }

// Same as for the attributes, applied here to all system variables
#define GETSYS(_att) inline NextionError get_##_att(unsigned int *val) { return getNum((const char *)#_att, val); }
#define SETGETSYS(_att) GETSYS(_att) \
   inline NextionError set_##_att(unsigned int val) { return setNum((const char *)#_att, val); }

 

 

Hi Paul, Those preprocessor #defines pay to my lazyness ;-)
Down the file you will find for instance a line "GETVAL(bco)".
That is epanded to inline

NextionError get_bco(const char *obj, unsigned int *val) {
return get_att(obj, (const char *) "bco", val) ;
}

The other #defines work accordingly.
Sorry, I wrote the post on a tablet. All the cleverly set line breaks got removed. I hope you got the idea anyway.
Ok. A "simple" substitution. I'll study that for my translation. Thanks for the quick reply (and the plain old C library!)

 

It is in fact C++, but leaving out the posh parts of that language ! :-)

Paul, out of curiousity: which language and compiler are you converting it too?

Patrick, I'm a little shy about posting much about what I'm going to do because I have spent so little time evaluating the project. I don't know enough about the library to know what I don't know. :D

 

Please do not hesitate to ask, if I can be of any help, Paul.
Login or Signup to post a comment