int main(void) { SetupHardware(); // LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); GlobalInterruptEnable(); // uint8_t BUT_current = PIND & (1 << PD1);; // текущее значение кнопки // uint8_t BUT_previous = BUT_current; // предыдущее значение кнопки //uint8_t BUT_message=0; // 0 - ничего не отправлять, 1 - отправить vel 0, 2 - отправить vel 127 //uint8_t LED_current = 0; // текущее значение светодиода int ADC_current[16]; // текущие значения АЦП int ADC_previous[16]; // предыдущие значения АЦП uint8_t ADC_deviation = 20; // порог фиксации изменения АЦП, давить шум for (uint8_t i=0; i<16; i++) { ADC_current[i] = raw_ADC(); ADC_previous[i] = ADC_current[i];} // инициализация for (;;) { // дроп входящих MIDI_EventPacket_t ReceivedMIDIEvent; while (MIDI_Device_ReceiveEventPacket(&Keyboard_MIDI_Interface, &ReceivedMIDIEvent)); // опрос 16 входов мультиплексора for (int MUX_pos = 0; MUX_pos < 3; MUX_pos++ ){ MUX_address(MUX_pos); // установить адрес // _delay_ms(1); // MUX_enable(); // _delay_ms(1); ADC_current[MUX_pos] = raw_ADC(); // АЦП // MUX_disable(); if ( abs(ADC_current[MUX_pos] - ADC_previous[MUX_pos]) >= ADC_deviation ) // если изменения больше порога, отправить сообщение CC { cc_send(MUX_pos, ADC_current[MUX_pos] / 8); ADC_previous[MUX_pos] = ADC_current[MUX_pos]; } _delay_ms(10); MIDI_Device_USBTask(&Keyboard_MIDI_Interface); USB_USBTask(); } // MIDI_Device_USBTask(&Keyboard_MIDI_Interface); // USB_USBTask(); } }
void usb_send_func(MidiDevice * device, uint8_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) { MIDI_EventPacket_t event; event.CableNumber = 0; event.Data1 = byte0; event.Data2 = byte1; event.Data3 = byte2; //if the length is undefined we assume it is a SYSEX message if (midi_packet_length(byte0) == UNDEFINED) { switch(cnt) { case 3: if (byte2 == SYSEX_END) event.Command = SYSEX_ENDS_IN_3; else event.Command = SYSEX_START_OR_CONT; break; case 2: if (byte1 == SYSEX_END) event.Command = SYSEX_ENDS_IN_2; else event.Command = SYSEX_START_OR_CONT; break; case 1: if (byte0 == SYSEX_END) event.Command = SYSEX_ENDS_IN_1; else event.Command = SYSEX_START_OR_CONT; break; default: return; //invalid cnt } } else { //deal with 'system common' messages //TODO are there any more? switch(byte0 & 0xF0){ case MIDI_SONGPOSITION: event.Command = SYS_COMMON_3; break; case MIDI_SONGSELECT: case MIDI_TC_QUATERFRAME: event.Command = SYS_COMMON_2; break; default: event.Command = byte0 >> 4; break; } } MIDI_Device_SendEventPacket(&USB_MIDI_Interface, &event); MIDI_Device_Flush(&USB_MIDI_Interface); MIDI_Device_USBTask(&USB_MIDI_Interface); USB_USBTask(); }
int main(void) { SetupHardware(); TCCR1B |= (1 << CS10); sei(); memset(inputs, 0, sizeof(inputs)); unsigned long tick = 0; for (;;) { if (TCNT1 >= 1000) { tick++; } //MIDI_EventPacket_t ReceivedMIDIEvent; /*while (MIDI_Device_ReceiveEventPacket(&Keyboard_MIDI_Interface, &ReceivedMIDIEvent)) { }*/ int inputPins[4] = { 0b00000010, 0b00001000, 0b00100000, 0b10000000 }; int port; int current; unsigned long currentTime; for (int i=0; i<8; i++) { port = (i<4) ? PIND : PINB; current = i%4; if (port & inputPins[current]) { if (!inputs[i].pressed && tick > inputs[i].lastOn) { inputs[i].pressed = true; inputs[i].on = !inputs[i].on; inputs[i].lastOn = tick; // Send midi stuff sendMidiNote(i, inputs[i].on); } } else { inputs[i].pressed = false; //sendMidiNote(i, false); } } MIDI_Device_USBTask(&tbase8_MIDI_Interface); USB_USBTask(); } return 0; }
/** Main program entry point. This routine configures the hardware required by the bootloader, then continuously * runs the bootloader processing routine until instructed to soft-exit, or hard-reset via the watchdog to start * the loaded application code. */ int main(void) { /* Setup hardware required for the bootloader */ SetupHardware(); /* Turn on first LED on the board to indicate that the bootloader has started */ LEDs_SetAllLEDs(LEDS_LED1); /* Fill in the UUID Report */ /* Bootloader ID */ UUIDReport[0] = BOOTLOADER_ID_SIG >> 8; UUIDReport[1] = BOOTLOADER_ID_SIG & 0xFF; /* Bootloader Version */ UUIDReport[2] = BOOTLOADER_VERSION_SIG >> 8; UUIDReport[3] = BOOTLOADER_VERSION_SIG & 0xFF; /* Device ID */ for (size_t i = 4; i < UUID_SIZE; i++) { UUIDReport[i] = eeprom_read_byte((uint8_t*)(intptr_t)(i)); } /* Enable global interrupts so that the USB stack can function */ GlobalInterruptEnable(); while (RunBootloader) { MIDI_Task(); CDC_Task(); MIDI_Device_USBTask(&Keyboard_MIDI_Interface); USB_USBTask(); } /* Wait a short time to end all USB transactions and then disconnect */ _delay_us(1000); /* Disconnect from the host - USB interface will be reset later along with the AVR */ USB_Detach(); /* Unlock the forced application start mode of the bootloader if it is restarted */ MagicBootKey = MAGIC_BOOT_KEY; /* Enable the watchdog and force a timeout to reset the AVR */ wdt_enable(WDTO_250MS); for (;;); }
void usb_get_midi(MidiDevice * device) { /* Select the MIDI OUT stream */ Endpoint_SelectEndpoint(MIDI_STREAM_OUT_EPNUM); MIDI_EventPacket_t event; while (MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, &event)) { midi_packet_length_t length = midi_packet_length(event.Data1); //pass the data to the device input function //not dealing with sysex yet if (length != UNDEFINED) midi_device_input(device, length, event.Data1, event.Data2, event.Data3); } MIDI_Device_USBTask(&USB_MIDI_Interface); USB_USBTask(); }
/** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. */ int main(void) { SetupHardware(); sei(); unsigned char c = 0; for (;;) { //Check if we've received any MIDI messages from the USB line if (MIDI_Device_ReceiveEventPacket(&Keyboard_MIDI_Interface, &MIDIpacket_out)) { handle_MIDI_out(); } if (Serial_getChar(&c)) { handle_MIDI_in(c); } MIDI_Device_USBTask(&Keyboard_MIDI_Interface); USB_USBTask(); } }
void usb_get_midi(MidiDevice * device) { MIDI_EventPacket_t event; while (MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, &event)) { midi_packet_length_t length = midi_packet_length(event.Data1); //pass the data to the device input function //not dealing with sysex yet if (length != UNDEFINED) { uint8_t input[3]; input[0] = event.Data1; input[1] = event.Data2; input[2] = event.Data3; midi_device_input(device, length, input); } } MIDI_Device_USBTask(&USB_MIDI_Interface); USB_USBTask(); }
/** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. */ int main(void) { SetupHardware(); LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); GlobalInterruptEnable(); for (;;) { CheckJoystickMovement(); MIDI_EventPacket_t ReceivedMIDIEvent; while (MIDI_Device_ReceiveEventPacket(&Keyboard_MIDI_Interface, &ReceivedMIDIEvent)) { if ((ReceivedMIDIEvent.Event == MIDI_EVENT(0, MIDI_COMMAND_NOTE_ON)) && (ReceivedMIDIEvent.Data3 > 0)) LEDs_SetAllLEDs(ReceivedMIDIEvent.Data2 > 64 ? LEDS_LED1 : LEDS_LED2); else LEDs_SetAllLEDs(LEDS_NO_LEDS); } MIDI_Device_USBTask(&Keyboard_MIDI_Interface); USB_USBTask(); } }
/** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. */ int main(void) { SetupHardware(); LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); GlobalInterruptEnable(); for (;;) { MIDI_EventPacket_t ReceivedMIDIEvent; if (MIDI_Device_ReceiveEventPacket(&Keyboard_MIDI_Interface, &ReceivedMIDIEvent)) { if ((ReceivedMIDIEvent.Event == MIDI_EVENT(0, MIDI_COMMAND_NOTE_ON)) && ((ReceivedMIDIEvent.Data1 & 0x0F) == 0)) { DDSNoteData* LRUNoteStruct = &NoteData[0]; /* Find a free entry in the note table to use for the note being turned on */ for (uint8_t i = 0; i < MAX_SIMULTANEOUS_NOTES; i++) { /* Check if the note is unused */ if (!(NoteData[i].Pitch)) { /* If a note is unused, it's age is essentially infinite - always prefer unused not entries */ LRUNoteStruct = &NoteData[i]; break; } else if (NoteData[i].LRUAge >= LRUNoteStruct->LRUAge) { /* If an older entry that the current entry has been found, prefer overwriting that one */ LRUNoteStruct = &NoteData[i]; } NoteData[i].LRUAge++; } /* Update the oldest note entry with the new note data and reset its age */ LRUNoteStruct->Pitch = ReceivedMIDIEvent.Data2; LRUNoteStruct->TableIncrement = (uint32_t)(BASE_INCREMENT * SCALE_FACTOR) + ((uint32_t)(BASE_INCREMENT * NOTE_OCTIVE_RATIO * SCALE_FACTOR) * (ReceivedMIDIEvent.Data2 - BASE_PITCH_INDEX)); LRUNoteStruct->TablePosition = 0; LRUNoteStruct->LRUAge = 0; /* Turn on indicator LED to indicate note generation activity */ LEDs_SetAllLEDs(LEDS_LED1); } else if ((ReceivedMIDIEvent.Event == MIDI_EVENT(0, MIDI_COMMAND_NOTE_OFF)) && ((ReceivedMIDIEvent.Data1 & 0x0F) == 0)) { bool FoundActiveNote = false; /* Find the note in the note table to turn off */ for (uint8_t i = 0; i < MAX_SIMULTANEOUS_NOTES; i++) { if (NoteData[i].Pitch == ReceivedMIDIEvent.Data2) NoteData[i].Pitch = 0; else if (NoteData[i].Pitch) FoundActiveNote = true; } /* If all notes off, turn off the indicator LED */ if (!(FoundActiveNote)) LEDs_SetAllLEDs(LEDS_NO_LEDS); } } MIDI_Device_USBTask(&Keyboard_MIDI_Interface); USB_USBTask(); } }
/** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. */ int main(void) { MIDI_EventPacket_t midiEvent; struct { uint8_t command; uint8_t channel; uint8_t data2; uint8_t data3; } midiMsg; int ind; int led1_ticks = 0; int led2_ticks = 0; SetupHardware(); RingBuffer_InitBuffer(&USBtoUSART_Buffer); RingBuffer_InitBuffer(&USARTtoUSB_Buffer); LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); sei(); for (;;) { RingBuff_Count_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer); /* See if we have a message yet */ if (BufferCount >= 4) { /* Read in the message from the serial buffer */ for (ind=0; ind<4; ind++) { ((uint8_t *)&midiMsg)[ind] = RingBuffer_Remove(&USARTtoUSB_Buffer); } /* Build a midi event to send via USB */ midiEvent.CableNumber = 0; midiEvent.Command = midiMsg.command >> 4; midiEvent.Data1 = (midiMsg.command & 0xF0) | ((midiMsg.channel-1) & 0x0F); midiEvent.Data2 = midiMsg.data2; midiEvent.Data3 = midiMsg.data3; MIDI_Device_SendEventPacket(&Keyboard_MIDI_Interface, &midiEvent); MIDI_Device_Flush(&Keyboard_MIDI_Interface); /* Turn on the TX led and starts its timer */ LEDs_TurnOnLEDs(LEDS_LED1); led1_ticks = LED_ON_TICKS; } /* Turn off the Tx LED when the tick count reaches zero */ if (led1_ticks) { led1_ticks--; if (led1_ticks == 0) { LEDs_TurnOffLEDs(LEDS_LED1); } } if (MIDI_Device_ReceiveEventPacket(&Keyboard_MIDI_Interface, &midiEvent)) { RingBuff_Count_t count = RingBuffer_GetCount(&USBtoUSART_Buffer); /* Room to send a message? */ if ((BUFFER_SIZE - count) >= sizeof(midiMsg)) { midiMsg.command = midiEvent.Command << 4; midiMsg.channel = (midiEvent.Data1 & 0x0F) + 1; midiMsg.data2 = midiEvent.Data2; midiMsg.data3 = midiEvent.Data3; for (ind=0; ind<sizeof(midiMsg); ind++) { RingBuffer_Insert(&USBtoUSART_Buffer, ((uint8_t *)&midiMsg)[ind]); } /* Turn on the RX led and start its timer */ LEDs_TurnOnLEDs(LEDS_LED2); led2_ticks = LED_ON_TICKS; } else { /* Turn on the RX led and leave it on to indicate the * buffer is full and the sketch is not reading it * fast enough. */ LEDs_TurnOnLEDs(LEDS_LED2); } /* if there's no room in the serial buffer the message gets dropped */ } /* Turn off the RX LED when the tick count reaches zero */ if (led2_ticks) { led2_ticks--; if (led2_ticks == 0) { LEDs_TurnOffLEDs(LEDS_LED2); } } /* any data to send to main processor? */ if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer))) { Serial_TxByte(RingBuffer_Remove(&USBtoUSART_Buffer)); } MIDI_Device_USBTask(&Keyboard_MIDI_Interface); USB_USBTask(); } }