So reading the above and taking my best GUESS as to what’s needed, here are the “fixed” files. Save the prior ones away someplace safe and copy these into the library directory. Then change their names back (note the “A” added) to the ones you just moved and give them a try. I was unsure whether the write function in the C++ file should return a 1 as in one of the examples (but not in another fix example) so I put it in but commented it out. Find it and uncomment it if you think it’s needed.
OLEDFourBitA.h
#ifndef OLEDFourBit_h
#define OLEDFourBit_h
#include <inttypes.h>
#include "Print.h"
// commands
#define LCD_CLEARDISPLAY 0x01
#define LCD_RETURNHOME 0x02
#define LCD_ENTRYMODESET 0x04
#define LCD_DISPLAYCONTROL 0x08
#define LCD_CURSORSHIFT 0x10
#define LCD_FUNCTIONSET 0x28
#define LCD_SETCGRAMADDR 0x40
#define LCD_SETDDRAMADDR 0x80
// flags for display entry mode
#define LCD_ENTRYRIGHT 0x00
#define LCD_ENTRYLEFT 0x02
#define LCD_ENTRYSHIFTINCREMENT 0x01
#define LCD_ENTRYSHIFTDECREMENT 0x00
// flags for display on/off control
#define LCD_DISPLAYON 0x04
#define LCD_DISPLAYOFF 0x00
#define LCD_CURSORON 0x02
#define LCD_CURSOROFF 0x00
#define LCD_BLINKON 0x01
#define LCD_BLINKOFF 0x00
// flags for display/cursor shift
#define LCD_DISPLAYMOVE 0x08
#define LCD_CURSORMOVE 0x00
#define LCD_MOVERIGHT 0x04
#define LCD_MOVELEFT 0x00
// flags for function set
#define LCD_8BITMODE 0x10
#define LCD_4BITMODE 0x00
#define LCD_ENGLISH_JAPANESE 0x00
#define LCD_WESTERN_EUROPEAN_1 0x01
#define LCD_ENGLISH_RUSSIAN 0x02
#define LCD_WESTERN_EUROPEAN_2 0x03
class OLEDFourBit : public Print {
public:
OLEDFourBit(uint8_t rs, uint8_t enable,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
void init(uint8_t rs, uint8_t enable,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
void begin(uint8_t cols, uint8_t rows);
void clear();
void home();
void noDisplay();
void display();
void noBlink();
void blink();
void noCursor();
void cursor();
void scrollDisplayLeft();
void scrollDisplayRight();
void leftToRight();
void rightToLeft();
void autoscroll();
void noAutoscroll();
void createChar(uint8_t, uint8_t[]);
void setCursor(uint8_t, uint8_t);
//virtual void write(uint8_t);
virtual size_t write(uint8_t);
void command(uint8_t);
private:
void send(uint8_t, uint8_t);
void write4bits(uint8_t);
void pulseEnable();
void waitBusy();
uint8_t _rs_pin; // LOW: command. HIGH: character.
uint8_t _enable_pin; // activated by a HIGH pulse.
uint8_t _busy_pin;
uint8_t _data_pins[4];
uint8_t _displayfunction;
uint8_t _displaycontrol;
uint8_t _displaymode;
uint8_t _initialized;
uint8_t _currline;
uint8_t _numlines;
};
#endif
OLEDFourBitA.cpp
#include "OLEDFourBit.h"
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "arduino.h"
OLEDFourBit::OLEDFourBit(uint8_t rs, uint8_t enable,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
{
init(rs, enable, d4, d5, d6, d7);
}
void OLEDFourBit::init(uint8_t rs, uint8_t enable,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
{
_rs_pin = rs;
_enable_pin = enable;
_busy_pin = d7;
_data_pins[0] = d4;
_data_pins[1] = d5;
_data_pins[2] = d6;
_data_pins[3] = d7;
pinMode(_rs_pin, OUTPUT);
pinMode(_enable_pin, OUTPUT);
_displayfunction = LCD_FUNCTIONSET | LCD_4BITMODE;
begin(20, 4);
}
void OLEDFourBit::begin(uint8_t cols, uint8_t lines) {
_numlines = lines;
_currline = 0;
pinMode(_rs_pin, OUTPUT);
pinMode(_enable_pin, OUTPUT);
digitalWrite(_rs_pin, LOW);
digitalWrite(_enable_pin, LOW);
// SEE PAGE 20 of NHD-0420DZW-AY5
delayMicroseconds(50000); // wait 50 ms just to be sure tha the lcd is initialized
// Now we pull both RS and R/W low to begin commands
for (int i = 0; i < 4; i++) {
pinMode(_data_pins[i], OUTPUT);
digitalWrite(_data_pins[i], LOW);
}
delayMicroseconds(100000);
write4bits(0x03);
delayMicroseconds(100000);
write4bits(0x02);
delayMicroseconds(10000);
write4bits(0x02);
delayMicroseconds(10000);
write4bits(0x08);
//command(0x28);
delayMicroseconds(10000);
command(0x08); // Display off
delayMicroseconds(10000);
command(0x01); // display clear
delayMicroseconds(10000);
command(0x06); // Entry Mode Set:
delayMicroseconds(10000);
command(0x02); // Home
delayMicroseconds(10000);
command(0x0C); // display on/ cursor on/ cursor blink
delayMicroseconds(10000);
}
/********** high level commands, for the user! */
void OLEDFourBit::clear()
{
command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
}
void OLEDFourBit::home()
{
command(LCD_RETURNHOME); // set cursor position to zero
}
void OLEDFourBit::setCursor(uint8_t col, uint8_t row)
{
uint8_t row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
if ( row >= _numlines ) {
row = 0; //write to first line if out off bounds
}
command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
}
// Turn the display on/off (quickly)
void OLEDFourBit::noDisplay() {
_displaycontrol &= ~LCD_DISPLAYON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void OLEDFourBit::display() {
_displaycontrol |= LCD_DISPLAYON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
// Turns the underline cursor on/off
void OLEDFourBit::noCursor() {
_displaycontrol &= ~LCD_CURSORON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void OLEDFourBit::cursor() {
_displaycontrol |= LCD_CURSORON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
// Turn on and off the blinking cursor
void OLEDFourBit::noBlink() {
_displaycontrol &= ~LCD_BLINKON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void OLEDFourBit::blink() {
_displaycontrol |= LCD_BLINKON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
// These commands scroll the display without changing the RAM
void OLEDFourBit::scrollDisplayLeft(void) {
command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
}
void OLEDFourBit::scrollDisplayRight(void) {
command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
}
// This is for text that flows Left to Right
void OLEDFourBit::leftToRight(void) {
_displaymode |= LCD_ENTRYLEFT;
command(LCD_ENTRYMODESET | _displaymode);
}
// This is for text that flows Right to Left
void OLEDFourBit::rightToLeft(void) {
_displaymode &= ~LCD_ENTRYLEFT;
command(LCD_ENTRYMODESET | _displaymode);
}
// This will 'right justify' text from the cursor
void OLEDFourBit::autoscroll(void) {
_displaymode |= LCD_ENTRYSHIFTINCREMENT;
command(LCD_ENTRYMODESET | _displaymode);
}
// This will 'left justify' text from the cursor
void OLEDFourBit::noAutoscroll(void) {
_displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
command(LCD_ENTRYMODESET | _displaymode);
}
// Allows us to fill the first 8 CGRAM locations
// with custom characters
void OLEDFourBit::createChar(uint8_t location, uint8_t charmap[]) {
location &= 0x7; // we only have 8 locations 0-7
command(LCD_SETCGRAMADDR | (location << 3));
for (int i=0; i<8; i++) {
write(charmap[i]);
}
}
/*********** mid level commands, for sending data/cmds */
inline void OLEDFourBit::command(uint8_t value) {
send(value, LOW);
waitBusy();
}
//inline void OLEDFourBit::write(uint8_t value) {
inline size_t OLEDFourBit::write(uint8_t value) {
send(value, HIGH);
waitBusy();
//return 1;
}
/************ low level data pushing commands **********/
// write either command or data
void OLEDFourBit::send(uint8_t value, uint8_t mode) {
digitalWrite(_rs_pin, mode);
write4bits(value>>4);
write4bits(value);
}
void OLEDFourBit::pulseEnable(void) {
digitalWrite(_enable_pin, HIGH);
delayMicroseconds(100); // enable pulse must be >450ns
digitalWrite(_enable_pin, LOW);
}
void OLEDFourBit::write4bits(uint8_t value) {
for (int i = 0; i < 4; i++) {
pinMode(_data_pins[i], OUTPUT);
digitalWrite(_data_pins[i], (value >> i) & 0x01);
}
delayMicroseconds(100);
pulseEnable();
}
void OLEDFourBit::waitBusy(void) {
delayMicroseconds(5000);
}