OP's code:
#!python
import pygame
import serial
import time
# Define some colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
# This is a simple class that will help us print to the screen
# It has nothing to do with the joysticks, just outputing the
# information.
class TextPrint:
def __init__(self):
self.reset()
self.font = pygame.font.Font(None, 20)
def plint(self, screen, textString):
textBitmap = self.font.render(textString, True, BLACK)
screen.blit(textBitmap, [self.x, self.y])
self.y += self.line_height
def reset(self):
self.x = 10
self.y = 10
self.line_height = 15
def indent(self):
self.x += 10
def unindent(self):
self.x -= 10
pygame.init()
# Set the width and height of the screen [width,height]
size = [500, 700]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
#Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Initialize the joysticks
pygame.joystick.init()
# Get ready to print
textPrint = TextPrint()
startMarker = '<'
endMarker = '\n'
dividingmarker = ','
In_Min = float(-1)
In_Max = float(1)
Out_Min = float(1000)
Out_Max = float(2000)
arduino = serial.Serial(port="COM15", baudrate=115200, timeout=0.01)
time.sleep(.01)
# -------- Main Program Loop -----------
while done==False:
# EVENT PROCESSING STEP
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
# DRAWING STEP
# First, clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
screen.fill(WHITE)
textPrint.reset()
# Get count of joysticks
joystick_count = pygame.joystick.get_count()
joystick1 = pygame.joystick.Joystick(0)
joystick2 = pygame.joystick.Joystick(1)
joystick1.init()
joystick2.init()
Joy1X = joystick1.get_axis(0)
Joy1Y = joystick1.get_axis(1)
Joy1Z = joystick1.get_axis(2)
Joy1Slider = joystick1.get_axis(3)
Joy2X = joystick2.get_axis(0)
Joy2Y = joystick2.get_axis(1)
Joy2Z = joystick2.get_axis(2)
Joy2Slider = joystick2.get_axis(3)
Joy1B1 = joystick1.get_button(0)
Joy1B2 = joystick1.get_button(1)
Joy1B3 = joystick1.get_button(2)
Joy1B4 = joystick1.get_button(3)
Joy1B5 = joystick1.get_button(4)
Joy1B6 = joystick1.get_button(5)
Joy1B7 = joystick1.get_button(6)
Joy1B8 = joystick1.get_button(7)
Joy1B9 = joystick1.get_button(8)
Joy1B10 = joystick1.get_button(9)
Joy1B11 = joystick1.get_button(10)
Joy1B12 = joystick1.get_button(11)
Joy2B1 = joystick2.get_button(0)
Joy2B2 = joystick2.get_button(1)
Joy2B3 = joystick2.get_button(2)
Joy2B4 = joystick2.get_button(3)
Joy2B5 = joystick2.get_button(4)
Joy2B6 = joystick2.get_button(5)
Joy2B7 = joystick2.get_button(6)
Joy2B8 = joystick2.get_button(7)
Joy2B9 = joystick2.get_button(8)
Joy2B10 = joystick2.get_button(9)
Joy2B11 = joystick2.get_button(10)
Joy2B12 = joystick2.get_button(11)
Joy1Hat = joystick1.get_hat(0)
Joy2Hat = joystick2.get_hat(0)
if Joy1Hat == (0,0):
Joy1Hat_Send = 0
elif Joy1Hat == (0,1):
Joy1Hat_Send = 1
elif Joy1Hat == (1,0):
Joy1Hat_Send = 2
elif Joy1Hat == (1,-1):
Joy1Hat_Send = 3
elif Joy1Hat == (0,-1):
Joy1Hat_Send = 4
elif Joy1Hat == (-1,-1):
Joy1Hat_Send = 5
elif Joy1Hat == (-1, 0):
Joy1Hat_Send = 6
elif Joy1Hat == (-1,1):
Joy1Hat_Send = 7
Joy1X_Send = (((Joy1X - In_Min) * (Out_Max - Out_Min)) / (In_Max - In_Min)) + 1000
Joy1Y_Send = (((Joy1Y - In_Min) * (Out_Max - Out_Min)) / (In_Max - In_Min)) + 1000
Joy1Z_Send = (((Joy1Z - In_Min) * (Out_Max - Out_Min)) / (In_Max - In_Min)) + 1000
Joy1Slider_Send = (((Joy1Slider - In_Min) * (Out_Max - Out_Min)) / (In_Max - In_Min)) + 1000
# JoyStick1_Data = (startMarker)
JoyStick1_Data = str(round(Joy1X_Send))
JoyStick1_Data += (dividingmarker)
JoyStick1_Data += str(round(Joy1Y_Send))
JoyStick1_Data += (dividingmarker)
JoyStick1_Data += str(round(Joy1Z_Send))
JoyStick1_Data += (dividingmarker)
JoyStick1_Data += str(round(Joy1Slider_Send))
JoyStick1_Data += (dividingmarker)
JoyStick1_Data += str(Joy1B1)
JoyStick1_Data += (dividingmarker)
JoyStick1_Data += str(Joy1B2)
JoyStick1_Data += (dividingmarker)
JoyStick1_Data += str(Joy1B3)
JoyStick1_Data += (dividingmarker)
JoyStick1_Data += str(Joy1B4)
JoyStick1_Data += (dividingmarker)
JoyStick1_Data += str(Joy1B5)
JoyStick1_Data += (dividingmarker)
JoyStick1_Data += str(Joy1B6)
JoyStick1_Data += (dividingmarker)
JoyStick1_Data += str(Joy1B7)
JoyStick1_Data += (dividingmarker)
JoyStick1_Data += str(Joy1B8)
JoyStick1_Data += (dividingmarker)
JoyStick1_Data += str(Joy1B9)
JoyStick1_Data += (dividingmarker)
JoyStick1_Data += str(Joy1B10)
JoyStick1_Data += (dividingmarker)
JoyStick1_Data += str(Joy1B11)
JoyStick1_Data += (dividingmarker)
JoyStick1_Data += str(Joy1B12)
JoyStick1_Data += (dividingmarker)
JoyStick1_Data += str(Joy1Hat_Send)
JoyStick1_Data += (endMarker)
JoyStick1_Data_ByteArray = bytearray(JoyStick1_Data, 'ascii')
# 1500,1500,1500,1500,0,0,0,0,0,0,0,0,0,0,0,0,0
print(str(JoyStick1_Data_ByteArray))
# arduino.write(JoyStick1_Data_ByteArray)
# data = arduino.readline()
# print(data + JoyStick1_Data_ByteArray)
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 20 frames per second
clock.tick(1)
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit ()
Arduino code:
// This is very similar to Example 3 - Receive with start- and end-markers
// in Serial Input Basics http://forum.arduino.cc/index.php?topic=396450.0
const byte numChars = 64;
char receivedChars[numChars];
boolean newData = false;
byte ledPin = 13; // the onboard LED
//===============
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
Serial.println("<Arduino is ready>");
}
//===============
void loop() {
recvWithStartEndMarkers();
replyToPython();
}
//===============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//===============
void replyToPython() {
if (newData == true) {
Serial.print("<This just in ... ");
Serial.print(receivedChars);
Serial.print(" ");
Serial.print(millis());
Serial.print('>');
// change the state of the LED everytime a reply is sent
digitalWrite(ledPin, ! digitalRead(ledPin));
newData = false;
}
}
//===============
Please explain, in what way is the Arduino code "non-optimized". How do you know that it is responsible for the slowness? I don't see anything in there that is egregiously inefficient or slow.