1
0
Fork 0

Maintenance: js_server

Fix memory leaks.
Better use of whitespace.
This commit is contained in:
Scott Giese 2021-02-26 20:59:06 -06:00
parent 21330d2f31
commit 0ee3fd4ab6

View file

@ -26,8 +26,9 @@
*/ */
#include <math.h> #include <math.h>
#include <stdio.h> #include <memory>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <plib/netSocket.h> #include <plib/netSocket.h>
@ -41,14 +42,11 @@ void usage(char * progname)
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
jsJoystick * js ;
float * ax;
int port = 16759; int port = 16759;
char* host; /* = "192.168.1.7"; */ char* host; /* = "192.168.1.7"; */
int activeaxes = 4; int activeaxes = 4;
if( argc != 3 ) if (argc != 3) {
{
usage(argv[0]); usage(argv[0]);
exit(1); exit(1);
} }
@ -57,21 +55,19 @@ int main ( int argc, char ** argv )
jsInit(); jsInit();
js = new jsJoystick ( 0 ) ; auto js = std::make_unique<jsJoystick>(0);
if ( js->notWorking () ) if (js->notWorking()) {
{
printf("no Joystick detected... exitting\n"); printf("no Joystick detected... exitting\n");
exit(1); exit(1);
} }
printf("Joystick is \"%s\"\n", js->getName()); printf("Joystick is \"%s\"\n", js->getName());
int numaxes = js->getNumAxes(); int numaxes = js->getNumAxes();
ax = new float [ numaxes ] ; auto ax = std::make_unique<float[]>(numaxes);
activeaxes = numaxes; activeaxes = numaxes;
if( numaxes > 4 ) if (numaxes > 4) {
{
printf("max 4 axes joysticks supported at the moment, however %i axes were detected\nWill only use the first 4 axes!\n", numaxes); printf("max 4 axes joysticks supported at the moment, however %i axes were detected\nWill only use the first 4 axes!\n", numaxes);
activeaxes = 4; activeaxes = 4;
} }
@ -93,25 +89,21 @@ int main ( int argc, char ** argv )
return -1; return -1;
} }
char packet[256] = "Hello world!"; char packet[256] = "Hello world!";
while(1) while (1) {
{
int b; int b;
int len = 0; int len = 0;
int axis = 0; int axis = 0;
js->read( &b, ax ); js->read(&b, ax.get());
for ( axis = 0 ; axis < activeaxes ; axis++ ) for (axis = 0; axis < activeaxes; axis++) {
{
int32_t axisvalue = (int32_t)(ax[axis] * 2147483647.0); int32_t axisvalue = (int32_t)(ax[axis] * 2147483647.0);
printf("axisval=%li\n", (long)axisvalue); printf("axisval=%li\n", (long)axisvalue);
memcpy(packet + len, &axisvalue, sizeof(axisvalue)); memcpy(packet + len, &axisvalue, sizeof(axisvalue));
len += sizeof(axisvalue); len += sizeof(axisvalue);
} }
// fill emtpy values into packes when less than 4 axes // fill emtpy values into packes when less than 4 axes
for( ; axis < 4; axis++ ) for (; axis < 4; axis++) {
{
int32_t axisvalue = 0; int32_t axisvalue = 0;
memcpy(packet + len, &axisvalue, sizeof(axisvalue)); memcpy(packet + len, &axisvalue, sizeof(axisvalue));
len += sizeof(axisvalue); len += sizeof(axisvalue);