|
| 1 | +/* |
| 2 | + SFE_MicroOLED Library Demo |
| 3 | + Paul Clark @ SparkFun Electronics |
| 4 | + Original Creation Date: December 11th, 2020 |
| 5 | +
|
| 6 | + This sketch uses the MicroOLED library to show all the functionality built into the library |
| 7 | + using the begin function defined in version v1.3 of the library - which allows different |
| 8 | + TwoWire ports and custom I2C addresses to be used. |
| 9 | +
|
| 10 | + Hardware Connections: |
| 11 | + This example assumes you are using Qwiic. See the SPI examples for |
| 12 | + a detailed breakdown of connection info. |
| 13 | +
|
| 14 | + This code is beerware; if you see me (or any other SparkFun employee) at the |
| 15 | + local, and you've found our code helpful, please buy us a round! |
| 16 | + |
| 17 | + Distributed as-is; no warranty is given. |
| 18 | +*/ |
| 19 | + |
| 20 | +#include <Wire.h> |
| 21 | +#include <SFE_MicroOLED.h> //Click here to get the library: http://librarymanager/All#SparkFun_Micro_OLED |
| 22 | + |
| 23 | +#define PIN_RESET 9 |
| 24 | + |
| 25 | +/* |
| 26 | +// This is the old way of instantiating oled. You can still do it this way if you want to. |
| 27 | +#define DC_JUMPER 1 |
| 28 | +MicroOLED oled(PIN_RESET, DC_JUMPER); // I2C declaration |
| 29 | +*/ |
| 30 | + |
| 31 | +// From version v1.3, we can also instantiate oled like this (but only for I2C) |
| 32 | +MicroOLED oled(PIN_RESET); // The TwoWire I2C port is passed to .begin instead |
| 33 | + |
| 34 | +void setup() |
| 35 | +{ |
| 36 | + delay(100); |
| 37 | + Wire.begin(); |
| 38 | + |
| 39 | + |
| 40 | +/* |
| 41 | + // This is the old way of initializing the OLED. |
| 42 | + // You can still do it this way if you want to - but only |
| 43 | + // if you instantiated oled using: MicroOLED oled(PIN_RESET, DC_JUMPER) |
| 44 | + oled.begin(); // Initialize the OLED |
| 45 | +*/ |
| 46 | + |
| 47 | + |
| 48 | + // This is the new way of initializing the OLED. |
| 49 | + // We can pass a different I2C address and TwoWire port |
| 50 | + oled.begin(0x3D, Wire); // Initialize the OLED |
| 51 | + |
| 52 | + |
| 53 | +/* |
| 54 | + // This is the new way of initializing the OLED. |
| 55 | + // We can pass a different I2C address and TwoWire port |
| 56 | + oled.begin(0x3D, Qwiic); // Initialize the OLED |
| 57 | +*/ |
| 58 | + |
| 59 | + |
| 60 | + oled.clear(ALL); // Clear the display's internal memory |
| 61 | + oled.display(); // Display what's in the buffer (splashscreen) |
| 62 | + |
| 63 | + delay(1000); // Delay 1000 ms |
| 64 | + |
| 65 | + oled.clear(PAGE); // Clear the buffer. |
| 66 | + |
| 67 | + randomSeed(analogRead(A0) + analogRead(A1)); |
| 68 | +} |
| 69 | + |
| 70 | +void pixelExample() |
| 71 | +{ |
| 72 | + printTitle("Pixels", 1); |
| 73 | + |
| 74 | + for (int i = 0; i < 512; i++) |
| 75 | + { |
| 76 | + oled.pixel(random(oled.getLCDWidth()), random(oled.getLCDHeight())); |
| 77 | + oled.display(); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +void lineExample() |
| 82 | +{ |
| 83 | + int middleX = oled.getLCDWidth() / 2; |
| 84 | + int middleY = oled.getLCDHeight() / 2; |
| 85 | + int xEnd, yEnd; |
| 86 | + int lineWidth = min(middleX, middleY); |
| 87 | + |
| 88 | + printTitle("Lines!", 1); |
| 89 | + |
| 90 | + for (int i = 0; i < 3; i++) |
| 91 | + { |
| 92 | + for (int deg = 0; deg < 360; deg += 15) |
| 93 | + { |
| 94 | + xEnd = lineWidth * cos(deg * PI / 180.0); |
| 95 | + yEnd = lineWidth * sin(deg * PI / 180.0); |
| 96 | + |
| 97 | + oled.line(middleX, middleY, middleX + xEnd, middleY + yEnd); |
| 98 | + oled.display(); |
| 99 | + delay(10); |
| 100 | + } |
| 101 | + for (int deg = 0; deg < 360; deg += 15) |
| 102 | + { |
| 103 | + xEnd = lineWidth * cos(deg * PI / 180.0); |
| 104 | + yEnd = lineWidth * sin(deg * PI / 180.0); |
| 105 | + |
| 106 | + oled.line(middleX, middleY, middleX + xEnd, middleY + yEnd, BLACK, NORM); |
| 107 | + oled.display(); |
| 108 | + delay(10); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +void shapeExample() |
| 114 | +{ |
| 115 | + printTitle("Shapes!", 0); |
| 116 | + |
| 117 | + // Silly pong demo. It takes a lot of work to fake pong... |
| 118 | + int paddleW = 3; // Paddle width |
| 119 | + int paddleH = 15; // Paddle height |
| 120 | + // Paddle 0 (left) position coordinates |
| 121 | + int paddle0_Y = (oled.getLCDHeight() / 2) - (paddleH / 2); |
| 122 | + int paddle0_X = 2; |
| 123 | + // Paddle 1 (right) position coordinates |
| 124 | + int paddle1_Y = (oled.getLCDHeight() / 2) - (paddleH / 2); |
| 125 | + int paddle1_X = oled.getLCDWidth() - 3 - paddleW; |
| 126 | + int ball_rad = 2; // Ball radius |
| 127 | + // Ball position coordinates |
| 128 | + int ball_X = paddle0_X + paddleW + ball_rad; |
| 129 | + int ball_Y = random(1 + ball_rad, oled.getLCDHeight() - ball_rad); //paddle0_Y + ball_rad; |
| 130 | + int ballVelocityX = 1; // Ball left/right velocity |
| 131 | + int ballVelocityY = 1; // Ball up/down velocity |
| 132 | + int paddle0Velocity = -1; // Paddle 0 velocity |
| 133 | + int paddle1Velocity = 1; // Paddle 1 velocity |
| 134 | + |
| 135 | + //while(ball_X >= paddle0_X + paddleW - 1) |
| 136 | + while ((ball_X - ball_rad > 1) && |
| 137 | + (ball_X + ball_rad < oled.getLCDWidth() - 2)) |
| 138 | + { |
| 139 | + // Increment ball's position |
| 140 | + ball_X += ballVelocityX; |
| 141 | + ball_Y += ballVelocityY; |
| 142 | + // Check if the ball is colliding with the left paddle |
| 143 | + if (ball_X - ball_rad < paddle0_X + paddleW) |
| 144 | + { |
| 145 | + // Check if ball is within paddle's height |
| 146 | + if ((ball_Y > paddle0_Y) && (ball_Y < paddle0_Y + paddleH)) |
| 147 | + { |
| 148 | + ball_X++; // Move ball over one to the right |
| 149 | + ballVelocityX = -ballVelocityX; // Change velocity |
| 150 | + } |
| 151 | + } |
| 152 | + // Check if the ball hit the right paddle |
| 153 | + if (ball_X + ball_rad > paddle1_X) |
| 154 | + { |
| 155 | + // Check if ball is within paddle's height |
| 156 | + if ((ball_Y > paddle1_Y) && (ball_Y < paddle1_Y + paddleH)) |
| 157 | + { |
| 158 | + ball_X--; // Move ball over one to the left |
| 159 | + ballVelocityX = -ballVelocityX; // change velocity |
| 160 | + } |
| 161 | + } |
| 162 | + // Check if the ball hit the top or bottom |
| 163 | + if ((ball_Y <= ball_rad) || (ball_Y >= (oled.getLCDHeight() - ball_rad - 1))) |
| 164 | + { |
| 165 | + // Change up/down velocity direction |
| 166 | + ballVelocityY = -ballVelocityY; |
| 167 | + } |
| 168 | + // Move the paddles up and down |
| 169 | + paddle0_Y += paddle0Velocity; |
| 170 | + paddle1_Y += paddle1Velocity; |
| 171 | + // Change paddle 0's direction if it hit top/bottom |
| 172 | + if ((paddle0_Y <= 1) || (paddle0_Y > oled.getLCDHeight() - 2 - paddleH)) |
| 173 | + { |
| 174 | + paddle0Velocity = -paddle0Velocity; |
| 175 | + } |
| 176 | + // Change paddle 1's direction if it hit top/bottom |
| 177 | + if ((paddle1_Y <= 1) || (paddle1_Y > oled.getLCDHeight() - 2 - paddleH)) |
| 178 | + { |
| 179 | + paddle1Velocity = -paddle1Velocity; |
| 180 | + } |
| 181 | + |
| 182 | + // Draw the Pong Field |
| 183 | + oled.clear(PAGE); // Clear the page |
| 184 | + // Draw an outline of the screen: |
| 185 | + oled.rect(0, 0, oled.getLCDWidth() - 1, oled.getLCDHeight()); |
| 186 | + // Draw the center line |
| 187 | + oled.rectFill(oled.getLCDWidth() / 2 - 1, 0, 2, oled.getLCDHeight()); |
| 188 | + // Draw the Paddles: |
| 189 | + oled.rectFill(paddle0_X, paddle0_Y, paddleW, paddleH); |
| 190 | + oled.rectFill(paddle1_X, paddle1_Y, paddleW, paddleH); |
| 191 | + // Draw the ball: |
| 192 | + oled.circle(ball_X, ball_Y, ball_rad); |
| 193 | + // Actually draw everything on the screen: |
| 194 | + oled.display(); |
| 195 | + delay(25); // Delay for visibility |
| 196 | + } |
| 197 | + delay(1000); |
| 198 | +} |
| 199 | + |
| 200 | +void textExamples() |
| 201 | +{ |
| 202 | + printTitle("Text!", 1); |
| 203 | + |
| 204 | + // Demonstrate font 0. 5x8 font |
| 205 | + oled.clear(PAGE); // Clear the screen |
| 206 | + oled.setFontType(0); // Set font to type 0 |
| 207 | + oled.setCursor(0, 0); // Set cursor to top-left |
| 208 | + // There are 255 possible characters in the font 0 type. |
| 209 | + // Lets run through all of them and print them out! |
| 210 | + for (int i = 0; i <= 255; i++) |
| 211 | + { |
| 212 | + // You can write byte values and they'll be mapped to |
| 213 | + // their ASCII equivalent character. |
| 214 | + oled.write(i); // Write a byte out as a character |
| 215 | + oled.display(); // Draw on the screen |
| 216 | + delay(10); // Wait 10ms |
| 217 | + // We can only display 60 font 0 characters at a time. |
| 218 | + // Every 60 characters, pause for a moment. Then clear |
| 219 | + // the page and start over. |
| 220 | + if ((i % 60 == 0) && (i != 0)) |
| 221 | + { |
| 222 | + delay(500); // Delay 500 ms |
| 223 | + oled.clear(PAGE); // Clear the page |
| 224 | + oled.setCursor(0, 0); // Set cursor to top-left |
| 225 | + } |
| 226 | + } |
| 227 | + delay(500); // Wait 500ms before next example |
| 228 | + |
| 229 | + // Demonstrate font 1. 8x16. Let's use the print function |
| 230 | + // to display every character defined in this font. |
| 231 | + oled.setFontType(1); // Set font to type 1 |
| 232 | + oled.clear(PAGE); // Clear the page |
| 233 | + oled.setCursor(0, 0); // Set cursor to top-left |
| 234 | + // Print can be used to print a string to the screen: |
| 235 | + oled.print(" !\"#$%&'()*+,-./01234"); |
| 236 | + oled.display(); // Refresh the display |
| 237 | + delay(1000); // Delay a second and repeat |
| 238 | + oled.clear(PAGE); |
| 239 | + oled.setCursor(0, 0); |
| 240 | + oled.print("56789:;<=>?@ABCDEFGHI"); |
| 241 | + oled.display(); |
| 242 | + delay(1000); |
| 243 | + oled.clear(PAGE); |
| 244 | + oled.setCursor(0, 0); |
| 245 | + oled.print("JKLMNOPQRSTUVWXYZ[\\]^"); |
| 246 | + oled.display(); |
| 247 | + delay(1000); |
| 248 | + oled.clear(PAGE); |
| 249 | + oled.setCursor(0, 0); |
| 250 | + oled.print("_`abcdefghijklmnopqrs"); |
| 251 | + oled.display(); |
| 252 | + delay(1000); |
| 253 | + oled.clear(PAGE); |
| 254 | + oled.setCursor(0, 0); |
| 255 | + oled.print("tuvwxyz{|}~"); |
| 256 | + oled.display(); |
| 257 | + delay(1000); |
| 258 | + |
| 259 | + // Demonstrate font 2. 10x16. Only numbers and '.' are defined. |
| 260 | + // This font looks like 7-segment displays. |
| 261 | + // Lets use this big-ish font to display readings from the |
| 262 | + // analog pins. |
| 263 | + for (int i = 0; i < 25; i++) |
| 264 | + { |
| 265 | + oled.clear(PAGE); // Clear the display |
| 266 | + oled.setCursor(0, 0); // Set cursor to top-left |
| 267 | + oled.setFontType(0); // Smallest font |
| 268 | + oled.print("A0: "); // Print "A0" |
| 269 | + oled.setFontType(2); // 7-segment font |
| 270 | + oled.print(analogRead(A0)); // Print a0 reading |
| 271 | + oled.setCursor(0, 16); // Set cursor to top-middle-left |
| 272 | + oled.setFontType(0); // Repeat |
| 273 | + oled.print("A1: "); |
| 274 | + oled.setFontType(2); |
| 275 | + oled.print(analogRead(A1)); |
| 276 | + oled.setCursor(0, 32); |
| 277 | + oled.setFontType(0); |
| 278 | + oled.print("A2: "); |
| 279 | + oled.setFontType(2); |
| 280 | + oled.print(analogRead(A2)); |
| 281 | + oled.display(); |
| 282 | + delay(100); |
| 283 | + } |
| 284 | + |
| 285 | + // Demonstrate font 3. 12x48. Stopwatch demo. |
| 286 | + oled.setFontType(3); // Use the biggest font |
| 287 | + int ms = 0; |
| 288 | + int s = 0; |
| 289 | + while (s <= 5) |
| 290 | + { |
| 291 | + oled.clear(PAGE); // Clear the display |
| 292 | + oled.setCursor(0, 0); // Set cursor to top-left |
| 293 | + if (s < 10) |
| 294 | + oled.print("00"); // Print "00" if s is 1 digit |
| 295 | + else if (s < 100) |
| 296 | + oled.print("0"); // Print "0" if s is 2 digits |
| 297 | + oled.print(s); // Print s's value |
| 298 | + oled.print(":"); // Print ":" |
| 299 | + oled.print(ms); // Print ms value |
| 300 | + oled.display(); // Draw on the screen |
| 301 | + ms++; // Increment ms |
| 302 | + if (ms >= 10) // If ms is >= 10 |
| 303 | + { |
| 304 | + ms = 0; // Set ms back to 0 |
| 305 | + s++; // and increment s |
| 306 | + } |
| 307 | + } |
| 308 | +} |
| 309 | + |
| 310 | +void loop() |
| 311 | +{ |
| 312 | + //pixelExample(); // Run the pixel example function |
| 313 | + lineExample(); // Then the line example function |
| 314 | + shapeExample(); // Then the shape example |
| 315 | + textExamples(); // Finally the text example |
| 316 | +} |
| 317 | + |
| 318 | +// Center and print a small title |
| 319 | +// This function is quick and dirty. Only works for titles one |
| 320 | +// line long. |
| 321 | +void printTitle(String title, int font) |
| 322 | +{ |
| 323 | + int middleX = oled.getLCDWidth() / 2; |
| 324 | + int middleY = oled.getLCDHeight() / 2; |
| 325 | + |
| 326 | + oled.clear(PAGE); |
| 327 | + oled.setFontType(font); |
| 328 | + // Try to set the cursor in the middle of the screen |
| 329 | + oled.setCursor(middleX - (oled.getFontWidth() * (title.length() / 2)), |
| 330 | + middleY - (oled.getFontHeight() / 2)); |
| 331 | + // Print the title: |
| 332 | + oled.print(title); |
| 333 | + oled.display(); |
| 334 | + delay(1500); |
| 335 | + oled.clear(PAGE); |
| 336 | +} |
0 commit comments