Skip to content

Commit def2008

Browse files
committed
Re-sort examples. Make primary examples I2C with SPI sub folder.
1 parent 21a19cc commit def2008

File tree

13 files changed

+604
-176
lines changed

13 files changed

+604
-176
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
MicroOLED_Hello.ino
3+
SFE_MicroOLED Hello World Demo
4+
Jim Lindblom @ SparkFun Electronics
5+
Original Creation Date: October 26, 2014
6+
7+
This sketch lights up a familiar pattern on the MicroOLED
8+
Breakout. It's a great way to prove you've connected everything
9+
correctly and that your display is in working order.
10+
11+
Hardware Connections:
12+
This example assumes you are using Qwiic. See the SPI examples for
13+
a detailed breakdown of connection info.
14+
15+
Note: The display on the MicroOLED is a 1.8V-3.3V device only.
16+
Don't try to connect a 5V Arduino directly to it! Use level
17+
shifters in between the data signals if you have to resort to
18+
a 5V board.
19+
20+
This code is beerware; if you see me (or any other SparkFun
21+
employee) at the local, and you've found our code helpful,
22+
please buy us a round!
23+
24+
Distributed as-is; no warranty is given.
25+
*/
26+
#include <Wire.h>
27+
#include <SFE_MicroOLED.h> //Click here to get the library: http://librarymanager/All#SparkFun_Micro_OLED
28+
29+
#define DC_JUMPER 1
30+
#define PIN_RESET 9 // Optional - Connect RST on display to pin 9 on Arduino
31+
32+
MicroOLED oled(PIN_RESET, DC_JUMPER); //Example I2C declaration
33+
34+
void setup()
35+
{
36+
delay(100); //Give display time to power on
37+
Wire.begin(); //Setup I2C bus
38+
39+
// Before you can start using the OLED, call begin() to init
40+
// all of the pins and configure the OLED.
41+
oled.begin();
42+
// clear(ALL) will clear out the OLED's graphic memory.
43+
// clear(PAGE) will clear the Arduino's display buffer.
44+
oled.clear(ALL); // Clear the display's memory (gets rid of artifacts)
45+
// To actually draw anything on the display, you must call the
46+
// display() function.
47+
oled.display();
48+
}
49+
50+
void loop()
51+
{
52+
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
MicroOLED_Clock.ino
3+
Analog Clock demo using SFE_MicroOLED Library
4+
Jim Lindblom @ SparkFun Electronics
5+
Original Creation Date: October 27, 2014
6+
7+
This sketch uses the MicroOLED library to draw an
8+
analog clock.
9+
10+
Hardware Connections:
11+
This example assumes you are using Qwiic. See the SPI examples for
12+
a detailed breakdown of connection info.
13+
14+
This code is beerware; if you see me (or any other SparkFun employee) at the
15+
local, and you've found our code helpful, please buy us a round!
16+
17+
Distributed as-is; no warranty is given.
18+
*/
19+
20+
#include <Wire.h>
21+
#include <SFE_MicroOLED.h> //Click here to get the library: http://librarymanager/All#SparkFun_Micro_OLED
22+
23+
#define DC_JUMPER 1
24+
#define PIN_RESET 9 // Optional - Connect RST on display to pin 9 on Arduino
25+
26+
MicroOLED oled(PIN_RESET, DC_JUMPER); //Example I2C declaration
27+
28+
// Use these variables to set the initial time
29+
int hours = 11;
30+
int minutes = 50;
31+
int seconds = 30;
32+
33+
// How fast do you want the clock to spin? Set this to 1 for fun.
34+
// Set this to 1000 to get _about_ 1 second timing.
35+
const int CLOCK_SPEED = 1000;
36+
37+
// Global variables to help draw the clock face:
38+
const int MIDDLE_Y = oled.getLCDHeight() / 2;
39+
const int MIDDLE_X = oled.getLCDWidth() / 2;
40+
41+
int CLOCK_RADIUS;
42+
int POS_12_X, POS_12_Y;
43+
int POS_3_X, POS_3_Y;
44+
int POS_6_X, POS_6_Y;
45+
int POS_9_X, POS_9_Y;
46+
int S_LENGTH;
47+
int M_LENGTH;
48+
int H_LENGTH;
49+
50+
unsigned long lastDraw = 0;
51+
52+
void setup()
53+
{
54+
delay(100); //Give display time to power on
55+
56+
Wire.begin(); //Set up I2C bus
57+
58+
oled.begin(); // Initialize the OLED
59+
oled.clear(PAGE); // Clear the display's internal memory
60+
oled.clear(ALL); // Clear the library's display buffer
61+
oled.display(); // Display what's in the buffer (splashscreen)
62+
63+
initClockVariables();
64+
65+
oled.clear(ALL);
66+
drawFace();
67+
drawArms(hours, minutes, seconds);
68+
oled.display(); // display the memory buffer drawn
69+
}
70+
71+
void loop()
72+
{
73+
// Check if we need to update seconds, minutes, hours:
74+
if (lastDraw + CLOCK_SPEED < millis())
75+
{
76+
lastDraw = millis();
77+
// Add a second, update minutes/hours if necessary:
78+
updateTime();
79+
80+
// Draw the clock:
81+
oled.clear(PAGE); // Clear the buffer
82+
drawFace(); // Draw the face to the buffer
83+
drawArms(hours, minutes, seconds); // Draw arms to the buffer
84+
oled.display(); // Draw the memory buffer
85+
}
86+
}
87+
88+
void initClockVariables()
89+
{
90+
// Calculate constants for clock face component positions:
91+
oled.setFontType(0);
92+
CLOCK_RADIUS = min(MIDDLE_X, MIDDLE_Y) - 1;
93+
POS_12_X = MIDDLE_X - oled.getFontWidth();
94+
POS_12_Y = MIDDLE_Y - CLOCK_RADIUS + 2;
95+
POS_3_X = MIDDLE_X + CLOCK_RADIUS - oled.getFontWidth() - 1;
96+
POS_3_Y = MIDDLE_Y - oled.getFontHeight() / 2;
97+
POS_6_X = MIDDLE_X - oled.getFontWidth() / 2;
98+
POS_6_Y = MIDDLE_Y + CLOCK_RADIUS - oled.getFontHeight() - 1;
99+
POS_9_X = MIDDLE_X - CLOCK_RADIUS + oled.getFontWidth() - 2;
100+
POS_9_Y = MIDDLE_Y - oled.getFontHeight() / 2;
101+
102+
// Calculate clock arm lengths
103+
S_LENGTH = CLOCK_RADIUS - 2;
104+
M_LENGTH = S_LENGTH * 0.7;
105+
H_LENGTH = S_LENGTH * 0.5;
106+
}
107+
108+
// Simple function to increment seconds and then increment minutes
109+
// and hours if necessary.
110+
void updateTime()
111+
{
112+
seconds++; // Increment seconds
113+
if (seconds >= 60) // If seconds overflows (>=60)
114+
{
115+
seconds = 0; // Set seconds back to 0
116+
minutes++; // Increment minutes
117+
if (minutes >= 60) // If minutes overflows (>=60)
118+
{
119+
minutes = 0; // Set minutes back to 0
120+
hours++; // Increment hours
121+
if (hours >= 12) // If hours overflows (>=12)
122+
{
123+
hours = 0; // Set hours back to 0
124+
}
125+
}
126+
}
127+
}
128+
129+
// Draw the clock's three arms: seconds, minutes, hours.
130+
void drawArms(int h, int m, int s)
131+
{
132+
double midHours; // this will be used to slightly adjust the hour hand
133+
static int hx, hy, mx, my, sx, sy;
134+
135+
// Adjust time to shift display 90 degrees ccw
136+
// this will turn the clock the same direction as text:
137+
h -= 3;
138+
m -= 15;
139+
s -= 15;
140+
if (h <= 0)
141+
h += 12;
142+
if (m < 0)
143+
m += 60;
144+
if (s < 0)
145+
s += 60;
146+
147+
// Calculate and draw new lines:
148+
s = map(s, 0, 60, 0, 360); // map the 0-60, to "360 degrees"
149+
sx = S_LENGTH * cos(PI * ((float)s) / 180); // woo trig!
150+
sy = S_LENGTH * sin(PI * ((float)s) / 180); // woo trig!
151+
// draw the second hand:
152+
oled.line(MIDDLE_X, MIDDLE_Y, MIDDLE_X + sx, MIDDLE_Y + sy);
153+
154+
m = map(m, 0, 60, 0, 360); // map the 0-60, to "360 degrees"
155+
mx = M_LENGTH * cos(PI * ((float)m) / 180); // woo trig!
156+
my = M_LENGTH * sin(PI * ((float)m) / 180); // woo trig!
157+
// draw the minute hand
158+
oled.line(MIDDLE_X, MIDDLE_Y, MIDDLE_X + mx, MIDDLE_Y + my);
159+
160+
midHours = minutes / 12; // midHours is used to set the hours hand to middling levels between whole hours
161+
h *= 5; // Get hours and midhours to the same scale
162+
h += midHours; // add hours and midhours
163+
h = map(h, 0, 60, 0, 360); // map the 0-60, to "360 degrees"
164+
hx = H_LENGTH * cos(PI * ((float)h) / 180); // woo trig!
165+
hy = H_LENGTH * sin(PI * ((float)h) / 180); // woo trig!
166+
// draw the hour hand:
167+
oled.line(MIDDLE_X, MIDDLE_Y, MIDDLE_X + hx, MIDDLE_Y + hy);
168+
}
169+
170+
// Draw an analog clock face
171+
void drawFace()
172+
{
173+
// Draw the clock border
174+
oled.circle(MIDDLE_X, MIDDLE_Y, CLOCK_RADIUS);
175+
176+
// Draw the clock numbers
177+
oled.setFontType(0); // set font type 0, please see declaration in SFE_MicroOLED.cpp
178+
oled.setCursor(POS_12_X, POS_12_Y); // points cursor to x=27 y=0
179+
oled.print(12);
180+
oled.setCursor(POS_6_X, POS_6_Y);
181+
oled.print(6);
182+
oled.setCursor(POS_9_X, POS_9_Y);
183+
oled.print(9);
184+
oled.setCursor(POS_3_X, POS_3_Y);
185+
oled.print(3);
186+
}

0 commit comments

Comments
 (0)