1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-24 21:21:24 +01:00

Rationalisation of SSD1306 driver

Merge SSD1306AsciiWire.cpp into SSD1306Ascii.cpp and rename SSD1306AsciiWire.h as SSD1306Ascii.h.
Merge allFonts.h into System5x7.h and rename as SSD1306font.h.
Move all SSD1306 files into root folder to facilitate compilation in Arduino IDE.
This commit is contained in:
Neil McKechnie 2021-02-20 12:57:36 +00:00
parent cf5eb891d7
commit 396b5c8299
7 changed files with 110 additions and 192 deletions

View File

@ -22,7 +22,7 @@
// It is NOT a .cpp file to prevent it being compiled and demanding libraries even when not needed. // It is NOT a .cpp file to prevent it being compiled and demanding libraries even when not needed.
#include "Wire.h" #include "Wire.h"
#include "SSD1306/SSD1306AsciiWire.h" #include "SSD1306Ascii.h"
SSD1306AsciiWire LCDDriver; SSD1306AsciiWire LCDDriver;
// DEVICE SPECIFIC LCDDisplay Implementation for OLED // DEVICE SPECIFIC LCDDisplay Implementation for OLED

View File

@ -1,106 +0,0 @@
/* Based on Arduino SSD1306Ascii Library, Copyright (C) 2015 by William Greiman
* Modifications (C) 2021 Neil McKechnie
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino SSD1306Ascii Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file SSD1306AsciiWire.h
* @brief Class for I2C displays using Wire.
*/
#ifndef SSD1306AsciiWire_h
#define SSD1306AsciiWire_h
#include <Wire.h>
#include "SSD1306Ascii.h"
/**
* @class SSD1306AsciiWire
* @brief Class for I2C displays using Wire.
*/
class SSD1306AsciiWire : public SSD1306Ascii {
public:
#define m_oledWire Wire
/**
* @brief Initialize the display controller.
*
* @param[in] dev A device initialization structure.
* @param[in] i2cAddr The I2C address of the display controller.
*/
void begin(const DevType* dev, uint8_t i2cAddr) {
#if OPTIMIZE_I2C
m_nData = 0;
#endif // OPTIMIZE_I2C
m_i2cAddr = i2cAddr;
init(dev);
}
/**
* @brief Initialize the display controller.
*
* @param[in] dev A device initialization structure.
* @param[in] i2cAddr The I2C address of the display controller.
* @param[in] rst The display controller reset pin.
*/
void begin(const DevType* dev, uint8_t i2cAddr, uint8_t rst) {
oledReset(rst);
begin(dev, i2cAddr);
}
/**
* @brief Set the I2C clock rate to 400 kHz.
* Deprecated use Wire.setClock(400000L)
*/
void set400kHz() __attribute__((deprecated("use Wire.setClock(400000L)"))) {
m_oledWire.setClock(400000L);
}
protected:
void writeDisplay(uint8_t b, uint8_t mode) {
#if OPTIMIZE_I2C
if (m_nData > 16 || (m_nData && mode == SSD1306_MODE_CMD)) {
m_oledWire.endTransmission();
m_nData = 0;
}
if (m_nData == 0) {
m_oledWire.beginTransmission(m_i2cAddr);
m_oledWire.write(mode == SSD1306_MODE_CMD ? 0X00 : 0X40);
}
m_oledWire.write(b);
if (mode == SSD1306_MODE_RAM_BUF) {
m_nData++;
} else {
m_oledWire.endTransmission();
m_nData = 0;
}
#else // OPTIMIZE_I2C
m_oledWire.beginTransmission(m_i2cAddr);
m_oledWire.write(mode == SSD1306_MODE_CMD ? 0X00: 0X40);
m_oledWire.write(b);
m_oledWire.endTransmission();
#endif // OPTIMIZE_I2C
}
void flushDisplay() {
#if OPTIMIZE_I2C
if (m_nData) {
m_oledWire.endTransmission();
m_nData = 0;
}
#endif // OPTIMIZE_I2C
}
protected:
uint8_t m_i2cAddr;
#if OPTIMIZE_I2C
uint8_t m_nData;
#endif // OPTIMIZE_I2C
};
#endif // SSD1306AsciiWire_h

View File

@ -1,60 +0,0 @@
/*
* allFonts.h font header for GLCD library
* The fonts listed below will be available in a sketch if this file is included
*
* If you create your own fonts you can add the header to this file
*
* Note that the build environment only holds a font in Flash if its selected
* so there is no penalty to including a font file here if its not used
*/
/**
* @file allFonts.h
* @brief Font definitions.
*/
#ifndef _allFonts_h_
#define _allFonts_h_
#ifdef __AVR__
#include <avr/pgmspace.h>
/** declare a font for AVR. */
#define GLCDFONTDECL(_n) static const uint8_t __attribute__ ((progmem))_n[]
#define readFontByte(addr) pgm_read_byte(addr)
#else // __AVR__
/** declare a font. */
#define GLCDFONTDECL(_n) static const uint8_t _n[]
/** Fake read from flash. */
#define readFontByte(addr) (*(const unsigned char *)(addr))
#endif // __AVR__
//------------------------------------------------------------------------------
// Font Indices
/** No longer used Big Endian length field. Now indicates font type.
*
* 00 00 (fixed width font with 1 padding pixel on right and below)
*
* 00 01 (fixed width font with no padding pixels)
*/
#define FONT_LENGTH 0
/** Maximum character width. */
#define FONT_WIDTH 2
/** Font hight in pixels */
#define FONT_HEIGHT 3
/** Ascii value of first character */
#define FONT_FIRST_CHAR 4
/** count of characters in font. */
#define FONT_CHAR_COUNT 5
/** Offset to width table. */
#define FONT_WIDTH_TABLE 6
//
// FONT_LENGTH is a 16 bit Big Endian length field.
// Unfortunately, FontCreator2 screwed up the value it put in the field
// so it is pretty much meaningless. However it still is used to indicate
// some special things.
// 00 00 (fixed width font with 1 padding pixel on right and below)
// 00 01 (fixed width font with no padding pixels)
// FONT_WIDTH it the max character width.
// any other value means variable width font in FontCreator2 (thiele)
// format with pixel padding
#include "System5x7.h" // system font (fixed width)
#endif

View File

@ -16,6 +16,7 @@
* <http://www.gnu.org/licenses/>. * <http://www.gnu.org/licenses/>.
*/ */
#include "SSD1306Ascii.h" #include "SSD1306Ascii.h"
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void SSD1306Ascii::clear() { void SSD1306Ascii::clear() {
clear(0, displayWidth() - 1, 0 , displayRows() - 1); clear(0, displayWidth() - 1, 0 , displayRows() - 1);

View File

@ -16,14 +16,17 @@
* <http://www.gnu.org/licenses/>. * <http://www.gnu.org/licenses/>.
*/ */
/** /**
* @file SSD1306Ascii.h * @file SSD1306AsciiWire.h
* @brief Base class for ssd1306 displays. * @brief Class for I2C displays using Wire.
*/ */
#ifndef SSD1306Ascii_h #ifndef SSD1306AsciiWire_h
#define SSD1306Ascii_h #define SSD1306AsciiWire_h
#include <Wire.h>
#include "Arduino.h" #include "Arduino.h"
#include "SSD1306init.h" #include "SSD1306init.h"
#include "fonts/allFonts.h" #include "SSD1306font.h"
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
/** SSD1306Ascii version basis */ /** SSD1306Ascii version basis */
#define SDD1306_ASCII_VERSION 1.3.0 #define SDD1306_ASCII_VERSION 1.3.0
@ -33,8 +36,6 @@
/** Use larger faster I2C code. */ /** Use larger faster I2C code. */
#define OPTIMIZE_I2C 1 #define OPTIMIZE_I2C 1
/** AvrI2c uses 400 kHz fast mode if AVRI2C_FASTMODE is nonzero else 100 kHz. */
#define AVRI2C_FASTMODE 1
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Values for writeDisplay() mode parameter. // Values for writeDisplay() mode parameter.
/** Write to Command register. */ /** Write to Command register. */
@ -44,19 +45,7 @@
/** Write to display RAM with possible buffering. */ /** Write to display RAM with possible buffering. */
#define SSD1306_MODE_RAM_BUF 2 #define SSD1306_MODE_RAM_BUF 2
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
/**
* @brief Reset the display controller.
*
* @param[in] rst Reset pin number.
*/
inline void oledReset(uint8_t rst) {
pinMode(rst, OUTPUT);
digitalWrite(rst, LOW);
delay(10);
digitalWrite(rst, HIGH);
delay(10);
}
//------------------------------------------------------------------------------
/** /**
* @class SSD1306Ascii * @class SSD1306Ascii
* @brief SSD1306 base class * @brief SSD1306 base class
@ -197,8 +186,6 @@ class SSD1306Ascii : public Print {
*/ */
size_t write(uint8_t c); size_t write(uint8_t c);
protected: protected:
virtual void writeDisplay(uint8_t b, uint8_t mode) = 0; virtual void writeDisplay(uint8_t b, uint8_t mode) = 0;
virtual void flushDisplay() = 0; virtual void flushDisplay() = 0;
@ -209,4 +196,68 @@ class SSD1306Ascii : public Print {
uint8_t m_colOffset; // Column offset RAM to SEG. uint8_t m_colOffset; // Column offset RAM to SEG.
const uint8_t* m_font = nullptr; // Current font. const uint8_t* m_font = nullptr; // Current font.
}; };
#endif // SSD1306Ascii_h
/**
* @class SSD1306AsciiWire
* @brief Class for I2C displays using Wire.
*/
class SSD1306AsciiWire : public SSD1306Ascii {
public:
#define m_oledWire Wire
/**
* @brief Initialize the display controller.
*
* @param[in] dev A device initialization structure.
* @param[in] i2cAddr The I2C address of the display controller.
*/
void begin(const DevType* dev, uint8_t i2cAddr) {
#if OPTIMIZE_I2C
m_nData = 0;
#endif // OPTIMIZE_I2C
m_i2cAddr = i2cAddr;
init(dev);
}
protected:
void writeDisplay(uint8_t b, uint8_t mode) {
#if OPTIMIZE_I2C
if (m_nData > 16 || (m_nData && mode == SSD1306_MODE_CMD)) {
m_oledWire.endTransmission();
m_nData = 0;
}
if (m_nData == 0) {
m_oledWire.beginTransmission(m_i2cAddr);
m_oledWire.write(mode == SSD1306_MODE_CMD ? 0X00 : 0X40);
}
m_oledWire.write(b);
if (mode == SSD1306_MODE_RAM_BUF) {
m_nData++;
} else {
m_oledWire.endTransmission();
m_nData = 0;
}
#else // OPTIMIZE_I2C
m_oledWire.beginTransmission(m_i2cAddr);
m_oledWire.write(mode == SSD1306_MODE_CMD ? 0X00: 0X40);
m_oledWire.write(b);
m_oledWire.endTransmission();
#endif // OPTIMIZE_I2C
}
void flushDisplay() {
#if OPTIMIZE_I2C
if (m_nData) {
m_oledWire.endTransmission();
m_nData = 0;
}
#endif // OPTIMIZE_I2C
}
protected:
uint8_t m_i2cAddr;
#if OPTIMIZE_I2C
uint8_t m_nData;
#endif // OPTIMIZE_I2C
};
#endif // SSD1306AsciiWire_h

View File

@ -29,12 +29,44 @@
* // bit field of all characters * // bit field of all characters
*/ */
#ifndef SYSTEM5x7_H #ifndef SSD1306font_H
#define SYSTEM5x7_H #define SSD1306font_H
#define SYSTEM5x7_WIDTH 5 #define SYSTEM5x7_WIDTH 5
#define SYSTEM5x7_HEIGHT 7 #define SYSTEM5x7_HEIGHT 7
#ifdef __AVR__
#include <avr/pgmspace.h>
/** declare a font for AVR. */
#define GLCDFONTDECL(_n) static const uint8_t __attribute__ ((progmem))_n[]
#define readFontByte(addr) pgm_read_byte(addr)
#else // __AVR__
/** declare a font. */
#define GLCDFONTDECL(_n) static const uint8_t _n[]
/** Fake read from flash. */
#define readFontByte(addr) (*(const unsigned char *)(addr))
#endif // __AVR__
//------------------------------------------------------------------------------
// Font Indices
/** No longer used Big Endian length field. Now indicates font type.
*
* 00 00 (fixed width font with 1 padding pixel on right and below)
*
* 00 01 (fixed width font with no padding pixels)
*/
#define FONT_LENGTH 0
/** Maximum character width. */
#define FONT_WIDTH 2
/** Font hight in pixels */
#define FONT_HEIGHT 3
/** Ascii value of first character */
#define FONT_FIRST_CHAR 4
/** count of characters in font. */
#define FONT_CHAR_COUNT 5
/** Offset to width table. */
#define FONT_WIDTH_TABLE 6
//------------------------------------------------------------------------------
GLCDFONTDECL(System5x7) = { GLCDFONTDECL(System5x7) = {
0x0, 0x0, // size of zero indicates fixed width font, 0x0, 0x0, // size of zero indicates fixed width font,
0x05, // width 0x05, // width