Jump to content

midi sysex head scratching - arduino project


cedd

Recommended Posts

Thank you so much Alister. I'm going to try it tomorrow with the 2 bytes as constants rather than being read from the midi input - I'm wondering if there's something quirky going on that's different between live values like I have, and simulated ones like you have. I've got Bome's Send SX software running on my laptop and am sending the value to the teensy that way.

Link to comment
Share on other sites

I've now had time to do some experimenting.

First, -905 is hex 78 77 not hex 78 7f but that's a minor detail. It's still a negative number (-897) actually.

Second, it looks as though the Teensy is treating the int as a 32 bit value which is why the code works on a real Arduino, which treats it as a 16bit value.

Third, although Alister was nearly right about sign extending the 32 bit number he should have put ffffc000 instead of 8888c000

I'm posting this on the mobile so can't see the code tags but I'll try and edit on the computer so that I can show you the code correctly.

Dave

void setup() {
Serial.begin(9600);
}

void loop() {
byte rxhigh_part = 0x78;
byte rxlow_part = 0x77;
long val = ((rxhigh_part&0x7f)<<7) | (rxlow_part & 0x7f);
if(val & 0x2000) val|=0xffffC000;
Serial.print (rxhigh_part, HEX);
Serial.print (rxlow_part, HEX);
Serial.print (" : ");
Serial.println(val);
while(1);
}

Resulting output is

7877 : -905

Of course I've had to declare val as a long to force the Arduino to use 32 bits. If I do that and use C000 instead of ffffc000 for the sign extension I get the same result that you did. And if I use 8888C000 I also get the other wrong value that you got!

Hope that helps

Dave

Edited by DrV
Added the CODE section
  • Thanks 1
Link to comment
Share on other sites

Well spotted, Dave, absolutely if should have been FFFFC000 - D'oh! It had been a long day.

My test was as follows:

inline short combine7bit(unsigned char high, unsigned char low)
{
	unsigned short u = ((high & 0x7f) << 7) | (low & 0x7f);
	if (u & 0x2000) { u |= 0xC000; }
	return (short)u;
}

int main()
{
	short s = combine7bit(0x78, 0x77);
	printf("%hd\n", s);
	s = combine7bit(0, 0x64);
	printf("%hd\n", s);
	return 0;
}

This outputs -905 and 100 for the given inputs.

  • Upvote 2
Link to comment
Share on other sites

You absolute beauties! That's it! 

You might also chuckle to note that after my last post yesterday I decided to try something else and get the name transfer working instead. Roland have a table of values 0-127 that correspond with characters. I set about building up an array of all the different characters. Took me ages. Then, and only then, did I realise that Roland had used values that corresponded to the ASCII codes for the relevant characters - all I had to do was write the received value to the display and it worked. 

Thanks again. Will share the final item with you once I've finished it. 

Chris 

  • Upvote 1
Link to comment
Share on other sites

Sorry, coming late to the party. It's always a good idea to cast 8-bit byte values to 16-bit int before doing integer operations.

So:

int val = ((rxhigh_part&0x7F)<<7)|(rxlow_part&0x7F);

should be:

int val = (((int)rxhigh_part&0x7F)<<7)|((int)rxlow_part&0x7F);

 

  • Upvote 1
Link to comment
Share on other sites

C always casts byte to signed int when you do a calculation, you don't need to do it separately. The trouble comes when int is 16bits (mega arduinos) as 32767 isn't all that big and quite easy to overflow. On ARM arduino it's 32 bit so doesn't overflow. 

I've started defining things as uint_32t or uint_16t rather than relying on the compiler to set the variable size, can save a lot of head scratching. 

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.