diff --git a/master_clock.ino b/master_clock.ino index 21b0913..2e43ef2 100644 --- a/master_clock.ino +++ b/master_clock.ino @@ -1,7 +1,6 @@ #include #include -#include -#include +#include #include #include "globals.h" @@ -14,7 +13,8 @@ #define CLK 2 #define DT 3 -Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); + +U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE); byte play = 1; byte BPM = 100; @@ -41,6 +41,10 @@ volatile long rPotPos = 0; unsigned long rPotLastUpdate = 0; const unsigned long rPotThrottle = 50; +unsigned long screenLastUpdate = 0; +const unsigned long screenThrottle = 200; +byte forceScreenUpdate = 1; + void setup() { Serial.begin(9600); @@ -52,21 +56,19 @@ void setup() { pinMode(DT, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(CLK), updateEncoder, FALLING); - if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { - Serial.println(F("SSD1306 allocation failed")); - for (;;); - } + // screen setup + u8g2.begin(); - display.clearDisplay(); - display.setTextSize(2); - display.setTextColor(WHITE); - display.setCursor(0, 0); - - String bpmString = "BPM: "; - String bpmCalculatedString = bpmString + BPM; - display.println(bpmCalculatedString); - - display.display(); + // display.clearDisplay(); + // display.setTextSize(2); + // display.setTextColor(WHITE); + // display.setCursor(0, 0); + // + // String bpmString = "BPM: "; + // String bpmCalculatedString = bpmString + BPM; + // display.println(bpmCalculatedString); + // + // display.display(); // init timer Timer1.initialize(1000000); @@ -140,6 +142,26 @@ void loop() { checkBtn(); checkRPot(); + updateScreen(); +} + +void updateScreen() { + unsigned long currentTime = millis(); + if (currentTime - screenLastUpdate >= screenThrottle && forceScreenUpdate == 1) { + screenLastUpdate = currentTime; + forceScreenUpdate = 0; + // The firstPage()/nextPage() loop manages where the drawing commands go + u8g2.firstPage(); + do { + // --- Drawing Commands for the CURRENT Page --- + // All drawing commands must live INSIDE this do/while loop. + u8g2.setFont(u8g2_font_helvR08_tf); + u8g2.setCursor(0, 10); + u8g2.print(F("BPM: ")); + u8g2.print(BPM); // Example of dynamic data + + } while (u8g2.nextPage()); + } } void checkBtn() { @@ -164,6 +186,15 @@ void checkRPot() { if (currentPos != previousPos) { Serial.print("Encoder Position: "); Serial.println(currentPos); + + if (currentPos > previousPos) { + BPM++; + } else { + BPM--; + } + + forceScreenUpdate = 1; + previousPos = currentPos; }