![](/media/cache/profiles/nikki.png.50x50_q85.jpg)
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
main.cpp@9:46f39b99737b, 2021-11-20 (annotated)
- Committer:
- tzwell
- Date:
- Sat Nov 20 13:09:32 2021 +0000
- Revision:
- 9:46f39b99737b
- Parent:
- 8:ead6147e31a4
First publish, first commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tzwell | 9:46f39b99737b | 1 | /* Moving the rectangle previewed on the OLED display |
tzwell | 9:46f39b99737b | 2 | * by using potentiometers POT1 and POT2 |
tzwell | 9:46f39b99737b | 3 | * |
tzwell | 9:46f39b99737b | 4 | * November, 2021. |
tzwell | 9:46f39b99737b | 5 | */ |
joeata2wh | 0:befb557584fb | 6 | #include "mbed.h" |
tzwell | 9:46f39b99737b | 7 | #include "Adafruit_GFX.h" |
tzwell | 9:46f39b99737b | 8 | #include "Adafruit_GFX_Config.h" |
tzwell | 9:46f39b99737b | 9 | #include "Adafruit_SSD1306.h" |
joeata2wh | 0:befb557584fb | 10 | |
tzwell | 9:46f39b99737b | 11 | // I2C bus pins: |
tzwell | 9:46f39b99737b | 12 | #define D_SDA PB_14 |
tzwell | 9:46f39b99737b | 13 | #define D_SCL PB_13 |
tzwell | 9:46f39b99737b | 14 | // I2C address, 60d or 0x3c: |
tzwell | 9:46f39b99737b | 15 | #define I2C_REAL_ADD 0x3c |
tzwell | 9:46f39b99737b | 16 | #define I2C_ADDRESS I2C_REAL_ADD << 1 |
tzwell | 9:46f39b99737b | 17 | // Set OLED width and heigth [pixel]: |
tzwell | 9:46f39b99737b | 18 | #define OLED_WIDTH_PX 128 |
tzwell | 9:46f39b99737b | 19 | #define OLED_HEIGHT_PX 64 |
tzwell | 9:46f39b99737b | 20 | // I2C frequency: |
tzwell | 9:46f39b99737b | 21 | #define I2C_FREQUENCY 400000 |
tzwell | 9:46f39b99737b | 22 | // Multipliers of POT1 and POT2 for OLED rectangle position: |
tzwell | 9:46f39b99737b | 23 | #define WIDTH_SCALER 128 |
tzwell | 9:46f39b99737b | 24 | #define HEIGHT_SCALER 64 |
tzwell | 9:46f39b99737b | 25 | // Initial rectangle position: |
tzwell | 9:46f39b99737b | 26 | #define INITIAL_X_POSITION OLED_WIDTH_PX / 2 |
tzwell | 9:46f39b99737b | 27 | #define INITIAL_Y_POSITION OLED_HEIGHT_PX / 2 |
tzwell | 9:46f39b99737b | 28 | // Refresh rate: |
tzwell | 9:46f39b99737b | 29 | #define REFRESH_RATE_MS 5 |
tzwell | 9:46f39b99737b | 30 | // Half of the potentiometer return value: |
tzwell | 9:46f39b99737b | 31 | #define HALF_INTERVAL 0.5f |
joeata2wh | 0:befb557584fb | 32 | |
tzwell | 9:46f39b99737b | 33 | // Initialize potentiometers' pins: |
tzwell | 9:46f39b99737b | 34 | AnalogIn POT1(PA_0); |
tzwell | 9:46f39b99737b | 35 | AnalogIn POT2(PA_1); |
tzwell | 9:46f39b99737b | 36 | |
tzwell | 9:46f39b99737b | 37 | // Initialize I2C: |
tzwell | 9:46f39b99737b | 38 | I2C i2c(PB_14,PB_13); |
tzwell | 9:46f39b99737b | 39 | |
tzwell | 9:46f39b99737b | 40 | // Initialize OLED display: |
tzwell | 9:46f39b99737b | 41 | Adafruit_SSD1306_I2c myOled(i2c,PB_5,I2C_ADDRESS,OLED_HEIGHT_PX,OLED_WIDTH_PX); |
joeata2wh | 0:befb557584fb | 42 | |
joeata2wh | 0:befb557584fb | 43 | int main() { |
tzwell | 9:46f39b99737b | 44 | |
tzwell | 9:46f39b99737b | 45 | // Initialize OLED: |
tzwell | 9:46f39b99737b | 46 | myOled.begin(); |
tzwell | 9:46f39b99737b | 47 | int x, y = 0; |
tzwell | 9:46f39b99737b | 48 | // i2c.frequency(I2C_FREQUENCY); |
tzwell | 9:46f39b99737b | 49 | while(true) { |
tzwell | 9:46f39b99737b | 50 | // #ifdef GFX_WANT_ABSTRACTS |
tzwell | 9:46f39b99737b | 51 | x = (POT1 - HALF_INTERVAL)*WIDTH_SCALER; |
tzwell | 9:46f39b99737b | 52 | y = (POT2 - HALF_INTERVAL)*HEIGHT_SCALER; |
tzwell | 9:46f39b99737b | 53 | myOled.fillRect(x, y, OLED_WIDTH_PX, OLED_HEIGHT_PX, WHITE); |
tzwell | 9:46f39b99737b | 54 | myOled.display(); |
tzwell | 9:46f39b99737b | 55 | // #endif |
tzwell | 9:46f39b99737b | 56 | wait_ms(REFRESH_RATE_MS); |
tzwell | 9:46f39b99737b | 57 | myOled.clearDisplay(); |
joeata2wh | 0:befb557584fb | 58 | } |
joeata2wh | 0:befb557584fb | 59 | } |
joeata2wh | 0:befb557584fb | 60 |