Jump to content

Change Pin Values


henrygr
 Share

Recommended Posts

I know i've seen it somewhere before, and yes, I know, my C was crap, I read up, never practrised, and I'm kind of back to square 1.

I have set up a switch to return 9 possible values, I want the core to send out nine different corresponding values?

Can anyone help?

Purpose- http://www.midibox.org/forum/index.php?topic=8276.0

Thanks.

Link to comment
Share on other sites

With the scaling routine (http://www.ucapps.de/mios_c_send_range.html) it would be very simple...

Here a special adaption of the programming example for your use case:


/////////////////////////////////////////////////////////////////////////////
// This function is called by MIOS after startup to initialize the
// application
/////////////////////////////////////////////////////////////////////////////
void Init(void) __wparam
{
  // initialize the AIN driver
  MIOS_AIN_NumberSet(8);  // 8 pots are connected
  MIOS_AIN_UnMuxed();        // the AINX4 modules are *not* used
  MIOS_AIN_DeadbandSet(7); // should be 7 when 7bit resolution is used
}

/////////////////////////////////////////////////////////////////////////////
// This is an assembly optimized function which scales a 7bit value between
// a minimum and maximum value
/////////////////////////////////////////////////////////////////////////////
unsigned char Scale_7bit(unsigned char value, unsigned char min, unsigned char max)
{
  // scaled value is (<8-bit random> * ) >> 8
  PRODL = value << 1; // 8bit value
  PRODH = max-min+1;  // range
__asm
    movf _PRODL, W
    mulwf _PRODH, 0
__endasm;

  return min + PRODH;
}

/////////////////////////////////////////////////////////////////////////////
// This function is called by MIOS when a pot has been moved
/////////////////////////////////////////////////////////////////////////////
void AIN_NotifyChange(unsigned char pin, unsigned int pin_value) __wparam
{
  unsigned char value;

  // scale 7bit value between min and max boundary
  value = Scale_7bit(MIOS_AIN_Pin7bitGet(pin), // converted to 7bit
                    0,                        // min value
                    8);                      // max value

  // send scaled CC value over channel #1, CC Number 16..23
  MIOS_MIDI_TxBufferPut(0xb0);
  MIOS_MIDI_TxBufferPut(16 + pin);
  MIOS_MIDI_TxBufferPut(value);
}
[/code]

Best Regards, Thorsten.

Link to comment
Share on other sites

Thank you TK.  ;D

I had a look at that code. I think it assumes that you are using a pot or fader that goes, in normal operation from 0 to 127 (I will keep it in midi terms), and will return a value scaled to whatever amount between those two values. My predicament with the matrix I built is that the input values go from 55 to 126 (and not necessarily on a scale), and need to output values between 0 and 127. There is on good bit of news though, and that is that the input and output values for all nine pins are the same.

I have attempted the following to call values for one pin. The plan was to get that right, and move to all nine....

// The first of each of the sets of two values is the 7 bit input value of the pin the second being the 
// value to pass as output

const unsigned char inputOutputMidiValues [9][2]={
{126,127}, {110,111}, {99,95}, {86,79}, {78,63}, {73,47}, {67,31}, {61,15}, {55,0},
};

/////////////////////////////////////////////////////////////////////////////
// This function is called by MIOS after startup to initialize the 
// application
/////////////////////////////////////////////////////////////////////////////
void Init(void) __wparam
{
  // initialize the AIN driver
  MIOS_AIN_NumberSet(1);   // only one pot is connected
  MIOS_AIN_UnMuxed();      // no AINX4 modules are used
  MIOS_AIN_DeadbandSet(7); // should be 7 when 7bit resolution is used
}

/////////////////////////////////////////////////////////////////////////////
// This function is called by MIOS when a pot has been moved
/////////////////////////////////////////////////////////////////////////////
void AIN_NotifyChange(unsigned char pin, unsigned int pin_value) __wparam
{if (MIOS_AIN_Pin7bitGet(pin)=inputOutputMidiValues[0])
  // send coresponding value
  MIOS_MIDI_TxBufferPut(0xb0); // Midi Channel One
  MIOS_MIDI_TxBufferPut(0x5b); // Reverb
  MIOS_MIDI_TxBufferPut(inputOutputMidiValues[1]); // Corresponding output value
}

Sould this work? Itried it nd it wouldn't compile, and even after that, am left in the lurch of figuring out the other eight pots!!

Thanks Again....

Link to comment
Share on other sites

He he. I have developed the great habit of compiling after every line of code, kind of helps pin point badly written p's and q's.

Anyways, going to go with switch and case. The string of variables isn't really that long, and I don't really seeing it taking up too much valuable RAM. I will say this though, that when I can't see the wood for the trees when it comes to programming, I find it helps just to get what's off of my chest any ideas I do have and post them here.

Thanks all for putting up with it. ;D

Link to comment
Share on other sites

Antoher idea struck me driving home from picking up the kids (always amazes me when these things strike home). The relationship between the AIN (call it x) and the desired midi output value (call it y) is roughly;

2(x-55) = y

....and that's close enough for jazz.

How is the PIC chip at dealing with these types of formulae?

Any comments, as ever, much appreciated.

Mark.

Link to comment
Share on other sites

Hi Mark,

2(x-55) = y

what should this formula do? The bracket is no mathmatical operator in C, so I don't know what you'd like to express with "2(x)" ???

I must admit, that I'm having troubles to follow what you want to do exactly, so it's hard to give any help (I know I could read all the threads you linked above, but I soon found out that the linkage level is quite deep, before grasping what's the point). Maybe you could conclude in one short sentece the aim of your question; this also makes it a good practice to get to an abstraction level needed for the program snippet ;)

Anyway, it makes no sense spreading values between 55...126 to 0...127, because the resolution for the in-between steps is missing. In this case you'd have to read the values as 10-bit and then scale it to a lower value and do a 7bit conversion at the end.

Although I think your approach with a lookup-table (array) seems quite fine for the task...

So, here's one more thing:

I wanted to express that you should send the compile error message last time, this would make things a lot easier. But I think I've found the error anyway ;D

if (MIOS_AIN_Pin7bitGet(pin)=inputOutputMidiValues[0]) 

This is a widespread error, that happens also to the pro's from time to time:

The logical comparison operator for equality ("is the same as") is this: ==

whereas the assignment operator that assigns the value of the right expression to the left side of the operator is this: =

Tricky, isn't it :)

Cheers,

Michael

Link to comment
Share on other sites

Modified Post. Rushed it first time. Apologies.

what should this formula do? The bracket is no mathmatical operator in C, so I don't know what you'd like to express with "2(x)"

The intention was to show the idea in algebraic form. My query is whether the Pic can handle this type of function on the fly.

What's it for?- This linkhttp://www.midibox.org/forum/index.php?topic=8276.0

The values I get from the resistors I used go from 55 to 126, almost sequntially- close enough for jazz. Reason- these were the resistors I had lying around, and there were no chances of me going to Maplin electronics during xmas/january sales.

So, moving along, I need to pass the signals to Native instruments B4, my clonewheel of choice. Values passed into this do not have to be exact, but close enough for jazz!!

Hence:

const unsigned char inputOutputMidiValues [9][2]={

{126,127}, {110,111}, {99,95}, {86,79}, {78,63}, {73,47}, {67,31}, {61,15}, {55,0},

};

.

First value is the value passed from the M1 drawbars, second value is what B4 will use for moving drawbar (ie the midi signal it sends on moving a drawbar), but it will accept a value close to latter for actually moving them eg will move to stop 8 from midi signal 121 and above.

This is a widespread error, that happens also to the pro's from time to time:

The logical comparison operator for equality ("is the same as") is this: ==

whereas the assignment operator that assigns the value of the right expression to the left side of the operator is this: =

This has made my day. I am not the only one doing it, yipppeee. And I must add- he he he.

So, passing the algebraic function into C

unsingned char midiInputValue = MIOS_AIN_7bitGet(pin); //Do I leave the underscores?
unsigned char midiOutputValue = (midiInputValue - 55) * 2;

//then sending the third value on pot movement:

 MIOS_MIDI_TxBufferPut(midiOutputValue); // where normally this reads -
                                                          // MIOS_MIDI_TxBufferPut(MIOS_AIN_Pin7bitGet(pin));

How does it look? Has it a leg to stand on?

Link to comment
Share on other sites

What the hell was I thinking. Next time, I'll multiply myself in two so I can take proper aim and shoot myself.  :o

I could remove all the crap below, but I'll leave as a humble reminder to myself of my own stupidity. :P

Apologies to all, and thanks to all. ;)

Switch case arguments worked out just dandy. I think i'll have a hot cup of tea to celebrate. Hoorah!! (my ar*e. Off to the pub for lashings of beer, me thinks.....he he he.)

Case Closed.

Mark. ;D

Link to comment
Share on other sites

I agree, stryo_one, if I had the math right!! Messed up on the equation end of thing for early. Anyway, the way it wotked out, the drawbars left a lot to be desired in terms of unwanted signal, so I was to filter that out by giving it a DEFAULT value with SWITCH/CASE and using a simple argument not to process it. Will do that WIKI at some point on the whole drawbar issue, as I've done a few at this stage. I was thinking that perhaps a section on retro-fitting in the WIKI would be a good article to start up, as there are quite a few thing here on the forum (QBAS getting the c-code scan matrix working on an old alesys being but one) that I think well deserve a topic of their own.

No?

Thanks again,

Mark.

Link to comment
Share on other sites

I agree, organ and other instrument retrofits are a pretty specific area and some extra doco would be very useful I'm sure.

Glad you got it working OK :) I think the moral of the story is to wait and get the right resistors, but hey, whatever works, works! :D

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.

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.

Loading...
 Share

×
×
  • Create New...