-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
128 lines (105 loc) · 2.45 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* Name: main.c
* Project: V-USB MIDI device on Low-Speed USB
* Author: Martin Homuth-Rosemann
* Creation Date: 2008-03-11
* Copyright: (c) 2008 by Martin Homuth-Rosemann.
* License: GPL.
*
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include "usbdrv/usbdrv.h"
#include "usb.h"
#include "midi.h"
#define KEY (1 << PB0)
#define KEY_COUNT 4
#define FALSE 0
#define TRUE 1
volatile uint8_t sent = TRUE;
MIDI_EVENT event = {MIDI_CMD_NOTE_OFF, 0x00, 0x3D, 0x40};
enum
{
NONE,
PRESSED,
DEPRESSED
};
uint8_t keyPressed(uint8_t *lock, volatile uint8_t *PINX, uint8_t pinNumber);
int main(void)
{
uint8_t i;
uint8_t keyLocks[KEY_COUNT];
uint8_t keyNotes[KEY_COUNT] = {61, 62, 64, 66};
PORTC |= (1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3); // przyciski
DDRB |= (1 << PB1); // dioda
wdt_enable(WDTO_1S);
USB_Init();
sei();
/* main event loop */
while (1)
{
wdt_reset();
usbPoll();
for (i = 0; i < KEY_COUNT; i++)
{
uint8_t keyState = keyPressed(&(keyLocks[i]), &PINC, i);
if (PRESSED == keyState)
{
event.command = MIDI_CMD_NOTE_ON;
event.val1 = keyNotes[i];
sent = FALSE;
}
else if (DEPRESSED == keyState)
{
event.command = MIDI_CMD_NOTE_OFF;
event.val1 = keyNotes[i];
sent = FALSE;
}
}
if (usbInterruptIsReady())
{
if (!sent)
{
sent = TRUE;
MIDI_SendEvent(&event);
}
}
}
return 0;
}
/*---------------------------------------------------------------------------*/
/* usbFunctionWriteOut */
/* */
/* this Function is called if a MIDI Out message (from PC) arrives. */
/* */
/*---------------------------------------------------------------------------*/
void usbFunctionWriteOut(uchar * data, uchar len)
{
// event.channel = data[0] & 0x0F;
// event.command = data[0] & 0xF0;
// event.val1 = data[1];
// event.val2 = data[2];
// event.channel = 0;
// event.command = MIDI_CMD_NOTE_ON;
// event.val1 = 61;
//
// sent = FALSE;
// PORTB |= (1 << PB1);
}
uint8_t keyPressed(uint8_t *lock, volatile uint8_t *PINX, uint8_t pinNumber)
{
register uint8_t isPressed = (*PINX & (1 << pinNumber));
if (!isPressed && !*lock)
{
*lock = TRUE;
return PRESSED;
}
else if (isPressed && *lock)
{
if (!++*lock)
{
return DEPRESSED;
}
}
return NONE;
}