/
electronic_modules
/
MP3_Audio_Player
Обзор
Документация
Войти
/
electronic_modules
/
MP3_Audio_Player
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
OLED_I2C.cpp
216 строк
7 KB
electronic_modules
upload files
18 окт 2025, 22:20
18 окт 2025, 22:20
c4032c7
Код
Авторство
О чём код?
#include "OLED_I2C.h" #include <Wire.h> // Simple 5x7 font (only printable ASCII characters) const uint8_t font5x7[] PROGMEM = { 0x00, 0x00, 0x00, 0x00, 0x00, // 20 space 0x00, 0x00, 0x5F, 0x00, 0x00, // 21 ! 0x00, 0x07, 0x00, 0x07, 0x00, // 22 " 0x14, 0x7F, 0x14, 0x7F, 0x14, // 23 # 0x24, 0x2A, 0x7F, 0x2A, 0x12, // 24 $ 0x23, 0x13, 0x08, 0x64, 0x62, // 25 % 0x36, 0x49, 0x55, 0x22, 0x50, // 26 & 0x00, 0x05, 0x03, 0x00, 0x00, // 27 ' 0x00, 0x1C, 0x22, 0x41, 0x00, // 28 ( 0x00, 0x41, 0x22, 0x1C, 0x00, // 29 ) 0x08, 0x2A, 0x1C, 0x2A, 0x08, // 2A * 0x08, 0x08, 0x3E, 0x08, 0x08, // 2B + 0x00, 0x50, 0x30, 0x00, 0x00, // 2C , 0x08, 0x08, 0x08, 0x08, 0x08, // 2D - 0x00, 0x60, 0x60, 0x00, 0x00, // 2E . 0x20, 0x10, 0x08, 0x04, 0x02, // 2F / 0x3E, 0x51, 0x49, 0x45, 0x3E, // 30 0 0x00, 0x42, 0x7F, 0x40, 0x00, // 31 1 0x42, 0x61, 0x51, 0x49, 0x46, // 32 2 0x21, 0x41, 0x45, 0x4B, 0x31, // 33 3 0x18, 0x14, 0x12, 0x7F, 0x10, // 34 4 0x27, 0x45, 0x45, 0x45, 0x39, // 35 5 0x3C, 0x4A, 0x49, 0x49, 0x30, // 36 6 0x01, 0x71, 0x09, 0x05, 0x03, // 37 7 0x36, 0x49, 0x49, 0x49, 0x36, // 38 8 0x06, 0x49, 0x49, 0x29, 0x1E, // 39 9 0x00, 0x36, 0x36, 0x00, 0x00, // 3A : 0x00, 0x56, 0x36, 0x00, 0x00, // 3B ; 0x00, 0x08, 0x14, 0x22, 0x41, // 3C < 0x14, 0x14, 0x14, 0x14, 0x14, // 3D = 0x41, 0x22, 0x14, 0x08, 0x00, // 3E > 0x02, 0x01, 0x51, 0x09, 0x06, // 3F ? 0x32, 0x49, 0x79, 0x41, 0x3E, // 40 @ 0x7E, 0x11, 0x11, 0x11, 0x7E, // 41 A 0x7F, 0x49, 0x49, 0x49, 0x36, // 42 B 0x3E, 0x41, 0x41, 0x41, 0x22, // 43 C 0x7F, 0x41, 0x41, 0x22, 0x1C, // 44 D 0x7F, 0x49, 0x49, 0x49, 0x41, // 45 E 0x7F, 0x09, 0x09, 0x09, 0x01, // 46 F 0x3E, 0x41, 0x49, 0x49, 0x7A, // 47 G 0x7F, 0x08, 0x08, 0x08, 0x7F, // 48 H 0x00, 0x41, 0x7F, 0x41, 0x00, // 49 I 0x20, 0x40, 0x41, 0x3F, 0x01, // 4A J 0x7F, 0x08, 0x14, 0x22, 0x41, // 4B K 0x7F, 0x40, 0x40, 0x40, 0x40, // 4C L 0x7F, 0x02, 0x04, 0x02, 0x7F, // 4D M 0x7F, 0x04, 0x08, 0x10, 0x7F, // 4E N 0x3E, 0x41, 0x41, 0x41, 0x3E, // 4F O 0x7F, 0x09, 0x09, 0x09, 0x06, // 50 P 0x3E, 0x41, 0x51, 0x21, 0x5E, // 51 Q 0x7F, 0x09, 0x19, 0x29, 0x46, // 52 R 0x46, 0x49, 0x49, 0x49, 0x31, // 53 S 0x01, 0x01, 0x7F, 0x01, 0x01, // 54 T 0x3F, 0x40, 0x40, 0x40, 0x3F, // 55 U 0x1F, 0x20, 0x40, 0x20, 0x1F, // 56 V 0x7F, 0x20, 0x18, 0x20, 0x7F, // 57 W 0x63, 0x14, 0x08, 0x14, 0x63, // 58 X 0x03, 0x04, 0x78, 0x04, 0x03, // 59 Y 0x61, 0x51, 0x49, 0x45, 0x43, // 5A Z 0x00, 0x7F, 0x41, 0x41, 0x00, // 5B [ 0x02, 0x04, 0x08, 0x10, 0x20, // 5C backslash 0x00, 0x41, 0x41, 0x7F, 0x00, // 5D ] 0x04, 0x02, 0x01, 0x02, 0x04, // 5E ^ 0x40, 0x40, 0x40, 0x40, 0x40, // 5F _ 0x00, 0x01, 0x02, 0x04, 0x00, // 60 ` 0x20, 0x54, 0x54, 0x54, 0x78, // 61 a 0x7F, 0x48, 0x44, 0x44, 0x38, // 62 b 0x38, 0x44, 0x44, 0x44, 0x20, // 63 c 0x38, 0x44, 0x44, 0x48, 0x7F, // 64 d 0x38, 0x54, 0x54, 0x54, 0x18, // 65 e 0x08, 0x7E, 0x09, 0x01, 0x02, // 66 f 0x08, 0x14, 0x54, 0x54, 0x3C, // 67 g 0x7F, 0x08, 0x04, 0x04, 0x78, // 68 h 0x00, 0x44, 0x7D, 0x40, 0x00, // 69 i 0x20, 0x40, 0x44, 0x3D, 0x00, // 6A j 0x7F, 0x10, 0x28, 0x44, 0x00, // 6B k 0x00, 0x41, 0x7F, 0x40, 0x00, // 6C l 0x7C, 0x04, 0x18, 0x04, 0x78, // 6D m 0x7C, 0x08, 0x04, 0x04, 0x78, // 6E n 0x38, 0x44, 0x44, 0x44, 0x38, // 6F o 0x7C, 0x14, 0x14, 0x14, 0x08, // 70 p 0x08, 0x14, 0x14, 0x18, 0x7C, // 71 q 0x7C, 0x08, 0x04, 0x04, 0x08, // 72 r 0x48, 0x54, 0x54, 0x54, 0x20, // 73 s 0x04, 0x3F, 0x44, 0x40, 0x20, // 74 t 0x3C, 0x40, 0x40, 0x20, 0x7C, // 75 u 0x1C, 0x20, 0x40, 0x20, 0x1C, // 76 v 0x3C, 0x40, 0x30, 0x40, 0x3C, // 77 w 0x44, 0x28, 0x10, 0x28, 0x44, // 78 x 0x0C, 0x50, 0x50, 0x50, 0x3C, // 79 y 0x44, 0x64, 0x54, 0x4C, 0x44, // 7A z 0x00, 0x08, 0x36, 0x41, 0x00, // 7B { 0x00, 0x00, 0x7F, 0x00, 0x00, // 7C | 0x00, 0x41, 0x36, 0x08, 0x00, // 7D } 0x08, 0x04, 0x08, 0x10, 0x08, // 7E ~ }; OLED::OLED(uint8_t sda, uint8_t scl) : _sda(sda), _scl(scl) {} void OLED::sendCommand(uint8_t command) { Wire.beginTransmission(SSD1306_I2C_ADDRESS); Wire.write(0x00); // Command mode Wire.write(command); Wire.endTransmission(); } void OLED::sendData(uint8_t data) { Wire.beginTransmission(SSD1306_I2C_ADDRESS); Wire.write(0x40); // Data mode Wire.write(data); Wire.endTransmission(); } void OLED::begin() { Wire.begin(); // Initialize SSD1306 OLED sendCommand(0xAE); // Display OFF sendCommand(0x20); // Memory addressing mode sendCommand(0x00); // Horizontal addressing mode sendCommand(0x21); // Set column address sendCommand(0x00); // Column start address sendCommand(0x7F); // Column end address (127) sendCommand(0x22); // Set page address sendCommand(0x00); // Page start address sendCommand(0x07); // Page end address sendCommand(0x40); // Set display start line sendCommand(0xA1); // Segment remap sendCommand(0xA8); // Set multiplex ratio sendCommand(0x3F); // 64-1 = 63 sendCommand(0xA4); // Output follows RAM content sendCommand(0xA6); // Normal display sendCommand(0xD3); // Set display offset sendCommand(0x00); // No offset sendCommand(0xD5); // Set display clock divide ratio/oscillator frequency sendCommand(0xF0); // Set divide ratio sendCommand(0xD9); // Set pre-charge period sendCommand(0x22); // sendCommand(0xDA); // Set com pins hardware configuration sendCommand(0x12); // sendCommand(0xDB); // Set vcomh sendCommand(0x20); // 0.77xVcc sendCommand(0x8D); // Set DC-DC enable sendCommand(0x14); // sendCommand(0xAF); // Display ON clrScr(); } void OLED::clrScr() { for (uint16_t i = 0; i < 1024; i++) { sendData(0x00); } } void OLED::print(String text, uint8_t alignment, uint8_t y) { uint8_t x = 0; uint8_t textWidth = text.length() * 6; switch (alignment) { case CENTER: x = (SSD1306_X_SIZE - textWidth) / 2; break; case RIGHT: x = SSD1306_X_SIZE - textWidth; break; case LEFT: default: x = 0; break; } printAt(text, x, y); } void OLED::printAt(String text, uint8_t x, uint8_t y) { if (y > 7) return; // Only 8 rows available // Set page address sendCommand(0xB0 + y); sendCommand(((x & 0xF0) >> 4) | 0x10); sendCommand((x & 0x0F) | 0x01); for (uint8_t i = 0; i < text.length(); i++) { char c = text[i]; if (c < 32 || c > 126) c = 32; // Replace non-printable with space uint8_t charIndex = (c - 32) * 5; for (uint8_t j = 0; j < 5; j++) { sendData(pgm_read_byte(&font5x7[charIndex + j])); } sendData(0x00); // Space between characters } } void OLED::drawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2) { // Simple horizontal line implementation if (y1 == y2 && y1 < 8) { sendCommand(0xB0 + y1); sendCommand(((x1 & 0xF0) >> 4) | 0x10); sendCommand((x1 & 0x0F) | 0x01); for (uint8_t x = x1; x <= x2; x++) { sendData(0xFF); } } } void OLED::update() { // For this simple implementation, we send data immediately }