/************************************************************
Cellerator_BenchTool.ino (MAIN SKETCH)
---------------------------------------------------------
This file:
- Declares all hardware pin mappings
- Runs setup() and loop()
- Calls into module init/update + high-level Functions.ino
************************************************************/
// ===== BUILD MODE SELECTION =====
// Uncomment for Wokwi simulation
#define WOKWI_SIMULATION
// Uncomment this when running in Wokwi:
#define WOKWI_SIMULATION
#ifdef WOKWI_SIMULATION
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define WIFI_CHANNEL 6
#endif
// ========= INCLUDES libraries ==========================
// You can adjust these as needed, everything else uses them.
#include <Arduino.h>
#include <Wire.h>
//D4to5
#include <ESP32-TWAI-CAN.hpp> // ESP32Can + CanFrame
#include <math.h> // fabsf()
#include "Cellerator_CANHealth.h"
//D13
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_NeoPixel.h>
#include "D13_neopixel_types.h"
#include <driver/twai.h> // for twai_message_t if needed
#include "BenchState.h"
BenchState benchState; // ✅ This is the ONE global instance
// Tell the compiler about the function and type explicitly:
bool CAN_receiveFrameNonBlocking(CanFrame &outFrame);
// ========= GLOBAL PIN DEFINITIONS ==========================
// You can adjust these as needed, everything else uses them.
constexpr uint8_t PIN_D2_LED = 2;
constexpr uint8_t PIN_D5_CAN_TX = 5;
constexpr uint8_t PIN_D4_CAN_RX = 4;
constexpr uint8_t PIN_A34_CANH_SENSE = 34;
constexpr uint8_t PIN_A35_CANL_SENSE = 35;
constexpr uint8_t PIN_D25_BUZZER = 25;
constexpr uint8_t PIN_D14_BUTTON = 14;
constexpr uint8_t PIN_D13_NEOPIXEL = 13;
constexpr uint8_t PIN_D2_STATUS_LED = 2;
constexpr uint8_t PIN_D16_LED = 16;
constexpr uint8_t PIN_D21_OLED_SDA = 21;
constexpr uint8_t PIN_D22_OLED_SCL = 22;
// ========= FORWARD DECLARATIONS FOR MODULE INIT/UPDATE ====
// (These come from your other .ino files / tabs)
// D2_led.ino
void D2_led_init();
void D2_led_update();
void D2_led_setEnabled(bool enabled);
// D25_buzzer.ino
void D25_buzzer_init();
void D25_buzzer_pattern(uint8_t patternId);
// D13_neopixel.ino
void D13_neopixel_init();
void D13_neopixel_update();
void D13_neopixel_setColor(uint8_t r, uint8_t g, uint8_t b);
void D13_neopixel_setMode(D13_NEOPIXEL_MODE mode);
// D14_button.ino
void D14_button_init();
void D14_button_update();
// D21to22_OLED.ino
void D21to22_OLED_init();
void D21to22_OLED_update(); // if you add one, optional
void D21to22_OLED_showStatus(const String &title,
const String &status,
const String &detail = "",
const String &extra = "");
// D4to5_CAN_34to35_Sense.ino
void D4to5_CAN_init();
void D34to35_CANsense_init();
void D34to35_CANsense_sample();
// Wifi_Prefs_WebServer.ino
void Wifi_Prefs_loadOrConfigure();
void Wifi_WebServer_init();
void Wifi_WebServer_update();
// BenchTool_Functions.ino (we’ll define below)
void BenchTool_init();
void BenchTool_update();
// ========= SETUP & LOOP ====================================
void setup() {
Serial.begin(115200);
delay(200);
// 1) Load or configure WiFi prefs (blocks on first boot)
Wifi_Prefs_loadOrConfigure();
// 2) Initialize hardware modules
D2_led_init();
D25_buzzer_init();
D13_neopixel_init();
D14_button_init();
D21to22_OLED_init();
D4to5_CAN_init();
D34to35_CANsense_init();
// 3) Initialize BenchTool high-level logic (modes, self-test)
BenchTool_init();
// 4) Start WiFi + WebServer (after benchState is ready)
Wifi_WebServer_init();
}
void loop() {
// Per-module updates
D2_led_update();
D14_button_update();
D13_neopixel_update();
Wifi_WebServer_update();
// --- DEBUG BLINK BLOCK ---
static unsigned long lastToggleMs = 0;
static bool ledOn = false;
unsigned long now = millis();
if (now - lastToggleMs >= 500) { // toggle every 500 ms
lastToggleMs = now;
ledOn = !ledOn;
D2_led_setEnabled(ledOn); // 👈 use your existing function
}
// --- END DEBUG BLOCK ---
// Sample bus voltages periodically
D34to35_CANsense_sample();
// D34to35_CANsense_debugPrint();
// // Try to receive CAN frames
// CanFrame frame;
// if (CAN_receiveFrameNonBlocking(frame)) {
// Serial.print("Got frame ID=0x");
// Serial.println(frame.identifier, HEX);
// }
// High-level behavior (modes, self-test trigger, CAN parsing, etc.)
BenchTool_update();
}Voltage Sense Circuit