display i2c

Dependencies:   mbed SSD1308_128x64_I2C USBDevice

Revision:
0:2ded56b8407d
Child:
1:00053cb70ac5
diff -r 000000000000 -r 2ded56b8407d main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Jun 19 20:00:10 2012 +0000
@@ -0,0 +1,205 @@
+/* mbed Seeed 128x64 OLED Test
+ * http://www.seeedstudio.com/depot/grove-oled-display-12864-p-781.html?cPath=163_167
+ *
+ * Copyright (c) 2012 Wim Huiskamp
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * version 0.2 Initial Release
+ */
+#include "mbed.h"
+#include "FixedWidthFont.h"
+#include "SSD1308.h"
+
+//Pin Defines for I2C Bus
+//#define D_SDA                  p9
+//#define D_SCL                  p10
+#define D_SDA                  p28
+#define D_SCL                  p27
+I2C i2c(D_SDA, D_SCL);
+
+//Host PC Baudrate (Virtual Com Port on USB)
+#define D_BAUDRATE            9600
+//#define D_BAUDRATE            57600
+
+// mbed Interface Hardware definitions
+DigitalOut myled1(LED1);
+DigitalOut myled2(LED2);
+DigitalOut myled3(LED3);
+DigitalOut heartbeatLED(LED4);
+
+// Host PC Communication channels
+Serial pc(USBTX, USBRX); // tx, rx
+
+// Instantiate OLED
+SSD1308 oled = SSD1308(i2c, SSD1308_SA0);
+
+void show_menu() {
+    pc.printf("0: Exit\n\r");
+    pc.printf("1: Show Menu\n\r");    
+    pc.printf("2: Send Message\n\r"); 
+    pc.printf("3: \n\r");    
+    pc.printf("4: \n\r");        
+    pc.printf("5: \n\r");            
+    pc.printf("6: \n\r");                
+    pc.printf("7: \n\r");                    
+    pc.printf("A: \n\r");                
+    pc.printf("B: \n\r");                    
+    pc.printf("P: \n\r");                        
+    pc.printf("\n\r");                
+}
+
+
+void oled_Test() {
+  pc.printf("OLED test start\r");  
+  
+  oled.setDisplayOff();
+  pc.printf("display off\r\n");          
+  wait(0.5);
+
+  oled.setDisplayOn();
+  pc.printf("display on\r\n");          
+  wait(0.5);  
+
+  oled.clearDisplay();
+  pc.printf("display cleared\r\n");        
+  wait(0.5);
+
+  oled.fillDisplay();
+  pc.printf("display filled\r\n");      
+  wait(0.5);
+  
+  oled.clearDisplay();
+  pc.printf("display cleared\r\n");    
+  wait(0.5);
+
+  oled.writeString(0, 0, 8, "Foobar!!");
+  pc.printf("printed something\r\n");    
+  wait(3);
+    
+  oled.writeString(1, 8, 8, "baz quux");
+  pc.printf("printed something\r\n");  
+  wait(3);
+    
+  oled.writeString(2, 0, 272, "a long, rather lengthy, extended string passage thing, eh, that just goes on, and on, and on, and on, and on, and on, and on, yes, further, continuing, extending, expanding beyond all reason or sanity!!!!! and yet, there's more!  so much more!  for ever and ever, oh yeah");
+  pc.printf("printed something\r\n");
+  wait(3);
+
+  pc.printf("OLED test done\r\n");  
+}
+
+// Variables for Heartbeat and Status monitoring
+Ticker heartbeat;
+bool heartbeatflag=false;
+
+// Local functions
+void clear_screen() {
+//ANSI Terminal Commands
+    pc.printf("\x1B[2J");
+    pc.printf("\x1B[H");
+}
+
+
+void init_interfaces() {
+    // Init Host PC communication, default is 9600
+    pc.baud(D_BAUDRATE);
+      
+    // Init I/F hardware
+//    i2c.frequency(100000);
+    i2c.frequency(400000); // according to the spec the max bitrate for the SSD1308 is 400 kbit/s
+}
+      
+
+// Heartbeat monitor
+void pulse() {
+  heartbeatLED = !heartbeatLED;
+}
+
+void heartbeat_start() {
+  heartbeat.attach(&pulse, 0.5);
+}
+
+void heartbeat_stop() {
+  heartbeat.detach();
+}
+
+
+int main() {
+    bool running=true;
+    char command;
+
+    init_interfaces();
+    
+    heartbeat_start();
+          
+    clear_screen(); 
+  
+    pc.printf("Hello World!\n\r");
+
+// Quick test
+    oled_Test(); 
+    
+    while(1) {
+        myled1 = 1;
+        wait(0.2);       
+         
+        myled1 = 0;
+        wait(0.2);     
+        pc.printf("*");
+    }
+    
+
+// Interactive Test
+    show_menu();
+      
+    while(running) {
+          
+       if(pc.readable()) {
+         command = pc.getc();       
+         pc.printf("command= %c \n\r", command);         
+
+         switch (command) {
+          case '0' :
+                     pc.printf("Done\n\r");                    
+                     running = false;  
+                     break;
+          
+          case '1' :
+                     show_menu();
+                     break;
+                    
+          case '2' :
+                     pc.printf("Hello World!\n\r");          
+                     break;
+          
+          case '3' :
+                     break;
+
+          case '4' :
+                     break;
+
+          case '5' :
+                     break;
+          case '6' :
+                     break;
+
+          case '7' :
+                     break;
+
+          case 'A' :
+                     break;
+
+          case 'B' :
+                   
+                     break;
+
+          case 'P' :
+          
+                    break;
+                              
+        } //switch
+      }//if
+    }//while
+
+    pc.printf("Bye World!\n\r");                       
+}//main    
+