|
| 1 | + |
| 2 | +// Example-04_Text.ino |
| 3 | +// |
| 4 | +// This is a library written for SparkFun Qwiic OLED boards that use the SSD1306. |
| 5 | +// |
| 6 | +// SparkFun sells these at its website: www.sparkfun.com |
| 7 | +// |
| 8 | +// Do you like this library? Help support SparkFun. Buy a board! |
| 9 | +// |
| 10 | +// Micro OLED https://www.sparkfun.com/products/14532 |
| 11 | +// Transparent OLED https://www.sparkfun.com/products/15173 |
| 12 | +// "Narrow" OLED https://www.sparkfun.com/products/17153 |
| 13 | +// |
| 14 | +// Written by Kirk Benell @ SparkFun Electronics, March 2022 |
| 15 | +// |
| 16 | +// This library configures and draws graphics to OLED boards that use the |
| 17 | +// SSD1306 display hardware. The library only supports I2C. |
| 18 | +// |
| 19 | +// Repository: |
| 20 | +// https://github.com/sparkfun/SparkFun_Qwiic_OLED_Arduino_Library |
| 21 | +// |
| 22 | +// Documentation: |
| 23 | +// https://sparkfun.github.io/SparkFun_Qwiic_OLED_Arduino_Library/ |
| 24 | +// |
| 25 | +// |
| 26 | +// SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT). |
| 27 | +// |
| 28 | +// SPDX-License-Identifier: MIT |
| 29 | +// |
| 30 | +// The MIT License (MIT) |
| 31 | +// |
| 32 | +// Copyright (c) 2022 SparkFun Electronics |
| 33 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and |
| 34 | +// associated documentation files (the "Software"), to deal in the Software without restriction, |
| 35 | +// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 36 | +// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to |
| 37 | +// do so, subject to the following conditions: |
| 38 | +// The above copyright notice and this permission notice shall be included in all copies or substantial |
| 39 | +// portions of the Software. |
| 40 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT |
| 41 | +// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 42 | +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 43 | +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 44 | +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 45 | +// |
| 46 | +////////////////////////////////////////////////////////////////////////////////////////// |
| 47 | +// Example 4 for the SparkFun Qwiic OLED Arduino Library |
| 48 | +// |
| 49 | +// >> Overview << |
| 50 | +// |
| 51 | +// This demo shows writing text to an OLED screen using various fonts |
| 52 | +// |
| 53 | +////////////////////////////////////////////////////////////////////////////////////////// |
| 54 | +// |
| 55 | +// >>> SELECT THE CONNECTED DEVICE FOR THIS EXAMPLE <<< |
| 56 | +// |
| 57 | +// The Library supports three different types of SparkFun boards. The demo uses the following |
| 58 | +// defines to determine which device is being used. Uncomment the device being used for this demo. |
| 59 | +// |
| 60 | +// The default is Micro OLED |
| 61 | + |
| 62 | +#define MICRO |
| 63 | +//#define NARROW |
| 64 | +//#define TRANSPARENT |
| 65 | + |
| 66 | +////////////////////////////////////////////////////////////////////////////////////////// |
| 67 | + |
| 68 | +#include <stdint.h> |
| 69 | + |
| 70 | +// Include the SparkFun qwiic OLED Library |
| 71 | +#include <SparkFun_Qwiic_OLED.h> |
| 72 | + |
| 73 | +// Fonts |
| 74 | +#include <res/qw_fnt_5x7.h> |
| 75 | +#include <res/qw_fnt_8x16.h> |
| 76 | +#include <res/qw_fnt_31x48.h> |
| 77 | +#include <res/qw_fnt_7segment.h> |
| 78 | +#include <res/qw_fnt_largenum.h> |
| 79 | + |
| 80 | +// What device is being used in this demo |
| 81 | + |
| 82 | +#if defined(TRANSPARENT) |
| 83 | +QwiicTransparentOLED myOLED; |
| 84 | +const char * deviceName = "Transparent OLED"; |
| 85 | + |
| 86 | +#elif defined(NARROW) |
| 87 | +QwiicNarrowOLED myOLED; |
| 88 | +const char * deviceName = "Narrow OLED"; |
| 89 | + |
| 90 | +#else |
| 91 | +QwiicMicroOLED myOLED; |
| 92 | +const char * deviceName = "Micro OLED"; |
| 93 | + |
| 94 | +#endif |
| 95 | + |
| 96 | +// An array of fonts to loop over |
| 97 | +QwiicFont * demoFonts[] = { |
| 98 | + &QW_FONT_5X7, |
| 99 | + &QW_FONT_8X16, |
| 100 | + &QW_FONT_31X48, |
| 101 | + &QW_FONT_LARGENUM, |
| 102 | + &QW_FONT_7SEGMENT |
| 103 | +}; |
| 104 | +uint8_t nFONTS = sizeof(demoFonts)/sizeof(demoFonts[0]); |
| 105 | +uint8_t iFont = 0; |
| 106 | + |
| 107 | +// Some vars for the title. |
| 108 | +uint8_t xTitle, yTitle; |
| 109 | +String strTitle = "<<Font>>"; |
| 110 | +QwiicFont * pFntTitle = &QW_FONT_5X7; |
| 111 | + |
| 112 | +//////////////////////////////////////////////////////////////////////////////////// |
| 113 | +// |
| 114 | +void setup() |
| 115 | +{ |
| 116 | + |
| 117 | + delay(500); //Give display time to power on |
| 118 | + Serial.begin(115200); |
| 119 | + |
| 120 | + Serial.println("\n\r-----------------------------------"); |
| 121 | + Serial.print("Example #4 on: "); |
| 122 | + Serial.println(String(deviceName)); |
| 123 | + |
| 124 | + if(!myOLED.begin()){ |
| 125 | + |
| 126 | + Serial.println("- Device Begin Failed"); |
| 127 | + while(1); |
| 128 | + } |
| 129 | + |
| 130 | + Serial.println("- Begin Successful"); |
| 131 | + |
| 132 | + // Position to use for the time/banner displayed before each font |
| 133 | + |
| 134 | + // starting x position - width minus string length (font width * number of characters) / 2 |
| 135 | + xTitle = (myOLED.getWidth() - (pFntTitle->width +1)* strTitle.length())/2; |
| 136 | + |
| 137 | + yTitle = (myOLED.getHeight() - pFntTitle->height)/2; |
| 138 | + |
| 139 | +} |
| 140 | + |
| 141 | +//////////////////////////////////////////////////////////////////////////////////// |
| 142 | +// writeFontChars() |
| 143 | +// |
| 144 | +// For the current font, write out all it's characters |
| 145 | + |
| 146 | +void writeFontChars(){ |
| 147 | + |
| 148 | + // get the font |
| 149 | + QwiicFont *pFont = myOLED.getFont(); |
| 150 | + |
| 151 | + // how many chars can a screen handle? (x * y) |
| 152 | + uint16_t screenChars = myOLED.getWidth()/(pFont->width+1); // X |
| 153 | + uint8_t nY = myOLED.getHeight()/pFont->height; // Y |
| 154 | + |
| 155 | + screenChars *= (nY == 0 ? 1 : nY); // need at least 1 row |
| 156 | + |
| 157 | + // Loop over the characters in the font. |
| 158 | + for(int i=0; i < pFont->n_chars; i++){ |
| 159 | + |
| 160 | + if( i % screenChars == 0){ // next page |
| 161 | + delay(400); |
| 162 | + myOLED.erase(); |
| 163 | + myOLED.setCursor(0,0); |
| 164 | + } |
| 165 | + |
| 166 | + // if the character is a carriage return, send a blank - otherwise the |
| 167 | + // write routine will perform a CR and lead to a confusing display. |
| 168 | + myOLED.write( (i + pFont->start != '\n') ? i+pFont->start : ' '); |
| 169 | + |
| 170 | + myOLED.display(); // show the added char |
| 171 | + |
| 172 | + delay(10); |
| 173 | + } |
| 174 | + |
| 175 | +} |
| 176 | +//////////////////////////////////////////////////////////////////////////////////// |
| 177 | +// write_title() |
| 178 | +// |
| 179 | +// Simple title for a font |
| 180 | + |
| 181 | +void write_title(){ |
| 182 | + |
| 183 | + // Set title font font |
| 184 | + myOLED.setFont(pFntTitle); |
| 185 | + |
| 186 | + myOLED.erase(); |
| 187 | + |
| 188 | + // Draw the text |
| 189 | + myOLED.text(xTitle, yTitle, strTitle); |
| 190 | + |
| 191 | + // There's nothing on the screen yet - Now send the graphics to the device |
| 192 | + myOLED.display(); |
| 193 | + |
| 194 | +} |
| 195 | + |
| 196 | +//////////////////////////////////////////////////////////////////////////////////// |
| 197 | +// loop() |
| 198 | + |
| 199 | +void loop() |
| 200 | +{ |
| 201 | + |
| 202 | + // Write out a title |
| 203 | + write_title(); |
| 204 | + |
| 205 | + delay(1000); |
| 206 | + |
| 207 | + // next font for display |
| 208 | + iFont = (iFont + 1) % nFONTS; |
| 209 | + myOLED.setFont(demoFonts[iFont]); |
| 210 | + |
| 211 | + // Write out the full font char set |
| 212 | + writeFontChars(); |
| 213 | + |
| 214 | + delay(2000); |
| 215 | + |
| 216 | +} |
0 commit comments