Advertisement
Guest User

Untitled

a guest
Apr 18th, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | Source Code | 0 0
  1. template <typename pins_conn>
  2. class lcd
  3. {
  4.   public:
  5.     lcd() = default;
  6.  
  7.     void attach()
  8.     {
  9.       con.attach();
  10.     }
  11.  
  12.     constexpr bool is_4bits() const
  13.     {
  14.       return con.is_4bits;
  15.     }
  16.  
  17.     pins_conn con;
  18. };
  19.  
  20. struct conn_4bits
  21. {
  22.   void attach()
  23.   {
  24.     // Attach 4 pins
  25.   }
  26.  
  27.   void send_cmd(int cmd)
  28.   {
  29.     // Send 4 bits command
  30.   }
  31.  
  32.   int d4, d5, d6, d7;
  33.   int is_4bits = 1;
  34. };
  35.  
  36. struct conn_8bits
  37. {
  38.   void attach()
  39.   {
  40.     // Attach 8 pins
  41.   }
  42.  
  43.   void send_cmd(int cmd)
  44.   {
  45.     // Send 8 bits command
  46.   }
  47.  
  48.   int d0, d1, d2, d3, d4, d5, d6, d7;
  49.   int is_4bits = 0;
  50. };
  51.  
  52. template <typename lcd_con>
  53. void do_something_with_lcd(lcd<lcd_con>& lcd)
  54. {
  55.   // Do something
  56. }
  57.  
  58. int main()
  59. {
  60.   lcd<conn_4bits> lcd_4bits;
  61.   lcd<conn_8bits> lcd_8bits;
  62.  
  63.   // Setup
  64.   lcd_4bits.con.d4 = 1;
  65.   lcd_8bits.con.d0 = 2;
  66.   // Etc
  67.  
  68.   lcd_4bits.attach();
  69.   lcd_8bits.attach();
  70.  
  71.   if (lcd_4bits.is_4bits())
  72.   {
  73.     // Do something...
  74.   }
  75.  
  76.   do_something_with_lcd(lcd_4bits);
  77.   do_something_with_lcd(lcd_8bits);
  78.  
  79.   return 0;
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement