1

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

enter image description here

Jestin
  • 33
  • 4
  • 4
    What's stopping you from doing it? There are some classes that most of us don't know, so please be precise about the problem you are facing. – Lukas-T Jan 01 '21 at 07:14
  • @churill atmega8 has only 8K ROM and 512 byte RAM. this atmel studio 7 having very limited set of libraries and most of the std:: library functions are not available. I have seen some ostream like implementations which is not possible with this tiny chip. – Jestin Jan 01 '21 at 07:17
  • 1
    Under those circumstances I'd stick to C. While C++ has value without a Standard Library, it's a to code the library from scratch. Behind iostream is a LOT of code. If you're doing this for educational reasons, Awesome. Learning is a great excuse to do stuff you can't afford to do in production. – user4581301 Jan 01 '21 at 07:44
  • @ user4581301 I'm new to c++ , yes of course, this is for learning purpose not for production. :) – Jestin Jan 01 '21 at 08:05
  • 1
    I've implemented LCD as a normal ostream. You can check my implementation here: https://github.com/amanuellperez/mcu/blob/master/src/dev/dev_LCD_HD44780_ostream.h. And here https://github.com/amanuellperez/mcu/blob/master/src/dev/test/HD44780/ostream/main.cpp you can find an example of how I use it (it works the usual way). – Antonio Jan 01 '21 at 09:28
  • @Antonio good work , how did you set the display position with cout<< `Lcd4bit2(4,0)<<"Atmega"; Lcd4bit2(0,1)<<"Val:"< – Jestin Jan 01 '21 at 09:32
  • 1
    @Jestin if you look my implementation of my LCD_HD4480_streambuf, I use a class called `LCD_HD4480_screen`. This class has operation to set de cursor position: `cursor_pos` (and also cursor_pos_x` and `cursor_pos_y`). I use those functions. But till now I didn't think about it, but maybe you can define an iomanip to select the position of the cursor. Something like: `lcd << cursor_pos(2,4) << "hello"`. If you have never implemented an iomanipulator you can check https://github.com/amanuellperez/mcu/blob/master/src/std/std_iomanip.h – Antonio Jan 02 '21 at 16:09
  • @Antonio , Going through your guthub repo there are enough things to learn for me :) – Jestin Jan 03 '21 at 15:44

1 Answers1

3

If you just want syntactic sugar for some basic output operations you could overload operator<< for the required datatypes. You could convert your overloads of LcdPrint to something like this:

LCD &operator<<(LCD &lcd, double val)
{
    char Str[16];
    sprintf(Str, "%f", val);
    lcd.Write(Str);

    return lcd;
}

LCD &operator<<(LCD &lcd, char val)
{
    char Str[2];
    sprintf(Str, "%c", val);
    lcd.Write(Str);

    return lcd;    
}

// Overload for every required datatype

Then you can chain them like this:

LCD lcd;
lcd << 0.5 << 'a';

Not much different from what you have now, but the syntax looks more C++-ish. I'm also not sure if you need to move the cursor between between these calls, but I hope you get the idea.

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
  • Yes, This approach is working, I tried something like this. I'm not sure my implementation is correct or not. `LCD& operator<<(double val) { char Str[16]; sprintf(Str, "%f", val); Write(Str); return *this; }` – Jestin Jan 01 '21 at 08:08
  • position auto increment option is available with 16x2 LCD chip. and I think this is the optimal solution for my issue. Thank you churill for the quick answer. – Jestin Jan 01 '21 at 08:18