From ac06b319b0db573df207513d811052d75fbd1599 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sat, 19 Apr 2008 03:31:36 +0000 Subject: [PATCH] Adding LiquidCrystal library. --- .../macosx/Arduino.xcodeproj/project.pbxproj | 3 +- .../libraries/LiquidCrystal/LiquidCrystal.cpp | 128 ++++++++++++++++++ .../libraries/LiquidCrystal/LiquidCrystal.h | 31 +++++ .../examples/HelloWorld/HelloWorld.pde | 12 ++ .../examples/SerialDisplay/SerialDisplay.pde | 19 +++ hardware/libraries/LiquidCrystal/keywords.txt | 23 ++++ 6 files changed, 214 insertions(+), 2 deletions(-) create mode 100755 hardware/libraries/LiquidCrystal/LiquidCrystal.cpp create mode 100755 hardware/libraries/LiquidCrystal/LiquidCrystal.h create mode 100644 hardware/libraries/LiquidCrystal/examples/HelloWorld/HelloWorld.pde create mode 100644 hardware/libraries/LiquidCrystal/examples/SerialDisplay/SerialDisplay.pde create mode 100755 hardware/libraries/LiquidCrystal/keywords.txt diff --git a/build/macosx/Arduino.xcodeproj/project.pbxproj b/build/macosx/Arduino.xcodeproj/project.pbxproj index a0afcc7c4..2c8016846 100644 --- a/build/macosx/Arduino.xcodeproj/project.pbxproj +++ b/build/macosx/Arduino.xcodeproj/project.pbxproj @@ -56,7 +56,7 @@ productName = App; productReference = 33DD8FB6096AC8DA0013AF8F /* Arduino.app */; productSettingsXML = " - + CFBundleDevelopmentRegion @@ -687,7 +687,6 @@ 33FFFD3F0965B1E40016AC38 /* Project object */ = { isa = PBXProject; buildConfigurationList = 33FFFD400965B1E40016AC38 /* Build configuration list for PBXProject "Arduino" */; - compatibilityVersion = "Xcode 2.4"; hasScannedForEncodings = 0; mainGroup = 33FFFD3D0965B1E40016AC38; productRefGroup = 33FFFD3D0965B1E40016AC38; diff --git a/hardware/libraries/LiquidCrystal/LiquidCrystal.cpp b/hardware/libraries/LiquidCrystal/LiquidCrystal.cpp new file mode 100755 index 000000000..c5acc8568 --- /dev/null +++ b/hardware/libraries/LiquidCrystal/LiquidCrystal.cpp @@ -0,0 +1,128 @@ +#include "LiquidCrystal.h" + +#include +#include +#include +#include "WProgram.h" + +// When the display powers up, it is configured as follows: +// +// 1. Display clear +// 2. Function set: +// DL = 1; 8-bit interface data +// N = 0; 1-line display +// F = 0; 5x8 dot character font +// 3. Display on/off control: +// D = 0; Display off +// C = 0; Cursor off +// B = 0; Blinking off +// 4. Entry mode set: +// I/D = 1; Increment by 1 +// S = 0; No shift +// +// Note, however, that resetting the Arduino doesn't reset the LCD, so we +// can't assume that its in that state when a sketch starts (and the +// LiquidCrystal constructor is called). + +LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable, + uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, + uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7) : Print(baseWrite), + _four_bit_mode(0), _rs_pin(rs), _rw_pin(rw), _enable_pin(enable) +{ + _data_pins[0] = d0; + _data_pins[1] = d1; + _data_pins[2] = d2; + _data_pins[3] = d3; + _data_pins[4] = d4; + _data_pins[5] = d5; + _data_pins[6] = d6; + _data_pins[7] = d7; + + pinMode(_rs_pin, OUTPUT); + pinMode(_rw_pin, OUTPUT); + pinMode(_enable_pin, OUTPUT); + + for (int i = 0; i < 8; i++) + pinMode(_data_pins[i], OUTPUT); + + command(0x38); // function set: 8 bits, 1 line, 5x8 dots + command(0x0C); // display control: turn display on, cursor off, no blinking + command(0x06); // entry mode set: increment automatically, display shift, right shift + clear(); +} + +LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable, + uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3) : Print(baseWrite), + _four_bit_mode(1), _rs_pin(rs), _rw_pin(rw), _enable_pin(enable) +{ + _data_pins[0] = d0; + _data_pins[1] = d1; + _data_pins[2] = d2; + _data_pins[3] = d3; + + pinMode(_rs_pin, OUTPUT); + pinMode(_rw_pin, OUTPUT); + pinMode(_enable_pin, OUTPUT); + + for (int i = 0; i < 4; i++) + pinMode(_data_pins[i], OUTPUT); + + command(0x28); // function set: 4 bits, 1 line, 5x8 dots + command(0x0C); // display control: turn display on, cursor off, no blinking + command(0x06); // entry mode set: increment automatically, display shift, right shift + clear(); +} + +void LiquidCrystal::clear() +{ + command(0x01); // clear display, set cursor position to zero + delayMicroseconds(2000); +} + +void LiquidCrystal::home() +{ + command(0x02); // set cursor position to zero + delayMicroseconds(2000); +} + +void LiquidCrystal::setCursor(int col, int row) +{ + int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 }; + command(0x80 | (col + row_offsets[row])); +} + +void LiquidCrystal::command(int value) { + write(value, LOW); +} + +void LiquidCrystal::write(int value, int mode) { + digitalWrite(_rs_pin, mode); + digitalWrite(_rw_pin, LOW); + + if (_four_bit_mode) { + for (int i = 0; i < 4; i++) { + digitalWrite(_data_pins[i], (value >> (i + 4)) & 0x01); + } + + digitalWrite(_enable_pin, HIGH); + digitalWrite(_enable_pin, LOW); + + for (int i = 0; i < 4; i++) { + digitalWrite(_data_pins[i], (value >> i) & 0x01); + } + + digitalWrite(_enable_pin, HIGH); + digitalWrite(_enable_pin, LOW); + } else { + for (int i = 0; i < 8; i++) { + digitalWrite(_data_pins[i], (value >> i) & 0x01); + } + + digitalWrite(_enable_pin, HIGH); + digitalWrite(_enable_pin, LOW); + } +} + +void baseWrite(uint8_t value, void *instance) { + ((LiquidCrystal *) instance)->write(value); +} diff --git a/hardware/libraries/LiquidCrystal/LiquidCrystal.h b/hardware/libraries/LiquidCrystal/LiquidCrystal.h new file mode 100755 index 000000000..c4746c59b --- /dev/null +++ b/hardware/libraries/LiquidCrystal/LiquidCrystal.h @@ -0,0 +1,31 @@ +#ifndef LiquidCrystal_h +#define LiquidCrystal_h + +#include +#include "Print.h" + +class LiquidCrystal : public Print { +public: + LiquidCrystal(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t); + LiquidCrystal(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, + uint8_t, uint8_t, uint8_t, uint8_t); + void clear(); + void home(); + void setCursor(int, int); + /* + void shiftDisplayLeft(); + void shiftDisplayRight(); + */ + void write(int, int = 1); + void command(int); +private: + uint8_t _four_bit_mode; + uint8_t _rs_pin; // LOW: command. HIGH: character. + uint8_t _rw_pin; // LOW: write to LCD. HIGH: read from LCD. + uint8_t _enable_pin; // activated by a HIGH pulse. + uint8_t _data_pins[8]; +}; + +void baseWrite(uint8_t, void *); + +#endif diff --git a/hardware/libraries/LiquidCrystal/examples/HelloWorld/HelloWorld.pde b/hardware/libraries/LiquidCrystal/examples/HelloWorld/HelloWorld.pde new file mode 100644 index 000000000..d5142159b --- /dev/null +++ b/hardware/libraries/LiquidCrystal/examples/HelloWorld/HelloWorld.pde @@ -0,0 +1,12 @@ +#include + +LiquidCrystal lcd(12, 11, 2, 7, 8, 9, 10); + +void setup() +{ + lcd.print("hello, world!"); +} + +void loop() +{ +} diff --git a/hardware/libraries/LiquidCrystal/examples/SerialDisplay/SerialDisplay.pde b/hardware/libraries/LiquidCrystal/examples/SerialDisplay/SerialDisplay.pde new file mode 100644 index 000000000..c1f405a4b --- /dev/null +++ b/hardware/libraries/LiquidCrystal/examples/SerialDisplay/SerialDisplay.pde @@ -0,0 +1,19 @@ +#include + +LiquidCrystal lcd(12, 11, 2, 7, 8, 9, 10); + +void setup() +{ + Serial.begin(9600); +} + +void loop() +{ + if (Serial.available()) { + delay(100); + lcd.clear(); + while (Serial.available() > 0) { + lcd.write(Serial.read()); + } + } +} diff --git a/hardware/libraries/LiquidCrystal/keywords.txt b/hardware/libraries/LiquidCrystal/keywords.txt new file mode 100755 index 000000000..367ab1f6f --- /dev/null +++ b/hardware/libraries/LiquidCrystal/keywords.txt @@ -0,0 +1,23 @@ +####################################### +# Syntax Coloring Map For LiquidCrystal +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +LiquidCrystal KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +clear KEYWORD2 +home KEYWORD2 +print KEYWORD2 +setCursor KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### +