From d9106c6951e63a2b6a4a884e5f91a5feca7af51d Mon Sep 17 00:00:00 2001 From: Adam Wonak Date: Sun, 8 Jun 2025 11:39:12 -0700 Subject: [PATCH] refactor calibrate analog demo to use tge u8g2 library. --- .../calibrate_analog/calibrate_analog.ino | 63 ++++++++++++------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/examples/calibrate_analog/calibrate_analog.ino b/examples/calibrate_analog/calibrate_analog.ino index 4dc1e2a..c3e6a5f 100644 --- a/examples/calibrate_analog/calibrate_analog.ino +++ b/examples/calibrate_analog/calibrate_analog.ino @@ -19,6 +19,10 @@ #include "gravity.h" +#define TEXT_FONT u8g2_font_profont11_tf +#define INDICATOR_FONT u8g2_font_open_iconic_arrow_1x_t +#define U8G2_UP_ARROW_GLYPH 0x43 // caret-up '˄' + byte selected_param = 0; // Initialize the gravity library and attach your handlers in the setup method. @@ -46,7 +50,7 @@ void CalibrateCV(Direction dir, int val) { cv->AdjustCalibrationLow(val); break; case 1: - cv->AdjustCalibrationOffset(val); + cv->SetOffset(val); break; case 2: cv->AdjustCalibrationHigh(val); @@ -55,51 +59,64 @@ void CalibrateCV(Direction dir, int val) { } void UpdateDisplay() { - gravity.display.clearDisplay(); - DisplayCalibration(&gravity.cv1, "CV1: ", 0); - DisplayCalibration(&gravity.cv2, "CV2: ", 1); - gravity.display.display(); + gravity.display.firstPage(); + do { + // Set default font mode and color for each page draw + gravity.display.setFontMode(0); // Transparent font background + gravity.display.setDrawColor(1); // Draw with color 1 (ON) + gravity.display.setFont(TEXT_FONT); + DisplayCalibration(&gravity.cv1, "CV1: ", 0); + DisplayCalibration(&gravity.cv2, "CV2: ", 1); + } while (gravity.display.nextPage()); + } -void DisplayCalibration(AnalogInput* cv, String title, int index) { +void DisplayCalibration(AnalogInput* cv, const char* title, int index) { + int barWidth = 100; int barHeight = 10; int textHeight = 10; int half = barWidth / 2; int offsetX = 16; int offsetY = (32 * index); - int color = 1; + + // U8g2 draw color: 1 for foreground (white/on), 0 for background (black/off) + gravity.display.setDrawColor(1); // CV value reading. - int value = constrain(cv->Read(), -512, 512); + int value = cv->Read(); - gravity.display.setCursor(0, offsetY); + // Set cursor and print title and value + gravity.display.setCursor(0, offsetY + textHeight); // Adjust y-position to align with text base line gravity.display.print(title); - gravity.display.print(value >= 0 ? " " : ""); + if (value >= 0) gravity.display.print(" "); // Add space for positive values for alignment gravity.display.print(value); - gravity.display.setCursor(92, offsetY); - gravity.display.print(value >= 0 ? " " : ""); - gravity.display.print(cv->Voltage()); + // Print voltage + gravity.display.setCursor(92, offsetY + textHeight); // Adjust x,y position + if (cv->Voltage() >= 0) gravity.display.print(" "); + gravity.display.print(cv->Voltage(), 1); // Print float with 1 decimal place gravity.display.print(F("V")); - gravity.display.drawRect(offsetX, textHeight + offsetY, barWidth, barHeight, color); + // Draw the main bar rectangle + gravity.display.drawFrame(offsetX, textHeight + offsetY + 2, barWidth, barHeight); // Using drawFrame instead of drawRect + if (value > 0) { // 0 to 512 - int x = (float(value) / 512.0) * half; - int fill = min(x, 512); - gravity.display.fillRect(half + offsetX, textHeight + offsetY, fill, barHeight, color); + int x = map(value, 0, 512, 0, half); // Map value to bar fill width + gravity.display.drawBox(half + offsetX, textHeight + offsetY + 2, x, barHeight); // Using drawBox instead of fillRect } else { // -512 to 0 - int x = (float(abs(value)) / 512.0) * half; - int fill = min(half, x); - gravity.display.fillRect((half + offsetX) - x, textHeight + offsetY, fill, barHeight, color); + int x = map(abs(value), 0, 512, 0, half); // Map absolute value to bar fill width + gravity.display.drawBox((half + offsetX) - x, textHeight + offsetY + 2, x, barHeight); // Using drawBox } // Display selected calibration point if selected calibration point belongs to current cv input. if (selected_param / 3 == index) { - int left = offsetX + ((half - 2) * (selected_param % 3)); - int top = barHeight + textHeight + offsetY + 2; - gravity.display.drawChar(left, top, 0x1E, 1, 0, 1); + int charWidth = 6; + int left = offsetX + (half * (selected_param % 3) - 2); // Adjust position + int top = barHeight + textHeight + offsetY + 12; + // Drawing an arrow character (ASCII 0x1E is often an up arrow in some fonts, might need adjustment) + gravity.display.drawStr(left, top, "^"); // Draw the arrow character } }