task
Dependencies: mbed Adafruit_GFX0
main.cpp
00001 /* Moving the rectangle previewed on the OLED display 00002 * by using potentiometers POT1 and POT2 00003 * 00004 * November, 2021. 00005 */ 00006 #include "mbed.h" 00007 #include "Adafruit_GFX.h" 00008 #include "Adafruit_GFX_Config.h" 00009 #include "Adafruit_SSD1306.h" 00010 00011 // I2C bus pins: 00012 #define D_SDA PB_14 00013 #define D_SCL PB_13 00014 // I2C address, 60d or 0x3c: 00015 #define I2C_REAL_ADD 0x3c 00016 #define I2C_ADDRESS I2C_REAL_ADD << 1 00017 // Set OLED width and heigth [pixel]: 00018 #define OLED_WIDTH_PX 128 00019 #define OLED_HEIGHT_PX 64 00020 // I2C frequency: 00021 #define I2C_FREQUENCY 400000 00022 // Multipliers of POT1 and POT2 for OLED rectangle position: 00023 #define WIDTH_SCALER 128 00024 #define HEIGHT_SCALER 64 00025 // Initial rectangle position: 00026 #define INITIAL_X_POSITION OLED_WIDTH_PX / 2 00027 #define INITIAL_Y_POSITION OLED_HEIGHT_PX / 2 00028 // Refresh rate: 00029 #define REFRESH_RATE_MS 5 00030 // Half of the potentiometer return value: 00031 #define HALF_INTERVAL 0.5f 00032 // x and y max values 00033 #define XMAX 127 00034 #define YMAX 63 00035 #define XMIN 0 00036 #define YMIN 0 00037 00038 // Initialize potentiometers' pins: 00039 AnalogIn POT1(PA_0); 00040 AnalogIn POT2(PA_1); 00041 00042 // Initialize I2C: 00043 I2C i2c(D_SDA,D_SCL); 00044 00045 // Initialize OLED display: 00046 Adafruit_SSD1306_I2c myOled(i2c,PB_5,I2C_ADDRESS,OLED_HEIGHT_PX,OLED_WIDTH_PX); 00047 00048 void screenSaver(){ 00049 int x = 7, y = 12, r = 5, xk = 1, yk = 1, dx = 4, dy = 3; 00050 00051 while(true){ 00052 myOled.drawRect(0, 0, OLED_WIDTH_PX - 1, OLED_HEIGHT_PX - 1, WHITE); 00053 00054 if((x - r) <= XMIN) xk = 1; 00055 if((x + r) >= XMAX) xk = -1; 00056 00057 if((y - r) <= YMIN) yk = 1; 00058 if((y + r) >= YMAX) yk = -1; 00059 00060 y += dy * yk; 00061 x += dx * xk; 00062 00063 myOled.fillCircle(x, y, r, WHITE); 00064 00065 myOled.display(); 00066 00067 wait_ms(REFRESH_RATE_MS); 00068 myOled.clearDisplay(); 00069 } 00070 } 00071 00072 void bounce(){ 00073 int x = 7, y = 12, r = 5, xk = 1, yk = 1, dx = 4, dy = 3, g = 3; 00074 00075 while(true){ 00076 myOled.drawRect(0, 0, OLED_WIDTH_PX - 1, OLED_HEIGHT_PX - 1, WHITE); 00077 00078 if((x - r) <= XMIN) xk = 1; 00079 if((x + r) >= XMAX) xk = -1; 00080 00081 if((y + r) <= YMAX){ 00082 dy += g; 00083 y += dy; 00084 } else { 00085 dy = -dy; 00086 y = 2 * YMAX - y; 00087 } 00088 00089 y += dy * yk; 00090 x += dx * xk; 00091 00092 myOled.fillCircle(x, y, r, WHITE); 00093 00094 myOled.display(); 00095 00096 wait_ms(REFRESH_RATE_MS); 00097 myOled.clearDisplay(); 00098 } 00099 } 00100 00101 int main() { 00102 00103 // Initialize OLED: 00104 myOled.begin(); 00105 myOled.setTextCursor(0, 0); 00106 i2c.frequency(I2C_FREQUENCY); 00107 00108 //screenSaver(); 00109 bounce(); 00110 }
Generated on Mon Jul 25 2022 02:29:08 by
1.7.2