i'm using a STM32f blue pill, running an arduino sketch to read lever positions in a train driving simulator controller i am building.
The train simulator (TSW2) does not accept standard joystick inputs, but will work with a proprietary device called a 'RailDriver'
Which someone has written a '.dll' which replaces the standard one that allows a real RailDriver to be used in the sim,
so you can make your own levers, and connect the potentiometers to an arduino board.
The arduino sketch simply reads the potentiometers positions and sends it to the simulator over serial in the format a RailDriver would send.
However it only reads the 7 potentiometer positions, and none of the buttons / switches.
So i thought i'd just add in abit of USB keyboard code to read the buttons i have on my controller, and send the keyboard strokes that operate the function in the sim.
Only it dosent work as simple as that it seems, as soon as i put any USB HID keyboard code in the serial sketch, the blue pill is seen as a HID Keyboard in windows, and the serial output doesn't work.
Is there a way to get both USB HID keyboard and serial output working together?
This is the RailDriver serial code:
Code: Select all
/*
Custom Hardware for use with Train Sim World 2 using my custom pieHid64.dll
By Skaako (Michael Huggins 2021)
*/
int Reverser = PA3;
int Power = PA4;
int TrainBrake = PA0;
int LocoBrake = PA2;
int BailOff = PA5; // Sets pins Potentiometers are connected to
int Wipers = PA6;
int Lights = PA7;
int val[7] = {0}; // variable to store the value read
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop() {
val[0] = analogRead(Reverser); // Read value of potentiometer on Reverser Switch
val[0] = map(val[0], 0, 4095, 0, 255); // Map 12 bit STM board ADC to 8 bit output expected for RailDriver
if (val[0] == 256){val[0] = 254;} // i assume this is to prevent errors if a potentiometer read is over 8 bits?
delay(10);
val[1] = analogRead(Power); // Read value of pot on power lever
val[1] = map(val[1], 0, 4095, 255, 0); // 12 to 8 bit mapping, and reverse the lever direction.
if (val[1] == 256){val[1] = 254;}
delay(10);
val[2] = analogRead(TrainBrake);
val[2] = map(val[2], 0, 4095, 0, 255);
if (val[2] == 256){val[2] = 254;}
delay(10);
val[3] = analogRead(LocoBrake);
val[3] = map(val[3], 0, 4095, 0, 255);
if (val[3] == 256){val[3] = 254;}
delay(10);
val[4] = analogRead(BailOff);
val[4] = map(val[4], 0, 4095, 0, 255);
if (val[4] == 256){val[4] = 254;}
delay(10);
val[5] = analogRead(Wipers);
val[5] = map(val[5], 0, 4095, 0, 255);
if (val[5] == 256){val[5] = 254;}
delay(10);
val[6] = analogRead(Lights);
val[6] = map(val[6], 0, 4095, 0, 255);
if (val[6] == 256){val[6] = 254;}
delay(10);
// Start of string output
Serial.print("Output: "); // Write 'Output' at beginning of every line
// Padding for numbers
for(int i = 0; i < 7; i++){
if(val[i] < 10){
Serial.print(" ");
} else if(val[i] < 100){
Serial.print(" ");
}
Serial.print(val[i]);
Serial.print(" ");
}
Serial.println(""); // Write potentiometer values one after the other
}
And this is the USB keyboard code i am playing with:
Code: Select all
#include <USBComposite.h>
USBHID HID;
HIDKeyboard Keyboard(HID); // Stuff needed to get USB keyboard started
int sifa = PB1; // Sifa button connected to this pin
int previousButtonState = HIGH; // Ensure button is off to start with
void setup() {
HID.begin(HID_KEYBOARD);
Keyboard.begin(); // Starts the HID Keyboard stuff
// pinMode(sifa, INPUT); // works without this, is it needed ?
}
void loop() {
int buttonState = digitalRead(sifa); //check the state of the button
if (buttonState == LOW && previousButtonState == HIGH) { // If button is pressed
Keyboard.press('q'); // Send letter 'q'
delay(50); // Debouncing?
}
if (buttonState == HIGH && previousButtonState == LOW) { // When button released
Keyboard.release('q'); // Stop sending letter 'q'
delay(50);
}
previousButtonState = buttonState; // Go back to waiting for button press
}