|
| 1 | +/** |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2016 by Daniel Eichhorn |
| 5 | + * Copyright (c) 2016 by Fabrice Weinberg |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in all |
| 15 | + * copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | + * SOFTWARE. |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +// Include the correct display library |
| 28 | +// For a connection via I2C using Wire include |
| 29 | +#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier |
| 30 | +#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"` |
| 31 | +// For a connection via I2C using brzo_i2c (must be installed) include |
| 32 | +// #include <brzo_i2c.h> // Only needed for Arduino 1.6.5 and earlier |
| 33 | +// #include "SSD1306Brzo.h" |
| 34 | +// For a connection via SPI include |
| 35 | +// #include <SPI.h> // Only needed for Arduino 1.6.5 and earlier |
| 36 | +// #include "SSD1306Spi.h" |
| 37 | + |
| 38 | +// Use the corresponding display class: |
| 39 | + |
| 40 | +// Initialize the OLED display using SPI |
| 41 | +// D5 -> CLK |
| 42 | +// D7 -> MOSI (DOUT) |
| 43 | +// D0 -> RES |
| 44 | +// D2 -> DC |
| 45 | +// D8 -> CS |
| 46 | +// SSD1306Spi display(D0, D2, D8); |
| 47 | + |
| 48 | +// Initialize the OLED display using brzo_i2c |
| 49 | +// D3 -> SDA |
| 50 | +// D4 -> SCL |
| 51 | +// SSD1306Brzo display(0x3c, D3, D5); |
| 52 | + |
| 53 | +// Initialize the OLED display using Wire library |
| 54 | +SSD1306 display(0x3c, D3, D5); |
| 55 | + |
| 56 | +// Adapted from Adafruit_SSD1306 |
| 57 | +void drawLines() { |
| 58 | + for (int16_t i=0; i<DISPLAY_WIDTH; i+=4) { |
| 59 | + display.drawLine(0, 0, i, DISPLAY_HEIGHT-1); |
| 60 | + display.display(); |
| 61 | + delay(10); |
| 62 | + } |
| 63 | + for (int16_t i=0; i<DISPLAY_HEIGHT; i+=4) { |
| 64 | + display.drawLine(0, 0, DISPLAY_WIDTH-1, i); |
| 65 | + display.display(); |
| 66 | + delay(10); |
| 67 | + } |
| 68 | + delay(250); |
| 69 | + |
| 70 | + display.clear(); |
| 71 | + for (int16_t i=0; i<DISPLAY_WIDTH; i+=4) { |
| 72 | + display.drawLine(0, DISPLAY_HEIGHT-1, i, 0); |
| 73 | + display.display(); |
| 74 | + delay(10); |
| 75 | + } |
| 76 | + for (int16_t i=DISPLAY_HEIGHT-1; i>=0; i-=4) { |
| 77 | + display.drawLine(0, DISPLAY_HEIGHT-1, DISPLAY_WIDTH-1, i); |
| 78 | + display.display(); |
| 79 | + delay(10); |
| 80 | + } |
| 81 | + delay(250); |
| 82 | + |
| 83 | + display.clear(); |
| 84 | + for (int16_t i=DISPLAY_WIDTH-1; i>=0; i-=4) { |
| 85 | + display.drawLine(DISPLAY_WIDTH-1, DISPLAY_HEIGHT-1, i, 0); |
| 86 | + display.display(); |
| 87 | + delay(10); |
| 88 | + } |
| 89 | + for (int16_t i=DISPLAY_HEIGHT-1; i>=0; i-=4) { |
| 90 | + display.drawLine(DISPLAY_WIDTH-1, DISPLAY_HEIGHT-1, 0, i); |
| 91 | + display.display(); |
| 92 | + delay(10); |
| 93 | + } |
| 94 | + delay(250); |
| 95 | + display.clear(); |
| 96 | + for (int16_t i=0; i<DISPLAY_HEIGHT; i+=4) { |
| 97 | + display.drawLine(DISPLAY_WIDTH-1, 0, 0, i); |
| 98 | + display.display(); |
| 99 | + delay(10); |
| 100 | + } |
| 101 | + for (int16_t i=0; i<DISPLAY_WIDTH; i+=4) { |
| 102 | + display.drawLine(DISPLAY_WIDTH-1, 0, i, DISPLAY_HEIGHT-1); |
| 103 | + display.display(); |
| 104 | + delay(10); |
| 105 | + } |
| 106 | + delay(250); |
| 107 | +} |
| 108 | + |
| 109 | +// Adapted from Adafruit_SSD1306 |
| 110 | +void drawRect(void) { |
| 111 | + for (int16_t i=0; i<DISPLAY_HEIGHT/2; i+=2) { |
| 112 | + display.drawRect(i, i, DISPLAY_WIDTH-2*i, DISPLAY_HEIGHT-2*i); |
| 113 | + display.display(); |
| 114 | + delay(10); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// Adapted from Adafruit_SSD1306 |
| 119 | +void fillRect(void) { |
| 120 | + uint8_t color = 1; |
| 121 | + for (int16_t i=0; i<DISPLAY_HEIGHT/2; i+=3) { |
| 122 | + display.setColor((color % 2 == 0) ? BLACK : WHITE); // alternate colors |
| 123 | + display.fillRect(i, i, DISPLAY_WIDTH - i*2, DISPLAY_HEIGHT - i*2); |
| 124 | + display.display(); |
| 125 | + delay(10); |
| 126 | + color++; |
| 127 | + } |
| 128 | + // Reset back to WHITE |
| 129 | + display.setColor(WHITE); |
| 130 | +} |
| 131 | + |
| 132 | +// Adapted from Adafruit_SSD1306 |
| 133 | +void drawCircle(void) { |
| 134 | + for (int16_t i=0; i<DISPLAY_HEIGHT; i+=2) { |
| 135 | + display.drawCircle(DISPLAY_WIDTH/2, DISPLAY_HEIGHT/2, i); |
| 136 | + display.display(); |
| 137 | + delay(10); |
| 138 | + } |
| 139 | + delay(1000); |
| 140 | + display.clear(); |
| 141 | + |
| 142 | + // This will draw the part of the circel in quadrant 1 |
| 143 | + // Quadrants are numberd like this: |
| 144 | + // 0010 | 0001 |
| 145 | + // ------|----- |
| 146 | + // 0100 | 1000 |
| 147 | + // |
| 148 | + display.drawCircleQuads(DISPLAY_WIDTH/2, DISPLAY_HEIGHT/2, DISPLAY_HEIGHT/4, 0b00000001); |
| 149 | + display.display(); |
| 150 | + delay(200); |
| 151 | + display.drawCircleQuads(DISPLAY_WIDTH/2, DISPLAY_HEIGHT/2, DISPLAY_HEIGHT/4, 0b00000011); |
| 152 | + display.display(); |
| 153 | + delay(200); |
| 154 | + display.drawCircleQuads(DISPLAY_WIDTH/2, DISPLAY_HEIGHT/2, DISPLAY_HEIGHT/4, 0b00000111); |
| 155 | + display.display(); |
| 156 | + delay(200); |
| 157 | + display.drawCircleQuads(DISPLAY_WIDTH/2, DISPLAY_HEIGHT/2, DISPLAY_HEIGHT/4, 0b00001111); |
| 158 | + display.display(); |
| 159 | +} |
| 160 | + |
| 161 | +void printBuffer(void) { |
| 162 | + // Initialize the log buffer |
| 163 | + // allocate memory to store 8 lines of text and 30 chars per line. |
| 164 | + display.setLogBuffer(5, 30); |
| 165 | + |
| 166 | + // Some test data |
| 167 | + const char* test[] = { |
| 168 | + "Hello", |
| 169 | + "World" , |
| 170 | + "----", |
| 171 | + "Show off", |
| 172 | + "how", |
| 173 | + "the log buffer", |
| 174 | + "is", |
| 175 | + "working.", |
| 176 | + "Even", |
| 177 | + "scrolling is", |
| 178 | + "working" |
| 179 | + }; |
| 180 | + |
| 181 | + for (uint8_t i = 0; i < 11; i++) { |
| 182 | + display.clear(); |
| 183 | + // Print to the screen |
| 184 | + display.println(test[i]); |
| 185 | + // Draw it to the internal screen buffer |
| 186 | + display.drawLogBuffer(0, 0); |
| 187 | + // Display it on the screen |
| 188 | + display.display(); |
| 189 | + delay(500); |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +void setup() { |
| 194 | + display.init(); |
| 195 | + |
| 196 | + // display.flipScreenVertically(); |
| 197 | + |
| 198 | + display.setContrast(255); |
| 199 | + |
| 200 | + drawLines(); |
| 201 | + delay(1000); |
| 202 | + display.clear(); |
| 203 | + |
| 204 | + drawRect(); |
| 205 | + delay(1000); |
| 206 | + display.clear(); |
| 207 | + |
| 208 | + fillRect(); |
| 209 | + delay(1000); |
| 210 | + display.clear(); |
| 211 | + |
| 212 | + drawCircle(); |
| 213 | + delay(1000); |
| 214 | + display.clear(); |
| 215 | + |
| 216 | + printBuffer(); |
| 217 | + delay(1000); |
| 218 | + display.clear(); |
| 219 | +} |
| 220 | + |
| 221 | +void loop() { } |
0 commit comments