From 34c6a104a2fe74c413d30c661002d47cb4cf08f4 Mon Sep 17 00:00:00 2001 From: DashyFox Date: Sun, 24 Sep 2023 22:26:05 +0300 Subject: [PATCH] abstract for external display lib --- MusicRing.ino | 91 ++++++++++++++------------- implement/Display__Adafruit_SSD1306.h | 26 ++++++++ modules/Display.h | 35 +++++++++++ modules/LED_Ring.h | 17 +++++ 4 files changed, 124 insertions(+), 45 deletions(-) create mode 100644 implement/Display__Adafruit_SSD1306.h create mode 100644 modules/Display.h create mode 100644 modules/LED_Ring.h diff --git a/MusicRing.ino b/MusicRing.ino index 16bbc71..f55cce1 100644 --- a/MusicRing.ino +++ b/MusicRing.ino @@ -1,8 +1,9 @@ -#include -#define SCREEN_WIDTH 128 // OLED display width, in pixels -#define SCREEN_HEIGHT 32 // OLED display height, in pixels +#include +//#include -Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); +#include "implement/Display__Adafruit_SSD1306.h" + +Display_Adafruit_SSD1306 screen(128, 32, Display::Ori_HORIZONTAL); #define pin_Data 5 // yellow #define pin_CLK_inside 6 // orange @@ -21,6 +22,7 @@ volatile uint16_t frec = 42; // частота MusicRing volatile uint16_t t_period_takts = F_CPU / 8 / LED_count / frec; void setup() { + Serial.begin(115200); pinMode(pin_Data, OUTPUT); @@ -29,45 +31,45 @@ void setup() { pinMode(pin_RESET, OUTPUT); pinMode(pin_OutputEnable, OUTPUT); - display.begin(SSD1306_SWITCHCAPVCC, 0x3C); - display.clearDisplay(); + screen.begin(); // 'Text1', 128x32px static const uint8_t DashyFox [] PROGMEM = { - 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, - 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, - 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, - 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, - 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, - 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, - 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, - 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, - 0xc0, 0x07, 0xf0, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x3f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x03, - 0x80, 0x07, 0xf8, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x3f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, - 0x80, 0x07, 0xfc, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x3f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, - 0x80, 0x06, 0x1c, 0x0f, 0x60, 0xfc, 0x7f, 0x03, 0x9c, 0x30, 0x01, 0xe0, 0xc3, 0x00, 0x00, 0x01, - 0x80, 0x06, 0x0e, 0x1f, 0xe1, 0xfc, 0x7f, 0x87, 0xbc, 0x30, 0x07, 0xf0, 0xe7, 0x00, 0x00, 0x01, - 0x80, 0x06, 0x0e, 0x3f, 0xe1, 0xfc, 0x7f, 0xc7, 0xbc, 0x3f, 0x87, 0xf8, 0xe7, 0x00, 0x00, 0x01, - 0x80, 0x06, 0x0e, 0x30, 0xe3, 0x8c, 0x71, 0xc3, 0x0c, 0x3f, 0xce, 0x38, 0x7e, 0x00, 0x00, 0x01, - 0x80, 0x06, 0x0e, 0x70, 0xe1, 0xe0, 0x70, 0xc7, 0x0c, 0x3f, 0x8e, 0x18, 0x3c, 0x00, 0x00, 0x01, - 0x80, 0x06, 0x0e, 0x70, 0xe1, 0xf8, 0x70, 0xc7, 0x0c, 0x30, 0x0c, 0x18, 0x3c, 0x00, 0x00, 0x01, - 0x80, 0x06, 0x0e, 0x70, 0xe0, 0xfc, 0x70, 0xc7, 0x0c, 0x30, 0x0c, 0x18, 0x3c, 0x00, 0x00, 0x01, - 0x80, 0x06, 0x0c, 0x70, 0xe0, 0x1c, 0x70, 0xc7, 0x0c, 0x30, 0x0c, 0x18, 0x3c, 0x00, 0x00, 0x01, - 0x80, 0x06, 0x1c, 0x31, 0xe3, 0x8c, 0x71, 0xc3, 0x0c, 0x30, 0x0e, 0x38, 0x7e, 0x00, 0x00, 0x01, - 0x80, 0x07, 0xf8, 0x3f, 0xf3, 0xfc, 0x79, 0xe3, 0xfc, 0x30, 0x07, 0xf8, 0xe7, 0x00, 0x00, 0x01, - 0x80, 0x07, 0xf0, 0x1f, 0xf1, 0xfc, 0x79, 0xe1, 0xfc, 0x30, 0x07, 0xf0, 0xe7, 0x00, 0x00, 0x01, - 0x80, 0x07, 0xe0, 0x0f, 0x60, 0xf8, 0x71, 0xc1, 0xfc, 0x30, 0x01, 0xe0, 0xc3, 0x00, 0x00, 0x01, - 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf0, 0x03, - 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xbc, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xf8, 0x03, - 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf0, 0x07, - 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, - 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, - 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, - 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, - 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, - 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff +/* 0 */// 1 8 | 9 16| 17 24| 25 32| 33 40| 41 48| 49 56| 57 64| 65 72| 73 80| 81 88| 89 96| 97 104| 105 112| 113 120| 121 128| +/* 1 */ 0b11111111, 0b11100000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000111, 0b11111111, +/* 2 */ 0b11111111, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b11111111, +/* 3 */ 0b11111100, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00111111, +/* 4 */ 0b11111000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00011111, +/* 5 */ 0b11110000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00001111, +/* 6 */ 0b11100000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000111, +/* 7 */ 0b11100000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b11110000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000111, +/* 8 */ 0b11000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b11110000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000011, +/* 9 */ 0b11000000, 0b00000111, 0b11110000, 0b00000000, 0b00000000, 0b00000000, 0b11110000, 0b00000000, 0b00000000, 0b00111111, 0b10000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000011, +/* 10 */ 0b10000000, 0b00000111, 0b11111000, 0b00000000, 0b00000000, 0b00000000, 0b01110000, 0b00000000, 0b00000000, 0b00111111, 0b10000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000001, +/* 11 */ 0b10000000, 0b00000111, 0b11111100, 0b00000000, 0b00000000, 0b00000000, 0b01110000, 0b00000000, 0b00000000, 0b00111111, 0b10000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000001, +/* 12 */ 0b10000000, 0b00000110, 0b00011100, 0b00001111, 0b01100000, 0b11111100, 0b01111111, 0b00000011, 0b10011100, 0b00110000, 0b00000001, 0b11100000, 0b11000011, 0b00000000, 0b00000000, 0b00000001, +/* 13 */ 0b10000000, 0b00000110, 0b00001110, 0b00011111, 0b11100001, 0b11111100, 0b01111111, 0b10000111, 0b10111100, 0b00110000, 0b00000111, 0b11110000, 0b11100111, 0b00000000, 0b00000000, 0b00000001, +/* 14 */ 0b10000000, 0b00000110, 0b00001110, 0b00111111, 0b11100001, 0b11111100, 0b01111111, 0b11000111, 0b10111100, 0b00111111, 0b10000111, 0b11111000, 0b11100111, 0b00000000, 0b00000000, 0b00000001, +/* 15 */ 0b10000000, 0b00000110, 0b00001110, 0b00110000, 0b11100011, 0b10001100, 0b01110001, 0b11000011, 0b00001100, 0b00111111, 0b11001110, 0b00111000, 0b01111110, 0b00000000, 0b00000000, 0b00000001, +/* 16 */ 0b10000000, 0b00000110, 0b00001110, 0b01110000, 0b11100001, 0b11100000, 0b01110000, 0b11000111, 0b00001100, 0b00111111, 0b10001110, 0b00011000, 0b00111100, 0b00000000, 0b00000000, 0b00000001, +/* 17 */ 0b10000000, 0b00000110, 0b00001110, 0b01110000, 0b11100001, 0b11111000, 0b01110000, 0b11000111, 0b00001100, 0b00110000, 0b00001100, 0b00011000, 0b00111100, 0b00000000, 0b00000000, 0b00000001, +/* 18 */ 0b10000000, 0b00000110, 0b00001110, 0b01110000, 0b11100000, 0b11111100, 0b01110000, 0b11000111, 0b00001100, 0b00110000, 0b00001100, 0b00011000, 0b00111100, 0b00000000, 0b00000000, 0b00000001, +/* 19 */ 0b10000000, 0b00000110, 0b00001100, 0b01110000, 0b11100000, 0b00011100, 0b01110000, 0b11000111, 0b00001100, 0b00110000, 0b00001100, 0b00011000, 0b00111100, 0b00000000, 0b00000000, 0b00000001, +/* 20 */ 0b10000000, 0b00000110, 0b00011100, 0b00110001, 0b11100011, 0b10001100, 0b01110001, 0b11000011, 0b00001100, 0b00110000, 0b00001110, 0b00111000, 0b01111110, 0b00000000, 0b00000000, 0b00000001, +/* 21 */ 0b10000000, 0b00000111, 0b11111000, 0b00111111, 0b11110011, 0b11111100, 0b01111001, 0b11100011, 0b11111100, 0b00110000, 0b00000111, 0b11111000, 0b11100111, 0b00000000, 0b00000000, 0b00000001, +/* 22 */ 0b10000000, 0b00000111, 0b11110000, 0b00011111, 0b11110001, 0b11111100, 0b01111001, 0b11100001, 0b11111100, 0b00110000, 0b00000111, 0b11110000, 0b11100111, 0b00000000, 0b00000000, 0b00000001, +/* 23 */ 0b10000000, 0b00000111, 0b11100000, 0b00001111, 0b01100000, 0b11111000, 0b01110001, 0b11000001, 0b11111100, 0b00110000, 0b00000001, 0b11100000, 0b11000011, 0b00000000, 0b00000000, 0b00000001, +/* 24 */ 0b11000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000011, 0b00001100, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00111111, 0b11110000, 0b00000011, +/* 25 */ 0b11000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000011, 0b10111100, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b01111111, 0b11111000, 0b00000011, +/* 26 */ 0b11100000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000011, 0b11111000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00111111, 0b11110000, 0b00000111, +/* 27 */ 0b11100000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000001, 0b11111000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000111, +/* 28 */ 0b11110000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00001111, +/* 29 */ 0b11111000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00011111, +/* 30 */ 0b11111100, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00111111, +/* 31 */ 0b11111111, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b11111111, +/* 32 */ 0b11111111, 0b11100000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000111, 0b11111111 }; - display.drawBitmap(0, 0, DashyFox, 128, 32, WHITE); - display.display(); + screen.adafruit->drawBitmap(0, 0, DashyFox, 128, 32, WHITE); + screen.adafruit->display(); digitalWrite(pin_RESET, HIGH); @@ -118,17 +120,16 @@ ISR(TIMER1_COMPA_vect) { void loop() { static uint32_t tmr; - static uint8_t arr [6] = { 0x3f>>1, 0xf0 ,0x7f>>3, 0xf8, 0x3f>>1, 0xf0 }; - static uint8_t arr2 [6]= { 0 }; + static uint8_t arr[6] = { 0x3f >> 1, 0xf0 ,0x7f >> 3, 0xf8, 0x3f >> 1, 0xf0 }; + static uint8_t arr2[6] = { 0 }; static bool f = false; if (millis() - tmr > 350) { - display.drawBitmap(105, 23, arr, 16, 3, f); - display.display(); + screen.adafruit->drawBitmap(105, 23, arr, 16, 3, f); + screen.adafruit->display(); tmr = millis(); f = !f; } - // delayMicroseconds(1000000 / 42 / LED_count); // Если больше 2х перейти на micros } void point_ini(uint8_t width) { diff --git a/implement/Display__Adafruit_SSD1306.h b/implement/Display__Adafruit_SSD1306.h new file mode 100644 index 0000000..3fd9f91 --- /dev/null +++ b/implement/Display__Adafruit_SSD1306.h @@ -0,0 +1,26 @@ +#pragma once +#include "../modules/Display.h" + +#include + +class Display_Adafruit_SSD1306 : public Display { +public: + Adafruit_SSD1306* adafruit; + Display_Adafruit_SSD1306(uint16_t width, uint16_t height, uint16_t orientation) : Display(width, height, orientation) { + adafruit = new Adafruit_SSD1306(width, height, &Wire, -1); + }; + + void begin() { //TODO: Добавить поддержку других режимов + adafruit->begin(SSD1306_SWITCHCAPVCC, 0x3C); + adafruit->clearDisplay(); + } + + + void writePixel() override { + + } + + ~Display_Adafruit_SSD1306() { + delete adafruit; + } +}; diff --git a/modules/Display.h b/modules/Display.h new file mode 100644 index 0000000..e82bfdc --- /dev/null +++ b/modules/Display.h @@ -0,0 +1,35 @@ +#pragma once +#include + +class Display { +public: + enum ScreenOrientation { + Ori_0 = 0, + Ori_90 = 90, + Ori_180 = 180, + Ori_270 = 270, + + Ori_HORIZONTAL = 0, + Ori_VERTICAL = 90, + Ori_HORIZONTAL_FLIP = 180, + Ori_VERTICAL_FLIP = 270, + }; + +protected: + struct ScreenParam { + uint16_t width, height, orientation; + } screen; + + uint8_t* buffer; +public: + Display(uint16_t width, uint16_t height, uint16_t orientation) { + screen.width = width; + screen.height = height; + screen.orientation = orientation; + + buffer = new uint8_t[((width * height) / 8)]; + } + + virtual void writePixel() {}; + +}; diff --git a/modules/LED_Ring.h b/modules/LED_Ring.h new file mode 100644 index 0000000..2b6c63e --- /dev/null +++ b/modules/LED_Ring.h @@ -0,0 +1,17 @@ +#pragma once +#include + +struct LED_Ring_PINOUT { + uint8_t Data, CLK_inside, CLK_outside, RESET, OutputEnable; +}; + +class LED_Ring { +public: + LED_Ring_PINOUT pinOut; + uint16_t ledCount; + uint16_t updateFrec; + + LED_Ring(uint16_t _ledCount, LED_Ring_PINOUT _pinOut) : ledCount(_ledCount), pinOut(_pinOut) {}; + + +};