|
| 1 | +// Example-07_Cube.ino |
| 2 | +// |
| 3 | +// This is a library written for SparkFun Qwiic OLED boards that use the SSD1306. |
| 4 | +// |
| 5 | +// SparkFun sells these at its website: www.sparkfun.com |
| 6 | +// |
| 7 | +// Do you like this library? Help support SparkFun. Buy a board! |
| 8 | +// |
| 9 | +// Micro OLED https://www.sparkfun.com/products/14532 |
| 10 | +// Transparent OLED https://www.sparkfun.com/products/15173 |
| 11 | +// "Narrow" OLED https://www.sparkfun.com/products/17153 |
| 12 | +// |
| 13 | +// |
| 14 | +// Written Jim Lindblom @ SparkFun Electronics |
| 15 | +// Original Creation Date: October 27, 2014 |
| 16 | +// |
| 17 | +// This library configures and draws graphics to OLED boards that use the |
| 18 | +// SSD1306 display hardware. The library only supports I2C. |
| 19 | +// |
| 20 | +// Repository: |
| 21 | +// https://github.com/sparkfun/SparkFun_Qwiic_OLED_Arduino_Library |
| 22 | +// |
| 23 | +// Documentation: |
| 24 | +// https://sparkfun.github.io/SparkFun_Qwiic_OLED_Arduino_Library/ |
| 25 | +// |
| 26 | +// |
| 27 | +// SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT). |
| 28 | +// |
| 29 | +// SPDX-License-Identifier: MIT |
| 30 | +// |
| 31 | +// The MIT License (MIT) |
| 32 | +// |
| 33 | +// Copyright (c) 2022 SparkFun Electronics |
| 34 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and |
| 35 | +// associated documentation files (the "Software"), to deal in the Software without restriction, |
| 36 | +// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 37 | +// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to |
| 38 | +// do so, subject to the following conditions: |
| 39 | +// The above copyright notice and this permission notice shall be included in all copies or substantial |
| 40 | +// portions of the Software. |
| 41 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT |
| 42 | +// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 43 | +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 44 | +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 45 | +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 46 | +// |
| 47 | +////////////////////////////////////////////////////////////////////////////////////////// |
| 48 | +// Example 7 for the SparkFun Qwiic OLED Arduino Library |
| 49 | +// |
| 50 | +// >> Overview << |
| 51 | +// |
| 52 | +// This demo draws a rotating 3D cube |
| 53 | +// |
| 54 | +////////////////////////////////////////////////////////////////////////////////////////// |
| 55 | +// |
| 56 | +// >>> SELECT THE CONNECTED DEVICE FOR THIS EXAMPLE <<< |
| 57 | +// |
| 58 | +// The Library supports three different types of SparkFun boards. The demo uses the following |
| 59 | +// defines to determine which device is being used. Uncomment the device being used for this demo. |
| 60 | +// |
| 61 | +// The default is Micro OLED |
| 62 | + |
| 63 | +#define MICRO |
| 64 | +//#define NARROW |
| 65 | +//#define TRANSPARENT |
| 66 | + |
| 67 | +////////////////////////////////////////////////////////////////////////////////////////// |
| 68 | + |
| 69 | +#include <stdint.h> |
| 70 | + |
| 71 | +// Include the SparkFun qwiic OLED Library |
| 72 | +#include <SparkFun_Qwiic_OLED.h> |
| 73 | + |
| 74 | +// What device is being used in this demo |
| 75 | + |
| 76 | +#if defined(TRANSPARENT) |
| 77 | +QwiicTransparentOLED myOLED; |
| 78 | +const char * deviceName = "Transparent OLED"; |
| 79 | + |
| 80 | +#elif defined(NARROW) |
| 81 | +QwiicNarrowOLED myOLED; |
| 82 | +const char * deviceName = "Narrow OLED"; |
| 83 | + |
| 84 | +#else |
| 85 | +QwiicMicroOLED myOLED; |
| 86 | +const char * deviceName = "Micro OLED"; |
| 87 | + |
| 88 | +#endif |
| 89 | + |
| 90 | + |
| 91 | +int width; |
| 92 | +int height; |
| 93 | + |
| 94 | +uint32_t draw_total_time; |
| 95 | +uint32_t n_draws; |
| 96 | + |
| 97 | +float d = 3; |
| 98 | +float px[] = { -d, d, d, -d, -d, d, d, -d}; |
| 99 | +float py[] = { -d, -d, d, d, -d, -d, d, d}; |
| 100 | +float pz[] = { -d, -d, -d, -d, d, d, d, d}; |
| 101 | + |
| 102 | +float p2x[8] = {0}; |
| 103 | +float p2y[8] = {0}; |
| 104 | +float r[3] = {0}; |
| 105 | + |
| 106 | +#define SHAPE_SIZE 400 |
| 107 | +// Define how fast the cube rotates. Smaller numbers are faster. |
| 108 | +// This is the number of ms between draws. |
| 109 | +#define ROTATION_SPEED 00 |
| 110 | + |
| 111 | +//////////////////////////////////////////////////////////////////////////////////////////////// |
| 112 | +// setup() |
| 113 | +// |
| 114 | +// Standard Arduino setup routine |
| 115 | + |
| 116 | +void setup() |
| 117 | +{ |
| 118 | + delay(500); //Give display time to power on |
| 119 | + Serial.begin(115200); |
| 120 | + |
| 121 | + Serial.println("\n\r-----------------------------------"); |
| 122 | + |
| 123 | + Serial.print("Running Test #2 on: "); |
| 124 | + Serial.println(String(deviceName)); |
| 125 | + |
| 126 | + if(!myOLED.begin()){ |
| 127 | + |
| 128 | + Serial.println("- Device Begin Failed"); |
| 129 | + while(1); |
| 130 | + } |
| 131 | + |
| 132 | + Serial.println("- Begin Successful"); |
| 133 | + |
| 134 | + width = myOLED.getWidth(); |
| 135 | + height = myOLED.getHeight(); |
| 136 | + |
| 137 | + // for frame rate calc |
| 138 | + draw_total_time =0; |
| 139 | + n_draws=0; |
| 140 | + |
| 141 | + // set a template for our framerate display |
| 142 | + Serial.print("- Frame Rate: 00.00"); |
| 143 | +} |
| 144 | + |
| 145 | +//////////////////////////////////////////////////////////////////////////////////////////////// |
| 146 | +// loop |
| 147 | + |
| 148 | +void loop() |
| 149 | +{ |
| 150 | + // just draw the cube - as fast as we can! |
| 151 | + drawCube(); |
| 152 | +} |
| 153 | + |
| 154 | +//////////////////////////////////////////////////////////////////////////////////////////////// |
| 155 | +// drawCube() |
| 156 | +// |
| 157 | + |
| 158 | +void drawCube() |
| 159 | +{ |
| 160 | + |
| 161 | + |
| 162 | + r[0] = r[0] + PI / 180.0; // Add a degree |
| 163 | + r[1] = r[1] + PI / 180.0; // Add a degree |
| 164 | + r[2] = r[2] + PI / 180.0; // Add a degree |
| 165 | + if(r[0] >= 360.0 * PI / 180.0) |
| 166 | + r[0] = 0; |
| 167 | + if(r[1] >= 360.0 * PI / 180.0) |
| 168 | + r[1] = 0; |
| 169 | + if(r[2] >= 360.0 * PI / 180.0) |
| 170 | + r[2] = 0; |
| 171 | + |
| 172 | + // This routine gets called often, so just make these statics |
| 173 | + static float px2, py2, pz2, px3, py3, pz3, ax, ay, az; |
| 174 | + |
| 175 | + for (int i = 0; i < 8; i++){ |
| 176 | + |
| 177 | + px2 = px[i]; |
| 178 | + py2 = cos(r[0]) * py[i] - sin(r[0]) * pz[i]; |
| 179 | + pz2 = sin(r[0]) * py[i] + cos(r[0]) * pz[i]; |
| 180 | + |
| 181 | + |
| 182 | + px3 = cos(r[1]) * px2 + sin(r[1]) * pz2; |
| 183 | + py3 = py2; |
| 184 | + pz3 = -sin(r[1]) * px2 + cos(r[1]) * pz2; |
| 185 | + |
| 186 | + ax = cos(r[2]) * px3 - sin(r[2]) * py3; |
| 187 | + ay = sin(r[2]) * px3 + cos(r[2]) * py3; |
| 188 | + az = pz3 - 150; |
| 189 | + |
| 190 | + p2x[i] = width / 2 + ax * SHAPE_SIZE / az; |
| 191 | + p2y[i] = height / 2 + ay * SHAPE_SIZE / az; |
| 192 | + |
| 193 | + } |
| 194 | + |
| 195 | + // Calculate draw time... |
| 196 | + uint32_t milStart = millis(); |
| 197 | + |
| 198 | + myOLED.erase(); |
| 199 | + for (int i = 0; i < 3; i++){ |
| 200 | + |
| 201 | + myOLED.line(p2x[i], p2y[i], p2x[i + 1], p2y[i + 1]); |
| 202 | + myOLED.line(p2x[i + 4], p2y[i + 4], p2x[i + 5], p2y[i + 5]); |
| 203 | + myOLED.line(p2x[i], p2y[i], p2x[i + 4], p2y[i + 4]); |
| 204 | + } |
| 205 | + |
| 206 | + myOLED.line(p2x[3], p2y[3], p2x[0], p2y[0]); |
| 207 | + myOLED.line(p2x[7], p2y[7], p2x[4], p2y[4]); |
| 208 | + myOLED.line(p2x[3], p2y[3], p2x[7], p2y[7]); |
| 209 | + myOLED.display(); |
| 210 | + |
| 211 | + // Write out our frame rate |
| 212 | + draw_total_time += millis() - milStart; |
| 213 | + n_draws++; |
| 214 | + |
| 215 | + // output framerate? |
| 216 | + if(n_draws % 120 == 0){ |
| 217 | + // backspace over old number |
| 218 | + Serial.print("\b\b\b\b\b"); // backspace over old number |
| 219 | + Serial.print(((float)draw_total_time)/n_draws); |
| 220 | + |
| 221 | + if(n_draws > 1000){ // reset after a bit... |
| 222 | + n_draws = 0; |
| 223 | + draw_total_time=0; |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | +} |
0 commit comments