Skip to content

Commit 758b08d

Browse files
author
Kirk
committed
rename of clock demo, ported to new lib
1 parent e1ae1a8 commit 758b08d

File tree

1 file changed

+270
-0
lines changed

1 file changed

+270
-0
lines changed
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
2+
3+
// Example-06_Clock.ino
4+
//
5+
// This is a library written for SparkFun Qwiic OLED boards that use the SSD1306.
6+
//
7+
// SparkFun sells these at its website: www.sparkfun.com
8+
//
9+
// Do you like this library? Help support SparkFun. Buy a board!
10+
//
11+
// Micro OLED https://www.sparkfun.com/products/14532
12+
// Transparent OLED https://www.sparkfun.com/products/15173
13+
// "Narrow" OLED https://www.sparkfun.com/products/17153
14+
//
15+
//
16+
// Written by
17+
// Jim Lindblom @ SparkFun Electronics
18+
// Original Creation Date: October 27, 2014
19+
//
20+
// This library configures and draws graphics to OLED boards that use the
21+
// SSD1306 display hardware. The library only supports I2C.
22+
//
23+
// Repository:
24+
// https://github.com/sparkfun/SparkFun_Qwiic_OLED_Arduino_Library
25+
//
26+
// Documentation:
27+
// https://sparkfun.github.io/SparkFun_Qwiic_OLED_Arduino_Library/
28+
//
29+
//
30+
// SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).
31+
//
32+
// SPDX-License-Identifier: MIT
33+
//
34+
// The MIT License (MIT)
35+
//
36+
// Copyright (c) 2022 SparkFun Electronics
37+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
38+
// associated documentation files (the "Software"), to deal in the Software without restriction,
39+
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
40+
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to
41+
// do so, subject to the following conditions:
42+
// The above copyright notice and this permission notice shall be included in all copies or substantial
43+
// portions of the Software.
44+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
45+
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
46+
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
47+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
49+
50+
// Example 06 for the SparkFun Qwiic OLED Arduino Library
51+
//
52+
// >> Overview <<
53+
//
54+
// Draws a clock face on the OLED display. This is a port of the demo for the original Micro OLED library.
55+
//
56+
//////////////////////////////////////////////////////////////////////////////////////////
57+
// >>> SELECT THE CONNECTED DEVICE FOR THIS EXAMPLE <<<
58+
//
59+
// The Library supports three different types of SparkFun boards. The demo uses the following
60+
// defines to determine which device is being used. Uncomment the device being used for this demo.
61+
//
62+
// The default is Micro OLED
63+
64+
#define MICRO
65+
//#define NARROW
66+
//#define TRANSPARENT
67+
68+
//////////////////////////////////////////////////////////////////////////////////////////
69+
70+
#include <stdint.h>
71+
72+
// Include the SparkFun qwiic OLED Library
73+
#include <SparkFun_Qwiic_OLED.h>
74+
75+
// What device is being used in this demo
76+
77+
#if defined(TRANSPARENT)
78+
QwiicTransparentOLED myOLED;
79+
const char * deviceName = "Transparent OLED";
80+
81+
#elif defined(NARROW)
82+
QwiicNarrowOLED myOLED;
83+
const char * deviceName = "Narrow OLED";
84+
85+
#else
86+
QwiicMicroOLED myOLED;
87+
const char * deviceName = "Micro OLED";
88+
89+
#endif
90+
91+
// Use these variables to set the initial time
92+
int hours = 11;
93+
int minutes = 50;
94+
int seconds = 30;
95+
96+
// How fast do you want the clock to spin? Set this to 1 for fun.
97+
// Set this to 1000 to get _about_ 1 second timing.
98+
const int CLOCK_SPEED = 1000;
99+
100+
// Global variables to help draw the clock face:
101+
const int MIDDLE_Y = myOLED.getHeight() / 2;
102+
const int MIDDLE_X = myOLED.getWidth() / 2;
103+
104+
int CLOCK_RADIUS;
105+
int POS_12_X, POS_12_Y;
106+
int POS_3_X, POS_3_Y;
107+
int POS_6_X, POS_6_Y;
108+
int POS_9_X, POS_9_Y;
109+
int S_LENGTH;
110+
int M_LENGTH;
111+
int H_LENGTH;
112+
113+
unsigned long lastDraw = 0;
114+
115+
QwiicFont *pFont;
116+
117+
void setup()
118+
{
119+
delay(500); //Give display time to power on
120+
121+
// Serial on!
122+
Serial.begin(115200);
123+
124+
Serial.println("\n\r-----------------------------------");
125+
126+
Serial.print("Running Example 01 on: ");
127+
Serial.println(String(deviceName));
128+
129+
// Initalize the OLED device and related graphics system
130+
if(!myOLED.begin()){
131+
132+
Serial.println(" - Device Begin Failed");
133+
while(1);
134+
}
135+
136+
Serial.println("- Begin Success");
137+
138+
pFont = myOLED.getFont();
139+
140+
initClockVariables();
141+
142+
drawFace();
143+
drawArms(hours, minutes, seconds);
144+
myOLED.display(); // display the memory buffer drawn
145+
}
146+
147+
void loop()
148+
{
149+
// Check if we need to update seconds, minutes, hours:
150+
if (lastDraw + CLOCK_SPEED < millis()){
151+
152+
lastDraw = millis();
153+
// Add a second, update minutes/hours if necessary:
154+
updateTime();
155+
156+
// Draw the clock:
157+
myOLED.erase();
158+
159+
drawFace(); // Draw the face to the buffer
160+
drawArms(hours, minutes, seconds); // Draw arms to the buffer
161+
162+
myOLED.display(); // Draw the memory buffer
163+
}
164+
}
165+
166+
void initClockVariables()
167+
{
168+
// Calculate constants for clock face component positions:
169+
170+
CLOCK_RADIUS = min(MIDDLE_X, MIDDLE_Y) - 1;
171+
POS_12_X = MIDDLE_X - pFont->width;
172+
POS_12_Y = MIDDLE_Y - CLOCK_RADIUS + 2;
173+
POS_3_X = MIDDLE_X + CLOCK_RADIUS - pFont->width - 1;
174+
POS_3_Y = MIDDLE_Y - pFont->height / 2;
175+
POS_6_X = MIDDLE_X - pFont->width / 2;
176+
POS_6_Y = MIDDLE_Y + CLOCK_RADIUS - pFont->height - 1;
177+
POS_9_X = MIDDLE_X - CLOCK_RADIUS + pFont->width - 2;
178+
POS_9_Y = MIDDLE_Y - pFont->height / 2;
179+
180+
// Calculate clock arm lengths
181+
S_LENGTH = CLOCK_RADIUS - 2;
182+
M_LENGTH = S_LENGTH * 0.7;
183+
H_LENGTH = S_LENGTH * 0.5;
184+
}
185+
186+
// Simple function to increment seconds and then increment minutes
187+
// and hours if necessary.
188+
void updateTime()
189+
{
190+
seconds++; // Increment seconds
191+
if (seconds >= 60){ // If seconds overflows (>=60)
192+
193+
seconds = 0; // Set seconds back to 0
194+
minutes++; // Increment minutes
195+
196+
if(minutes >= 60){ // If minutes overflows (>=60)
197+
198+
minutes = 0; // Set minutes back to 0
199+
hours++; // Increment hours
200+
201+
if(hours >= 12) // If hours overflows (>=12)
202+
hours = 0; // Set hours back to 0
203+
}
204+
}
205+
}
206+
207+
// Draw the clock's three arms: seconds, minutes, hours.
208+
void drawArms(int h, int m, int s)
209+
{
210+
double midHours; // this will be used to slightly adjust the hour hand
211+
static int hx, hy, mx, my, sx, sy;
212+
213+
// Adjust time to shift display 90 degrees ccw
214+
// this will turn the clock the same direction as text:
215+
h -= 3;
216+
m -= 15;
217+
s -= 15;
218+
219+
if(h <= 0)
220+
h += 12;
221+
222+
if(m < 0)
223+
m += 60;
224+
225+
if(s < 0)
226+
s += 60;
227+
228+
// Calculate and draw new lines:
229+
s = map(s, 0, 60, 0, 360); // map the 0-60, to "360 degrees"
230+
sx = S_LENGTH * cos(PI * ((float)s) / 180); // woo trig!
231+
sy = S_LENGTH * sin(PI * ((float)s) / 180); // woo trig!
232+
233+
// draw the second hand:
234+
myOLED.line(MIDDLE_X, MIDDLE_Y, MIDDLE_X + sx, MIDDLE_Y + sy);
235+
236+
m = map(m, 0, 60, 0, 360); // map the 0-60, to "360 degrees"
237+
mx = M_LENGTH * cos(PI * ((float)m) / 180); // woo trig!
238+
my = M_LENGTH * sin(PI * ((float)m) / 180); // woo trig!
239+
240+
// draw the minute hand
241+
myOLED.line(MIDDLE_X, MIDDLE_Y, MIDDLE_X + mx, MIDDLE_Y + my);
242+
243+
midHours = minutes / 12; // midHours is used to set the hours hand to middling levels between whole hours
244+
h *= 5; // Get hours and midhours to the same scale
245+
h += midHours; // add hours and midhours
246+
h = map(h, 0, 60, 0, 360); // map the 0-60, to "360 degrees"
247+
hx = H_LENGTH * cos(PI * ((float)h) / 180); // woo trig!
248+
hy = H_LENGTH * sin(PI * ((float)h) / 180); // woo trig!
249+
250+
// draw the hour hand:
251+
myOLED.line(MIDDLE_X, MIDDLE_Y, MIDDLE_X + hx, MIDDLE_Y + hy);
252+
}
253+
254+
// Draw an analog clock face
255+
void drawFace()
256+
{
257+
// Draw the clock border
258+
myOLED.circle(MIDDLE_X, MIDDLE_Y, CLOCK_RADIUS);
259+
260+
// Draw the clock numbers
261+
262+
myOLED.setCursor(POS_12_X, POS_12_Y); // points cursor to x=27 y=0
263+
myOLED.print(12);
264+
myOLED.setCursor(POS_6_X, POS_6_Y);
265+
myOLED.print(6);
266+
myOLED.setCursor(POS_9_X, POS_9_Y);
267+
myOLED.print(9);
268+
myOLED.setCursor(POS_3_X, POS_3_Y);
269+
myOLED.print(3);
270+
}

0 commit comments

Comments
 (0)