7 Segment LED Displaydriver, I2C interface, SAA1064

Dependents:   812_hello

Files at this revision

API Documentation at this revision

Comitter:
wim
Date:
Tue Sep 10 19:59:35 2013 +0000
Parent:
0:48adc4a70511
Child:
2:970360b29a2a
Commit message:
First release version

Changed in this revision

SAA1064.cpp Show annotated file Show diff for this revision Revisions of this file
SAA1064.h Show annotated file Show diff for this revision Revisions of this file
--- a/SAA1064.cpp	Sun Sep 08 22:23:52 2013 +0000
+++ b/SAA1064.cpp	Tue Sep 10 19:59:35 2013 +0000
@@ -8,30 +8,24 @@
 #include "mbed.h"
 #include "SAA1064.h"
 
-/** Create an SAA1064 object connected to the specified I2C object and using the specified deviceAddress
- *
- * @param I2C &i2c the I2C port to connect to 
- * @param char deviceAddress the address of the SAA1064
-*/
-SAA1064::SAA1064(I2C *i2c, char deviceAddress) : _i2c(i2c) {
+
+/** Create a SAA1064 LED displaydriver object using a specified I2C bus and slaveaddress
+  *
+  * @param I2C &i2c the I2C port to connect to 
+  * @param char deviceAddress the address of the SAA1064
+  */  
+SAA1064::SAA1064(I2C *i2c, uint8_t deviceAddress) : _i2c(i2c) {
 
    _slaveAddress = deviceAddress;
    _init(); 
 };
 
-char SAA1064::read() {
-  char tmp = 0;
-  
-  return tmp;
-};
-
-void SAA1064::write(char byte) {
-};
-
-
-
-void SAA1064::setIntensity(unsigned char intensity) {
-  char data[6];
+/** Set segment brightness
+  *
+  * @param intensity       intensity value, valid Range between 0-7, 0 = 0 mA/segment, 1 = 3 mA/segment etc 
+  */  
+void SAA1064::setIntensity(uint8_t intensity) {
+  uint8_t data[6];
   
   intensity = (intensity & 0x07) << 4;        // Valid Range between 0-7
                                               //  0 = 0 mA/segment, 1 = 3 mA/segment etc 
@@ -39,14 +33,20 @@
   data[1] = SAA1064_CTRL_DEF | intensity;     // Init Control Reg
 
   // write data to the display
-  _i2c->write(_slaveAddress, data, 2);  
+  _i2c->write(_slaveAddress, (char*) data, 2);  
 
 };
 
 
-
-void SAA1064::write(unsigned char digit1, unsigned char digit2, unsigned char digit3, unsigned char digit4) {
-  char data[6];
+/** Write digits 
+  *
+  * @param digit1  LED segment pattern for digit1 (MSB) 
+  * @param digit2  LED segment pattern for digit2 
+  * @param digit3  LED segment pattern for digit3
+  * @param digit4  LED segment pattern for digit4 (LSB)       
+  */  
+void SAA1064::write(uint8_t digit1, uint8_t digit2, uint8_t digit3, uint8_t digit4) {
+  uint8_t data[6];
   
   data[0] = SAA1064_DIG1;                     // Select Digit1 Reg
   data[1] = digit1;                           // Digit 1
@@ -55,13 +55,20 @@
   data[4] = digit4;                           // Digit 4
 
   // write data to the display
-  _i2c->write(_slaveAddress, data, 5);   
+  _i2c->write(_slaveAddress, (char*) data, 5);   
 
 };
 
-void SAA1064::writeInt(int value, unsigned char dp_digit, bool leading) {
-  unsigned char digit_value;
-  char data[6];  
+
+/** Write Integer 
+  *
+  * @param value     integer value to display, valid range -999...9999 
+  * @param dp_digit  digit where decimal point is set, valid range 1..4 (no DP shown for dp_digit = 0)
+  * @param leading   suppress leading zero (false=show leading zero, true=suppress leading zero)
+  */  
+void SAA1064::writeInt(int value, uint8_t dp_digit, bool leading) {
+  uint8_t digit_value;
+  uint8_t data[6];  
 
   data[0] = SAA1064_DIG1;                     // Select Digit1 Reg
   
@@ -139,13 +146,72 @@
   } 
 
   // write data to the display
-  _i2c->write(_slaveAddress, data, 5);   
+  _i2c->write(_slaveAddress, (char*) data, 5);   
      
 };
 
+/** snake: show a short animation
+  *
+  * @param count     number of times animation is repeated, valid range 0..15 
+  *
+  */  
+void SAA1064::snake(uint8_t count) {
+  uint8_t i;
+  const float step = 0.1;
+//  const float loop = 0.1;
+  
+  count = count & 0x0F; // Limit max count
+    
+  for (i=0; i<count; i++) {
+    write(0x00,0x00,0x00,0x01); wait(step); 
+    write(0x00,0x00,0x01,0x01); wait(step);     
+    write(0x00,0x01,0x01,0x01); wait(step); 
+    write(0x01,0x01,0x01,0x00); wait(step); 
+    write(0x21,0x01,0x00,0x00); wait(step);     
+    write(0x31,0x00,0x00,0x00); wait(step);         
+    write(0x38,0x00,0x00,0x00); wait(step);         
+    write(0x18,0x08,0x00,0x00); wait(step);         
+    write(0x08,0x08,0x08,0x00); wait(step);         
+    write(0x00,0x08,0x08,0x08); wait(step);           
+    write(0x00,0x00,0x08,0x0C); wait(step);         
+    write(0x00,0x00,0x00,0x0E); wait(step);         
+    write(0x00,0x00,0x00,0x06); wait(step);         
+    write(0x00,0x00,0x00,0x02); wait(step);         
+    write(0x00,0x00,0x00,0x00); wait(step);             
+                                        
+//    wait(loop)    
+  }
 
+}        
+
+
+/** splash: show a short animation
+  *
+  * @param count     number of times animation is repeated, valid range 0..15 
+  *
+  */  
+void SAA1064::splash (uint8_t count){  
+  uint8_t i;
+  const float step = 0.3;
+//  const float loop = 0.1;
+  
+  count = count & 0x0F; // Limit max count
+    
+  for (i=0; i<count; i++) {
+    write(0x00,0x40,0x40,0x00); wait(step);       
+    write(0x39,0x09,0x09,0x0F); wait(step); 
+                                        
+//    wait(loop)    
+  }
+  write(0x00,0x00,0x00,0x00);
+}        
+
+
+/** Initialise LED driver 
+  *
+  */  
 void SAA1064::_init() { 
-  char data[6];
+  uint8_t data[6];
   
   data[0] = SAA1064_CTRL;                     // Select Control Reg
   data[1] = SAA1064_CTRL_DEF | SAA1064_INT3;  // Init Control Reg
@@ -160,6 +226,6 @@
 //  data[5] = SAA1064_ALL;                      // Digit 4: All Segments On     
 
   // write data to the display
-  _i2c->write(_slaveAddress, data, 6);  
+  _i2c->write(_slaveAddress, (char*) data, 6);  
   
 };
--- a/SAA1064.h	Sun Sep 08 22:23:52 2013 +0000
+++ b/SAA1064.h	Tue Sep 10 19:59:35 2013 +0000
@@ -8,6 +8,37 @@
 #ifndef _SAA1064_H
 #define _SAA1064_H
 
+/** Driver for SAA1064 I2C 4-Digit 7-Segment LED Driver
+ *
+ * @code
+ * #include "mbed.h"
+ * #include "SAA1064.h"
+ * 
+ * // I2C Communication
+ * I2C i2c_lcd(p28,p27); // SDA, SCL for LPC1768
+ * //I2C i2c_lcd(P0_10,P0_11); // SDA, SCL for LPC812
+ *
+ * SAA1064 LED(&i2c_lcd); // I2C bus, Default SAA1064 Slaveaddress
+ * 
+ * int main() {
+ *   uint8_t count = 0; 
+ *
+ *   // Display 0, 1, 2, 3
+ *   LED.write(SAA1064_SEGM[0], SAA1064_SEGM[1], SAA1064_SEGM[2], SAA1064_SEGM[3]);
+ *   wait(1);    
+ *   
+ *   while(1) {
+ *     wait(0.3);    
+ *     count++;       
+ *   
+ *     LED.writeInt(-150 + count, 3, false);  // Display value, dont suppress leading zero's
+ *   }
+ *  
+ * }
+ * @endcode
+ */
+
+
 //Address Defines for SAA1064
 #define SAA1064_SA0 0x70
 #define SAA1064_SA1 0x72
@@ -53,50 +84,89 @@
 #define D_L7                 0x80
 
 //Defines for Segments
-const char SAA1064_SEGM[] = {0x3F,  //0
-                             0x06,  //1
-                             0x5B,  //2
-                             0x4F,  //3
-                             0x66,  //4
-                             0x6D,  //5
-                             0x7D,  //6
-                             0x07,  //7
-                             0x7F,  //8
-                             0x6F,  //9
-                             0x77,  //A
-                             0x7C,  //B
-                             0x39,  //C
-                             0x5E,  //D
-                             0x79,  //E
-                             0x71}; //F
+const uint8_t SAA1064_SEGM[] = {0x3F,  //0
+                                0x06,  //1
+                                0x5B,  //2
+                                0x4F,  //3
+                                0x66,  //4
+                                0x6D,  //5
+                                0x7D,  //6
+                                0x07,  //7
+                                0x7F,  //8
+                                0x6F,  //9
+                                0x77,  //A
+                                0x7C,  //B
+                                0x39,  //C
+                                0x5E,  //D
+                                0x79,  //E
+                                0x71}; //F
 
-#define SAA1064_DP           0x80   //Decimal Point
-#define SAA1064_MINUS        0x40   //Minus Sign
-#define SAA1064_BLNK         0x00   //Blank Digit
-#define SAA1064_ALL          0xFF   //All Segments On
+#define SAA1064_DP              0x80   //Decimal Point
+#define SAA1064_MINUS           0x40   //Minus Sign
+#define SAA1064_BLNK            0x00   //Blank Digit
+#define SAA1064_ALL             0xFF   //All Segments On
                
 
-/** Create an SAA1064 object connected to the specified I2C object and using the specified deviceAddress
+
+
+/** Create an SAA1064 object connected to the specified I2C bus and deviceAddress
  *
- * @param I2C &i2c the I2C port to connect to 
- * @param char deviceAddress the address of the SAA1064
 */
 class SAA1064 {
 public:
-  SAA1064(I2C *i2c, char deviceAddress = SAA1064_SA0);
-  char read();
-  void write(char byte);
-    
-  void setIntensity(unsigned char intensity);
+  /** Create a SAA1064 LED displaydriver object using a specified I2C bus and slaveaddress
+   *
+   * @param I2C &i2c the I2C port to connect to 
+   * @param char deviceAddress the address of the SAA1064
+  */  
+  SAA1064(I2C *i2c, uint8_t deviceAddress = SAA1064_SA0);
+  
+  /** Set segment brightness
+   *
+   * @param intensity       intensity value, valid Range between 0-7, 0 = 0 mA/segment, 1 = 3 mA/segment etc 
+  */  
+  void setIntensity(uint8_t intensity);
+
+
+/** Write digits 
+  *
+  * @param digit1  LED segment pattern for digit1 (MSB) 
+  * @param digit2  LED segment pattern for digit2 
+  * @param digit3  LED segment pattern for digit3
+  * @param digit4  LED segment pattern for digit4 (LSB)       
+  */  
+  void write(uint8_t digit1, uint8_t digit2, uint8_t digit3, uint8_t digit4);        
 
-  void write(unsigned char digit1, unsigned char digit2, unsigned char digit3, unsigned char digit4);        
+/** Write Integer 
+  *
+  * @param value     integer value to display, valid range -999...9999 
+  * @param dp_digit  digit where decimal point is set, valid range 1..4 (no DP shown for dp_digit = 0)
+  * @param leading   suppress leading zero (false=show leading zero, true=suppress leading zero)
+  */  
+  void writeInt(int value, uint8_t dp_digit=0, bool leading=true);
 
-  void writeInt(int value, unsigned char dp_digit=0, bool leading=true);
 
+/** snake: show a short animation
+  *
+  * @param count     number of times animation is repeated, valid range 0..15 
+  *
+  */  
+  void snake(uint8_t count);        
+
+/** splash: show a short animation
+  *
+  * @param count     number of times animation is repeated, valid range 0..15 
+  *
+  */  
+  void splash (uint8_t count);  
+  
 protected:
-  I2C *_i2c;
-  unsigned char _slaveAddress;    
+  I2C *_i2c;                    //I2C bus reference
+  uint8_t _slaveAddress;        //I2C Slave address of device
 
+/** Initialise LED driver 
+  *
+  */  
   void _init(); 
 };