Guest User

QCW Ramp Gen V1 ESP32 Arduino IDE

a guest
Feb 23rd, 2026
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.67 KB | None | 0 0
  1. 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))"
  2.  
  3.  
  4.  
  5.  
  6.  
  7. 📘 QCW Ramp Generator With Display — V1
  8.  
  9. (ESP32 + ST7789 + Hardware PWM Ramp Engine)
  10.  
  11. 🔧 Required Arduino IDE Setup
  12.  
  13. Board Package:
  14.  
  15. ESP32 by Espressif
  16.  
  17. Version: 2.0.14 (important — 3.x timer API differs)
  18.  
  19. Libraries Required:
  20.  
  21. TFT_eSPI (Bodmer)
  22.  
  23. In User_Setup.h for TFT_eSPI you must have your working ST7789 config pinout proper so the screen works in your sketch
  24.  
  25. No other external libraries required.
  26.  
  27.  
  28. Screen displays a relative representation of the real envelope.
  29. Actual BPS is displayed. Actual on-time of ramp envelope.
  30. Total Enable on time is a bit longer.
  31.  
  32. 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.
  33.  
  34. There is a ramp out and an enable out.
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. /*
  42. =========================================================
  43. QCW Ramp Generator – Clean Engine Architecture
  44. Engine always running
  45. Button = gate + phase align when idle
  46. Hold = continuous at BPS
  47. Tap = single burst
  48. No mid-ramp restart
  49. Wick scaled relative to amplitude
  50. Knee added (stable, matched to display)
  51. =========================================================
  52. */
  53.  
  54. #include <Arduino.h>
  55. #include <TFT_eSPI.h>
  56.  
  57. TFT_eSPI tft = TFT_eSPI();
  58. TFT_eSprite spr = TFT_eSprite(&tft);
  59.  
  60. #define TFT_BL 32
  61. #define ONESHOT_PIN 26
  62.  
  63. #define PWM_PIN 14
  64. #define ENABLE_PIN 13
  65. #define BURST_LED_PIN 22
  66.  
  67. #define AMP_PIN 36
  68. #define PULSE_PIN 39
  69. #define DOWNSLOPE_PIN 34
  70. #define BPS_PIN 35
  71. #define WICK_PIN 33
  72. #define KNEE_PIN 25 // Electrically GPIO25
  73.  
  74. #define PWM_FREQ 31250
  75. #define PWM_RESOLUTION 10
  76. #define PWM_CHANNEL 0
  77.  
  78. #define TIMER_US 100
  79. #define WICK_TIME_US 2000
  80. #define DOT_PULSE_US 40000
  81. #define DEBOUNCE_MS 30
  82.  
  83. hw_timer_t *timer100us = NULL;
  84.  
  85. struct RampParams
  86. {
  87. uint32_t upTime_us;
  88. uint32_t downTime_us;
  89. uint32_t period_us;
  90. uint16_t dutyMax;
  91. uint16_t dutyWick;
  92.  
  93. uint32_t kneeTime_us;
  94. uint16_t slope1Scale;
  95. uint16_t slope2Scale;
  96. uint32_t kneeOffset;
  97. };
  98.  
  99. volatile RampParams activeParams;
  100. RampParams pendingParams;
  101.  
  102. volatile uint64_t globalTime_us = 0;
  103. volatile uint64_t burstStartTime_us = 0;
  104. volatile uint64_t lastBurstTime_us = 0;
  105. volatile bool burstActive = false;
  106. volatile uint32_t measuredPeriod_us = 0;
  107.  
  108. int SCREEN_W;
  109. int SCREEN_H;
  110.  
  111. uint32_t lastButtonEdge_ms = 0;
  112. bool buttonHeld = false;
  113.  
  114. int read10bit(int pin)
  115. {
  116. long sum = 0;
  117. for (int i = 0; i < 4; i++)
  118. sum += analogRead(pin);
  119. return (sum >> 2) >> 2;
  120. }
  121.  
  122. // ================= ISR =================
  123. void IRAM_ATTR onTimer()
  124. {
  125. globalTime_us += TIMER_US;
  126. uint64_t now = globalTime_us;
  127.  
  128. uint32_t envelopeDuration =
  129. WICK_TIME_US +
  130. activeParams.upTime_us +
  131. activeParams.downTime_us;
  132.  
  133. if (!burstActive)
  134. {
  135. if ((now - lastBurstTime_us) >= activeParams.period_us)
  136. {
  137. burstStartTime_us = now;
  138.  
  139. if (lastBurstTime_us != 0)
  140. measuredPeriod_us = now - lastBurstTime_us;
  141.  
  142. lastBurstTime_us = now;
  143. burstActive = true;
  144. }
  145. }
  146.  
  147. uint16_t duty = 0;
  148.  
  149. if (burstActive)
  150. {
  151. uint64_t T = now - burstStartTime_us;
  152.  
  153. if (T <= WICK_TIME_US)
  154. {
  155. duty = activeParams.dutyWick;
  156. }
  157. else if (T <= (WICK_TIME_US + activeParams.upTime_us))
  158. {
  159. uint32_t t = T - WICK_TIME_US;
  160.  
  161. uint32_t totalRise =
  162. activeParams.dutyMax - activeParams.dutyWick;
  163.  
  164. uint32_t linearRise =
  165. (totalRise * t) / activeParams.upTime_us;
  166.  
  167. if (t < activeParams.kneeTime_us)
  168. {
  169. linearRise = (linearRise * activeParams.slope1Scale) >> 8;
  170. }
  171. else
  172. {
  173. uint32_t t2 = t - activeParams.kneeTime_us;
  174.  
  175. uint32_t rise2 =
  176. (totalRise * t2) / activeParams.upTime_us;
  177.  
  178. rise2 = (rise2 * activeParams.slope2Scale) >> 8;
  179.  
  180. linearRise = activeParams.kneeOffset + rise2;
  181. }
  182.  
  183. duty = activeParams.dutyWick + linearRise;
  184. }
  185. else if (T <= envelopeDuration)
  186. {
  187. uint32_t t = T - (WICK_TIME_US + activeParams.upTime_us);
  188.  
  189. duty = activeParams.dutyMax -
  190. ((uint32_t)activeParams.dutyMax * t) /
  191. activeParams.downTime_us;
  192. }
  193. else
  194. {
  195. burstActive = false;
  196. duty = 0;
  197. }
  198. }
  199.  
  200. if (burstActive && buttonHeld)
  201. {
  202. ledcWrite(PWM_CHANNEL, duty);
  203. digitalWrite(ENABLE_PIN, HIGH);
  204. digitalWrite(BURST_LED_PIN, HIGH);
  205. }
  206. else
  207. {
  208. ledcWrite(PWM_CHANNEL, 0);
  209. digitalWrite(ENABLE_PIN, LOW);
  210. digitalWrite(BURST_LED_PIN, LOW);
  211. }
  212. }
  213. // =======================================
  214.  
  215. void setup()
  216. {
  217. pinMode(TFT_BL, OUTPUT);
  218. digitalWrite(TFT_BL, HIGH);
  219.  
  220. pinMode(ONESHOT_PIN, INPUT_PULLUP);
  221. pinMode(ENABLE_PIN, OUTPUT);
  222. pinMode(BURST_LED_PIN, OUTPUT);
  223.  
  224. ledcSetup(PWM_CHANNEL, PWM_FREQ, PWM_RESOLUTION);
  225. ledcAttachPin(PWM_PIN, PWM_CHANNEL);
  226.  
  227. timer100us = timerBegin(0, 80, true);
  228. timerAttachInterrupt(timer100us, &onTimer, true);
  229. timerAlarmWrite(timer100us, TIMER_US, true);
  230. timerAlarmEnable(timer100us);
  231.  
  232. tft.init();
  233. tft.setRotation(3);
  234.  
  235. SCREEN_W = tft.width();
  236. SCREEN_H = tft.height();
  237.  
  238. spr.setColorDepth(16);
  239. spr.createSprite(SCREEN_W, SCREEN_H);
  240. }
  241.  
  242. void loop()
  243. {
  244. int amp10 = read10bit(AMP_PIN);
  245. int pulse10 = read10bit(PULSE_PIN);
  246. int wick10 = read10bit(WICK_PIN);
  247. int bps10 = read10bit(BPS_PIN);
  248. int down10 = read10bit(DOWNSLOPE_PIN);
  249. int knee10 = read10bit(KNEE_PIN);
  250.  
  251. float onTime_ms = 5.0f + (pulse10 / 1023.0f) * 20.0f;
  252. float amplitude = amp10 / 1023.0f;
  253.  
  254. float wickRatio = (wick10 / 1023.0f) * 0.2f;
  255. float wick = amplitude * wickRatio;
  256. if (wick > amplitude)
  257. wick = amplitude;
  258. float wickPercent = wickRatio * 100.0f;
  259.  
  260. float bps_hz = 1.0f + (bps10 / 1023.0f) * 14.0f;
  261. float downRatio = 0.10f + (down10 / 1023.0f) * 0.25f;
  262.  
  263. // 25% stronger than original knee
  264. float kneeStrength = (knee10 / 1023.0f) * 0.375f;
  265. float kneePos = 0.7f;
  266.  
  267. pendingParams.upTime_us = onTime_ms * 1000;
  268. pendingParams.downTime_us = pendingParams.upTime_us * downRatio;
  269. pendingParams.period_us = 1000000.0f / bps_hz;
  270. pendingParams.dutyMax = amplitude * 1023;
  271. pendingParams.dutyWick = wick * 1023;
  272.  
  273. pendingParams.kneeTime_us =
  274. (uint32_t)(pendingParams.upTime_us * kneePos);
  275.  
  276. float s1 = 1.0f - kneeStrength;
  277. float s2 = (1.0f - s1 * kneePos) / (1.0f - kneePos);
  278.  
  279. pendingParams.slope1Scale = (uint16_t)(s1 * 256.0f);
  280. pendingParams.slope2Scale = (uint16_t)(s2 * 256.0f);
  281.  
  282. uint32_t totalRise =
  283. pendingParams.dutyMax - pendingParams.dutyWick;
  284.  
  285. uint32_t kneeRise =
  286. (totalRise * pendingParams.kneeTime_us) /
  287. pendingParams.upTime_us;
  288.  
  289. pendingParams.kneeOffset =
  290. (kneeRise * pendingParams.slope1Scale) >> 8;
  291.  
  292. // -------- FIELD COPY (NO STRUCT ASSIGNMENT) --------
  293. noInterrupts();
  294. activeParams.upTime_us = pendingParams.upTime_us;
  295. activeParams.downTime_us = pendingParams.downTime_us;
  296. activeParams.period_us = pendingParams.period_us;
  297. activeParams.dutyMax = pendingParams.dutyMax;
  298. activeParams.dutyWick = pendingParams.dutyWick;
  299. activeParams.kneeTime_us = pendingParams.kneeTime_us;
  300. activeParams.slope1Scale = pendingParams.slope1Scale;
  301. activeParams.slope2Scale = pendingParams.slope2Scale;
  302. activeParams.kneeOffset = pendingParams.kneeOffset;
  303. interrupts();
  304. // ---------------------------------------------------
  305.  
  306. bool pressed = (digitalRead(ONESHOT_PIN) == LOW);
  307. uint32_t now_ms = millis();
  308. static bool lastRaw = false;
  309.  
  310. if (pressed != lastRaw)
  311. {
  312. if ((now_ms - lastButtonEdge_ms) > DEBOUNCE_MS)
  313. {
  314. lastButtonEdge_ms = now_ms;
  315. lastRaw = pressed;
  316.  
  317. if (pressed)
  318. {
  319. buttonHeld = true;
  320. noInterrupts();
  321. if (!burstActive)
  322. lastBurstTime_us = globalTime_us - activeParams.period_us;
  323. interrupts();
  324. }
  325. else
  326. {
  327. buttonHeld = false;
  328. }
  329. }
  330. }
  331.  
  332. // ---------- DISPLAY ----------
  333.  
  334. uint32_t periodSnapshot;
  335. uint64_t startSnapshot;
  336. uint64_t now_us;
  337.  
  338. noInterrupts();
  339. periodSnapshot = measuredPeriod_us;
  340. startSnapshot = burstStartTime_us;
  341. now_us = globalTime_us;
  342. interrupts();
  343.  
  344. float hzSnapshot = 0.0f;
  345. if (periodSnapshot > 0)
  346. hzSnapshot = 1000000.0f / periodSnapshot;
  347. float kneePercent = (knee10 / 1023.0f) * 100.0f;
  348.  
  349. spr.fillSprite(TFT_BLACK);
  350. spr.setTextColor(TFT_WHITE);
  351. spr.setTextSize(2);
  352.  
  353. spr.setCursor(0, 0);
  354.  
  355. spr.setTextColor(TFT_RED);
  356. spr.printf("%.1f ms\n", onTime_ms);
  357.  
  358. spr.setTextColor(TFT_RED);
  359. spr.printf("%.1f ms\n", onTime_ms * downRatio);
  360.  
  361. spr.setTextColor(TFT_BLUE);
  362. spr.printf("%.1f Hz\n", hzSnapshot);
  363.  
  364. spr.setTextColor(TFT_GREENYELLOW);
  365. spr.printf("%.0f %%\n", amplitude * 100.0f);
  366.  
  367.  
  368. spr.setTextColor(TFT_WHITE);
  369. spr.printf("W %.1f %%\n", wickPercent);
  370. // ----- KNEE % TOP CENTER -----
  371. spr.setTextColor(TFT_ORANGE);
  372.  
  373. int kneeTextX = SCREEN_W / 2 - 10;
  374. int kneeTextY = 0;
  375.  
  376. spr.setCursor(kneeTextX, kneeTextY);
  377. spr.printf("%.0f %%", kneePercent);
  378.  
  379. int graphLeft = 70;
  380. int graphWidth = SCREEN_W - graphLeft - 5;
  381. int graphTop = 5;
  382. int graphHeight = SCREEN_H - 10;
  383. int bottom = graphTop + graphHeight;
  384.  
  385. float maxTime = 25.0f + (25.0f * 0.35f);
  386. float scale = graphWidth / maxTime;
  387.  
  388. int rampUpWidth = onTime_ms * scale;
  389. int rampDownWidth = (onTime_ms * downRatio) * scale;
  390.  
  391. int startX = graphLeft;
  392. int lastX = startX;
  393. int lastY = bottom;
  394.  
  395. float s1v = 1.0f - kneeStrength;
  396. float s2v = (1.0f - s1v * kneePos) / (1.0f - kneePos);
  397.  
  398. for (int i = 0; i <= rampUpWidth; i++)
  399. {
  400. float t = (float)i / rampUpWidth;
  401. float shaped;
  402.  
  403. if (t < kneePos)
  404. shaped = t * s1v;
  405. else
  406. shaped = kneePos * s1v + (t - kneePos) * s2v;
  407.  
  408. float env = wick + (amplitude - wick) * shaped;
  409.  
  410. int x = startX + i;
  411. int y = bottom - env * graphHeight;
  412.  
  413. spr.drawLine(lastX, lastY, x, y, TFT_GREEN);
  414. lastX = x;
  415. lastY = y;
  416. }
  417.  
  418. for (int i = 0; i <= rampDownWidth; i++)
  419. {
  420. float t = (float)i / rampDownWidth;
  421. float env = amplitude * (1.0f - t);
  422.  
  423. int x = startX + rampUpWidth + i;
  424. int y = bottom - env * graphHeight;
  425.  
  426. spr.drawLine(lastX, lastY, x, y, TFT_GREEN);
  427. lastX = x;
  428. lastY = y;
  429. }
  430.  
  431. bool dotOn = (now_us - startSnapshot) < DOT_PULSE_US;
  432.  
  433. if (dotOn)
  434. spr.fillCircle(SCREEN_W - 12, 12, 6, TFT_RED);
  435. else
  436. spr.drawCircle(SCREEN_W - 12, 12, 6, TFT_DARKGREY);
  437.  
  438. spr.pushSprite(0, 0);
  439. }
Advertisement
Add Comment
Please, Sign In to add comment