-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdevices.cpp
195 lines (165 loc) · 4.52 KB
/
devices.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
#include <avr/pgmspace.h>
#include "monty.h"
extern const uint8_t font[128] PROGMEM;
extern Monty monty;
Uart0::Uart0() {
UCSR0B |= 1<<RXEN0; // read only
UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01);
UBRR0H = (BAUD_PRESCALE_0 >> 8);
UBRR0L = BAUD_PRESCALE_0;
}
uint8_t Uart0::read() {
while ((UCSR0A & (1 << RXC0)) == 0);
uint8_t value = UDR0;
if (monty.dom) {
monty.gimpMidi.write(value);
}
return value;
}
Uart1::Uart1() {
UCSR1B |= (1<<RXEN0) | (1<<TXEN0); // read and write
UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11);
UBRR1H = (BAUD_PRESCALE_1 >> 8);
UBRR1L = BAUD_PRESCALE_1;
}
uint8_t Uart1::read() {
while ((UCSR1A & (1<<RXC1)) == 0);
return UDR1;
}
void Uart1::write(uint8_t data) {
while (((UCSR1A & (1<<UDRE1)) == 0));
UDR1=data;
}
SidClock::SidClock() {
// SID 1mhz clock OSC 2 on PD7
TCNT2 = 0;
OCR2A = 7;
TCCR2A = (1<<COM2A0) + (1<<WGM21);
TCCR2B = (1<<CS20);
DDRD |= (1<<7);
}
SevenSeg::SevenSeg() {
DDRB = 0xFF;
}
uint8_t SevenSeg::getFontValue(uint8_t value) {
return pgm_read_byte_near(&font[value]);
}
void SevenSeg::updateNumeric(uint8_t value) {
value &= 0xf;
updateFont((value < 0xa ? '0' : 'A'-0xa) + value);
}
void SevenSeg::updateFont(uint8_t value) {
update(getFontValue(value));
}
uint8_t SevenSeg::scrollDown2(uint8_t value) {
return (value&SEG_A)<<('D'-'A');
}
uint8_t SevenSeg::scrollDown1(uint8_t value) {
return
((value&SEG_A)<<('G'-'A')) |
((value&SEG_G)>>('G'-'D')) |
((value&SEG_F)>>('F'-'E')) |
((value&SEG_B)<<('C'-'B'));
}
uint8_t SevenSeg::scrollUp1(uint8_t value) {
return
((value&SEG_G)>>('G'-'A')) |
((value&SEG_D)<<('G'-'D')) |
((value&SEG_E)<<('F'-'E')) |
((value&SEG_C)>>('C'-'B'));
}
uint8_t SevenSeg::scrollUp2(uint8_t value) {
return (value&SEG_D)>>('D'-'A');
}
void SevenSeg::update(uint8_t value) {
#ifdef LED_ANODE
PORTB = ~value;
#else
PORTB = value;
#endif
}
Button::Button(uint8_t bitmask) {
this->bit = bitmask;
DDRD &= ~bitmask;
PORTD |= bitmask;
}
void Button::poll() {
bool down = (PIND & this->bit) == 0;
this->changed = down != this->down;
this->down = down;
}
bool Button::pressed() {
return this->changed && this->down;
}
bool Button::released() {
return this->changed && !this->down;
}
uint8_t Knob::get() {
return this->upperNibble ? *this->location >> 4 : *this->location & 0x0f;
}
void Knob::set(uint8_t value) {
*this->location = this->upperNibble ?
(*this->location & 0x0f) | value << 4 :
(*this->location & 0xf0) | (value & 0x0f);
}
Menu::Menu() :
buttonA(BUTTON_1), buttonB(BUTTON_2), buttonC(BUTTON_3) {
}
void Menu::update() {
this->buttonA.poll();
this->buttonB.poll();
this->buttonC.poll();
if (this->buttonB.down) {
if (this->buttonA.pressed()) {
monty.enableDom();
monty.synth.setAllVoicesOff();
return;
} else if (this->buttonC.pressed()) {
// todo: broken. regular mode will be blocking on the wrong UART
monty.enableGimp();
monty.synth.setAllVoicesOff();
return;
}
}
if (this->timeout == 0) {
if (this->buttonA.pressed() || this->buttonB.pressed() || this->buttonC.pressed()) {
// reset the timeout. ignore this first press.
this->timeout = FRAMES_PER_SECOND*10;
} else {
// if menus are not being used (no buttons have been pressed in a while), mirror voice status to the 7 seg
this->sevenSeg.update(monty.synth.getVoiceOnBits());
}
return;
} else {
this->timeout--;
}
Knob * knob = &knobs[this->selectedKnob];
if (this->buttonB.pressed()) {
this->edit = !this->edit;
}
if (this->edit) {
uint8_t value = knob->get();
if (this->buttonA.pressed() && value > 0) {
knob->set(--value);
}
if (this->buttonC.pressed() && value < 0x0f) {
knob->set(++value);
}
this->sevenSeg.updateNumeric(value);
} else {
if (this->buttonA.pressed() && this->selectedKnob > 0) {
this->selectedKnob--;
}
if (this->buttonC.pressed() && this->selectedKnob < this->knobCount-1) {
this->selectedKnob++;
}
this->sevenSeg.updateFont(this->flash++ & 0b00001000 ? 0 : knob->label);
}
}