Advertisement
Guest User

Untitled

a guest
Jun 11th, 2020
11,294
2
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 2 0
  1. #include <Arduino.h>
  2. #include <U8g2lib.h>
  3. #define FASTLED_INTERNAL
  4. #include <FastLED.h>
  5.  
  6. #define OLED_CLOCK  15          // Pins for the OLED display
  7. #define OLED_DATA   4
  8. #define OLED_RESET  16
  9.  
  10. #define NUM_LEDS    20          // FastLED definitions
  11. #define LED_PIN     5
  12.  
  13. CRGB g_LEDs[NUM_LEDS] = {0};    // Frame buffer for FastLED
  14.  
  15. U8G2_SSD1306_128X64_NONAME_F_HW_I2C g_OLED(U8G2_R2, OLED_RESET, OLED_CLOCK, OLED_DATA);
  16. int g_lineHeight = 0;
  17.  
  18. // FramesPerSecond
  19. //
  20. // Tracks a weighted average to smooth out the values that it calcs as the simple reciprocal
  21. // of the amount of time taken specified by the caller.  So 1/3 of a second is 3 fps, and it
  22. // will take up to 10 frames or so to stabilize on that value.
  23.  
  24. double FramesPerSecond(double seconds)
  25. {
  26.   static double framesPerSecond;
  27.   framesPerSecond = (framesPerSecond * .9) + (1.0 / seconds * .1);
  28.   return framesPerSecond;
  29. }
  30.  
  31. void setup()
  32. {
  33.   pinMode(LED_BUILTIN, OUTPUT);
  34.   pinMode(LED_PIN, OUTPUT);
  35.  
  36.   Serial.begin(115200);
  37.   while (!Serial) { }
  38.   Serial.println("ESP32 Startup");
  39.  
  40.   g_OLED.begin();
  41.   g_OLED.clear();
  42.   g_OLED.setFont(u8g2_font_profont15_tf);
  43.   g_lineHeight = g_OLED.getFontAscent() - g_OLED.getFontDescent();        // Descent is a negative number so we add it to the total
  44.  
  45.   FastLED.addLeds<WS2812B, LED_PIN, GRB>(g_LEDs, NUM_LEDS);               // Add our LED strip to the FastLED library
  46.   FastLED.setBrightness(4);
  47. }
  48.  
  49. void loop()
  50. {
  51.   bool bLED = 0;
  52.   double fps = 0;
  53.  
  54.   uint8_t initialHue = 0;
  55.   const uint8_t deltaHue = 16;
  56.   const uint8_t hueDensity = 4;
  57.  
  58.   for (;;)
  59.   {
  60.     bLED = !bLED;                                                         // Blink the LED off and on  
  61.     digitalWrite(LED_BUILTIN, bLED);
  62.  
  63.     double dStart = millis() / 1000.0;                                    // Display a frame and calc how long it takes
  64.  
  65.     // Handle OLED drawing
  66.  
  67.     g_OLED.clearBuffer();
  68.     g_OLED.setCursor(0, g_lineHeight);
  69.     g_OLED.printf("FPS: %.1lf", fps);
  70.     g_OLED.sendBuffer();
  71.  
  72.     // Handle LEDs
  73.  
  74.     fill_rainbow(g_LEDs, NUM_LEDS, initialHue += hueDensity, deltaHue);
  75.  
  76.     FastLED.show();
  77.  
  78.     double dEnd = millis() / 1000.0;
  79.     fps = FramesPerSecond(dEnd - dStart);
  80.   }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement