Advertisement
LandoRo

GROK Esp32 HID XYZ axis Bluetooth/usb ssd1306 screen

Mar 23rd, 2025 (edited)
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.35 KB | Gaming | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_ADS1X15.h>
  3. #include <Adafruit_SSD1306.h>
  4. #include <BleGamepad.h>
  5. #include <TinyUSB.h>
  6. #include "driver/usb_phy.h"
  7.  
  8. // OLED Config
  9. #define SCREEN_WIDTH 128
  10. #define SCREEN_HEIGHT 64
  11. #define OLED_RESET -1
  12. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  13.  
  14. // ADS1115 Instance
  15. Adafruit_ADS1115 ads;
  16.  
  17. // BLE Gamepad Instance
  18. BleGamepad bleGamepad("Generic Sim Pedals BLE", "xAI", 100);
  19.  
  20. // TinyUSB HID Descriptor (3-axis joystick)
  21. static const uint8_t hid_report_descriptor[] = {
  22.     TUD_HID_REPORT_DESC_GAMEPAD(HID_REPORT_ID(1), 0, 0, 0, 32767, 32767, 32767)
  23. };
  24.  
  25. // TinyUSB Device Descriptor
  26. static const tusb_desc_device_t usb_device_descriptor = {
  27.     .bLength = 18, .bDescriptorType = TUSB_DESC_DEVICE, .bcdUSB = 0x0200,
  28.     .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, .idVendor = 0xCafe, .idProduct = 0x4001,
  29.     .bcdDevice = 0x0100, .iManufacturer = 0x01, .iProduct = 0x02, .iSerialNumber = 0x03,
  30.     .bNumConfigurations = 0x01
  31. };
  32.  
  33. // USB Strings
  34. static const char *usb_string_descriptor[] = {
  35.     (char[]){0x09, 0x04}, "xAI", "Generic Sim Pedals USB", "123456"
  36. };
  37.  
  38. // TinyUSB Config
  39. static const tinyusb_config_t tusb_cfg = {
  40.     .device_descriptor = &usb_device_descriptor,
  41.     .string_descriptor = usb_string_descriptor,
  42.     .hid_report_descriptor = hid_report_descriptor,
  43.     .hid_report_descriptor_len = sizeof(hid_report_descriptor)
  44. };
  45.  
  46. // Pins
  47. #define SDA_PIN 9
  48. #define SCL_PIN 10
  49. #define USB_DP_PIN 13
  50. #define USB_DM_PIN 12
  51.  
  52. // Calibration (volts)
  53. const float GAS_MIN_VOLT = 0.5f, GAS_MAX_VOLT = 4.5f;
  54. const float BRAKE_MIN_VOLT = 0.5f, BRAKE_MAX_VOLT = 4.5f;
  55. const float CLUTCH_MIN_VOLT = 0.5f, CLUTCH_MAX_VOLT = 4.5f;
  56.  
  57. // Axis Values
  58. int16_t clutchValue, brakeValue, gasValue;
  59.  
  60. void setup() {
  61.   Wire.begin(SDA_PIN, SCL_PIN);
  62.  
  63.   // ADS1115 Init
  64.   if (!ads.begin(0x48)) while (1);
  65.   ads.setGain(GAIN_TWOTHIRDS);
  66.  
  67.   // SSD1306 Init
  68.   if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) while (1);
  69.   display.clearDisplay();
  70.   display.setTextSize(2);
  71.   display.setTextColor(SSD1306_WHITE);
  72.   display.setCursor(0, 0);
  73.   display.println("Sim Pedals");
  74.   display.display();
  75.  
  76.   // USB PHY Config
  77.   usb_phy_config_t phy_conf = {
  78.     .controller = USB_PHY_CTRL_OTG, .target = USB_PHY_TARGET_INT,
  79.     .otg_mode = USB_OTG_MODE_DEVICE, .speed = USB_PHY_SPEED_FULL,
  80.     .gpio_conf = { .dp_pin = USB_DP_PIN, .dm_pin = USB_DM_PIN }
  81.   };
  82.   usb_phy_handle_t phy_handle;
  83.   if (usb_new_phy(&phy_conf, &phy_handle) != ESP_OK) while (1);
  84.  
  85.   // TinyUSB Init
  86.   tinyusb_driver_install(&tusb_cfg);
  87.  
  88.   // BLE Init
  89.   bleGamepad.begin();
  90.   bleGamepad.setAutoReport(false);
  91. }
  92.  
  93. void loop() {
  94.   // Read ADC (inline to reduce function calls)
  95.   float gasVoltage = ads.computeVolts(ads.readADC_SingleEnded(0));
  96.   float brakeVoltage = ads.computeVolts(ads.readADC_SingleEnded(1));
  97.   float clutchVoltage = ads.computeVolts(ads.readADC_SingleEnded(2));
  98.  
  99.   // Map to HID range (0-32767)
  100.   gasValue = constrain(map(gasVoltage * 1000, GAS_MIN_VOLT * 1000, GAS_MAX_VOLT * 1000, 0, 32767), 0, 32767);
  101.   brakeValue = constrain(map(brakeVoltage * 1000, BRAKE_MIN_VOLT * 1000, BRAKE_MAX_VOLT * 1000, 0, 32767), 0, 32767);
  102.   clutchValue = constrain(map(clutchVoltage * 1000, CLUTCH_MIN_VOLT * 1000, CLUTCH_MAX_VOLT * 1000, 0, 32767), 0, 32767);
  103.  
  104.   // USB or BLE Reporting
  105.   if (tud_ready()) {
  106.     uint8_t report[6] = {
  107.       (uint8_t)(gasValue & 0xFF), (uint8_t)(gasValue >> 8),
  108.       (uint8_t)(brakeValue & 0xFF), (uint8_t)(brakeValue >> 8),
  109.       (uint8_t)(clutchValue & 0xFF), (uint8_t)(clutchValue >> 8)
  110.     };
  111.     tud_hid_report(1, report, 6);
  112.   } else if (bleGamepad.isConnected()) {
  113.     bleGamepad.setAxes(gasValue, brakeValue, clutchValue, 0, 0, 0, 0, DPAD_CENTERED);
  114.     bleGamepad.sendReport();
  115.   }
  116.  
  117.   // Brake PSI
  118.   float brakePSI = constrain(((brakeVoltage - 0.5f) / 4.0f) * 100.0f, 0.0f, 100.0f);
  119.  
  120.   // OLED Update
  121.   display.clearDisplay();
  122.   display.setTextSize(1);
  123.   display.setCursor(0, 0);
  124.   if (tud_ready()) display.print("USB");
  125.   else if (bleGamepad.isConnected()) display.print("BT");
  126.   display.setTextSize(2);
  127.   display.setCursor(40, 16);  // Precomputed for "Brake"
  128.   display.print("Brake");
  129.   display.setCursor(28, 40);  // Precomputed for "XX.XX PSI"
  130.   display.print(brakePSI, 2);
  131.   display.print(" PSI");
  132.   display.display();
  133.  
  134.   delay(5);  // Reduced delay for responsiveness
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement