/
debugMenu.c
172 lines (154 loc) · 3.68 KB
/
debugMenu.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
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
/**
* 2011 Martin Ongsiek
*
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#include "debugMenu.h"
#define TIME_ENTPRELL 5
typedef struct {
uint8_t wert;
uint8_t format;
} debugMenu_t;
typedef enum {
ST_MENU_START,
ST_MENU_ON,
ST_MENU_OFF,
ST_MENU_WERT,
} states_menu_t;
static uint8_t menuEntry;
static states_menu_t stateMenu;
static debugMenu_t debugMenu[8];
static uint8_t entprellTimer[4];
static uint8_t tasterCur, timer, alterWert, updateWert;
static uint8_t tasterEntprellt;
void debugMenuInit() {
DDRC = 0xff;
// Taster als Eingaenge
DDRB &= ~(_BV(PB0) | _BV(PB1) | _BV(PB2) | _BV(PB3));
// Pullups
PORTB |= _BV(PB0) | _BV(PB1) | _BV(PB2) | _BV(PB3);
menuEntry = 0;
stateMenu = ST_MENU_START;
#ifdef DEBUG_MIT_INTTERUPT
// Timer initialisieren
GICR = 0;
TCCR0 = _BV(CS01) | _BV(CS00);
TIMSK |= (1 << TOIE0);
sei();
#endif // DEBUG_MENU
}
static uint8_t tastenEntprellen() {
uint8_t i, changes, mask, event = 0;
changes = (PINB & 0x0f) ^ tasterCur;
tasterCur = (PINB & 0x0f);
mask = 1;
for (i = 0; i < 4; i++) {
if (changes & mask) { // Eingang geändert
entprellTimer[i] = 0;
} else if (entprellTimer[i] < TIME_ENTPRELL) { // Entprellzeit nicht abgelaufen
entprellTimer[i]++;
} else if ((tasterCur & mask) != (tasterEntprellt & mask)) { // Änderung
if (tasterCur & mask) { // Setzt Event
tasterEntprellt |= mask;
} else { // Rücksetztevent, (Taster gedrückt)
tasterEntprellt &= ~mask;
event |= mask;
}
}
mask <<= 1;
}
return event;
}
void debugMenuCont() {
uint8_t i, help;
uint8_t tastenDruckEvent = tastenEntprellen();
if (tastenDruckEvent & _BV(PB0)) { // Taste Menu +
menuEntry++;
if (menuEntry >= 8)
menuEntry = 0;
stateMenu = ST_MENU_START;
} else if (tastenDruckEvent & _BV(PB1)) { // Taste Menu +
if (menuEntry)
menuEntry--;
else
menuEntry = 7;
stateMenu = ST_MENU_START;
}
switch (stateMenu) {
case ST_MENU_START:
PORTC = (1 << menuEntry);
timer = 50;
stateMenu = ST_MENU_ON;
break;
case ST_MENU_ON:
if (timer) {
timer--;
PORTC = 0;
stateMenu = ST_MENU_OFF; // Wechsel zu ST_MENU_OFF damit das Menu dunkler erscheint.
} else {
stateMenu = ST_MENU_WERT;
updateWert = 1;
}
break;
case ST_MENU_OFF:
PORTC = (1 << menuEntry);
stateMenu = ST_MENU_ON;
break;
case ST_MENU_WERT:
if (tastenDruckEvent & _BV(PB2)) { // Taste Wert +
updateWert = 1;
debugMenu[menuEntry].wert++;
} else if (tastenDruckEvent & _BV(PB3)) { // Taste Wert -
updateWert = 1;
debugMenu[menuEntry].wert--;
}
if (alterWert != debugMenu[menuEntry].wert) // Wert geändert
updateWert = 1;
alterWert = debugMenu[menuEntry].wert;
if (updateWert) {
updateWert = 0;
switch (debugMenu[menuEntry].format) { // Welches Ausgabeformat
case DM_FRMT_VOLUME:
help = 0;
if (debugMenu[menuEntry].wert) {
help = 1;
for (i = 1; i < debugMenu[menuEntry].wert && i < 8; i++) {
help |= help << 1;
}
}
PORTC = help;
break;
case DM_FRMT_BIN:
default:
PORTC = debugMenu[menuEntry].wert;
}
}
break;
}
}
void debugMenuSetValue(uint8_t led, uint8_t wert) {
if (led < 8)
debugMenu[led].wert = wert;
}
void debugMenuSetFormat(uint8_t led, format_t format) {
if (led < 8)
debugMenu[led].format = format;
}
uint8_t debugMenuGetValue(uint8_t led) {
if (led < 8)
return debugMenu[led].wert;
return 0;
}
#ifdef DEBUG_MIT_INTTERUPT
static volatile uint8_t logicTimer;
ISR(TIMER0_OVF_vect) {
TCNT0 = 255-125;
logicTimer++;
if (logicTimer > 9) {
logicTimer = 0;
debugMenuCont();
}
}
#endif // DEBUG_MENU