Forum: Projekte & Code Einfaches Menue fuer GLCDs


von Stephan K. (dustpuppy)


Lesenswert?

Hi,
nachdem ich den Beitrag Beitrag "Struct aus Flash lesen" 
gelesen hab,
hier mal meine Menu-Funktionen fuer Grafikdisplays. Jedes Menue hat 
einen Eintrag, der sich folgendermassen zusammen setzt:
Zeile , Spalte, Text, Funktion fuer Callback, Naechstes Menue, 
Vorheriges Menue

Die Hauptstruktur muss folgende Daten enthalten:
Kopfzeile, Fusszeile, Anzahl Eintraege im Menue, Name des Menues.

Das Menue wird dann mit Show_Menu(); aufgerufen. Vorblaettern mit 
Menu_Down();. Zureuckblaettern mit Menu_Up();. Ausfuehrung des Menues 
mit Menu_Execute();.
Diese Funktionen muessen in den Keyboard-Routinen mit eingebunden 
werden.

Im Source sind 4 Stellen, die mit // ... eingegrenzt sind. da muessen 
die passenden Print-Funktionen rein. Hab mal die Ausgaben fuer die 
Font-lib von
Christian R. drin gelassen, damit man sieht, wie's sein kann.

Das Ganze muesste sich eigendlich mit andern Scrollfunktionen auch fuer 
Text-Lcds umarbeiten lassen.

Gruesse

Dusty
1
/// ----------------MENU--------------------------------------
2
void Show_Menu(void);
3
void Menu_up(void);
4
void Menu_Down(void);
5
void Menu_Back(void);
6
void Menu_Execute(void);
7
8
uint8_t MENU_INDEX;
9
uint8_t MENU_ENTRIES;
10
uint8_t MENU_ACTIVE;
11
12
uint8_t MENU_RUNNING=TRUE;
13
14
typedef const struct {
15
  uint8_t x;
16
  uint8_t y;
17
  char *text;
18
  void (*function)(int);
19
  uint8_t next;
20
  uint8_t last;
21
}_menu_struct_;
22
23
typedef const struct {
24
  char *top;
25
  char *bottom;
26
  uint8_t entries;
27
  _menu_struct_ *menu_struct;
28
}_menu_;
29
30
/// ----------------MENU CALLBACKS----------------------------
31
void callback_menu(int n)
32
{
33
}
34
35
/// ----------------MENU STRUKTUREN---------------------------
36
37
/// MENU 0 - Die 2 ist die Anzahl der Eintraege
38
const _menu_struct_ MAIN_MENU[2] PROGMEM =
39
{
40
  {0,20, "MAIN MENU 1"  , NULL,  1, 0},  // x, y, text, function, next, last
41
  {0,34, "MAIN MENU 2"  , NULL,  2, 0},  // x, y, text, function, next, last
42
};
43
44
/// Menu 1 - 1 ist die Anzahl Eintraege
45
const _menu_struct_ SUB_MENU_1[1] PROGMEM =
46
{
47
  {0,20, "SUB MENU 1 - 1" , NULL, 3, 0},  // x, y, text, function, next, last
48
};
49
50
/// Menu 2 - 3 ist die Anzahl der Eintraege
51
const _menu_struct_ SUB_MENU_2[3] PROGMEM =
52
{
53
  {0,20, "SUB MENU 2 - 1" , &callback_menu, 255, 0},  // x, y, text, function, next, last
54
  {0,34, "SUB MENU 2 - 2" , &callback_menu, 255, 0},  // x, y, text, function, next, last
55
  {0,48, "SUB MENU 2 - 3" , &callback_menu, 255, 0},  // x, y, text, function, next, last
56
};
57
58
/// Menu 3 - 3 ist die Anzahl der Eintraege
59
const _menu_struct_ SUB_MENU_3[3] PROGMEM =
60
{
61
  {0,20,  "SUB MENU 3 - 1" , &callback_menu, 255, 1},  // x, y, text, function, next, last
62
  {0,34,  "SUB MENU 3 - 2" , &callback_menu, 255, 1},  // x, y, text, function, next, last
63
  {0,48,  "SUB MENU 3 - 3" , &callback_menu, 255, 1},  // x, y, text, function, next, last
64
};
65
66
/// Das eigendliche Menue - 4 Eintraege
67
const _menu_ MENU[4] PROGMEM =
68
{
69
  {"MAIN MENU",  "", 2,  MAIN_MENU },  //Top Line, Bottom Line, Entries, menu_struct
70
  {"SUB MENU 1", "", 1,  SUB_MENU_1 },  //Top Line, Bottom Line, Entries, menu_struct
71
  {"SUB MENU 2", "", 3,  SUB_MENU_2 },  //Top Line, Bottom Line, Entries, menu_struct
72
  {"SUB MENU 3", "", 3,  SUB_MENU_3 },  //Top Line, Bottom Line, Entries, menu_struct
73
};
74
75
76
/// ------------------------Menu Functions------------------------------
77
void Show_Menu(void)
78
{
79
  uint8_t x,y;
80
  int c;
81
  char *text;
82
  _menu_struct_ *M=NULL;
83
  
84
  text=(void*)pgm_read_word(&(MENU[MENU_ACTIVE].top));
85
  //...
86
  //...
87
  // Hier die Ausgabe fuer die Ueberschrift
88
  //...
89
  //...
90
  text=(void*)pgm_read_word(&(MENU[MENU_ACTIVE].bottom));
91
  //...
92
  //...
93
  // Hier die Statuszeile am unteren Bildschrimrand
94
  //...
95
  //...
96
  MENU_ENTRIES=pgm_read_byte(&(MENU[MENU_ACTIVE].entries));
97
  M= (_menu_struct_ *) pgm_read_word(&(MENU[MENU_ACTIVE].menu_struct));
98
99
  for(c=0;c<MENU_ENTRIES;c++)
100
  {
101
    text=(void*)pgm_read_word(&(M[c].text));
102
    x=pgm_read_byte(&(M[c].x));
103
    y=pgm_read_byte(&(M[c].y));
104
    if(c==MENU_INDEX)
105
    {
106
      //...
107
      //...
108
      // Hier die Ausgabe des aktiven Eintrags
109
//      GLCD_PRINT(x, y, text, &FONT_NORMAL, TRUE);
110
      // ...
111
      // ...
112
    }
113
    else
114
    {
115
      // ...
116
      // ...
117
      // Hier die Ausgabe des nicht aktiven Eintrags
118
//      GLCD_PRINT(x, y, text, &FONT_NORMAL, FALSE);
119
      // ...
120
      // ...
121
    }
122
  }
123
  MENU_RUNNING=TRUE;
124
}
125
126
void Menu_up(void)
127
{
128
  if(MENU_ACTIVE<255)
129
  {
130
    if(MENU_INDEX>0)
131
    {
132
      MENU_INDEX--;
133
    }
134
    else
135
    {
136
      MENU_INDEX=MENU_ENTRIES-1;
137
    }
138
    Show_Menu();
139
  }
140
}
141
142
void Menu_Down(void)
143
{
144
  if(MENU_ACTIVE<255)
145
  {
146
    if(MENU_INDEX<MENU_ENTRIES-1)
147
    {
148
      MENU_INDEX++;
149
    }
150
    else
151
    {
152
      MENU_INDEX=0;
153
    }
154
    Show_Menu();
155
  }
156
}
157
158
void Menu_Back(void)
159
{
160
  _menu_struct_ *M=NULL;
161
  if(MENU_ACTIVE<255)
162
  {
163
    uint8_t last;
164
    M= (_menu_struct_ *) pgm_read_word(&(MENU[MENU_ACTIVE].menu_struct));
165
    last=pgm_read_byte(&(M[MENU_INDEX].last));
166
    GLCD_CLEAR();
167
    MENU_INDEX=0;
168
    MENU_ACTIVE=last;
169
    Show_Menu();
170
  }
171
}
172
173
void Menu_Execute(void)
174
{
175
  if(MENU_ACTIVE<255)
176
  {
177
    _menu_struct_ *M=NULL;
178
    void (*fptr)(int);
179
    uint8_t next;
180
    M= (_menu_struct_ *) pgm_read_word(&(MENU[MENU_ACTIVE].menu_struct));
181
    next=pgm_read_byte(&(M[MENU_INDEX].next));
182
    fptr=(void*)pgm_read_word(&(M[MENU_INDEX].function));
183
    if( fptr!=NULL )
184
    {
185
      fptr(MENU_INDEX);
186
      if( (next!=MENU_ACTIVE) && (next!=255) )
187
      {
188
  // ...
189
  // ...
190
  // Display loeschen
191
//  GLCD_CLEAR();
192
  // ...
193
  // ...
194
  MENU_INDEX=0;
195
  MENU_ACTIVE=next;
196
  Show_Menu();
197
      }
198
    }
199
    else
200
    {
201
  // ...
202
  // ...
203
  // Display loeschen
204
//  GLCD_CLEAR();
205
  // ...
206
  // ...
207
      MENU_INDEX=0;
208
      MENU_ACTIVE=next;
209
      Show_Menu();
210
    }
211
  }
212
}
213
214
///-------------End of Menu Functions-----------------------------

von W.S. (Gast)


Lesenswert?

Netter Anfang, aber zu kurz gesprungen. Ich sag's mal ganz generell: Du 
hast dabei zu prozedural gedacht und nix anderes als eine Liste von Text 
im Sinn gehabt.

Versuche es mal mit einem objektorientierten Ansatz:
1. es gibt grafische Elemente auf dem Bildschirm
2. ein solches Element hat eine Koordinate und eine Größe
3. ein solches Element gehört einem übergeordneten Element (Owner) und 
das "übergeordneteste" Element ist der Bildschirm selbst
4. ein solches Element kann selbst andere Elemente (Members) enthalten
5. ein solches Element muß sich selbst zeichnen können
6. ein solches Element muß auf Botschaften (Events) reagieren können, 
die von außen in das ganze Menüsystem hineingeworfen werden, also z.B. 
"jemand hat Taste XYZ gedrückt" oder "es ist eine Sekunde vergangen" 
oder "zeichne dich" oder "initialisiere dich" usw.

W.S.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.