[SOLVED] Trying to understand classes

I've got some code I'm trying to adapt, the original code someone wrote and put on git displays data on a 1602 LCD. I'm trying to get it working on a Nokia 5110 LCD.

I have managed to some basic code to display some text:

#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);


void setup()   {
  display.begin();
  display.clearDisplay();   // clears the screen and buffer
  display.println("TEST");
  display.display();
}


void loop() {
  
}

The problem I am having is in the original code it all configured to use classes and I can't figure out how to change this to use the 5110.

I've inserted

#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

to the beginning of the sketch.

But these lines are confusing me:

DSPL(byte RS, byte E, byte DB4, byte DB5, byte DB6, byte DB7) : LiquidCrystal(RS, E, DB4, DB5, DB6, DB7) { }

and

DSPL       disp(LCD_RS_PIN, LCD_E_PIN, LCD_DB4_PIN, LCD_DB5_PIN, LCD_DB6_PIN, LCD_DB7_PIN);

Am thinking I need to replace LiquidCrystal with Adafruit_PCD8544

and then need to change some of the calls so

void DSPL::init(void) {
  LiquidCrystal::begin(8, 2);
  LiquidCrystal::clear();
  full_second_line = false;
}

becomes:

void DSPL::init(void) {
  Adafruit_PCD8544::begin();
  Adafruit_PCD8544::clearDisplay();
  Adafruit_PCD8544::display();
  full_second_line = false;
}

Some pointers would be appreciated. I read up a bit on classes but totally lost.

It looks to me like you are on the right track. Everywhere the DSPL class says "LiquidCrystal", change it to say "Adafruit_PCD8544". Then fix all the places where the compiler chokes because Adafruit_PCD8544 doesn't implement the same methods (functions) as LiquidCrystal.

Thanks. That did the trick. Changed it to Adafruit_PCD8544 and fixed up all the methods and its working perfectly.

Still don't understand classes but will save that learning for a rainy day.

An 'object' or 'object class' is a way of encapsulating (grouping together) variables and functions to protect them from the rest of your program. In the definition of a class you list the "member variables" and "methods" (or "member functions"). You can then declare an object/instance of the class. Each instance has its own copy of the member variables. You can tell an instance to do something by calling a method and that method knows which copy of the member variables it should work on.

In your case you have an object that represents a 'display'. The rest of the sketch does not need to know anything about the display hardware because that is handled by the object. You can switch to an entirely different display device by changing what the methods do, without having to change any other part of the sketch.

1 Like