Advertisement
LandoRo

Grok Sim pedal ex 1

Mar 13th, 2025
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_ADS1X15.h>
  3. #include <Adafruit_SSD1306.h>
  4. #include <BleGamepad.h>
  5.  
  6. // Display settings
  7. #define SCREEN_WIDTH 128
  8. #define SCREEN_HEIGHT 32
  9. #define OLED_RESET -1
  10. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  11.  
  12. // ADS1115 instance
  13. Adafruit_ADS1115 ads;
  14.  
  15. // BLE Gamepad instance
  16. BleGamepad bleGamepad("Generic Sim Pedals", "xAI", 100);
  17.  
  18. // Pin definitions for I2C (adjust if needed)
  19. #define I2C_SDA 21
  20. #define I2C_SCL 22
  21.  
  22. // Joystick axis ranges
  23. #define AXIS_MIN 0
  24. #define AXIS_MAX 32767  // 15-bit resolution for HID
  25.  
  26. void setup() {
  27.   // Initialize Serial for debugging
  28.   Serial.begin(115200);
  29.  
  30.   // Initialize I2C
  31.   Wire.begin(I2C_SDA, I2C_SCL);
  32.  
  33.   // Initialize ADS1115
  34.   if (!ads.begin()) {
  35.     Serial.println("Failed to initialize ADS1115");
  36.     while (1);
  37.   }
  38.  
  39.   // Initialize OLED display
  40.   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
  41.     Serial.println(F("SSD1306 allocation failed"));
  42.     for(;;);
  43.   }
  44.  
  45.   // Clear display
  46.   display.clearDisplay();
  47.   display.setTextSize(1);
  48.   display.setTextColor(SSD1306_WHITE);
  49.   display.setCursor(0,0);
  50.   display.println("Sim Pedals");
  51.   display.display();
  52.  
  53.   // Initialize BLE Gamepad
  54.   bleGamepad.begin();
  55.   bleGamepad.setAutoReport(false);  // We'll manually send reports
  56. }
  57.  
  58. void loop() {
  59.   if (bleGamepad.isConnected()) {
  60.     // Read ADC values (0-65535 range)
  61.     int16_t clutchRaw = ads.readADC_SingleEnded(0);  // A0
  62.     int16_t brakeRaw = ads.readADC_SingleEnded(1);   // A1
  63.     int16_t gasRaw = ads.readADC_SingleEnded(2);     // A2
  64.    
  65.     // Convert to voltage (assuming 4.096V reference)
  66.     float clutchVoltage = ads.computeVolts(clutchRaw);
  67.     float brakeVoltage = ads.computeVolts(brakeRaw);
  68.     float gasVoltage = ads.computeVolts(gasRaw);
  69.    
  70.     // Map voltages to HID range (0-32767)
  71.     int32_t clutch = mapVoltageToAxis(clutchVoltage);
  72.     int32_t brake = mapVoltageToAxis(brakeVoltage);
  73.     int32_t gas = mapVoltageToAxis(gasVoltage);
  74.    
  75.     // Calculate PSI for brake (0.5V-4.5V -> 0-100 PSI)
  76.     float brakePSI = mapVoltageToPSI(brakeVoltage);
  77.    
  78.     // Update display
  79.     updateDisplay(brakePSI);
  80.    
  81.     // Set joystick axes (X=Gas, Y=Brake, Z=Clutch)
  82.     bleGamepad.setX(gas);
  83.     bleGamepad.setY(brake);
  84.     bleGamepad.setZ(clutch);
  85.    
  86.     // Send the report
  87.     bleGamepad.sendReport();
  88.    
  89.     delay(10);  // Adjust report rate as needed
  90.   }
  91. }
  92.  
  93. int32_t mapVoltageToAxis(float voltage) {
  94.   // Map 0.5V-4.5V to 0-32767
  95.   voltage = constrain(voltage, 0.5, 4.5);
  96.   return map(voltage * 1000, 500, 4500, AXIS_MIN, AXIS_MAX);
  97. }
  98.  
  99. float mapVoltageToPSI(float voltage) {
  100.   // Map 0.5V-4.5V to 0-100 PSI
  101.   voltage = constrain(voltage, 0.5, 4.5);
  102.   return map(voltage * 1000, 500, 4500, 0, 100);
  103. }
  104.  
  105. void updateDisplay(float brakePSI) {
  106.   display.clearDisplay();
  107.   display.setCursor(0,0);
  108.   display.println("Sim Pedals");
  109.   display.setCursor(0,10);
  110.   display.print("Brake: ");
  111.   display.print(brakePSI, 1);
  112.   display.println(" PSI");
  113.   display.display();
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement