Skip to content

Commit 21a19cc

Browse files
committed
Add drawIcon() and example
1 parent 1c3544a commit 21a19cc

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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. This is helpful
7+
when you need a icon (GPS, battery, etc) on a certain part of the screen.
8+
9+
This example assumes an I2C connection (Qwiic)
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,
30+
0x01,
31+
0xC1,
32+
0x41,
33+
0x41,
34+
0x41,
35+
0x71,
36+
0x11,
37+
0x11,
38+
0x11,
39+
0x11,
40+
0x11,
41+
0x71,
42+
0x41,
43+
0x41,
44+
0xC1,
45+
0x81,
46+
0x01,
47+
0xFF,
48+
0xFF,
49+
0x80,
50+
0x83,
51+
0x82,
52+
0x86,
53+
0x8F,
54+
0x8F,
55+
0x86,
56+
0x82,
57+
0x82,
58+
0x82,
59+
0x86,
60+
0x8F,
61+
0x8F,
62+
0x86,
63+
0x83,
64+
0x81,
65+
0x80,
66+
0xFF,
67+
};
68+
69+
int iconHeight = 17;
70+
int iconWidth = 20;
71+
72+
bool displayDetected = false;
73+
74+
void setup()
75+
{
76+
Wire.begin();
77+
Wire.setClock(400000);
78+
79+
Serial.begin(115200);
80+
delay(100);
81+
Serial.println("Display Icon OLED example");
82+
83+
//0x3D is default address on Qwiic board
84+
if (isConnected(0x3D) == true || isConnected(0x3C) == true)
85+
{
86+
Serial.println("Display detected");
87+
displayDetected = true;
88+
}
89+
else
90+
{
91+
Serial.println("No display detected. Freezing...");
92+
while (1)
93+
;
94+
}
95+
96+
if (displayDetected)
97+
{
98+
oled.begin(); // Initialize the OLED
99+
oled.clear(PAGE); // Clear the display's internal memory
100+
oled.clear(ALL); // Clear the library's display buffer
101+
oled.display(); // Display what's in the buffer (splashscreen)
102+
}
103+
}
104+
105+
int iconX = 0;
106+
int iconXChangeAmount = 1;
107+
int iconY = 0;
108+
int iconYChangeAmount = 1;
109+
110+
void loop()
111+
{
112+
if (displayDetected)
113+
{
114+
oled.drawIcon(iconX, iconY, iconWidth, iconHeight, truck, sizeof(truck), true);
115+
oled.display();
116+
117+
//Move the icon
118+
iconX += iconXChangeAmount;
119+
iconY += iconYChangeAmount;
120+
121+
if (iconX + iconWidth >= 64)
122+
iconXChangeAmount *= -1; //Change direction
123+
if (iconX == 0)
124+
iconXChangeAmount *= -1; //Change direction
125+
126+
if (iconY + iconHeight >= 48)
127+
iconYChangeAmount *= -1; //Change direction
128+
if (iconY == 0)
129+
iconYChangeAmount *= -1; //Change direction
130+
}
131+
}
132+
133+
bool isConnected(uint8_t deviceAddress)
134+
{
135+
Wire.beginTransmission(deviceAddress);
136+
if (Wire.endTransmission() == 0)
137+
return true;
138+
return false;
139+
}
1014 Bytes
Binary file not shown.

src/SFE_MicroOLED.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,3 +1047,50 @@ void MicroOLED::drawBitmap(uint8_t *bitArray)
10471047
for (int i = 0; i < (LCDWIDTH * LCDHEIGHT / 8); i++)
10481048
screenmemory[i] = bitArray[i];
10491049
}
1050+
1051+
//Draw an icon at a given spot
1052+
//Use http://en.radzio.dxp.pl/bitmap_converter/ to generate output
1053+
//Make sure the bitmap is n*8 pixels tall (pad white pixels to lower area as needed)
1054+
//Otherwise the bitmap bitmap_converter will compress some of the bytes together
1055+
void MicroOLED::drawIcon(uint8_t offsetX, uint8_t offsetY, uint8_t iconWidth, uint8_t iconHeight, uint8_t *bitArray, uint8_t arraySizeInBytes, bool overwrite)
1056+
{
1057+
uint8_t columnNumber = offsetX;
1058+
uint8_t rowNumber = offsetY / 8;
1059+
1060+
uint8_t bitOffset = offsetY % 8;
1061+
1062+
for (uint8_t i = 0; i < arraySizeInBytes; i++)
1063+
{
1064+
uint16_t byteNumber = rowNumber * 64 + columnNumber;
1065+
1066+
//If we have an offset, then straddle the bytes between rows
1067+
if (bitOffset)
1068+
{
1069+
//Zero out the bits before entering new data
1070+
if (overwrite == true)
1071+
{
1072+
screenmemory[byteNumber] &= ~(0xFF << bitOffset);
1073+
screenmemory[byteNumber + 64] &= ~(0xFF >> (8 - bitOffset));
1074+
}
1075+
1076+
//Write in new data across two rows
1077+
screenmemory[byteNumber] |= bitArray[i] << bitOffset;
1078+
screenmemory[byteNumber + 64] |= bitArray[i] >> (8 - bitOffset);
1079+
}
1080+
else //No shift needed
1081+
{
1082+
//Regular clear
1083+
if (overwrite == true)
1084+
screenmemory[byteNumber] = 0;
1085+
screenmemory[byteNumber] |= bitArray[i];
1086+
}
1087+
1088+
columnNumber++;
1089+
if (columnNumber == offsetX + iconWidth - 1)
1090+
{
1091+
//Wrap to next row
1092+
rowNumber++;
1093+
columnNumber = offsetX;
1094+
}
1095+
}
1096+
}

src/SFE_MicroOLED.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ class MicroOLED : public Print
177177
void drawChar(uint8_t x, uint8_t y, uint8_t c);
178178
void drawChar(uint8_t x, uint8_t y, uint8_t c, uint8_t color, uint8_t mode);
179179
void drawBitmap(uint8_t *bitArray);
180+
void drawIcon(uint8_t locationX, uint8_t locationY, uint8_t iconWidth, uint8_t iconHeight, uint8_t *bitArray, uint8_t arraySizeInBytes, bool overwrite = false);
180181
uint8_t getLCDWidth(void);
181182
uint8_t getLCDHeight(void);
182183
void setColor(uint8_t color);

0 commit comments

Comments
 (0)