-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathExample-07_Cube.ino
157 lines (122 loc) · 4.25 KB
/
Example-07_Cube.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Example-07_Cube.ino
This demo draws a rotating 3D cube
This library configures and draws graphics to OLED boards that use the
SSD1306 display hardware. The library only supports I2C.
SparkFun sells these at its website: www.sparkfun.com
Do you like this library? Help support SparkFun. Buy a board!
Micro OLED https://www.sparkfun.com/products/14532
Transparent OLED https://www.sparkfun.com/products/15173
"Narrow" OLED https://www.sparkfun.com/products/17153
Qwiic OLED 1.3in https://www.sparkfun.com/products/23453
Written by
Jim Lindblom @ SparkFun Electronics
Original Creation Date: October 27, 2014
Repository:
https://github.com/sparkfun/SparkFun_Qwiic_OLED_Arduino_Library
Documentation:
https://sparkfun.github.io/SparkFun_Qwiic_OLED_Arduino_Library/
SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).
*/
#include <SparkFun_Qwiic_OLED.h> //http://librarymanager/All#SparkFun_Qwiic_OLED
// The Library supports three different types of SparkFun boards. The demo uses the following
// defines to determine which device is being used. Uncomment the device being used for this demo.
QwiicMicroOLED myOLED;
// QwiicTransparentOLED myOLED;
// QwiicNarrowOLED myOLED;
// Qwiic1in3OLED myOLED;
int width;
int height;
// For simple frame rate calculations
long drawTotalTime = 0;
int numberOfDraws = 0;
float d = 3;
float px[] = {-d, d, d, -d, -d, d, d, -d};
float py[] = {-d, -d, d, d, -d, -d, d, d};
float pz[] = {-d, -d, -d, -d, d, d, d, d};
float p2x[8] = {0};
float p2y[8] = {0};
float r[3] = {0};
#define SHAPE_SIZE 400
// Define how fast the cube rotates. Smaller numbers are faster.
// This is the number of ms between draws.
#define ROTATION_SPEED 00
void setup()
{
Serial.begin(115200);
Serial.println("Running OLED example");
Wire.begin();
// Wire.setClock(400000); //Optionally increase I2C bus speed to 400kHz
// Initalize the OLED device and related graphics system
if (myOLED.begin() == false)
{
Serial.println("Device begin failed. Freezing...");
while (true)
;
}
Serial.println("Begin success");
width = myOLED.getWidth();
height = myOLED.getHeight();
// For frame rate calc
drawTotalTime = 0;
numberOfDraws = 0;
// Set a template for our framerate display
Serial.print("- Frame Rate: 00.00");
}
void loop()
{
// Draw the cube - as fast as we can!
drawCube();
}
void drawCube()
{
r[0] = r[0] + PI / 180.0; // Add a degree
r[1] = r[1] + PI / 180.0; // Add a degree
r[2] = r[2] + PI / 180.0; // Add a degree
if (r[0] >= 360.0 * PI / 180.0)
r[0] = 0;
if (r[1] >= 360.0 * PI / 180.0)
r[1] = 0;
if (r[2] >= 360.0 * PI / 180.0)
r[2] = 0;
// This routine gets called often, so just make these statics
static float px2, py2, pz2, px3, py3, pz3, ax, ay, az;
for (int i = 0; i < 8; i++)
{
px2 = px[i];
py2 = cos(r[0]) * py[i] - sin(r[0]) * pz[i];
pz2 = sin(r[0]) * py[i] + cos(r[0]) * pz[i];
px3 = cos(r[1]) * px2 + sin(r[1]) * pz2;
py3 = py2;
pz3 = -sin(r[1]) * px2 + cos(r[1]) * pz2;
ax = cos(r[2]) * px3 - sin(r[2]) * py3;
ay = sin(r[2]) * px3 + cos(r[2]) * py3;
az = pz3 - 150;
p2x[i] = width / 2 + ax * SHAPE_SIZE / az;
p2y[i] = height / 2 + ay * SHAPE_SIZE / az;
}
// Calculate draw time
uint32_t startTime = millis();
myOLED.erase();
for (int i = 0; i < 3; i++)
{
myOLED.line(p2x[i], p2y[i], p2x[i + 1], p2y[i + 1]);
myOLED.line(p2x[i + 4], p2y[i + 4], p2x[i + 5], p2y[i + 5]);
myOLED.line(p2x[i], p2y[i], p2x[i + 4], p2y[i + 4]);
}
myOLED.line(p2x[3], p2y[3], p2x[0], p2y[0]);
myOLED.line(p2x[7], p2y[7], p2x[4], p2y[4]);
myOLED.line(p2x[3], p2y[3], p2x[7], p2y[7]);
myOLED.display();
// Write out our frame rate
drawTotalTime += millis() - startTime;
numberOfDraws++;
// Output framerate once every 120 frames
if (numberOfDraws % 120 == 0)
{
Serial.print(" Frame rate: ");
Serial.println(numberOfDraws / (float)drawTotalTime * 1000.0);
numberOfDraws = 0;
drawTotalTime = 0;
}
}