From 4f96161a387c3bc32ee084f14095e50ab9716b5e Mon Sep 17 00:00:00 2001 From: EnricoGuccii Date: Sun, 11 May 2025 21:42:04 +0200 Subject: add oled --- lib/motors/motors.cpp | 4 +-- lib/oled/oled.cpp | 88 ++++++++++++++++++++++++++------------------------- lib/oled/oled.h | 32 +++++++++---------- src/main.cpp | 60 +++++------------------------------ 4 files changed, 71 insertions(+), 113 deletions(-) diff --git a/lib/motors/motors.cpp b/lib/motors/motors.cpp index d0e71b4..68c633d 100644 --- a/lib/motors/motors.cpp +++ b/lib/motors/motors.cpp @@ -123,7 +123,7 @@ void MOTORS::backward(int speed, int duration) } } -void MOTORS::leftTurn(int speed, int duration) +void MOTORS::rightTurn(int speed, int duration) { rightForward(speed); leftBackward(speed); @@ -134,7 +134,7 @@ void MOTORS::leftTurn(int speed, int duration) } } -void MOTORS::rightTurn(int speed, int duration) +void MOTORS::leftTurn(int speed, int duration) { rightBackward(speed); leftForward(speed); diff --git a/lib/oled/oled.cpp b/lib/oled/oled.cpp index db1b556..6036c70 100644 --- a/lib/oled/oled.cpp +++ b/lib/oled/oled.cpp @@ -1,43 +1,45 @@ -// #include -// #include -// #include -// #include -// #include "settings.h" -// #include "oled.h" - - -// OLED::OLED() : display(OLED_WIDTH, OLED_HEIGHT, &Wire, -1) -// { -// // Najpierw uruchom I2C -// Wire.begin(SDA_PIN, SCL_PIN); - -// // Potem dopiero inicjalizuj OLED -// if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) { -// Serial.println("OLED init failed!"); -// } - -// display.clearDisplay(); -// display.display(); -// } - - -// void OLED::print(const String &text, int x, int y) -// { -// display.setTextSize(1); -// display.setTextColor(WHITE); -// display.setCursor(x, y); -// display.println(text); -// display.display(); -// } - -// void OLED::clear() -// { -// display.clearDisplay(); -// display.display(); -// } - -// void OLED::drawBox() -// { -// display.drawRect(0, 0, 128, 64, WHITE); -// display.display(); -// } +#include +#include +#include +#include +#include "settings.h" +#include "oled.h" + + +OLED::OLED() +: display(OLED_WIDTH, OLED_HEIGHT, &Wire, -1) {} + +void OLED::init() { + Wire.begin(SDA_PIN, SCL_PIN); + display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR); + display.clearDisplay(); + display.setTextSize(1); + display.setTextColor(SSD1306_WHITE); +} + +void OLED::showMessage(const String& msg, int size, int x, int y) { + display.clearDisplay(); + display.setCursor(x, y); + display.setTextSize(size); + display.setTextColor(SSD1306_WHITE); + display.println(msg); + display.display(); +} + +void OLED::drawBitmap(const uint8_t* bitmap) { + display.clearDisplay(); + display.drawBitmap(0, 0, bitmap, OLED_WIDTH, OLED_HEIGHT, SSD1306_WHITE); + display.display(); +} + +void OLED::showAnimation(const uint8_t* animation[], int frames, int delayTime) { + for (int i = 0; i < frames; i++) { + drawBitmap(animation[i]); + delay(delayTime); + } +} + +void OLED::clear() { + display.clearDisplay(); + display.display(); +} diff --git a/lib/oled/oled.h b/lib/oled/oled.h index 7cda336..4873285 100644 --- a/lib/oled/oled.h +++ b/lib/oled/oled.h @@ -1,18 +1,18 @@ -// #pragma once -// #include -// #include "settings.h" +#pragma once +#include +#include "settings.h" -// class OLED -// { -// public: -// OLED(); -// void clear(); -// void print(const String &text, int x, int y); -// void drawBox(); -// void setCursor(int x, int y); -// void setTextSize(int size); -// void showAnimation(); -// private: -// Adafruit_SSD1306 display; -// }; +class OLED { +public: + OLED(); + + void init(); + void showMessage(const String& msg,int size = 1 ,int x = 0, int y = 0); + void drawBitmap(const uint8_t* bitmap); + void showAnimation(const uint8_t* animation[], int frames, int delayTime); + void clear(); + +private: + Adafruit_SSD1306 display; +}; diff --git a/src/main.cpp b/src/main.cpp index 84b5b85..9a6be80 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,66 +6,22 @@ #include "motors.h" RGB rgb; -BUZZER buzzer; -MOTORS motors; // speed 80 is prefered, because of higher input voltage (5V -> 3.7V after H-bridge), motor is rated for 3V - // in practice, speed below 70 dosn't even work. - - +BUZZER buzzer; +MOTORS motors; // speed 80 is prefered, because of higher input voltage (5V -> 3.7V after H-bridge), motor is rated for 3V +// in practice, speed below 70 dosn't even work. +OLED oled; void setup() { rgb.init(); buzzer.init(); motors.init(); - + oled.init(); + oled.showMessage("czesc"); + //oled.drawBitmap(Logo); } void loop() { - rgb.setColorRGB(0, 255, 0, true); - motors.forward(100, 2000); - delay(1000); - rgb.setColorRGB(255, 0, 0, true); - motors.backward(100, 2000); - delay(1000); - rgb.setColorRGB(0, 255, 255, true); - motors.leftTurn(100, 2000); - delay(1000); - rgb.setColorRGB(255, 255, 0, true); - motors.rightTurn(100, 2000); - delay(1000); - - motors.rightForward(100, 2000); - delay(1000); - motors.rightBackward(100, 2000); - delay(1000); - motors.leftForward(100, 2000); - delay(1000); - motors.leftBackward(100, 2000); - delay(1000); - - // buzzer.playMelody(happy, 8); - // delay(1000); - // buzzer.playMelody(anger, 8); - // delay(1000); - // buzzer.playMelody(alaarm, 9); - // delay(1000); - // buzzer.playMelody(surprise, 9); - // delay(1000); - - //buzzer.playMelody(melody, 8); - // rgb.setColorRGB(255, 0, 0, true); - // delay(1000); - // rgb.fadeColor(0, 255, 0, 1000); - // delay(1000); - // rgb.blink(3, 500); - // delay(1000); - // rgb.breathe(4, 1000); - // rgb.rainbowCycle(8000, 1.0); - // rgb.setColorRGB(0, 0, 0, true); - // delay(1000); - // rgb.rainbowCycle(8000, 1); - // rgb.setColorRGB(0, 0, 0, true); - // delay(1000); - // rgb.rainbowCycle(8000, 2); + } \ No newline at end of file -- cgit v1.2.3