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+ }
0 commit comments