// sprintf() LCD1602 and Pi Pico!
// https://programmersqtcpp.blogspot.com/2022/04/arduino-lcd-con-sprintf.html
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

// sprintBuff è il buffer di riga
char sprintBuff[17];

/* void floatPosCounter402()
  stampa da 0.00 a 1.00
    - allineato a destra (default).
    - 4 caratteri incluso il punto decimale.
    - .2 decimali.
  */
void floatPosCounter402_01() {
    float fnum = 0.0;
    while (true) {
        sprintf(sprintBuff, "%4.2f", fnum);
        lcd.setCursor(0, 0);
        lcd.print(sprintBuff);
        fnum += 0.01;
        if (fnum > 1.0)
          break;
        delay(10);
    }
    delay(1000);
    lcd.clear();
}

/* void floatPosCounter402_10()
  stampa da 1.00 a 0.00
    - allineato a destra (default).
    - 4 caratteri incluso il punto decimale.
    - .2 decimali.
*/
void floatPosCounter402_10() {
    float fnum = 1.0;
    while (true) {
        sprintf(sprintBuff, "%4.2f", fnum);
        lcd.setCursor(0, 0);
        lcd.print(sprintBuff);
        fnum -= 0.01;
        if (fnum < 0.0)
          break;
        delay(10);
    }
    delay(1000);
    lcd.clear();
}

/* void floatPosNegAl502_1()
    stampa -1.00 su riga 0 e +1.00 su riga 1
*/
void floatPosNegAl502_1() {
    float fnum = -1.0;
    sprintf(sprintBuff, "%+5.2f", fnum);
    lcd.setCursor(0, 0);
    lcd.print(sprintBuff);

    fnum = 1.0;
    sprintf(sprintBuff, "%+5.2f", fnum);
    lcd.setCursor(0, 1);
    lcd.print(sprintBuff);

    delay(1000);
    lcd.clear();
}

/* void hell()
   Stampa solo .4 caratteri della stringa "Hello World!".
*/ 
void hell() {
    const char hell[] = "Hello World!";
    sprintf(sprintBuff, "%.4s", hell);
    lcd.setCursor(0, 0);
    lcd.print(sprintBuff);
    delay(1000);
    lcd.clear();
}

/* void faround123()
  Arrotonda per difetto
*/
void faround123() {
    float fnum = 123.4;
    sprintf(sprintBuff, "%.3g", fnum);
    lcd.setCursor(0, 0);
    lcd.print(sprintBuff);
    delay(1000);
    lcd.clear();
}

/* void faround124()
  Arrotornda per eccesso
*/
void faround124() {
    float fnum = 123.5;
    sprintf(sprintBuff, "%.3g", fnum);
    lcd.setCursor(0, 0);
    lcd.print(sprintBuff);
    delay(1000);
    lcd.clear();
}

/* void earound()
  Arrotornda per difetto.
  Stampa in notazione esponenziale
*/
void earound() {
    float fnum = 1234.120;
    sprintf(sprintBuff, "%.3e", fnum);
    lcd.setCursor(0, 0);
    lcd.print(sprintBuff);
    delay(1000);
    lcd.clear();
}

/* void garound()
   Non usa notazione esponenziale.
   Non arrotonda
*/
void garound() {
    float fnum = 1234567.12;
    sprintf(sprintBuff, "%.9g", fnum);
    lcd.setCursor(0, 0);
    lcd.print(sprintBuff);
    delay(1000);
    lcd.clear();
}

void setup() {
    Serial.begin(115200);
    lcd.begin(16, 2);

    // termina il buffer di riga con '\0'
    sprintBuff[ 17 - 1 ] = '\0';

    floatPosCounter402_01();
    floatPosCounter402_10();
    floatPosNegAl502_1();
    hell();
    faround123();
    faround124();
    earound();
    garound();
    delay(1000);
}

void loop() {
    delay(1); // Adding a delay() here speeds up the simulation
}
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT