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>
- // Display settings
- #define SCREEN_WIDTH 128
- #define SCREEN_HEIGHT 32
- #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", "xAI", 100);
- // Pin definitions for I2C (adjust if needed)
- #define I2C_SDA 21
- #define I2C_SCL 22
- // Joystick axis ranges
- #define AXIS_MIN 0
- #define AXIS_MAX 32767 // 15-bit resolution for HID
- void setup() {
- // Initialize Serial for debugging
- Serial.begin(115200);
- // Initialize I2C
- Wire.begin(I2C_SDA, I2C_SCL);
- // Initialize ADS1115
- if (!ads.begin()) {
- Serial.println("Failed to initialize ADS1115");
- while (1);
- }
- // Initialize OLED display
- if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
- Serial.println(F("SSD1306 allocation failed"));
- for(;;);
- }
- // Clear display
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(SSD1306_WHITE);
- display.setCursor(0,0);
- display.println("Sim Pedals");
- display.display();
- // Initialize BLE Gamepad
- bleGamepad.begin();
- bleGamepad.setAutoReport(false); // We'll manually send reports
- }
- void loop() {
- if (bleGamepad.isConnected()) {
- // Read ADC values (0-65535 range)
- int16_t clutchRaw = ads.readADC_SingleEnded(0); // A0
- int16_t brakeRaw = ads.readADC_SingleEnded(1); // A1
- int16_t gasRaw = ads.readADC_SingleEnded(2); // A2
- // Convert to voltage (assuming 4.096V reference)
- float clutchVoltage = ads.computeVolts(clutchRaw);
- float brakeVoltage = ads.computeVolts(brakeRaw);
- float gasVoltage = ads.computeVolts(gasRaw);
- // Map voltages to HID range (0-32767)
- int32_t clutch = mapVoltageToAxis(clutchVoltage);
- int32_t brake = mapVoltageToAxis(brakeVoltage);
- int32_t gas = mapVoltageToAxis(gasVoltage);
- // Calculate PSI for brake (0.5V-4.5V -> 0-100 PSI)
- float brakePSI = mapVoltageToPSI(brakeVoltage);
- // Update display
- updateDisplay(brakePSI);
- // Set joystick axes (X=Gas, Y=Brake, Z=Clutch)
- bleGamepad.setX(gas);
- bleGamepad.setY(brake);
- bleGamepad.setZ(clutch);
- // Send the report
- bleGamepad.sendReport();
- delay(10); // Adjust report rate as needed
- }
- }
- int32_t mapVoltageToAxis(float voltage) {
- // Map 0.5V-4.5V to 0-32767
- voltage = constrain(voltage, 0.5, 4.5);
- return map(voltage * 1000, 500, 4500, AXIS_MIN, AXIS_MAX);
- }
- float mapVoltageToPSI(float voltage) {
- // Map 0.5V-4.5V to 0-100 PSI
- voltage = constrain(voltage, 0.5, 4.5);
- return map(voltage * 1000, 500, 4500, 0, 100);
- }
- void updateDisplay(float brakePSI) {
- display.clearDisplay();
- display.setCursor(0,0);
- display.println("Sim Pedals");
- display.setCursor(0,10);
- display.print("Brake: ");
- display.print(brakePSI, 1);
- display.println(" PSI");
- display.display();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement