Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create openlcd library #13

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
615 changes: 615 additions & 0 deletions src/Sparkfun_OpenLCD/Sparkfun_OpenLCD.cpp

Large diffs are not rendered by default.

121 changes: 121 additions & 0 deletions src/Sparkfun_OpenLCD/Sparkfun_OpenLCD.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#ifndef SPARKFUN_OPEN_LCD_H
#define SPARKFUN_OPEN_LCD_H

#include <Arduino.h>
#include <Wire.h>
#include <Stream.h>
#include <SPI.h>

//Uncomment next line to use the RGB backlight command in newer firmware, comment next line if using originl firmware without command
#define FIRMWARE_HAS_RGB_COMMAND

#define DISPLAY_ADDRESS1 0x72 //This is the default address of the OpenLCD
#define MAX_ROWS 4
#define MAX_COLUMNS 20

//OpenLCD command characters
#define SPECIAL_COMMAND 254 //Magic number for sending a special command
#define SETTING_COMMAND 0x7C //124, |, the pipe character: The command to change settings: baud, lines, width, backlight, splash, etc

//OpenLCD commands
#define CLEAR_COMMAND 0x2D //45, -, the dash character: command to clear and home the display
#define CONTRAST_COMMAND 0x18 //Command to change the contrast setting
#define ADDRESS_COMMAND 0x19 //Command to change the i2c address
#define SET_RGB_COMMAND 0x2B //43, +, the plus character: command to set backlight RGB value


// special commands
#define LCD_RETURNHOME 0x02
#define LCD_ENTRYMODESET 0x04
#define LCD_DISPLAYCONTROL 0x08
#define LCD_CURSORSHIFT 0x10
#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

class Sparkfun_OpenLCD : public Print {

public:
Sparkfun_OpenLCD();
~Sparkfun_OpenLCD();
void begin(TwoWire &wirePort);
void begin(TwoWire &wirePort, byte i2c_addr);
void begin(Stream &serial);
void begin(SPIClass &spiPort, byte csPin);
//Only available for Arduino 1.6 and greater
#ifdef SPI_HAS_TRANSACTION
//pass SPISettings by value to allow settings object creation in fucntion call like examples
void begin(SPIClass &spiPort, byte csPin, SPISettings spiSettings);
#endif
void clear();
void home();
void setCursor(byte col, byte row);
void createChar(byte location, byte charmap[]);
void writeChar(byte location);
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *buffer, size_t size);
virtual size_t write(const char *str);
void noDisplay();
void display();
void noCursor();
void cursor();
void noBlink();
void blink();
void scrollDisplayLeft();
void scrollDisplayRight();
void scrollDisplayLeft(byte count);
void scrollDisplayRight(byte count);
void moveCursorLeft();
void moveCursorRight();
void moveCursorLeft(byte count);
void moveCursorRight(byte count);
void setBacklight(unsigned long rgb);
void setBacklight(byte r, byte g, byte b);
void leftToRight();
void rightToLeft();
void autoscroll();
void noAutoscroll();
void setContrast(byte new_val);
void setAddress(byte new_addr);
void command(byte command);
void specialCommand(byte command);
void specialCommand(byte command, byte count);
private:
TwoWire *_i2cPort = NULL; //The generic connection to user's chosen I2C hardware
Stream *_serialPort = NULL; //The generic connection to user's chosen serial hardware
SPIClass *_spiPort = NULL; //The generic connection to user's chosen spi hardware

//SPI transactions only available for Arduino 1.6 and later
#ifdef SPI_HAS_TRANSACTION
SPISettings _spiSettings = SPISettings(100000, MSBFIRST, SPI_MODE0);
bool _spiTransaction = false; //since we pass by value, we need a flag
#endif
byte _csPin = 10;
byte _i2cAddr = DISPLAY_ADDRESS1;
byte _displayControl = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
byte _displayMode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
void init();
void beginTransmission();
void transmit(byte data);
void endTransmission();
};

#endif
92 changes: 92 additions & 0 deletions src/Sparkfun_OpenLCD/examples/Autoscroll/Autoscroll.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Sparkfun_OpenLCD Library - Autoscroll

Demonstrates the use of a Sparkfun AVR-Based Serial Enabled LCD
display with a Qwiic adapter.

This sketch demonstrates the use of the autoscroll()
and noAutoscroll() functions to make new text scroll or not.

The circuit:
* Sparkfun RGB OpenLCD Serial display connected through
* a Sparkfun Qwiic adpater to an Ardruino with a
* Qwiic shield or a Sparkfun Blackboard with Qwiic built in.
*
* The Qwiic adapter should be attached to the display as follows:
*
* Display Qwiic Qwiic Cable Color
* GND GND Black
* RAW 3.3V Red
* SDA SDA Blue
* SCL SCL Yellow
*
* Note: If you connect directly to a 5V Arduino instead, you *MUST* use
* a level-shifter to convert the i2c voltage levels down to 3.3V for the display.

Based on the LiquidCrystal code originally by David A. Mellis
and the OpenLCD code by Nathan Seidle at Sparkfun.

Libray modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
modified 7 Nov 2016
by Arturo Guadalupi
modified 29 Aug 2018
by Gaston Williams

This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystalAutoscroll

More info on Qwiic here:
https://www.sparkfun.com/qwiic

AVR-Based Serial Enabled LCDs Hookup Guide
https://learn.sparkfun.com/tutorials/avr-based-serial-enabled-lcds-hookup-guide

*/

// include the Sparkfun OpenLCD library code:
#include <Sparkfun_OpenLCD.h>

// initialize the library with default i2c address 0x72
Sparkfun_OpenLCD lcd;


void setup() {
//Setup Wire
Wire.begin();
//By default .begin() will set I2C SCL to Standard Speed mode of 100kHz
Wire.setClock(400000); //Optional - set I2C SCL to High Speed Mode of 400kHz

// set up the LCD for I2C
lcd.begin(Wire);
}

void loop() {
// set the cursor to (0,0):
lcd.setCursor(0, 0);
// print from 0 to 9:
for (int thisChar = 0; thisChar < 10; thisChar++) {
lcd.print(thisChar);
delay(500);
}

// set the cursor to (16,1):
lcd.setCursor(16, 1);
// set the display to automatically scroll:
lcd.autoscroll();
// print from 0 to 9:
for (int thisChar = 0; thisChar < 10; thisChar++) {
lcd.print(thisChar);
delay(500);
}
// turn off automatic scrolling
lcd.noAutoscroll();

// clear screen for the next loop:
lcd.clear();
}

107 changes: 107 additions & 0 deletions src/Sparkfun_OpenLCD/examples/Backlight/Backlight.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
Sparkfun_OpenLCD Library - Backlight

Demonstrates the use of a Sparkfun AVR-Based Serial Enabled LCD
display with a Qwiic adapter.

This sketch changes the backlight color and displays text using
the OpenLCD functions.

The circuit:
* Sparkfun RGB OpenLCD Serial display connected through
* a Sparkfun Qwiic adpater to an Ardruino with a
* Qwiic shield or a Sparkfun Blackboard with Qwiic built in.
*
* The Qwiic adapter should be attached to the display as follows:
*
* Display Qwiic Qwiic Cable Color
* GND GND Black
* RAW 3.3V Red
* SDA SDA Blue
* SCL SCL Yellow
*
* Note: If you connect directly to a 5V Arduino instead, you *MUST* use
* a level-shifter to convert the i2c voltage levels down to 3.3V for the display.


Based on the LiquidCrystal code originally by David A. Mellis
and the OpenLCD code by Nathan Seidle at Sparkfun.

Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example created 23 Aug 2018
by Gaston Williams
modified 29 Aug 2018
by Gaston Williams

This example code is in the public domain.

More info on Qwiic here:
https://www.sparkfun.com/qwiic

AVR-Based Serial Enabled LCDs Hookup Guide
https://learn.sparkfun.com/tutorials/avr-based-serial-enabled-lcds-hookup-guide

*/

// include the Sparkfun OpenLCD library code:
#include <Sparkfun_OpenLCD.h>

// initialize the library with default i2c address 0x72
Sparkfun_OpenLCD lcd;

void setup() {
//Setup Wire
Wire.begin();
//By default .begin() will set I2C SCL to Standard Speed mode of 100kHz
Wire.setClock(400000); //Optional - set I2C SCL to High Speed Mode of 400kHz

// set up the LCD for I2C
lcd.begin(Wire);
}

void loop() {
//change the backlight color to various colors
lcd.setBacklight(0, 0, 0); //black is off
lcd.clear();
lcd.print("Black (off)");
delay(3000);
lcd.setBacklight(255, 0, 0); //bright red
lcd.clear();
lcd.print("Red");
delay(3000);
lcd.setBacklight(0xFF8C00); //orange
lcd.clear();
lcd.print("Orange");
delay(3000);
lcd.setBacklight(255, 255, 0); //bright yellow
lcd.clear();
lcd.print("Yellow");
delay(3000);
lcd.setBacklight(0, 255, 0); //bright green
lcd.clear();
lcd.print("Green");
delay(3000);
lcd.setBacklight(0, 0, 255); //bright blue
lcd.clear();
lcd.print("Blue");
delay(3000);
lcd.setBacklight(0x4B0082); //indigo, a kind of dark purplish blue
lcd.clear();
lcd.print("Indigo");
delay(3000);
lcd.setBacklight(0xA020F0); //violet
lcd.clear();
lcd.print("Violet");
delay(3000);
lcd.setBacklight(0x808080); //grey
lcd.clear();
lcd.print("Grey");
delay(3000);
lcd.setBacklight(255, 255, 255); //bright white
lcd.clear();
lcd.print("White");
delay(3000);
}
Loading