I'm trying to design a menu interface on a 1.8'' TFT display module. I've got it working with the Adafruit_ST7735 + Adafruit_GFX libraries. At least when I was just trying out the features... But now I'm trying to organize the code in Classes as this thing will get too complex soon. I have done some Java and C, so I started learning C++. I've been reading and coding nonstop for a week now, but there are some things that I can't wrap my head around that keep me from making any progress for a couple of days already.
So... For example there's a function public void drawRect(); in Adafruit_ST7735 library and I have a function draw() that draws a bunch of related stuff to the display in my own class (Rectangle.cpp). Say I initialize the display in my main program file how can I use that drawRect() function in my Rectangle::draw()?
Also Rectangle will be used for composing other parts of the menu interface and will probably be initialized in a collection or as a member of another class.
I've tried a bunch of different things but I've come to a dead end.
EDIT:
main program:
#include "_v0_0_0.h"
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include "Point.h"
#include "Rectangle.h"
#include "SomeView.h"
#define TFT_CS 4
#define TFT_RST 0
#define TFT_DC 10
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
SomeView a_view;
void setup()
{
tft.initR(INITR_BLACKTAB); // initializes the display
}
void loop()
{
a_view.draw(); //draw something to view (a button, scrollbar, menu item, etc)
while (true) {}
}
SomeView.h:
#include "Rectangle.h"
class SomeView {
private:
Rectangle a_rectangle;
public:
SomeView();
void draw();
const Rectangle& getRectangle() const {
return a_rectangle;
}
void setRectangle(const Rectangle& rectangle) {
a_rectangle = rectangle;
}
};
SomeView.cpp:
#include "SomeView.h"
SomeView::SomeView() :
a_rectangle(Point::Point(0, 0), Point::Point(0, 0)) {
}
void SomeView::draw() {
a_rectangle.setCorners(Point::Point(0, 0), Point::Point(2, 6));
a_rectangle.draw();
a_rectangle.setCorners(Point::Point(20, 66), Point::Point(100, 100));
a_rectangle.draw();
}
Rectangle.h:
#include "Point.h"
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
class Rectangle{
private:
Point p1;
Point p2;
public:
Rectangle();
Rectangle(Point& p1, Point& p2);
void setCorners(const Point& p1, const Point& p2);
void draw();
// ... getters and setters for the points
};
Rectangle.cpp:
#include "Rectangle.h"
Rectangle::Rectangle() : p1(), p2()
{
}
Rectangle::Rectangle(Point& p1, Point& p2) : p1(p1), p2(p2)
{
}
void Rectangle::setCorners(const Point& p1, const Point& p2) {
this->p1 = p1;
this->p2 = p2;
}
// this is where I want to use a function from the adafruit library
void Rectangle::draw() {
Adafruit_ST7735::drawRect(p1.getX(), p1.getY(), p2.getX(), p2.getY(), ST7735_GREEN);
}