diff --git a/platformio.ini b/platformio.ini index 86cb044..91794fc 100644 --- a/platformio.ini +++ b/platformio.ini @@ -4,10 +4,8 @@ board = adafruit_feather framework = arduino board_build.core = earlephilhower monitor_speed = 115200 -build_flags = - -DUSE_TINYUSB + lib_deps = - adafruit/Adafruit SSD1306 adafruit/Adafruit GFX Library - adafruit/Adafruit TinyUSB Library - adafruit/SdFat - Adafruit Fork \ No newline at end of file + adafruit/Adafruit SSD1306 + adafruit/Adafruit BusIO \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index e4b0249..f619d12 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,17 +4,37 @@ #include // OLED on SPI1 (SCK = GP14, MOSI = GP15) -// If your display worked with other pins, change these three lines. -#define OLED_CS 9 +#define OLED_CS 24 // change to 24 when the PCM5100 uses D9 #define OLED_DC 6 #define OLED_RST 5 - -#define BAT_PIN A3 // GP29, battery voltage / 2 +#define BAT_PIN A3 #define SAMPLES 16 +#define ENC_CLK 26 // A0 +#define ENC_DT 27 // A1 +#define ENC_SW 28 // A2 + +#define NUM_MIN 0 +#define NUM_MAX 999 +#define NUM_STEP 1 + Adafruit_SSD1306 display(128, 64, &SPI1, OLED_DC, OLED_RST, OLED_CS); +volatile int encPos = 0; +volatile uint8_t encState = 0; +const int8_t ENC_TABLE[16] = { + 0, -1, 1, 0, + 1, 0, 0, -1, + -1, 0, 0, 1, + 0, 1, -1, 0 +}; + +int screenId = 0; // 0 = battery, 1 = number +int number = 0; +float batVolts = 0; +int batPct = 0; + struct BatPoint { float v; int pct; }; const BatPoint CURVE[] = { {4.20f, 100}, {4.10f, 90}, {4.00f, 80}, {3.92f, 70}, {3.87f, 60}, @@ -23,10 +43,16 @@ const BatPoint CURVE[] = { }; const int CURVE_N = sizeof(CURVE) / sizeof(CURVE[0]); +void encISR() { + uint8_t newState = (digitalRead(ENC_CLK) << 1) | digitalRead(ENC_DT); + encPos += ENC_TABLE[(encState << 2) | newState]; + encState = newState; +} + float readVolts() { uint32_t sum = 0; for (int i = 0; i < SAMPLES; i++) sum += analogRead(BAT_PIN); - return (sum / (float)SAMPLES) * 3.3f / 4095.0f * 3.55f; + return (sum / (float)SAMPLES) * 3.3f / 4095.0f * 2.0f; // unverified divider } int voltsToPercent(float v) { @@ -48,56 +74,104 @@ void drawBatteryIcon(int x, int y, int pct) { display.fillRect(x + 2, y + 2, w, 8, SSD1306_WHITE); } +void drawBatteryScreen() { + display.setTextSize(1); + display.setCursor(0, 0); + display.print("Battery"); + display.setTextSize(3); + display.setCursor(0, 18); + display.print(batPct); + display.print('%'); + display.setTextSize(1); + display.setCursor(0, 54); + display.print(batVolts, 2); + display.print(" V"); + drawBatteryIcon(96, 52, batPct); +} + +void drawNumberScreen() { + display.setTextSize(1); + display.setCursor(0, 0); + display.print("Number"); + + char buf[8]; + snprintf(buf, sizeof(buf), "%d", number); + display.setTextSize(4); + int textW = strlen(buf) * 6 * 4; + display.setCursor((128 - textW) / 2, 20); + display.print(buf); + + display.drawRect(0, 56, 128, 8, SSD1306_WHITE); + int w = map(number, NUM_MIN, NUM_MAX, 0, 124); + display.fillRect(2, 58, w, 4, SSD1306_WHITE); +} + +void drawScreen() { + display.clearDisplay(); + display.setTextColor(SSD1306_WHITE); + if (screenId == 0) drawBatteryScreen(); + else drawNumberScreen(); + display.display(); +} + void setup() { Serial.begin(115200); analogReadResolution(12); - + pinMode(ENC_CLK, INPUT_PULLUP); + pinMode(ENC_DT, INPUT_PULLUP); + pinMode(ENC_SW, INPUT_PULLUP); + encState = (digitalRead(ENC_CLK) << 1) | digitalRead(ENC_DT); + attachInterrupt(digitalPinToInterrupt(ENC_CLK), encISR, CHANGE); + attachInterrupt(digitalPinToInterrupt(ENC_DT), encISR, CHANGE); + SPI1.setSCK(14); SPI1.setTX(15); if (!display.begin(SSD1306_SWITCHCAPVCC)) { Serial.println("OLED init failed"); - while (true); + while (true) delay(100); } - display.clearDisplay(); - display.setTextColor(SSD1306_WHITE); - display.setTextSize(2); - display.setCursor(0, 0); - display.print("Hello"); - display.display(); - delay(1500); - + batVolts = readVolts(); + batPct = voltsToPercent(batVolts); + drawScreen(); } void loop() { - float volts = readVolts(); - int pct = voltsToPercent(volts); + static int lastDetent = 0; + static bool lastBtn = HIGH; + static uint32_t lastPress = 0; + static uint32_t lastBat = 0; + bool redraw = false; - Serial.print(volts, 2); - Serial.print(" V "); - Serial.print(pct); - Serial.println(" %"); + noInterrupts(); + int pos = encPos; + interrupts(); + int detent = pos / 4; // use /2 if one click gives 2 counts + if (detent != lastDetent) { + int delta = detent - lastDetent; + lastDetent = detent; + if (screenId == 1) { + number = constrain(number + delta * NUM_STEP, NUM_MIN, NUM_MAX); + Serial.println(number); + redraw = true; + } + } - display.clearDisplay(); - display.setTextColor(SSD1306_WHITE); + bool btn = digitalRead(ENC_SW); + if (btn == LOW && lastBtn == HIGH && millis() - lastPress > 50) { + lastPress = millis(); + screenId = (screenId + 1) % 2; + redraw = true; + } + lastBtn = btn; - display.setTextSize(1); - display.setCursor(0, 0); - display.print("Battery"); + if (millis() - lastBat >= 1000) { + lastBat = millis(); + batVolts = readVolts(); + batPct = voltsToPercent(batVolts); + if (screenId == 0) redraw = true; + } - display.setTextSize(3); - display.setCursor(0, 18); - display.print(pct); - display.print('%'); - - display.setTextSize(1); - display.setCursor(0, 54); - display.print(volts, 2); - display.print(" V"); - - drawBatteryIcon(96, 52, pct); - - display.display(); - delay(1000); + if (redraw) drawScreen(); } \ No newline at end of file