robinfawell Posted November 4, 2005 Report Posted November 4, 2005 Dear ThorstenMaybe I'm being naive, but I think that I should be able to use the MIOS_DOUT_PinGet , MIOS_DOUT_PinSet0 and MIOS_DOUT_PinSet1 functions to achieve the "Toggle Mode." required for the LED's and to set the necessary "gates" needed to double up the keynotes.(coupling)I am experimenting with two switch/LED's on my core 2. This is now using the small program that you kindly sent me. It works fine and I'm very impressed with the very small number of lines of program required.I'm not sure how to use the first function above. The other 2 seem to work for me.Do I define variables for this function? Please send me a few tips.Regards Robin Quote
TK. Posted November 6, 2005 Report Posted November 6, 2005 Hi Robin,I wouldn't use the MIOS_DOUT_PinGet value to toggle the LED, but I would store the state in an array. This has the advantage, that the LEDs could temporary display different informations, and could be switched back to the old saved state. Maybe you don't see the need for such a feature yet, but sooner or later you will be happy that you took this into account from the beginning.There is a simple, and a clever way to realize toggle LEDs.The simple way is the use of an array, which contains one variable for each button/LED.Let's say, DIN 16..31 should be used. Then declare a global array at the top of your .c file:unsigned char toggle_state[16]; // 16 buttons should toggle and write your DIN_NotifyToggle like shown here: void DIN_NotifyToggle(unsigned char pin, unsigned char pin_value) __wparam{ unsigned char array_index; // DIN 16..31 should toggle if( pin >= 16 && pin <= 31 ) { if( pin_value == 0 ) { // only react when button is pressed // determine the index of the array element (starts from 0) array_index = pin - 16; // toggle the flag which saves the current button state toggle_state[array_index] ^= 0x01; // '^' is a XOR operation // send MIDI Note event depending on toggle state MIOS_MIDI_TxBufferPut(0x90); MIOS_MIDI_TxBufferPut(0x30 + array_index); MIOS_MIDI_TxBufferPut(toggle_state[array_index] ? 0x7f : 0x00); // set LED (same pin number like button) MIOS_DOUT_PinSet(pin, toggle_state[array_index] ? 0x7f : 0x00); } } else { // this branch handles DIN 0..15 and DIN 32..127 }}[/code] The disadvantage of this method: it consumes one byte (8bit) per DIN, but only 1 bit is used to save the toggle state. In order to save memory, it's more clever to store 8 toggle states per byte. This can be done in the following way: global array: [code]unsigned char toggle_state[16 / 8]; // 16 buttons should toggle DIN hook: void DIN_NotifyToggle(unsigned char pin, unsigned char pin_value) __wparam{ unsigned char array_index; unsigned char bit_index; // DIN 16..31 should toggle if( pin >= 16 && pin <= 31 ) { if( pin_value == 0 ) { // only react when button is pressed // determine the index of the array element (starts from 0) array_index = (pin - 16) >> 3; // fastest way to divide value / 8; // determine bit position bit_index = (pin - 16) & 0x07; // remainder of x/8 // toggle the flag which saves the current button state toggle_state[array_index] ^= (1 << bit_index); // '^' is a XOR operation // send MIDI Note event depending on toggle state MIOS_MIDI_TxBufferPut(0x90); MIOS_MIDI_TxBufferPut(0x30 + pin - 16); MIOS_MIDI_TxBufferPut((toggle_state[array_index] & (1 << bit_index)) ? 0x7f : 0x00); // set LED (same pin number like button) MIOS_DOUT_PinSet(pin, (toggle_state[array_index] & (1 << bit_index)) ? 0x7f : 0x00); } } else { // this branch handles DIN 0..15 and DIN 32..127 }}[/code]Best Regards, Thorsten. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.