Program allows for a rectangle displayed on SSD1306-based OLED display to be moved by x- and y-axis uisng two user potentiometers.

Dependencies:   mbed Adafruit_GFX

Revision:
9:46f39b99737b
Parent:
8:ead6147e31a4
--- a/main.cpp	Wed Mar 30 14:58:05 2016 +0000
+++ b/main.cpp	Sat Nov 20 13:09:32 2021 +0000
@@ -1,54 +1,60 @@
-/* Scan I2C bus on specified pins and prints out
-* the all address where an active device responds.
-*
-  By Joseph Ellsworth CTO of A2WH
-  Take a look at A2WH.com Producing Water from Air using Solar Energy
-  March-2016 License: https://developer.mbed.org/handbook/MIT-Licence 
-  Please contact us http://a2wh.com for help with custom design projects.
-
-  
-* Don't forget 3K pull-up resistors on sda,scl 
-* 
-* I tested this by soldering in a I2C chip known to respond at
-* address dec=120 hex=70 and the utility got the ack as expected.
-* when the chip was de-soldered it was no longer detected.  
-*/
-
+/* Moving the rectangle previewed on the OLED display
+ * by using potentiometers POT1 and POT2
+ * 
+ * November, 2021.
+ */
 #include "mbed.h"
-
-Serial pc(USBTX, USBRX);
-Serial pc2(USBTX, USBRX);
+#include "Adafruit_GFX.h"
+#include "Adafruit_GFX_Config.h"
+#include "Adafruit_SSD1306.h"
 
- #define D_SDA                  PB_7 
- #define D_SCL                  PB_6 
-   // sda=PB7, scl=PB_6 Pins specific to Nucleo-F303K8
-  // must change pins to match your board.
+// I2C bus pins:
+#define D_SDA                  PB_14 
+#define D_SCL                  PB_13     
+// I2C address, 60d or 0x3c:
+#define I2C_REAL_ADD                                                        0x3c
+#define I2C_ADDRESS                                            I2C_REAL_ADD << 1 
+// Set OLED width and heigth [pixel]:
+#define OLED_WIDTH_PX                                                        128
+#define OLED_HEIGHT_PX                                                        64
+// I2C frequency:
+#define I2C_FREQUENCY                                                     400000
+// Multipliers of POT1 and POT2 for OLED rectangle position:
+#define WIDTH_SCALER                                                         128
+#define HEIGHT_SCALER                                                         64
+// Initial rectangle position:
+#define INITIAL_X_POSITION                                     OLED_WIDTH_PX / 2
+#define INITIAL_Y_POSITION                                    OLED_HEIGHT_PX / 2
+// Refresh rate:
+#define REFRESH_RATE_MS                                                        5
+// Half of the potentiometer return value: 
+#define HALF_INTERVAL                                                       0.5f   
 
-I2C i2c(D_SDA, D_SCL);  
-  
-DigitalOut myled(LED1);
- 
-int ack;   
-int address;  
-void scanI2C() {
-  for(address=1;address<127;address++) {    
-    ack = i2c.write(address, "11", 1);
-    if (ack == 0) {
-       pc.printf("\tFound at %3d -- %3x\r\n", address,address);
-    }    
-    wait(0.05);
-  } 
-}
+// Initialize potentiometers' pins:
+AnalogIn POT1(PA_0);
+AnalogIn POT2(PA_1);
+
+// Initialize I2C:
+I2C i2c(PB_14,PB_13);
+
+// Initialize OLED display:
+Adafruit_SSD1306_I2c myOled(i2c,PB_5,I2C_ADDRESS,OLED_HEIGHT_PX,OLED_WIDTH_PX);
  
 int main() {
-  pc.baud(9600);
-  pc.printf("I2C scanner \r\n");
-  scanI2C();
-  pc.printf("Finished Scan\r\n");
-  // just blink to let us know the CPU is alive 
-  while(1) { 
-      wait(5.0);          
-      myled = !myled;
+  
+    // Initialize OLED:
+    myOled.begin();
+    int x, y = 0;
+//    i2c.frequency(I2C_FREQUENCY);
+    while(true) { 
+//        #ifdef GFX_WANT_ABSTRACTS
+        x = (POT1 - HALF_INTERVAL)*WIDTH_SCALER;
+        y = (POT2 - HALF_INTERVAL)*HEIGHT_SCALER;
+        myOled.fillRect(x, y, OLED_WIDTH_PX, OLED_HEIGHT_PX, WHITE);
+        myOled.display();
+//        #endif 
+        wait_ms(REFRESH_RATE_MS);
+        myOled.clearDisplay();
   }
 }
  
\ No newline at end of file