|
| 1 | +/* |
| 2 | + Micro OLED Display Icon |
| 3 | + Nathan Seidle @ SparkFun Electronics |
| 4 | + Original Creation Date: November 15, 2020 |
| 5 | + |
| 6 | + Draw a variable sized icon anywhere on the display |
| 7 | +
|
| 8 | + This is helpful when you need a icon (GPS, battery, etc) |
| 9 | + on a certain part of the screen. |
| 10 | + |
| 11 | + This code is beerware; if you see me (or any other SparkFun |
| 12 | + employee) at the local, and you've found our code helpful, |
| 13 | + please buy us a round! |
| 14 | + |
| 15 | + Distributed as-is; no warranty is given. |
| 16 | +*/ |
| 17 | +#include <Wire.h> |
| 18 | +#include <SFE_MicroOLED.h> //Click here to get the library: http://librarymanager/All#SparkFun_Micro_OLED |
| 19 | + |
| 20 | +#define PIN_RESET 9 |
| 21 | +#define DC_JUMPER 1 // Set to either 0 (SPI, default) or 1 (I2C) based on jumper, matching the value of the DC Jumper |
| 22 | +MicroOLED oled(PIN_RESET, DC_JUMPER); |
| 23 | + |
| 24 | +//A 20 x 17 pixel image of a truck in a box |
| 25 | +//Use http://en.radzio.dxp.pl/bitmap_converter/ to generate output |
| 26 | +//Make sure the bitmap is n*8 pixels tall (pad white pixels to lower area as needed) |
| 27 | +//Otherwise the bitmap bitmap_converter will compress some of the bytes together |
| 28 | +uint8_t truck [] = { |
| 29 | + 0xFF, 0x01, 0xC1, 0x41, 0x41, 0x41, 0x71, 0x11, 0x11, 0x11, 0x11, 0x11, 0x71, 0x41, 0x41, 0xC1, |
| 30 | + 0x81, 0x01, 0xFF, 0xFF, 0x80, 0x83, 0x82, 0x86, 0x8F, 0x8F, 0x86, 0x82, 0x82, 0x82, 0x86, 0x8F, |
| 31 | + 0x8F, 0x86, 0x83, 0x81, 0x80, 0xFF, |
| 32 | +}; |
| 33 | + |
| 34 | +int iconHeight = 17; |
| 35 | +int iconWidth = 20; |
| 36 | + |
| 37 | +bool displayDetected = false; |
| 38 | + |
| 39 | +void setup() |
| 40 | +{ |
| 41 | + Wire.begin(); |
| 42 | + Wire.setClock(2000000); |
| 43 | + |
| 44 | + Serial.begin(115200); |
| 45 | + delay(100); |
| 46 | + Serial.println("Display Icon OLED example"); |
| 47 | + |
| 48 | + //0x3D is default on Qwiic board |
| 49 | + if (isConnected(0x3D) == true || isConnected(0x3C) == true) |
| 50 | + { |
| 51 | + Serial.println("Display detected"); |
| 52 | + displayDetected = true; |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + Serial.println("No display detected. Freezing..."); |
| 57 | + while(1); |
| 58 | + } |
| 59 | + |
| 60 | + if (displayDetected) |
| 61 | + { |
| 62 | + oled.begin(); // Initialize the OLED |
| 63 | + oled.clear(PAGE); // Clear the display's internal memory |
| 64 | + oled.clear(ALL); // Clear the library's display buffer |
| 65 | + oled.display(); // Display what's in the buffer (splashscreen) |
| 66 | + |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +int iconX = 0; |
| 71 | +int iconXChangeAmount = 1; |
| 72 | +int iconY = 0; |
| 73 | +int iconYChangeAmount = 1; |
| 74 | + |
| 75 | +void loop() |
| 76 | +{ |
| 77 | + if (displayDetected) |
| 78 | + { |
| 79 | + oled.drawIcon(iconX, iconY, iconWidth, iconHeight, truck, sizeof(truck), true); |
| 80 | + oled.display(); |
| 81 | + |
| 82 | + //Move the icon |
| 83 | + iconX += iconXChangeAmount; |
| 84 | + iconY += iconYChangeAmount; |
| 85 | + |
| 86 | + if (iconX + iconWidth >= 64) |
| 87 | + iconXChangeAmount *= -1; //Change direction |
| 88 | + if (iconX == 0) |
| 89 | + iconXChangeAmount *= -1; //Change direction |
| 90 | + |
| 91 | + if (iconY + iconHeight >= 48) |
| 92 | + iconYChangeAmount *= -1; //Change direction |
| 93 | + if (iconY == 0) |
| 94 | + iconYChangeAmount *= -1; //Change direction |
| 95 | + } |
| 96 | +} |
0 commit comments