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