Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- This was built on the "ideaspark® ESP32 Development Board Integrated 1.14 inch ST7789 135x240 TFT LCD Display,WiFi+BL Wireless Module,CH340 Driver USB Type-C for Arduino Micropython (ESP32 1.14 inch LCD(Solder PIN))"
- 📘 QCW Ramp Generator With Display — V1
- (ESP32 + ST7789 + Hardware PWM Ramp Engine)
- 🔧 Required Arduino IDE Setup
- Board Package:
- ESP32 by Espressif
- Version: 2.0.14 (important — 3.x timer API differs)
- Libraries Required:
- TFT_eSPI (Bodmer)
- In User_Setup.h for TFT_eSPI you must have your working ST7789 config pinout proper so the screen works in your sketch
- No other external libraries required.
- Screen displays a relative representation of the real envelope.
- Actual BPS is displayed. Actual on-time of ramp envelope.
- Total Enable on time is a bit longer.
- This uses a single momentary + toggle switch arrangement for easier logic. The momentary is the Fire button but the toggle goes in parallel with it in order to toggle it into continuous running.
- There is a ramp out and an enable out.
- /*
- =========================================================
- QCW Ramp Generator – Clean Engine Architecture
- Engine always running
- Button = gate + phase align when idle
- Hold = continuous at BPS
- Tap = single burst
- No mid-ramp restart
- Wick scaled relative to amplitude
- Knee added (stable, matched to display)
- =========================================================
- */
- #include <Arduino.h>
- #include <TFT_eSPI.h>
- TFT_eSPI tft = TFT_eSPI();
- TFT_eSprite spr = TFT_eSprite(&tft);
- #define TFT_BL 32
- #define ONESHOT_PIN 26
- #define PWM_PIN 14
- #define ENABLE_PIN 13
- #define BURST_LED_PIN 22
- #define AMP_PIN 36
- #define PULSE_PIN 39
- #define DOWNSLOPE_PIN 34
- #define BPS_PIN 35
- #define WICK_PIN 33
- #define KNEE_PIN 25 // Electrically GPIO25
- #define PWM_FREQ 31250
- #define PWM_RESOLUTION 10
- #define PWM_CHANNEL 0
- #define TIMER_US 100
- #define WICK_TIME_US 2000
- #define DOT_PULSE_US 40000
- #define DEBOUNCE_MS 30
- hw_timer_t *timer100us = NULL;
- struct RampParams
- {
- uint32_t upTime_us;
- uint32_t downTime_us;
- uint32_t period_us;
- uint16_t dutyMax;
- uint16_t dutyWick;
- uint32_t kneeTime_us;
- uint16_t slope1Scale;
- uint16_t slope2Scale;
- uint32_t kneeOffset;
- };
- volatile RampParams activeParams;
- RampParams pendingParams;
- volatile uint64_t globalTime_us = 0;
- volatile uint64_t burstStartTime_us = 0;
- volatile uint64_t lastBurstTime_us = 0;
- volatile bool burstActive = false;
- volatile uint32_t measuredPeriod_us = 0;
- int SCREEN_W;
- int SCREEN_H;
- uint32_t lastButtonEdge_ms = 0;
- bool buttonHeld = false;
- int read10bit(int pin)
- {
- long sum = 0;
- for (int i = 0; i < 4; i++)
- sum += analogRead(pin);
- return (sum >> 2) >> 2;
- }
- // ================= ISR =================
- void IRAM_ATTR onTimer()
- {
- globalTime_us += TIMER_US;
- uint64_t now = globalTime_us;
- uint32_t envelopeDuration =
- WICK_TIME_US +
- activeParams.upTime_us +
- activeParams.downTime_us;
- if (!burstActive)
- {
- if ((now - lastBurstTime_us) >= activeParams.period_us)
- {
- burstStartTime_us = now;
- if (lastBurstTime_us != 0)
- measuredPeriod_us = now - lastBurstTime_us;
- lastBurstTime_us = now;
- burstActive = true;
- }
- }
- uint16_t duty = 0;
- if (burstActive)
- {
- uint64_t T = now - burstStartTime_us;
- if (T <= WICK_TIME_US)
- {
- duty = activeParams.dutyWick;
- }
- else if (T <= (WICK_TIME_US + activeParams.upTime_us))
- {
- uint32_t t = T - WICK_TIME_US;
- uint32_t totalRise =
- activeParams.dutyMax - activeParams.dutyWick;
- uint32_t linearRise =
- (totalRise * t) / activeParams.upTime_us;
- if (t < activeParams.kneeTime_us)
- {
- linearRise = (linearRise * activeParams.slope1Scale) >> 8;
- }
- else
- {
- uint32_t t2 = t - activeParams.kneeTime_us;
- uint32_t rise2 =
- (totalRise * t2) / activeParams.upTime_us;
- rise2 = (rise2 * activeParams.slope2Scale) >> 8;
- linearRise = activeParams.kneeOffset + rise2;
- }
- duty = activeParams.dutyWick + linearRise;
- }
- else if (T <= envelopeDuration)
- {
- uint32_t t = T - (WICK_TIME_US + activeParams.upTime_us);
- duty = activeParams.dutyMax -
- ((uint32_t)activeParams.dutyMax * t) /
- activeParams.downTime_us;
- }
- else
- {
- burstActive = false;
- duty = 0;
- }
- }
- if (burstActive && buttonHeld)
- {
- ledcWrite(PWM_CHANNEL, duty);
- digitalWrite(ENABLE_PIN, HIGH);
- digitalWrite(BURST_LED_PIN, HIGH);
- }
- else
- {
- ledcWrite(PWM_CHANNEL, 0);
- digitalWrite(ENABLE_PIN, LOW);
- digitalWrite(BURST_LED_PIN, LOW);
- }
- }
- // =======================================
- void setup()
- {
- pinMode(TFT_BL, OUTPUT);
- digitalWrite(TFT_BL, HIGH);
- pinMode(ONESHOT_PIN, INPUT_PULLUP);
- pinMode(ENABLE_PIN, OUTPUT);
- pinMode(BURST_LED_PIN, OUTPUT);
- ledcSetup(PWM_CHANNEL, PWM_FREQ, PWM_RESOLUTION);
- ledcAttachPin(PWM_PIN, PWM_CHANNEL);
- timer100us = timerBegin(0, 80, true);
- timerAttachInterrupt(timer100us, &onTimer, true);
- timerAlarmWrite(timer100us, TIMER_US, true);
- timerAlarmEnable(timer100us);
- tft.init();
- tft.setRotation(3);
- SCREEN_W = tft.width();
- SCREEN_H = tft.height();
- spr.setColorDepth(16);
- spr.createSprite(SCREEN_W, SCREEN_H);
- }
- void loop()
- {
- int amp10 = read10bit(AMP_PIN);
- int pulse10 = read10bit(PULSE_PIN);
- int wick10 = read10bit(WICK_PIN);
- int bps10 = read10bit(BPS_PIN);
- int down10 = read10bit(DOWNSLOPE_PIN);
- int knee10 = read10bit(KNEE_PIN);
- float onTime_ms = 5.0f + (pulse10 / 1023.0f) * 20.0f;
- float amplitude = amp10 / 1023.0f;
- float wickRatio = (wick10 / 1023.0f) * 0.2f;
- float wick = amplitude * wickRatio;
- if (wick > amplitude)
- wick = amplitude;
- float wickPercent = wickRatio * 100.0f;
- float bps_hz = 1.0f + (bps10 / 1023.0f) * 14.0f;
- float downRatio = 0.10f + (down10 / 1023.0f) * 0.25f;
- // 25% stronger than original knee
- float kneeStrength = (knee10 / 1023.0f) * 0.375f;
- float kneePos = 0.7f;
- pendingParams.upTime_us = onTime_ms * 1000;
- pendingParams.downTime_us = pendingParams.upTime_us * downRatio;
- pendingParams.period_us = 1000000.0f / bps_hz;
- pendingParams.dutyMax = amplitude * 1023;
- pendingParams.dutyWick = wick * 1023;
- pendingParams.kneeTime_us =
- (uint32_t)(pendingParams.upTime_us * kneePos);
- float s1 = 1.0f - kneeStrength;
- float s2 = (1.0f - s1 * kneePos) / (1.0f - kneePos);
- pendingParams.slope1Scale = (uint16_t)(s1 * 256.0f);
- pendingParams.slope2Scale = (uint16_t)(s2 * 256.0f);
- uint32_t totalRise =
- pendingParams.dutyMax - pendingParams.dutyWick;
- uint32_t kneeRise =
- (totalRise * pendingParams.kneeTime_us) /
- pendingParams.upTime_us;
- pendingParams.kneeOffset =
- (kneeRise * pendingParams.slope1Scale) >> 8;
- // -------- FIELD COPY (NO STRUCT ASSIGNMENT) --------
- noInterrupts();
- activeParams.upTime_us = pendingParams.upTime_us;
- activeParams.downTime_us = pendingParams.downTime_us;
- activeParams.period_us = pendingParams.period_us;
- activeParams.dutyMax = pendingParams.dutyMax;
- activeParams.dutyWick = pendingParams.dutyWick;
- activeParams.kneeTime_us = pendingParams.kneeTime_us;
- activeParams.slope1Scale = pendingParams.slope1Scale;
- activeParams.slope2Scale = pendingParams.slope2Scale;
- activeParams.kneeOffset = pendingParams.kneeOffset;
- interrupts();
- // ---------------------------------------------------
- bool pressed = (digitalRead(ONESHOT_PIN) == LOW);
- uint32_t now_ms = millis();
- static bool lastRaw = false;
- if (pressed != lastRaw)
- {
- if ((now_ms - lastButtonEdge_ms) > DEBOUNCE_MS)
- {
- lastButtonEdge_ms = now_ms;
- lastRaw = pressed;
- if (pressed)
- {
- buttonHeld = true;
- noInterrupts();
- if (!burstActive)
- lastBurstTime_us = globalTime_us - activeParams.period_us;
- interrupts();
- }
- else
- {
- buttonHeld = false;
- }
- }
- }
- // ---------- DISPLAY ----------
- uint32_t periodSnapshot;
- uint64_t startSnapshot;
- uint64_t now_us;
- noInterrupts();
- periodSnapshot = measuredPeriod_us;
- startSnapshot = burstStartTime_us;
- now_us = globalTime_us;
- interrupts();
- float hzSnapshot = 0.0f;
- if (periodSnapshot > 0)
- hzSnapshot = 1000000.0f / periodSnapshot;
- float kneePercent = (knee10 / 1023.0f) * 100.0f;
- spr.fillSprite(TFT_BLACK);
- spr.setTextColor(TFT_WHITE);
- spr.setTextSize(2);
- spr.setCursor(0, 0);
- spr.setTextColor(TFT_RED);
- spr.printf("%.1f ms\n", onTime_ms);
- spr.setTextColor(TFT_RED);
- spr.printf("%.1f ms\n", onTime_ms * downRatio);
- spr.setTextColor(TFT_BLUE);
- spr.printf("%.1f Hz\n", hzSnapshot);
- spr.setTextColor(TFT_GREENYELLOW);
- spr.printf("%.0f %%\n", amplitude * 100.0f);
- spr.setTextColor(TFT_WHITE);
- spr.printf("W %.1f %%\n", wickPercent);
- // ----- KNEE % TOP CENTER -----
- spr.setTextColor(TFT_ORANGE);
- int kneeTextX = SCREEN_W / 2 - 10;
- int kneeTextY = 0;
- spr.setCursor(kneeTextX, kneeTextY);
- spr.printf("%.0f %%", kneePercent);
- int graphLeft = 70;
- int graphWidth = SCREEN_W - graphLeft - 5;
- int graphTop = 5;
- int graphHeight = SCREEN_H - 10;
- int bottom = graphTop + graphHeight;
- float maxTime = 25.0f + (25.0f * 0.35f);
- float scale = graphWidth / maxTime;
- int rampUpWidth = onTime_ms * scale;
- int rampDownWidth = (onTime_ms * downRatio) * scale;
- int startX = graphLeft;
- int lastX = startX;
- int lastY = bottom;
- float s1v = 1.0f - kneeStrength;
- float s2v = (1.0f - s1v * kneePos) / (1.0f - kneePos);
- for (int i = 0; i <= rampUpWidth; i++)
- {
- float t = (float)i / rampUpWidth;
- float shaped;
- if (t < kneePos)
- shaped = t * s1v;
- else
- shaped = kneePos * s1v + (t - kneePos) * s2v;
- float env = wick + (amplitude - wick) * shaped;
- int x = startX + i;
- int y = bottom - env * graphHeight;
- spr.drawLine(lastX, lastY, x, y, TFT_GREEN);
- lastX = x;
- lastY = y;
- }
- for (int i = 0; i <= rampDownWidth; i++)
- {
- float t = (float)i / rampDownWidth;
- float env = amplitude * (1.0f - t);
- int x = startX + rampUpWidth + i;
- int y = bottom - env * graphHeight;
- spr.drawLine(lastX, lastY, x, y, TFT_GREEN);
- lastX = x;
- lastY = y;
- }
- bool dotOn = (now_us - startSnapshot) < DOT_PULSE_US;
- if (dotOn)
- spr.fillCircle(SCREEN_W - 12, 12, 6, TFT_RED);
- else
- spr.drawCircle(SCREEN_W - 12, 12, 6, TFT_DARKGREY);
- spr.pushSprite(0, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment