Long time no see

Discuss anything programming related.
Post Reply
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Long time no see

Post by bumlove »

Hi there chaps I hope everyone is still alive, I'm making a HID joystick on a PIC/PLC (to be used as a DJ controller) 128 buttons and 48 pots, I've taken some code and adapted it to match my desires. I now need to reduce the code as it stands at 22.6KB and my limit is 12KB the bad/good news is I adapted the code like an absolute noob, so there is quite alot in the way of long lists that could be reduced. Would somebody be up for helping me along, I shall post some snippets if someone says yup.
User avatar
Click16
Posts: 1941
Joined: Mon Dec 31, 2007 4:36 am
Location: United States

Re: Long time no see

Post by Click16 »

How does one write code for this? I'm not entirely sure what you're trying to accomplish. I would be willing to help if I knew what to do or how to do it.
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Long time no see

Post by bumlove »

Thanks for the speedy reply click,

The coding base is C++ and all this stuff is for writing the firmware to make a Comport device appear as a HID device,
it is compiled with Amtel Studio, (based on visual studio)

all I have done is follow the example of Unojoy and expanded
I'm still noob double 0 zero with code so the stuff I need help with should be pretty simple.

In my source files, I have some long lists that I think I could shorten with a i++ operation (an array I've been told)
Being a noob, I'm not sure how to do it

Below is my code and ............... represents missing numbers.

Code: Select all

// zero when we are not configured, non-zero when enumerated
static volatile uint8_t usb_configuration = 0;

//THIS PART ZEROS ALL BUTTONS AND CENTERS ALL JOYSTICKS

//I KNOW THIS COULD BE REDUCED FROM 30 OR SO LINES DOWN TO 2 or 3
static const gamepad_state_t PROGMEM gamepad_idle_state = {
	.b0_btn = 0, .b1_btn = 0, .b2_btn = 0, .b3_btn = 0,
	.......................        .b126_btn = 0, .b127_btn = 0,	
	
//I THINK THE SAME WITH THIS
	.a0_axis = 0x80, .a1_axis = 0x80, .a2_axis = 0x80, .a3_axis = 0x80,
	...........................                    .a46_axis = 0x80, .a47_axis = 0x80,

//AND THIS TOO
	.b0_axis = 0x00, .b1_axis = 0x00, .b2_axis = 0x00, .b3_axis = 0x00,
	..................                          .b126_axis = 0x00, .b127_axis = 0x00
};

There's also this bit where I think the implementation would be slightly different

Code: Select all


//THIS BIT TAKES DATA FROM THE MICROCONTROLLER SOFTWARE AND MAKES IT AVAILABLE FOR THE FIRMWARE

int8_t sendPS3Data(dataForController_t btnList){

	
gamepad_state.b0_btn = btnList.bt0On;
gamepad_state.b1_btn = btnList.bt1On;
	gamepad_state.b2_btn = btnList.bt2On;
        ....................
	gamepad_state.b127_btn = btnList.bt127On;
	
		
//AND THIS BIT TELLS THE FIRMWARE WHAT DATA TO SPIT OUT IN THE HID REPORT
	
	if (gamepad_state.b0_btn == 1)
	gamepad_state.b0_axis = 0x01;
	else
	gamepad_state.b0_axis = 0;
	
	if (gamepad_state.b1_btn == 1)
	gamepad_state.b1_axis = 0x01;
	else
	gamepad_state.b1_axis = 0;

if (gamepad_state.b2_btn == 1)
gamepad_state.b2_axis = 0x01;
else
gamepad_state.b2_axis = 0;
	
 .....................................

if (gamepad_state.b127_btn == 1)
gamepad_state.b127_axis = 0x01;
else
gamepad_state.b127_axis = 0;
	

	// left and right analog sticks, 0x00 left/up, 0x80 middle, 0xff right/down

	gamepad_state.a0_axis = btnList.al0;
	gamepad_state.a1_axis = btnList.al1;
	gamepad_state.a2_axis = btnList.al2;
        ..........................
	gamepad_state.a47_axis = btnList.al47;
	
	
	// Send the data out via USB
	return usb_gamepad_send();
}

there is a bit more that could be reduced and I can explain it further if you need but all I think i need to do is cut down long lists with arrays.
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Long time no see

Post by bumlove »

I'll give more info as I have a bit of time.

There are 2 parts to the coding for this, you have the microcontroller software that takes human input and passes it on to the firmware.

So you upload the software (while you can as in HID mode you can't) then you flash the firmware.

The firmware is the bit that sends the pc the info that it is a HID device and it also sends the HID report packet.

The only bit I need help with (so far) is the Firmware

The firmware (HexFile) is compiled with
Unojoy.C
dataForController_t.H
usb_gamepad.C
usb_gamepad.H
physicalButtonList_t.H

I think if I tried to explain further I would use the wrong term and confuse the issue, but knowing that you haz skillz you should understand from the source
User avatar
CaptainPoopface
Posts: 714
Joined: Sat Feb 16, 2008 5:47 am

Re: Long time no see

Post by CaptainPoopface »

JavaScript has what is called a minifier that strips unnecessary characters out of the source and compresses variable names to single characters when possible. You may be able to get the ~50% reduction you are looking for without modifying the code beyond that, if there is such a thing as a minifier for c++. Note that the output of such a thing is not human readable.
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Long time no see

Post by bumlove »

I think I could thin down the names for the variables, I just hoped I could get some help putting them into an array, I'm not asking too much am I? I thought it would be something like 2 or 3 lines of code.
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Long time no see

Post by bumlove »

Not to worry lads I minimised the variable names to as short as possible, the device now spits out a full 64 byte report, my next part is the device software which I have some more examples to work from.

If anyone cares this project is based on the Arduino uno, and you can use it for HID Dj software controlling or as a huge HID joystick,

I'll post results as they happen.
User avatar
OwnZ joO
Posts: 1197
Joined: Sun Dec 09, 2007 4:46 pm

Re: Long time no see

Post by OwnZ joO »

CaptainPoopface wrote:JavaScript has what is called a minifier that strips unnecessary characters out of the source and compresses variable names to single characters when possible. You may be able to get the ~50% reduction you are looking for without modifying the code beyond that, if there is such a thing as a minifier for c++. Note that the output of such a thing is not human readable.
Make sure that you compile the code in release mode and not debug mode(that is if there are any options like that on your compiler), it should strip out any extra junk that isn't necessary. A obfuscator would do the trick for what captain poopface is describing, as I believe they usually rename variables. I would hope your compiler in release mode would shorten anything like that already. Also, you may want to check out the results with optimization settings, but that could possibly increase the size as optimization can sometimes add padding so that the code is properly aligned for the processor to read it faster.
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Long time no see

Post by bumlove »

Well I have it working, the arduino software side was really simple, at present I don't yet have the multiplexerers to fit 48 analogue inputs into the 6 that I have on the microcontroller (nor for the 128 into 12 possible digital ins) but by writing delays into the muxing scheme and recording the report packets I have proved every single bit of the 64 bytes.

Since that wasn't too painful I'm trying for 2 report packets because 128 bytes of data will allow for more inputs and at higher resolution and I would never need more than that.

Although a little help as to how I would shorten these snippets (if possible) with a i++ operation would be really helpful

Code: Select all

static const gamepad_state_t PROGMEM gamepad_idle_state = {
	.b0 = 0, .b1 = 0, .b2 = 0, .b3 = 0,
	.b4 = 0, .b5 = 0, .b6 = 0, .b7 = 0,
	.b8 = 0, .b9 = 0, .b10 = 0, .b11 = 0

};

Code: Select all

typedef struct {

	uint8_t b0 : 1;
 	uint8_t b1 : 1;
	uint8_t b2 : 1;
	uint8_t b3 : 1;
	uint8_t b4 : 1;
	uint8_t b5 : 1;
	uint8_t b6 : 1;

} gamepad_state_t;

Code: Select all

void setControllersToZero(void){
	dataForController_t emptyData;
	emptyData.al0 = 128;
	emptyData.al1 = 128;
	emptyData.al2 = 128;
	emptyData.al3 = 128;
	emptyData.al4 = 128;
	emptyData.al5 = 128;
	emptyData.al6 = 128;
	emptyData.al7 = 128;
	
	sendPS3Data(emptyData);
}

Code: Select all

typedef struct dataForController_t
{
	uint8_t bI0 : 1;  // variables to abstractly tell us data size of the buttons pressed
	uint8_t bI1 : 1;
	uint8_t bI2 : 1;
	uint8_t bI3 : 1;
	uint8_t bI4 : 1;
	uint8_t bI5 : 1;
	uint8_t bI6 : 1;
	uint8_t bI7 : 1;

	uint8_t al0 : 8;
	uint8_t al1 : 8;
	uint8_t al2 : 8;
	uint8_t al3 : 8;
	uint8_t al4 : 8;
	uint8_t al5 : 8;
	uint8_t al6 : 8;
	uint8_t al7 : 8;
		
} dataForController_t;

User avatar
OwnZ joO
Posts: 1197
Joined: Sun Dec 09, 2007 4:46 pm

Re: Long time no see

Post by OwnZ joO »

You could probably do some pointer arithmetic on the different structs if you really need to shorten the code.

as in

Code: Select all

int loops = sizeof(theStruct) / sizeof(int);
int i = 0;
int * address = &structVariable;
while(i < loops){
  address[i++] = 1;
}
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Long time no see

Post by bumlove »

Thanks I'll look into that,
I've discovered my device has 8 KB memory for firmware, and my code is 7246 bytes, and I haven't even started with pc to device communication, fortunately I only have a few floats to send.

I have it now outputting 128 bytes of HID data, but it appears as thought I outputting the first 64 bytes twice.
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Long time no see

Post by bumlove »

Minor update, I solved the double send error, it was an overflow, the device was only expecting 64 bytes so 128 bytes screwed things up, I've now got 128 buttons, 48 * 8 bit analogue & 32 * 10 bit analogue all working nicely, next battle is pc to device communication.
Post Reply