John Bailey / freetronicsLCDShield

Fork of freetronicsLCDShield by Koen Kempeneers

Files at this revision

API Documentation at this revision

Comitter:
KKempeneers
Date:
Tue Oct 08 08:46:39 2013 +0000
Child:
1:ddcefddda4a7
Commit message:
First attempt at publishing a library.

Changed in this revision

freetronicsLCDShield.cpp Show annotated file Show diff for this revision Revisions of this file
freetronicsLCDShield.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/freetronicsLCDShield.cpp	Tue Oct 08 08:46:39 2013 +0000
@@ -0,0 +1,109 @@
+#include "mbed.h"
+#include "freetronicsLCDShield.h"
+
+#define PULSE_E     wait(0.000001f);    \
+                    _e = 0;             \
+                    wait(0.000001f);    \
+                    _e = 1;
+
+
+freetronicsLCDShield::freetronicsLCDShield (PinName rs, PinName e, PinName d0, PinName d1, PinName d2, PinName d3, PinName bl, PinName a0) 
+            : _rs(rs), _e(e), _d(d0, d1, d2, d3), _bl(bl), _a0(a0) {
+    // Class constructor
+    // Init the display, wait 15ms to insure the power is up
+    _e = true;
+    _rs = false;
+    wait(0.015f);
+    
+    for (int i = 0; i < 3; i++){
+        writeByte(0x3);
+        wait(0.00164f);  // this command takes 1.64ms, so wait for it
+    }
+
+    writeByte(0x2);     // 4-bit mode
+    writeCommand(0x28); // Function set 001 BW N F - -
+    writeCommand(0x0C); // Display on/off controll 0000 1 D C B (D(isplay) On/Off C(ursor) On/Off B(link) On/Off 
+    writeCommand(0x6);  // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
+    cls();
+    
+    // Set the PWM period to 20ms
+    _bl.period_ms(1);
+    _bl.write(0.0);
+}
+
+void freetronicsLCDShield::setCursorPosition (int line, int col) {
+    // Set the new cursorposition
+    writeCommand(0x80 + (line * 0x40) + col);
+}
+
+void freetronicsLCDShield::setBackLight (bool blStatus) {
+    // Switch the backlight on (true) or off (false)
+    _bl = (blStatus) ? 1.0 : 0.0;
+}
+
+void freetronicsLCDShield::setBackLight (float blIntensity) {
+    // Switch the backlight on (true) or off (false)
+    _bl = blIntensity;
+}
+
+void freetronicsLCDShield::setCursor (bool cStatus, bool blink) {
+    int tmp = 0;
+    
+    if (blink) tmp = 0x01;
+    if (cStatus) tmp |= 0x02; 
+    writeCommand(0x0C + tmp);
+}
+
+void freetronicsLCDShield::shift (bool left) {
+    int tmp = 0;
+    
+    if (left) tmp = 0x04;
+    writeCommand(0x18 + tmp);
+}
+
+void freetronicsLCDShield::cls(void) {
+    // Clear the display and place the cursor at 0, 0
+    writeCommand(0x01);
+    wait(0.00164f);
+}
+
+void freetronicsLCDShield::home(void) {
+    // Undo shift operations and place cursor at 0,0
+    writeCommand(0x02);
+    wait(0.00164f);
+}
+
+// Low level output functions
+void freetronicsLCDShield::writeByte (int byte) {
+    // Split the byte in high and low nibble, write high nibble first
+    _d = byte >> 4;
+    PULSE_E;
+    _d = byte >> 0;
+    PULSE_E;
+    // Most instructions take 40us
+    wait(0.000040f);  
+}
+
+void freetronicsLCDShield::writeData (int data) {
+    _rs = true;
+    writeByte(data);
+} 
+
+void freetronicsLCDShield::writeCommand (int command) {
+    _rs = false;
+    writeByte(command);
+} 
+
+float freetronicsLCDShield::readButton(void) {
+    return(_a0.read());
+}
+
+// Virtual functions for stream
+int freetronicsLCDShield::_putc(int value) {   
+    writeData(value);
+    return value;
+}
+
+int freetronicsLCDShield::_getc() {
+    return -1;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/freetronicsLCDShield.h	Tue Oct 08 08:46:39 2013 +0000
@@ -0,0 +1,43 @@
+#define LEFT  0
+#define RIGHT 1
+
+class freetronicsLCDShield : public Stream {
+    private:
+        // Functions
+        void writeByte (int byte);
+        void writeCommand (int command);
+        void writeData (int data);
+        void character(int line, int col, int value);
+        
+        // Hardware         
+        DigitalOut _rs, _e;
+        BusOut _d;
+        PwmOut _bl;
+        AnalogIn _a0;
+        
+    public:
+        // Constructor
+        freetronicsLCDShield (PinName rs = PTA13, 
+                              PinName e  = PTD5, 
+                              PinName d0 = PTA4, 
+                              PinName d1 = PTA5, 
+                              PinName d2 = PTC8, 
+                              PinName d3 = PTC9,
+                              PinName bl = PTA12,
+                              PinName a0 = PTB0);                               
+                              
+        void setCursorPosition (int line, int col);
+        void setBackLight (bool blStatus);
+        void setBackLight (float blIntensity);
+        void setCursor (bool cStatus, bool blink = false);
+        void shift (bool left); 
+        void cls (void);
+        void home(void);
+        
+        float readButton(void);
+             
+    protected:
+        // Stream implementation functions
+        virtual int _putc(int value);
+        virtual int _getc();
+};
\ No newline at end of file