Skip to content

Commit 7df7c99

Browse files
committed
Added digital clock example using drawLine and drawCircle
Signed-off-by: CaptainStouf <stouf@hardware-libre.org>
1 parent 42cd0bc commit 7df7c99

File tree

2 files changed

+196
-0
lines changed

2 files changed

+196
-0
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 by Daniel Eichhorn
5+
* Copyright (c) 2016 by Fabrice Weinberg
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*
25+
*/
26+
27+
#include <Wire.h>
28+
#include <TimeLib.h>
29+
#include "SSD1306.h"
30+
#include "SSD1306Ui.h"
31+
#include "images.h"
32+
33+
// Initialize the OLED display on address 0x3c
34+
SSD1306 display(0x3c, 5, 4);
35+
SSD1306Ui ui ( &display );
36+
37+
int screenW = 128;
38+
int screenH = 64;
39+
int clockCenterX = screenW/2;
40+
int clockCenterY = ((screenH-16)/2)+16; // top yellow part is 16 px height
41+
int clockRadius = 23;
42+
43+
// utility function for digital clock display: prints leading 0
44+
String twoDigits(int digits){
45+
if(digits < 10) {
46+
String i = '0'+String(digits);
47+
return i;
48+
}
49+
else {
50+
return String(digits);
51+
}
52+
}
53+
54+
void clockOverlay(SSD1306 *display, SSD1306UiState* state) {
55+
56+
}
57+
58+
void analogClockFrame(SSD1306 *display, SSD1306UiState* state, int16_t x, int16_t y) {
59+
// ui.disableIndicator();
60+
61+
// Draw the clock face
62+
// display->drawCircle(clockCenterX + x, clockCenterY + y, clockRadius);
63+
display->drawCircle(clockCenterX + x, clockCenterY + y, 2);
64+
//
65+
//hour ticks
66+
for( int z=0; z < 360;z= z + 30 ){
67+
//Begin at 0° and stop at 360°
68+
float angle = z ;
69+
angle = ( angle / 57.29577951 ) ; //Convert degrees to radians
70+
int x2 = ( clockCenterX + ( sin(angle) * clockRadius ) );
71+
int y2 = ( clockCenterY - ( cos(angle) * clockRadius ) );
72+
int x3 = ( clockCenterX + ( sin(angle) * ( clockRadius - ( clockRadius / 8 ) ) ) );
73+
int y3 = ( clockCenterY - ( cos(angle) * ( clockRadius - ( clockRadius / 8 ) ) ) );
74+
display->drawLine( x2 + x , y2 + y , x3 + x , y3 + y);
75+
}
76+
77+
// display second hand
78+
float angle = second() * 6 ;
79+
angle = ( angle / 57.29577951 ) ; //Convert degrees to radians
80+
int x3 = ( clockCenterX + ( sin(angle) * ( clockRadius - ( clockRadius / 5 ) ) ) );
81+
int y3 = ( clockCenterY - ( cos(angle) * ( clockRadius - ( clockRadius / 5 ) ) ) );
82+
display->drawLine( clockCenterX + x , clockCenterY + y , x3 + x , y3 + y);
83+
//
84+
// display minute hand
85+
angle = minute() * 6 ;
86+
angle = ( angle / 57.29577951 ) ; //Convert degrees to radians
87+
x3 = ( clockCenterX + ( sin(angle) * ( clockRadius - ( clockRadius / 4 ) ) ) );
88+
y3 = ( clockCenterY - ( cos(angle) * ( clockRadius - ( clockRadius / 4 ) ) ) );
89+
display->drawLine( clockCenterX + x , clockCenterY + y , x3 + x , y3 + y);
90+
//
91+
// display hour hand
92+
angle = hour() * 30 + int( ( minute() / 12 ) * 6 ) ;
93+
angle = ( angle / 57.29577951 ) ; //Convert degrees to radians
94+
x3 = ( clockCenterX + ( sin(angle) * ( clockRadius - ( clockRadius / 2 ) ) ) );
95+
y3 = ( clockCenterY - ( cos(angle) * ( clockRadius - ( clockRadius / 2 ) ) ) );
96+
display->drawLine( clockCenterX + x , clockCenterY + y , x3 + x , y3 + y);
97+
}
98+
99+
void digitalClockFrame(SSD1306 *display, SSD1306UiState* state, int16_t x, int16_t y) {
100+
String timenow = String(hour())+":"+twoDigits(minute())+":"+twoDigits(second());
101+
display->setTextAlignment(TEXT_ALIGN_CENTER);
102+
display->setFont(ArialMT_Plain_24);
103+
display->drawString(clockCenterX + x , clockCenterY + y, timenow );
104+
}
105+
106+
// This array keeps function pointers to all frames
107+
// frames are the single views that slide in
108+
FrameCallback frames[] = { analogClockFrame, digitalClockFrame };
109+
110+
// how many frames are there?
111+
int frameCount = 2;
112+
113+
// Overlays are statically drawn on top of a frame eg. a clock
114+
OverlayCallback overlays[] = { clockOverlay };
115+
int overlaysCount = 1;
116+
117+
void setup() {
118+
Serial.begin(9600);
119+
Serial.println();
120+
121+
// The ESP is capable of rendering 60fps in 80Mhz mode
122+
// but that won't give you much time for anything else
123+
// run it in 160Mhz mode or just set it to 30 fps
124+
ui.setTargetFPS(60);
125+
126+
// Customize the active and inactive symbol
127+
ui.setActiveSymbol(activeSymbol);
128+
ui.setInactiveSymbol(inactiveSymbol);
129+
130+
// You can change this to
131+
// TOP, LEFT, BOTTOM, RIGHT
132+
ui.setIndicatorPosition(TOP);
133+
134+
// Defines where the first frame is located in the bar.
135+
ui.setIndicatorDirection(LEFT_RIGHT);
136+
137+
// You can change the transition that is used
138+
// SLIDE_LEFT, SLIDE_RIGHT, SLIDE_UP, SLIDE_DOWN
139+
ui.setFrameAnimation(SLIDE_LEFT);
140+
141+
// Add frames
142+
ui.setFrames(frames, frameCount);
143+
144+
// Add overlays
145+
ui.setOverlays(overlays, overlaysCount);
146+
147+
// Initialising the UI will init the display too.
148+
ui.init();
149+
150+
display.flipScreenVertically();
151+
152+
unsigned long secsSinceStart = millis();
153+
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
154+
const unsigned long seventyYears = 2208988800UL;
155+
// subtract seventy years:
156+
unsigned long epoch = secsSinceStart - seventyYears * SECS_PER_HOUR;
157+
setTime(epoch);
158+
159+
}
160+
161+
162+
void loop() {
163+
int remainingTimeBudget = ui.update();
164+
165+
if (remainingTimeBudget > 0) {
166+
// You can do some work here
167+
// Don't do stuff if you are below your
168+
// time budget.
169+
delay(remainingTimeBudget);
170+
171+
}
172+
173+
174+
}
175+

examples/SSD1306ClockDemo/images.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const char activeSymbol[] PROGMEM = {
2+
B00000000,
3+
B00000000,
4+
B00011000,
5+
B00100100,
6+
B01000010,
7+
B01000010,
8+
B00100100,
9+
B00011000
10+
};
11+
12+
const char inactiveSymbol[] PROGMEM = {
13+
B00000000,
14+
B00000000,
15+
B00000000,
16+
B00000000,
17+
B00011000,
18+
B00011000,
19+
B00000000,
20+
B00000000
21+
};

0 commit comments

Comments
 (0)