From a8025d154d51e6247147bf9322e2726106bcfe5c Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Sun, 1 Jun 2014 17:58:14 +0000
Subject: added eventqueue sending events works now


diff --git a/software/rhmixxx/Makefile b/software/rhmixxx/Makefile
index 231005b..1e4b67c 100644
--- a/software/rhmixxx/Makefile
+++ b/software/rhmixxx/Makefile
@@ -21,7 +21,7 @@
 
 NAME := rhmixxx
 BOARD_TYPE := rhmixxx
-OBJ := $(NAME).o keypad.o
+OBJ := $(NAME).o keypad.o eventqueue.o
 LIBS := util led lufa-descriptor-midi
 EXTERNAL_LIBS := lufa
 
diff --git a/software/rhmixxx/eventqueue.c b/software/rhmixxx/eventqueue.c
new file mode 100644
index 0000000..3b093d1
--- /dev/null
+++ b/software/rhmixxx/eventqueue.c
@@ -0,0 +1,49 @@
+/*
+ *  rhmidi
+ *
+ *  Copyright (C) 2014 Christian Pointner <equinox@helsinki.at>
+ *
+ *  This file is part of rhmidi.
+ *
+ *  rhmidi is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  any later version.
+ *
+ *  rhmidi is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with rhmidi. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <LUFA/Drivers/Misc/RingBuffer.h>
+#include "eventqueue.h"
+
+static RingBuffer_t event_queue;
+static uint8_t event_queue_data[16];
+
+void eventqueue_init(void)
+{
+  RingBuffer_InitBuffer(&event_queue, event_queue_data, sizeof(event_queue_data));
+}
+
+uint8_t eventqueue_pop(uint8_t* key, uint8_t* state)
+{
+  if (RingBuffer_IsEmpty(&event_queue))
+    return 0;
+
+  uint8_t event = RingBuffer_Remove(&event_queue);
+  *state = (event & 0x80) ? 1 : 0;
+  *key = event & 0x7F;
+  return 1;
+}
+
+void eventqueue_push(uint8_t key, uint8_t state)
+{
+  uint8_t event = (state) ? 0x80 : 0;
+  event |= (key & 0x7F);
+  RingBuffer_Insert(&event_queue, event);
+}
diff --git a/software/rhmixxx/eventqueue.h b/software/rhmixxx/eventqueue.h
new file mode 100644
index 0000000..50e6d81
--- /dev/null
+++ b/software/rhmixxx/eventqueue.h
@@ -0,0 +1,29 @@
+/*
+ *  rhmidi
+ *
+ *  Copyright (C) 2014 Christian Pointner <equinox@helsinki.at>
+ *
+ *  This file is part of rhmidi.
+ *
+ *  rhmidi is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  any later version.
+ *
+ *  rhmidi is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with rhmidi. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef RHMIXXX_eventqueue_h_INCLUDED
+#define RHMIXXX_eventqueue_h_INCLUDED
+
+void eventqueue_init(void);
+uint8_t eventqueue_pop(uint8_t* key, uint8_t* state);
+void eventqueue_push(uint8_t key, uint8_t state);
+
+#endif
diff --git a/software/rhmixxx/keypad.c b/software/rhmixxx/keypad.c
index 6e39cc1..30b8102 100644
--- a/software/rhmixxx/keypad.c
+++ b/software/rhmixxx/keypad.c
@@ -21,7 +21,9 @@
 
 #include <avr/io.h>
 #include <util/delay.h>
+
 #include "keypad.h"
+#include "eventqueue.h"
 
 #define KEYPAD_PIN PINA
 #define KEYPAD_PORT PORTA
@@ -140,8 +142,10 @@ void keypad_task(void)
           keypad_state[key_idx].last_sent = current_state;
 
           if(current_state) {
+            eventqueue_push(key_idx, 0);
             keypad_led_off(key_idx);
           } else {
+            eventqueue_push(key_idx, 1);
             keypad_led_on(key_idx);
           }
         }
diff --git a/software/rhmixxx/rhmixxx.c b/software/rhmixxx/rhmixxx.c
index 1817c0e..07a139e 100644
--- a/software/rhmixxx/rhmixxx.c
+++ b/software/rhmixxx/rhmixxx.c
@@ -60,9 +60,7 @@ USB_ClassInfo_MIDI_Device_t MIDI_Interface =
 /** Event handler for the library USB Configuration Changed event. */
 void EVENT_USB_Device_ConfigurationChanged(void)
 {
-  bool ConfigSuccess = true;
-
-  ConfigSuccess &= MIDI_Device_ConfigureEndpoints(&MIDI_Interface);
+  MIDI_Device_ConfigureEndpoints(&MIDI_Interface);
 }
 
 /** Event handler for the library USB Control Request reception event. */
@@ -75,6 +73,7 @@ void EVENT_USB_Device_ControlRequest(void)
 
 #include "util.h"
 #include "keypad.h"
+#include "eventqueue.h"
 
 int main(void)
 {
@@ -82,11 +81,29 @@ int main(void)
   wdt_disable();
 
   cpu_init();
+  eventqueue_init();
   keypad_init();
 
   USB_Init();
   sei();
 
+  MIDI_EventPacket_t MIDIEventOn = (MIDI_EventPacket_t)
+    {
+      .CableNumber = 0,
+      .Command     = (MIDI_COMMAND_NOTE_ON >> 4),
+      .Data1       = MIDI_COMMAND_NOTE_ON | 0,
+      .Data2       = 0,
+      .Data3       = 0x7F,
+    };
+  MIDI_EventPacket_t MIDIEventOff = (MIDI_EventPacket_t)
+    {
+      .CableNumber = 0,
+      .Command     = (MIDI_COMMAND_NOTE_OFF >> 4),
+      .Data1       = MIDI_COMMAND_NOTE_OFF | 0,
+      .Data2       = 0,
+      .Data3       = 0,
+    };
+
   for(;;) {
     MIDI_EventPacket_t ReceivedMIDIEvent;
     while(MIDI_Device_ReceiveEventPacket(&MIDI_Interface, &ReceivedMIDIEvent)) {
@@ -94,7 +111,16 @@ int main(void)
     }
     keypad_task();
 
-        // TODO: send out MIDI Events
+    uint8_t key, state;
+    while(eventqueue_pop(&key, &state)) {
+      if (USB_DeviceState == DEVICE_STATE_Configured) {
+        MIDI_EventPacket_t* MIDIEvent = state ? &MIDIEventOn : &MIDIEventOff;
+        MIDIEvent->Data2 = key;
+        MIDI_Device_SendEventPacket(&MIDI_Interface, MIDIEvent);
+      }
+    }
+    if (USB_DeviceState == DEVICE_STATE_Configured)
+      MIDI_Device_Flush(&MIDI_Interface);
 
     MIDI_Device_USBTask(&MIDI_Interface);
     USB_USBTask();
-- 
cgit v0.10.2