How to write a Cout<< Like print function for 16×2 LCD?

  atmel, avr, c++, cout

Development Environment

Controller: Atmega8
Language: C++ (-std=gnu++14 flag added )

IDE: atmel studio 7

Note: Most of the std:: libraries like ostream ,string ,cout, are not available with this minimal c++ support.

almost similar functionality has been implemented ,but the function call is different

printf1(<object>, ... ); 
 printf1(Lcd4bit2,"Val:",x,'=',5,"+",3.4);

I would like to change the code mentioned above ,something like this
LcdOut<<"Val:"<<m<<'='<<5<<3.4; , with minimal and efficient code.

class LCD
{
  public:  

  void Write(const char* prnt)  
  {
     //16x2 LCD write code 
  }
};

    void LcdPrint(const char * val,LCD obj)
    {
        obj.Write(val);
    }
    
    void LcdPrint(char val,LCD obj)
    {
        char Str[2];
        sprintf(Str, "%c", val);
        obj.Write(Str);
    }
    
    void LcdPrint(double val,LCD obj)
    {
        char Str[16];
        sprintf(Str, "%f", val);
        obj.Write(Str);
    }
    
    void LcdPrint(int val,LCD obj)
    {
        char Str[16];
        sprintf(Str, "%d", val);
        obj.Write(Str);
    }
    
    template <typename T1,typename T0>
    void printf1(T1 Obj,T0 value) 
    {
        LcdPrint(value,Obj);
    }
    
    template <typename T1,typename T, typename... Ts>
    void printf1(T1 Obj,T value, Ts ... args) 
    {
        LcdPrint(value,Obj);
        printf1(Obj,args...);
    }
    
    int main(void)
    {
        char x ='m';
        Lcd4bit2.SetPos();  // set curser to 0,0 
        printf1(Lcd4bit2,"Val:",x,'=',5,"+",3.4);
    }

expected output
https://i.stack.imgur.com/NYNo2.png

Source: Windows Questions C++

LEAVE A COMMENT