Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <Adafruit_ADS1X15.h>
- #include <Adafruit_SSD1306.h>
- #include <BleGamepad.h>
- #include <TinyUSB.h>
- #include "driver/usb_phy.h"
- // OLED Config
- #define SCREEN_WIDTH 128
- #define SCREEN_HEIGHT 64
- #define OLED_RESET -1
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- // ADS1115 Instance
- Adafruit_ADS1115 ads;
- // BLE Gamepad Instance
- BleGamepad bleGamepad("Generic Sim Pedals BLE", "xAI", 100);
- // TinyUSB HID Descriptor (3-axis joystick)
- static const uint8_t hid_report_descriptor[] = {
- TUD_HID_REPORT_DESC_GAMEPAD(HID_REPORT_ID(1), 0, 0, 0, 32767, 32767, 32767)
- };
- // TinyUSB Device Descriptor
- static const tusb_desc_device_t usb_device_descriptor = {
- .bLength = 18, .bDescriptorType = TUSB_DESC_DEVICE, .bcdUSB = 0x0200,
- .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, .idVendor = 0xCafe, .idProduct = 0x4001,
- .bcdDevice = 0x0100, .iManufacturer = 0x01, .iProduct = 0x02, .iSerialNumber = 0x03,
- .bNumConfigurations = 0x01
- };
- // USB Strings
- static const char *usb_string_descriptor[] = {
- (char[]){0x09, 0x04}, "xAI", "Generic Sim Pedals USB", "123456"
- };
- // TinyUSB Config
- static const tinyusb_config_t tusb_cfg = {
- .device_descriptor = &usb_device_descriptor,
- .string_descriptor = usb_string_descriptor,
- .hid_report_descriptor = hid_report_descriptor,
- .hid_report_descriptor_len = sizeof(hid_report_descriptor)
- };
- // Pins
- #define SDA_PIN 9
- #define SCL_PIN 10
- #define USB_DP_PIN 13
- #define USB_DM_PIN 12
- // Calibration (volts)
- const float GAS_MIN_VOLT = 0.5f, GAS_MAX_VOLT = 4.5f;
- const float BRAKE_MIN_VOLT = 0.5f, BRAKE_MAX_VOLT = 4.5f;
- const float CLUTCH_MIN_VOLT = 0.5f, CLUTCH_MAX_VOLT = 4.5f;
- // Axis Values
- int16_t clutchValue, brakeValue, gasValue;
- void setup() {
- Wire.begin(SDA_PIN, SCL_PIN);
- // ADS1115 Init
- if (!ads.begin(0x48)) while (1);
- ads.setGain(GAIN_TWOTHIRDS);
- // SSD1306 Init
- if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) while (1);
- display.clearDisplay();
- display.setTextSize(2);
- display.setTextColor(SSD1306_WHITE);
- display.setCursor(0, 0);
- display.println("Sim Pedals");
- display.display();
- // USB PHY Config
- usb_phy_config_t phy_conf = {
- .controller = USB_PHY_CTRL_OTG, .target = USB_PHY_TARGET_INT,
- .otg_mode = USB_OTG_MODE_DEVICE, .speed = USB_PHY_SPEED_FULL,
- .gpio_conf = { .dp_pin = USB_DP_PIN, .dm_pin = USB_DM_PIN }
- };
- usb_phy_handle_t phy_handle;
- if (usb_new_phy(&phy_conf, &phy_handle) != ESP_OK) while (1);
- // TinyUSB Init
- tinyusb_driver_install(&tusb_cfg);
- // BLE Init
- bleGamepad.begin();
- bleGamepad.setAutoReport(false);
- }
- void loop() {
- // Read ADC (inline to reduce function calls)
- float gasVoltage = ads.computeVolts(ads.readADC_SingleEnded(0));
- float brakeVoltage = ads.computeVolts(ads.readADC_SingleEnded(1));
- float clutchVoltage = ads.computeVolts(ads.readADC_SingleEnded(2));
- // Map to HID range (0-32767)
- gasValue = constrain(map(gasVoltage * 1000, GAS_MIN_VOLT * 1000, GAS_MAX_VOLT * 1000, 0, 32767), 0, 32767);
- brakeValue = constrain(map(brakeVoltage * 1000, BRAKE_MIN_VOLT * 1000, BRAKE_MAX_VOLT * 1000, 0, 32767), 0, 32767);
- clutchValue = constrain(map(clutchVoltage * 1000, CLUTCH_MIN_VOLT * 1000, CLUTCH_MAX_VOLT * 1000, 0, 32767), 0, 32767);
- // USB or BLE Reporting
- if (tud_ready()) {
- uint8_t report[6] = {
- (uint8_t)(gasValue & 0xFF), (uint8_t)(gasValue >> 8),
- (uint8_t)(brakeValue & 0xFF), (uint8_t)(brakeValue >> 8),
- (uint8_t)(clutchValue & 0xFF), (uint8_t)(clutchValue >> 8)
- };
- tud_hid_report(1, report, 6);
- } else if (bleGamepad.isConnected()) {
- bleGamepad.setAxes(gasValue, brakeValue, clutchValue, 0, 0, 0, 0, DPAD_CENTERED);
- bleGamepad.sendReport();
- }
- // Brake PSI
- float brakePSI = constrain(((brakeVoltage - 0.5f) / 4.0f) * 100.0f, 0.0f, 100.0f);
- // OLED Update
- display.clearDisplay();
- display.setTextSize(1);
- display.setCursor(0, 0);
- if (tud_ready()) display.print("USB");
- else if (bleGamepad.isConnected()) display.print("BT");
- display.setTextSize(2);
- display.setCursor(40, 16); // Precomputed for "Brake"
- display.print("Brake");
- display.setCursor(28, 40); // Precomputed for "XX.XX PSI"
- display.print(brakePSI, 2);
- display.print(" PSI");
- display.display();
- delay(5); // Reduced delay for responsiveness
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement