11/* *****************************************************************************
2- * MicroOLED_Cube.ino
3- * Rotating a 3-D Cube on the MicroOLED Breakout
4- * Jim Lindblom @ SparkFun Electronics
5- * Original Creation Date: October 27, 2014
6- *
7- * This sketch uses the MicroOLED library to draw a 3-D projected
8- * cube, and rotate it along all three axes.
9- *
10- * Development environment specifics:
11- * Arduino 1.0.5
12- * Arduino Pro 3.3V
13- * Micro OLED Breakout v1.0
14- *
15- * This code is beerware; if you see me (or any other SparkFun employee) at the
16- * local, and you've found our code helpful, please buy us a round!
17- *
18- * Distributed as-is; no warranty is given.
2+ MicroOLED_Cube.ino
3+ Rotating a 3-D Cube on the MicroOLED Breakout
4+ Jim Lindblom @ SparkFun Electronics
5+ Original Creation Date: October 27, 2014
6+
7+ This sketch uses the MicroOLED library to draw a 3-D projected
8+ cube, and rotate it along all three axes.
9+
10+ Development environment specifics:
11+ Arduino 1.0.5
12+ Arduino Pro 3.3V
13+ Micro OLED Breakout v1.0
14+
15+ This code is beerware; if you see me (or any other SparkFun employee) at the
16+ local, and you've found our code helpful, please buy us a round!
17+
18+ Distributed as-is; no warranty is given.
1919 ******************************************************************************/
2020#include < Wire.h> // Include Wire if you're using I2C
2121#include < SPI.h> // Include SPI if you're using SPI
2727#define PIN_RESET 9 // Connect RST to pin 9
2828#define PIN_DC 8 // Connect DC to pin 8
2929#define PIN_CS 10 // Connect CS to pin 10
30- #define DC_JUMPER 0
30+ #define DC_JUMPER 0 // Set to either 0 (SPI, default) or 1 (I2C) based on jumper, matching the value of the DC Jumper
31+ // Also connect pin 13 to SCK and pin 11 to MOSI
3132
3233// ////////////////////////////////
3334// MicroOLED Object Declaration //
3435// ////////////////////////////////
35- MicroOLED oled (PIN_RESET, PIN_DC, PIN_CS); // SPI declaration
36- // MicroOLED oled(PIN_RESET, DC_JUMPER); // I2C declaration
36+ // Declare a MicroOLED object. The parameters include:
37+ // 1 - Reset pin: Any digital pin
38+ // 2 - D/C pin: Any digital pin (SPI mode only)
39+ // 3 - CS pin: Any digital pin (SPI mode only, 10 recommended)
40+
41+ MicroOLED oled (PIN_RESET, PIN_DC, PIN_CS); // Example SPI declaration, comment out if using I2C
42+ // MicroOLED oled(PIN_RESET, DC_JUMPER); //Example I2C declaration, uncomment if using I2C
3743
3844int SCREEN_WIDTH = oled.getLCDWidth();
3945int SCREEN_HEIGHT = oled.getLCDHeight();
4046
4147float d = 3 ;
42- float px[] = {
43- -d, d, d, -d, -d, d, d, -d };
44- float py[] = {
45- -d, -d, d, d, -d, -d, d, d };
46- float pz[] = {
47- -d, -d, -d, -d, d, d, d, d };
48+ float px[] = {
49+ -d, d, d, -d, -d, d, d, -d
50+ };
51+ float py[] = {
52+ -d, -d, d, d, -d, -d, d, d
53+ };
54+ float pz[] = {
55+ -d, -d, -d, -d, d, d, d, d
56+ };
4857
4958float p2x[] = {
50- 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 };
59+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
60+ };
5161float p2y[] = {
52- 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 };
62+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
63+ };
5364
5465float r[] = {
55- 0 ,0 ,0 };
66+ 0 , 0 , 0
67+ };
5668
5769#define SHAPE_SIZE 600
5870// Define how fast the cube rotates. Smaller numbers are faster.
5971// This is the number of ms between draws.
6072#define ROTATION_SPEED 0
6173
74+ // I2C is great, but will result in a much slower update rate. The
75+ // slower framerate may be a worthwhile tradeoff, if you need more
76+ // pins, though.
6277void setup ()
6378{
79+ delay (100 );
80+ // Wire.begin(); //set up I2C bus, uncomment if you are using I2C
81+
6482 oled.begin ();
6583 oled.clear (ALL);
66- oled.display ();
84+ oled.display ();
6785}
6886
6987void loop ()
@@ -74,41 +92,41 @@ void loop()
7492
7593void drawCube ()
7694{
77- r[0 ]= r[0 ]+PI/ 180.0 ; // Add a degree
78- r[1 ]= r[1 ]+PI/ 180.0 ; // Add a degree
79- r[2 ]= r[2 ]+PI/ 180.0 ; // Add a degree
80- if (r[0 ] >= 360.0 *PI/ 180.0 ) r[0 ] = 0 ;
81- if (r[1 ] >= 360.0 *PI/ 180.0 ) r[1 ] = 0 ;
82- if (r[2 ] >= 360.0 *PI/ 180.0 ) r[2 ] = 0 ;
83-
84- for (int i= 0 ;i< 8 ; i++)
95+ r[0 ] = r[0 ] + PI / 180.0 ; // Add a degree
96+ r[1 ] = r[1 ] + PI / 180.0 ; // Add a degree
97+ r[2 ] = r[2 ] + PI / 180.0 ; // Add a degree
98+ if (r[0 ] >= 360.0 * PI / 180.0 ) r[0 ] = 0 ;
99+ if (r[1 ] >= 360.0 * PI / 180.0 ) r[1 ] = 0 ;
100+ if (r[2 ] >= 360.0 * PI / 180.0 ) r[2 ] = 0 ;
101+
102+ for (int i = 0 ; i < 8 ; i++)
85103 {
86104 float px2 = px[i];
87- float py2 = cos (r[0 ])* py[i] - sin (r[0 ])* pz[i];
88- float pz2 = sin (r[0 ])* py[i] + cos (r[0 ])* pz[i];
105+ float py2 = cos (r[0 ]) * py[i] - sin (r[0 ]) * pz[i];
106+ float pz2 = sin (r[0 ]) * py[i] + cos (r[0 ]) * pz[i];
89107
90- float px3 = cos (r[1 ])* px2 + sin (r[1 ])* pz2;
108+ float px3 = cos (r[1 ]) * px2 + sin (r[1 ]) * pz2;
91109 float py3 = py2;
92- float pz3 = -sin (r[1 ])* px2 + cos (r[1 ])* pz2;
110+ float pz3 = -sin (r[1 ]) * px2 + cos (r[1 ]) * pz2;
93111
94- float ax = cos (r[2 ])* px3 - sin (r[2 ])* py3;
95- float ay = sin (r[2 ])* px3 + cos (r[2 ])* py3;
96- float az = pz3- 150 ;
112+ float ax = cos (r[2 ]) * px3 - sin (r[2 ]) * py3;
113+ float ay = sin (r[2 ]) * px3 + cos (r[2 ]) * py3;
114+ float az = pz3 - 150 ;
97115
98- p2x[i] = SCREEN_WIDTH/ 2 +ax* SHAPE_SIZE/ az;
99- p2y[i] = SCREEN_HEIGHT/ 2 +ay* SHAPE_SIZE/ az;
116+ p2x[i] = SCREEN_WIDTH / 2 + ax * SHAPE_SIZE / az;
117+ p2y[i] = SCREEN_HEIGHT / 2 + ay * SHAPE_SIZE / az;
100118 }
101119
102120 oled.clear (PAGE);
103- for (int i= 0 ;i< 3 ; i++)
121+ for (int i = 0 ; i < 3 ; i++)
104122 {
105- oled.line (p2x[i],p2y[i],p2x[i+ 1 ],p2y[i+ 1 ]);
106- oled.line (p2x[i+ 4 ],p2y[i+ 4 ],p2x[i+ 5 ],p2y[i+ 5 ]);
107- oled.line (p2x[i],p2y[i],p2x[i+ 4 ],p2y[i+ 4 ]);
108- }
109- oled.line (p2x[3 ],p2y[3 ],p2x[0 ],p2y[0 ]);
110- oled.line (p2x[7 ],p2y[7 ],p2x[4 ],p2y[4 ]);
111- oled.line (p2x[3 ],p2y[3 ],p2x[7 ],p2y[7 ]);
123+ oled.line (p2x[i], p2y[i], p2x[i + 1 ], p2y[i + 1 ]);
124+ oled.line (p2x[i + 4 ], p2y[i + 4 ], p2x[i + 5 ], p2y[i + 5 ]);
125+ oled.line (p2x[i], p2y[i], p2x[i + 4 ], p2y[i + 4 ]);
126+ }
127+ oled.line (p2x[3 ], p2y[3 ], p2x[0 ], p2y[0 ]);
128+ oled.line (p2x[7 ], p2y[7 ], p2x[4 ], p2y[4 ]);
129+ oled.line (p2x[3 ], p2y[3 ], p2x[7 ], p2y[7 ]);
112130 oled.display ();
113131}
114132
0 commit comments