6 x 7 segment display library for PCA9637 driven breakout board

Dependents:   FTSE100 InternetDispBoB digitalThermometer Counter ... more

Files at this revision

API Documentation at this revision

Comitter:
d_worrall
Date:
Thu Jun 30 15:03:18 2011 +0000
Parent:
8:a875c4229c57
Child:
10:1fda6b0ee3bc
Commit message:
version5

Changed in this revision

dispBoB.cpp Show annotated file Show diff for this revision Revisions of this file
dispBoB.h Show annotated file Show diff for this revision Revisions of this file
--- a/dispBoB.cpp	Wed Jun 29 14:08:42 2011 +0000
+++ b/dispBoB.cpp	Thu Jun 30 15:03:18 2011 +0000
@@ -6,13 +6,11 @@
 #include "PCA9635.h"
 #include "dispBoB.h"
 
-Serial pc(USBTX, USBRX);
-
-    static const char _loc[] = {0x02, 0x02, 0x04, 0x04, 0x10, 0x10, 0x12, 0x12}; 
-    static const short _dispL[] = {
+    static const char loc[] = {0x02, 0x02, 0x04, 0x04, 0x10, 0x10, 0x12, 0x12}; 
+    static const short dispL[] = {
     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
-    0x0000, 0x1008, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0002, 0xC006, 0x600C, 0x0000, 0x0000, 0x0000, 0x0001, 0x0000, 0x0000,
+    0x0000, 0x1008, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0002, 0xC006, 0x600C, 0x0000, 0x0000, 0x0000, 0x0000, 0x1000, 0x0000,
     0xE00E, 0x2008, 0xC00D, 0x600D, 0x200B, 0x6007, 0xE007, 0x200E, 0xE00F, 0x600F, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
     
     0x0000, 0xF001, 0xE003, 0xC001, 0xE009, 0xC007, 0x8007, 0x600F, 0xA003, 0x8000, 0x6008, 0xA007, 0x3008, 0xA005, 0xA001, 0xE001,
@@ -20,10 +18,10 @@
     0x1000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC006, 0x0000, 0x600C, 0x0000, 0x0000
     };
-    static const short _dispR[] = {
+    static const short dispR[] = {
     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
-    0x0000, 0x0180, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0020, 0x0C60, 0x06C0, 0x0000, 0x0000, 0x0000, 0x0010, 0x0000, 0x0000,
+    0x0000, 0x0180, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0020, 0x0C60, 0x06C0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0100, 0x0000,
     0x0EE0, 0x0280, 0x0CD0, 0x06D0, 0x02B0, 0x0670, 0x0E70, 0x02E0, 0x0EF0, 0x06F0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
     
     0x0000, 0x0F10, 0x0E30, 0x0C10, 0x0E90, 0x0C70, 0x0870, 0x06F0, 0x0A30, 0x0800, 0x0680, 0x0A70, 0x0380, 0x0A50, 0x0A10, 0x0E10,
@@ -31,13 +29,13 @@
     0x0100, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0C60, 0x0000, 0x06C0, 0x0000, 0x0000
     };
-    short _element[8];
-    short _pair[4];
+    short element[8];
+    short ppair[4];
     string nullArray = "      ";
 
 dispBoB::dispBoB(PinName sda, PinName scl, PinName en): _pca(sda, scl), _en(en) {
     _en = 0;
-    _pca.init(_loc[5]);
+    _pca.init(loc[0]);
 }
 
 void dispBoB::cls(void){
@@ -45,47 +43,51 @@
         locate(j);
         _pca.bus(0x0000);
     }
-    _cursor = 0x10;
+    locate(0);
 }
 
 void dispBoB::locate(char pos){
-    _pca.setAddress(_loc[pos]);
+    _pca.setAddress(loc[pos]);
     _cursor = pos;
+    _pos = pos;
 }
 
-void dispBoB::putc(char c){
+int dispBoB::_putc(int c){
+    if(_pos>5) return -1;
     
     if(_cursor & 1){
-        _element[_cursor] = _dispR[c];
+        element[_cursor] = dispR[toupper(c)];
     } else {
-        _element[_cursor] = _dispL[c];
+        element[_cursor] = dispL[toupper(c)];
     }
     
     int i = _cursor/2;
-    _pair[i] = _element[(2*i)] + _element[(2*i)+1];
-    _pca.bus(_pair[i]); 
+    ppair[i] = element[(2*i)] + element[(2*i)+1];
+    _pca.bus(ppair[i]); 
+    
+    locate(_pos+1);
+    
+    return c;
 }
 
-void dispBoB::write(string str){ 
-    for(int j = 5; j >= 0; j--){
-        locate(j);
-        putc(toupper(str[j]));
-    }
+int dispBoB::_getc(){
+    return -1;
 }
 
 void dispBoB::scroll(string str, float speed){
     char buffer[6];
-
-    str.insert(0, nullArray);
+    str.insert(0, nullArray);               
     str.insert(str.length(), nullArray);
     
     for(int k = 0; k < str.length(); k++){
-        str.copy(buffer, 6, k);
-        write(buffer);
-        wait(speed);
+        str.copy(buffer, 6, k);    
+        locate(0);         
+        printf("%s", buffer);               
+        wait(speed);                
     }
 }
 
+//supply PCA9635 functionality to dispBoB class
 void dispBoB::bus(short leds){
     _pca.bus(leds);
 }
--- a/dispBoB.h	Wed Jun 29 14:08:42 2011 +0000
+++ b/dispBoB.h	Thu Jun 30 15:03:18 2011 +0000
@@ -28,7 +28,7 @@
 * }
 * @endcode
 */
-class dispBoB {
+class dispBoB : public Stream {
 public:
     //constructor
     /** Create a dispBoB object defined on the I2C master bus
@@ -43,23 +43,12 @@
     /** Clear screen
     *
     */
-    void cls(void);
+    virtual void cls(void);
     /** Set cursor position
     *
     * @param pos display location left to right (0-5)
     */
-    void locate(char pos);
-    /** Put an ASCII encoded character onto display at current location
-    *
-    * @param c ASCII encoded character or number (no punctuation)
-    */
-    void putc(char c);
-    /** Put an ASCII encoded character onto display at specified location
-    *
-    * @param c ASCII encoded character or number
-    * @param pos display location left to right (0-5)  
-    */
-    void write(string str);
+    virtual void locate(char pos);    
     /** Write a scrolling string (right to left) to display 
     *
     * @param str String to be displayed (no punctuation)
@@ -72,11 +61,31 @@
     */
     void bus(short leds);
     
-private:
+#if DOXYGEN_ONLY
+    /** Write a character to the display
+     *
+     * @param c The character to write to the display
+     */
+    int putc(int c);
+    /** Write a formated string to the LCD
+     *
+     * @param format A printf-style format string, followed by the
+     *               variables to use in formating the string. N.B. it will
+     *               only display the first 6 characters.
+     */
+    int printf(const char* format, ...);
+#endif
+    
+protected:
+
+    //Stream implementation functions
+    virtual int _putc(int c);
+    virtual int _getc();
 
     PCA9635 _pca;
     DigitalOut _en;
     char _cursor; 
+    char _pos;
 };
 
 #endif