//Copyright (C) 24.09.2023, Kirill ZHivotkov
/*
Эта программа - демонстрация работы OLED-дисплея.
Для запуска необходимо запустить симуляцию. Далее программа
использует джойстик для перемещения пиксела по экрану.
*/
#include <Adafruit_SSD1306.h>
#define OLED_WIDTH 128
#define OLED_HEIGHT 64
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT);
class Pixel {
private:
int x = 0;
int y = 0;
public:
//Сеттеры
void setPosX(int x) {
this->x = x;
}
void setPosY(int y) {
this->y = y;
}
//Сеттеры
//Геттеры
int getPosX() {
return this->x;
}
int getPosY() {
return this->y;
}
//Геттеры
//Методы класса
void move(int speed) {
int a = analogRead(A1);
int b = analogRead(A0);
if (a == 1023 && b == 512) { //LEFT
if (this->getPosX() >= 1 && this->getPosX() <= OLED_WIDTH - 1) {
this->setPosX(this->getPosX() - 1);
}
} else if (a == 0 && b == 512) { //RIGHT
if (this->getPosX() >= 0 && this->getPosX() < OLED_WIDTH - 1) {
this->setPosX(this->getPosX() + 1);
}
} else if (b == 1023 && a == 512) { //UP
if (this->getPosY() >= 1 && this->getPosY() <= OLED_HEIGHT - 1) {
this->setPosY(this->getPosY() - 1);
}
} else if (b == 0 && a == 512) { //DOWN
if (this->getPosY() >= 0 && this->getPosY() < OLED_HEIGHT - 1) {
this->setPosY(this->getPosY() + 1);
}
}
display.drawPixel(this->x, this->y, SSD1306_WHITE);
display.display();
display.clearDisplay();
Serial.print("x => ");
Serial.println(this->x);
Serial.print("y => ");
Serial.println(this->y);
delay(speed);
}
//Методы класса
//Конструктор по умолчанию
Pixel() {
this->x = OLED_WIDTH / 2;
this->y = OLED_HEIGHT / 2;
}
//Конструктор по умолчанию
//Деструктор
~Pixel() {}
//Деструктор
};
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
Serial.begin(9600);
}
Pixel* px = new Pixel(); //Создаем объект класса Pixel (пиксель на экране дисплея)
void loop() {
px->move(30); //Двигаться в пределах дисплея с задержкой 30 миллисекунд
}